首页 > 解决方案 > 因为要计算从绿色三角形到红色三角形而不是绿色三角形到另一个绿色的距离

问题描述

我有一个用于多头的绿色三角形和一个用于短裤的红色三角形。Barssince 正在从绿色三角形到另一个绿色三角形,从一个红色到另一个红色。我想要的是从绿色到红色三角形。

fastSMA = sma(close, 15)
slowSMA = sma(close, 45)

enterLong  = crossover(fastSMA, slowSMA)
enterShort = crossunder(fastSMA, slowSMA)

entryPrice = valuewhen(enterLong or enterShort, close, 0)

plot(barsSinceLong, color = color.yellow)
plot(barsSinceShort, color = color.purple)

if barsSinceLong == 0 and barsSinceShort == 0
    c := c_NOT_IN_TRADE
if barsSinceLong > 0 and close < entryPrice
    c := c_LONG_ABOVE_ENTRYPRICE
else if barsSinceLong > 0 and close > entryPrice
    c := c_LONG_BELOW_ENTRYPRICE
else if barsSinceShort > 0 and close > entryPrice
    c := c_SHORT_BELOW_ENTRYPRICE
else if barsSinceShort > 0 and close < entryPrice
    c := c_SHORT_ABOVE_ENTRYPRICE

标签: pine-script

解决方案


你已经绘制了两个 barsince(),所以你可以看到这个函数是如何工作的,你可以做一个cross(barsSinceLong, barsSinceShort),这样你就可以从长到短。


推荐阅读