【问题标题】:Using the last (engulfing) candles low as stoploss使用最后一根(吞没)蜡烛作为止损
【发布时间】:2020-06-04 14:55:22
【问题描述】:

我对我的 pine 脚本有疑问。 战略思路: - 简单的趋势跟踪策略 - 检查是否处于上升/下降趋势 - 当吞没蜡烛形态时,我想在下一根蜡烛上进行交易

问题: 我想在吞没蜡烛的低点设置止损。 TP 应该是(前一个)吞没蜡烛高度的 1.25 倍。

最后一部分我有点挣扎。

谁能帮忙?

一切顺利!

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © c4ss10p314

//@version=4
strategy(title = "3emaFX", shorttitle = "3ema", overlay = true, initial_capital = 100000, process_orders_on_close = false)
price = strategy.position_avg_price

//input variables
hammerBodyHeight = input(title="Hammer Body, Max Height (Pips)",minval=1,maxval=100,step=1,defval=300,confirm=false)
bullEnHeight = input(title="BullEn, Height (Pips)",minval=1,maxval=100,step=1,defval=3,confirm=false)
bullEnAbove = input(title="BullEn Above (Pips)",minval=1,maxval=100,step=1,defval=1,confirm=false)
bullEnBelow = input(title="BullEn, Below (Pips)",minval=1,maxval=100,step=1,defval=0,confirm=false)
bearEnHeight = input(title="BearEn, Height (Pips)",minval=1,maxval=100,step=1,defval=3,confirm=false)
bearEnAbove = input(title="BearEn Above (Pips)",minval=1,maxval=100,step=1,defval=0,confirm=false)
bearEnBelow = input(title="BearEn, Below (Pips)",minval=1,maxval=100,step=1,defval=1,confirm=false)


//Candlestick patterns
invertedHammer = (abs(close - open) / syminfo.mintick <= hammerBodyHeight) and (close > open) and ((high - close) > (close - open)) and ((open - low) <= (close - open) / 2)
hammer = (abs(close - open) / syminfo.mintick <= hammerBodyHeight) and (close > open) and ((close - open) < (open - low)) and ((high - close) <= (close - open) / 2)
bullEngulfing = ((open[1] - close[1]) / syminfo.mintick >= bullEnHeight) and (close > open) and ((close - open[1]) / syminfo.mintick >= bullEnAbove) and ((close[1] - open) / syminfo.mintick >= bullEnBelow)
bearEngulfing = ((close[1] - open[1]) / syminfo.mintick >= bearEnHeight) and (close < open) and ((open - close[1]) / syminfo.mintick >= bearEnAbove) and ((open[1] - close) / syminfo.mintick >= bearEnBelow)
ema8 = ema(close, 8)
ema13 = ema(close, 13)
ema21 = ema(close, 21)

//chart plotters
plotshape(bullEngulfing, style=shape.arrowup, location=location.abovebar, color = color.green, text='BullEng')
plotshape(invertedHammer, style=shape.flag, location=location.abovebar, color = color.green, text='Inv Hammer')
plotshape(hammer, style=shape.flag, location=location.abovebar, color = color.green, text='Hammer')
plotshape(bearEngulfing, style=shape.arrowdown, location=location.belowbar, color = color.red, text='BearEng')
plot(ema8, color = color.blue, linewidth=1)
plot(ema13, color = color.green, linewidth=1)
plot(ema21, color = color.orange, linewidth=1)


//Trade conditions
longEntry = close > ema8 and ema8 > ema13 and bullEngulfing and barstate.isconfirmed
shortEntry = close < ema8 and ema8 < ema13 and bearEngulfing
longExit = close <= ema13


//Risk management variables
// RM - inputs
// position_size = 

clockwork = barssince(valuewhen(bullEngulfing, price, 1)) + 1
EngHeight = abs(open[clockwork] - close[clockwork]) / syminfo.mintick
execProfitTarget = (price + (EngHeight * 1.25))
execStopLoss = (price - EngHeight)

//timeframe
start = timestamp(2019, 6, 3, 0, 0)
end = timestamp(2020, 6, 5 , 0, 0)

//Entry
if time >= start and time <= end
    strategy.entry("Long", strategy.long, strategy.equity[0.01], when = longEntry)
//    strategy.entry("Short", strategy.short, strategy.equity[0.01], when = shortEntry)

// Exit
strategy.exit("Exit Long", from_entry = "Long", profit = execProfitTarget, stop = execStopLoss)
// strategy.close("Close Long", from_entry = "Long", profit = execProfitTarget, loss = execStopLoss)

// Check ob trend die letzten x Kerzen schon trend war
// Line 49: Strategy.equity - andere Lösung (Positionsgröße berechnen)

【问题讨论】:

    标签: pine-script trading


    【解决方案1】:

    版本 1

    这应该会让你更接近你的目标:

    • 我们使用 10% 的净值来确定头寸规模。
    • 只有在我们未进行交易时才会触发入场。
    • 止损指定为价格,而不是报价。
    • 最后包含调试图。
    • 更改了日期以查看更多交易示例。
    // © c4ss10p314
    
    //@version=4
    strategy(title = "3emaFXlong", shorttitle = "3emaL", overlay=true, initial_capital = 100000, default_qty_type=strategy.percent_of_equity, default_qty_value = 10)
    price = close
    
    //input variables
    hammerBodyHeight = input(title="Hammer Body, Max Height (Pips)",minval=1,maxval=100,step=1,defval=300,confirm=false)
    bullEnHeight = input(title="BullEn, Height (Pips)",minval=1,maxval=100,step=1,defval=3,confirm=false)
    bullEnAbove = input(title="BullEn Above (Pips)",minval=1,maxval=100,step=1,defval=1,confirm=false)
    bullEnBelow = input(title="BullEn, Below (Pips)",minval=1,maxval=100,step=1,defval=0,confirm=false)
    inpTakeProfit = input(1.25, title = "Example-Take Profit Multiplier", minval = 0.1, maxval = 5, step = 0.1)
    
    //Candlestick patterns
    invertedHammer = (abs(close - open) / syminfo.mintick <= hammerBodyHeight) and (close > open) and ((high - close) > (close - open)) and ((open - low) <= (close - open) / 2)
    bullEngulfing = ((open[1] - close[1]) / syminfo.mintick >= bullEnHeight) and (close > open) and ((close - open[1]) / syminfo.mintick >= bullEnAbove) and ((close[1] - open) / syminfo.mintick >= bullEnBelow)
    ema8 = ema(close, 8)
    ema13 = ema(close, 13)
    ema21 = ema(close, 21)
    
    //chart plotters
    plotshape(bullEngulfing, style=shape.arrowup, location=location.abovebar, color = color.green, text='BullEng')
    plotshape(invertedHammer, style=shape.flag, location=location.abovebar, color = color.green, text='Inv Hammer')
    
    plot(ema8, color = color.blue, linewidth=1)
    plot(ema13, color = color.green, linewidth=1)
    plot(ema21, color = color.orange, linewidth=1)
    
    
    //Trade conditions
    longEntry = close > ema8 and ema8 > ema13 and bullEngulfing
    longExit = close <= ema13
    
    
    //Risk management variables
    // RM - inputs
    // position_size = 
    execProfitTarget = (price * 1.05) / syminfo.mintick
    execStopLoss = (price * 0.98)
    
    //timeframe
    start = timestamp(2000, 5, 27, 0, 0)
    end = timestamp(2020, 6, 30 , 0, 0)
    
    //Entry & exit
    if time >= start and time <= end and longEntry and strategy.opentrades == 0
        strategy.entry("Long", strategy.long)
        strategy.exit("Exit Long", from_entry = "Long", profit = execProfitTarget, stop = execStopLoss)
    
    // Debugging.
    plotchar(longEntry, "longEntry", "▲", location.top, size = size.tiny)
    plotchar(close > ema8, "close > ema8", "•", location.bottom, size = size.tiny)
    plotchar(ema8 > ema13, "ema8 > ema13", "—", location.bottom, size = size.tiny)
    plotchar(execProfitTarget, "execProfitTarget", "", location.top, size = size.tiny)
    plotchar(execStopLoss, "execStopLoss", "", location.top, size = size.tiny)
    plotchar(strategy.opentrades, "strategy.opentrades", "", location.top, size = size.tiny)
    

    版本 2

    // © c4ss10p314
    
    //@version=4
    strategy(title = "3emaFXlong", shorttitle = "3emaL", overlay=true, initial_capital = 100000, default_qty_type=strategy.percent_of_equity, default_qty_value = 10)
    price = close
    
    //input variables
    hammerBodyHeight = input(title="Hammer Body, Max Height (Pips)",minval=1,maxval=100,step=1,defval=300,confirm=false)
    bullEnHeight = input(title="BullEn, Height (Pips)",minval=1,maxval=100,step=1,defval=3,confirm=false)
    bullEnAbove = input(title="BullEn Above (Pips)",minval=1,maxval=100,step=1,defval=1,confirm=false)
    bullEnBelow = input(title="BullEn, Below (Pips)",minval=1,maxval=100,step=1,defval=0,confirm=false)
    inpTakeProfit = input(1.25, title = "Example-Take Profit Multiplier", minval = 0.1, maxval = 5, step = 0.1)
    
    //Candlestick patterns
    invertedHammer = (abs(close - open) / syminfo.mintick <= hammerBodyHeight) and (close > open) and ((high - close) > (close - open)) and ((open - low) <= (close - open) / 2)
    bullEngulfing = ((open[1] - close[1]) / syminfo.mintick >= bullEnHeight) and (close > open) and ((close - open[1]) / syminfo.mintick >= bullEnAbove) and ((close[1] - open) / syminfo.mintick >= bullEnBelow)
    ema8 = ema(close, 8)
    ema13 = ema(close, 13)
    ema21 = ema(close, 21)
    
    //chart plotters
    plotshape(bullEngulfing, style=shape.arrowup, location=location.abovebar, color = color.green, text='BullEng')
    plotshape(invertedHammer, style=shape.flag, location=location.abovebar, color = color.green, text='Inv Hammer')
    
    plot(ema8, color = color.blue, linewidth=1)
    plot(ema13, color = color.green, linewidth=1)
    plot(ema21, color = color.orange, linewidth=1)
    
    
    //Trade conditions
    longEntry = close > ema8 and ema8 > ema13 and bullEngulfing
    longExit = close <= ema13
    
    
    //Risk management variables
    // RM - inputs
    // position_size = 
    
    //timeframe
    start = timestamp(2000, 5, 27, 0, 0)
    end = timestamp(2020, 6, 30 , 0, 0)
    
    //Entry & exit
    var float execProfitTarget = na
    var float execProfitPrice = na
    var float execStopLoss = na
    if time >= start and time <= end and longEntry and strategy.opentrades == 0
        // We know that we enter here only when a bullEngulfing was encountered, 
        // which means the current OHLC prices are those of that engulfing bar.
        execProfitTarget := (abs(close - open) * 1.25) / syminfo.mintick
        execProfitPrice := close + execProfitTarget * syminfo.mintick
        execStopLoss := low
        strategy.entry("Long", strategy.long)
        strategy.exit("Exit Long", from_entry = "Long", profit = execProfitTarget, stop = execStopLoss)
    
    // Plot stop and target.
    inTrade = strategy.opentrades != 0
    plot(inTrade ? execStopLoss : na, "Stop", color.red, 1, plot.style_circles)
    plot(inTrade ? execProfitPrice : na, "Target", color.lime, 1, plot.style_circles)
    
    // Debugging.
    // plotchar(strategy.opentrades, "strategy.opentrades", "", location.top, size = size.tiny)
    // plotchar(inTrade, "inTrade", "", location.top, size = size.tiny)
    // plotchar(longEntry, "longEntry", "▲", location.top, size = size.tiny)
    // plotchar(close > ema8, "close > ema8", "•", location.bottom, size = size.tiny)
    // plotchar(ema8 > ema13, "ema8 > ema13", "—", location.bottom, size = size.tiny)
    // plotchar(execProfitTarget, "execProfitTarget", "", location.top, size = size.tiny)
    // plotchar(execStopLoss, "execStopLoss", "", location.top, size = size.tiny)
    // plotchar(strategy.opentrades, "strategy.opentrades", "", location.top, size = size.tiny)
    

    【讨论】:

    • 嘿!您发布的代码仅用于调试。
    • 我的问题是,测量“预入场”栏并将其用作止损或获利..
    • ) 调试图帮助您可视化您的策略使用的信息是否与预期相符。 execProfitTarget = (price * 1.05) / syminfo.mintick 线是计算您的目标的那条线,现在可以通过您的策略达到,因为我将您的止损值更改为价格而不是像以前那样更改为分时,这导致您的头寸过早退出,最常见的是同一个酒吧。我所做的更改记录在代码之前的文本中。
    • 嘿!非常感谢您的所有努力。由于我是松树的新手,我并没有真正理解这一点。您是否有想法,如何使用进入头寸前的“最后一个”蜡烛作为 SL 和 TP 的大小?几天以来一直坚持下去.. 一切顺利!
    • 这就是您当前的代码所做的。它计算收盘价 + 5% 的止盈水平和收盘价 - 2% 的止损,收盘价是执行订单的柱线的收盘价,因此是入场前的收盘价。欢迎来到松树!这是您开始旅程的最佳地点:pinecoders.com
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2022-07-22
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多