根据以前的蜡烛数量找到最高或最低的价格 - pinescript
我正在根据先前数量的蜡烛(14个蜡烛)寻找最高或最低的价格,并以长订单或短订单的最高价格计算出停止损失价格。
但是,当包含在图表中时,SL和TP不正确(附加了链接)。
我的脚本:
//The price of the highest high or the lowest low is based on the previous 14 candles
highest = ta.highest (high, 14)
lowest = ta.lowest(low, 14)
//Calculate the price range from the closing price to the highest high or lowest low to make a stop loss
stoplossLong = highest - close
stoplossShort = close - lowest
// TP & SL for Long & Short
longstoppercent = float(strategy.position_avg_price - stoplossLong)
longtakeprofitpercentTP1 = float(strategy.position_avg_price + stoplossLong*1.5)
longtakeprofitpercentTP2 = float(strategy.position_avg_price + stoplossLong*3)
shortstoppercent = float(strategy.position_avg_price + stoplossShort)
shorttakeprofitpercentTP1 = float(strategy.position_avg_price - stoplossShort*1.5)
shorttakeprofitpercentTP2 = float(strategy.position_avg_price - stoplossShort*3)
I am looking for the highest or lowest price based on the previous number of candles (14 candles) and work out the stop loss price from entry to highest high for long orders or lowest price for short orders.
But when included in the chart, the SL and TP are not correct (link is attached).
My script:
//The price of the highest high or the lowest low is based on the previous 14 candles
highest = ta.highest (high, 14)
lowest = ta.lowest(low, 14)
//Calculate the price range from the closing price to the highest high or lowest low to make a stop loss
stoplossLong = highest - close
stoplossShort = close - lowest
// TP & SL for Long & Short
longstoppercent = float(strategy.position_avg_price - stoplossLong)
longtakeprofitpercentTP1 = float(strategy.position_avg_price + stoplossLong*1.5)
longtakeprofitpercentTP2 = float(strategy.position_avg_price + stoplossLong*3)
shortstoppercent = float(strategy.position_avg_price + stoplossShort)
shorttakeprofitpercentTP1 = float(strategy.position_avg_price - stoplossShort*1.5)
shorttakeprofitpercentTP2 = float(strategy.position_avg_price - stoplossShort*3)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为,您的问题不是要找到过去14个酒吧中最高和最低的。我建议通过绘制最高和最低的方式来检查这一点。
我认为您的问题在于您的计算或战略测试人员部分。
展示:
使差异绝对(
math.abs()
)是我通常要做的,因为在某些市场中,可能会有一个错误,其中差异可能为负面,这很糟糕,我并不真正理解,为什么您使用
stragent.position_avg_price < / code> variable,因为这是您除了停止损失 /获取利润之外需要的地方。
我使用
关闭
作为您的条目。代码描述:
:=
)变量I think, your problem is not to find the highest and lowest of 14 bars past. I'd recommend checking this by plotting highest and lowest.
I think that your problem lies in your calculations or in the strategy tester part.
SHowcase :
Making the differences absolute (
math.abs()
) is something I usually do, because in some markets there can be a bug, where the difference can be negative, which is badI didn't really understand, why you used the
strategy.position_avg_price
variable, because that is something you need somewhere else than stop loss / take profit.I used
close
as your entry.Code Description:
:=
) variables