根据以前的蜡烛数量找到最高或最低的价格 - pinescript

发布于 2025-02-09 05:06:45 字数 1126 浏览 1 评论 0原文

我正在根据先前数量的蜡烛(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).

Illustrations

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狠疯拽 2025-02-16 05:06:45

我认为,您的问题不是要找到过去14个酒吧中最高和最低的。我建议通过绘制最高和最低的方式来检查这一点。

我认为您的问题在于您的计算或战略测试人员部分。

var float long_sl_dist = 0.0
var float short_sl_dist = 0.0
var float long_tp1 = 0.0
var float long_tp2 = 0.0
var float short_tp1 = 0.0
var float short_tp2 = 0.0

// highest high and lowest low.
highest = ta.highest (high, 14)
lowest = ta.lowest(low, 14)

if condition //sample condition, for the showcase image
    // distance of higest/lowest to close (entry).
    long_sl_dist := math.abs(highest - close)
    short_sl_dist := math.abs(close - lowest)
    
    // stop loss and take profit.
    long_tp1 := close + (long_sl_dist*1.5)
    long_tp2 := close + (long_sl_dist*3.0)
    
    short_tp1 := close - (short_sl_dist*1.5)
    short_tp2 := close - (short_sl_dist*3.0)

展示:

使差异绝对(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.

var float long_sl_dist = 0.0
var float short_sl_dist = 0.0
var float long_tp1 = 0.0
var float long_tp2 = 0.0
var float short_tp1 = 0.0
var float short_tp2 = 0.0

// highest high and lowest low.
highest = ta.highest (high, 14)
lowest = ta.lowest(low, 14)

if condition //sample condition, for the showcase image
    // distance of higest/lowest to close (entry).
    long_sl_dist := math.abs(highest - close)
    short_sl_dist := math.abs(close - lowest)
    
    // stop loss and take profit.
    long_tp1 := close + (long_sl_dist*1.5)
    long_tp2 := close + (long_sl_dist*3.0)
    
    short_tp1 := close - (short_sl_dist*1.5)
    short_tp2 := close - (short_sl_dist*3.0)

SHowcase : enter image description here

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 bad

I 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:

  • Variable Declaration in the beginning
  • Defining highest/lowest
  • When Condition is true, set (:=) variables
  • close is the entry and the rest is self-explanatory
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文