[英]Take profit/Stop loss right on opening candle
我一直在尝试为我的策略止损并获利,但总是发现自己遇到同样的问题
我的获利信号实际上与交易开始时在同一根蜡烛上,如果我更改 tp 金额,它们不会受到影响
这是代码:
//@version=4
strategy("sma")
//dropdown menu to change the values of the EMA periods, stop loss, and take profit levels:
emaShortPeriod = input(100, minval=1)
emaLongPeriod = input(150, minval=1)
stopLoss = input(100, minval=0)
takeProfit = input(5, minval=0)
//use the input values to define the EMA series:
emaShort = sma(close, emaShortPeriod)
emaLong = sma(close, emaLongPeriod)
//plot the EMA series on the chart:
plot(emaShort, title="EMA (short period)", color=color.green)
plot(emaLong, title="EMA (long period)", color=color.orange)
//use the input values to define the long and short entry signals:
longEntry = crossover(emaShort, emaLong)
shortEntry = crossover(emaLong, emaShort)
//plot the long and short entry signals on the chart:
plotchar(longEntry, "Long Entry", "▲", location.top, color = #00FF00, transp = 0)
plotchar(shortEntry, "Short Entry", "▼", location.top, color = #FF0000, transp = 0)
//function to generate market orders when the entry signals are triggered:
strategy.entry("Long", strategy.long, when=longEntry)
strategy.entry("Short", strategy.short, when=shortEntry)
//function to generate stop loss and take profit levels for the strategy:
strategy.exit("Take Profit", "Long", profit=takeProfit)
strategy.exit("Take Profit", "Short", profit=takeProfit)
strategy.exit("Stop Loss", "Long", loss=stopLoss)
strategy.exit("Stop Loss", "Short", loss=stopLoss)
和一张照片
在此处输入图像描述
只是希望 TP 和 SL 能正常工作...
我刚开始编码,如果这个解释不是顶级的,我很抱歉
当通过输入违抗止损百分比和止盈百分比时,我没有意识到代码中的 int 数字必须大于您在下拉菜单中输入的数字。 因此,例如,如果我的代码如下所示:
stopLossPercent = input(2, minval=0, maxval=200) takeProfitPercent = input(2, minval=0, maxval=200)
如果不在图表上遇到错误,我将无法将止损或获利设置为 2 以上,但是如果我更改它并将其设置为 200,例如:
stopLossPercent = input(200, minval=0, maxval=200)
takeProfitPercent = input(200, minval=0, maxval=200)
然后低于 200 的任何值都将起作用。 我认为您不需要 minval 或 maxval,但我只是使用它。
对于计算止盈和止损的其余代码,我设置了 4 个变量:
stopLossPriceOne
takeProfitPriceOne
stopLossPricetwo
takeprofitPricetwo
一个用于计算多头 TP 和 SL。 简称二。 我只是用一个非常简单的数学来计算它......:
entryPrice = strategy.position_avg_price
stopLossPriceOne = entryPrice / 100 * (100 - stopLossPercent)
takeProfitPriceOne = entryPrice / 100 * (100 + takeProfitPercent)
stopLossPricetwo = entryPrice / 100 * (100 + stopLossPercent)
takeprofitPricetwo = entryPrice / 100 * (100 - takeProfitPercent)
entryPrice 是您开始交易的地方
然后我退出:
strategy.exit("Long Exit", "Long", stop=stopLossPriceOne,
limit=takeProfitPriceOne)
strategy.exit("Short Exit", "Short", stop=stopLossPricetwo,
limit=takeprofitPricetwo)
我希望这对像我这样的任何新的 pinescript 编码器有所帮助。
这是完整的代码:
//@version=4
strategy("sma")
//dropdown menu to change the values of the SMA periods, stop loss, and
take profit levels:
smaShortPeriod = input(100, minval=1)
smaLongPeriod = input(150, minval=1)
stopLossPercent = input(200, minval=0, maxval=200)
takeProfitPercent = input(200, minval=0, maxval=200)
//use the input values to define the SMA series:
smaShort = sma(close, smaShortPeriod)
smaLong = sma(close, smaLongPeriod)
//plot the SMA series on the chart:
plot(smaShort, title="SMA (short period)", color=color.green)
plot(smaLong, title="SMA (long period)", color=color.orange)
//use the input values to define the long and short entry signals:
longEntry = crossover(smaShort, smaLong)
shortEntry = crossover(smaLong, smaShort)
//plot the long and short entry signals on the chart:
plotshape(longEntry, "Long Entry", shape.arrowup, location.top,
color=color.green, transp=0)
plotshape(shortEntry, "Short Entry", shape.arrowdown, location.top,
color=color.red, transp=0)
//function to generate market orders when the entry signals are
triggered:
strategy.entry("Long", strategy.long, when=longEntry)
strategy.entry("Short", strategy.short, when=shortEntry)
//function to generate stop loss and take profit levels for the
strategy:
entryPrice = strategy.position_avg_price
stopLossPriceOne = entryPrice / 100 * (100 - stopLossPercent)
takeProfitPriceOne = entryPrice / 100 * (100 + takeProfitPercent)
stopLossPricetwo = entryPrice / 100 * (100 + stopLossPercent)
takeprofitPricetwo = entryPrice / 100 * (100 - takeProfitPercent)
strategy.exit("Long Exit", "Long", stop=stopLossPriceOne,
limit=takeProfitPriceOne)
strategy.exit("Short Exit", "Short", stop=stopLossPricetwo,
limit=takeprofitPricetwo)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.