我有一个关于pinescript的基本策略,该策略基于2个移动平均线的交叉点进入头寸。我会做的是这样的: 当发生交叉时,输入多头/空头(天气是牛市或熊市交叉) 稍后再退出3支蜡烛。 我已经尝试过使用'barssince'函数,但是我不太擅长编码。 这是我的策略:
strategy("MovingAvg2Line Cross", overlay=true, initial_capital=10000)
fastLength = input(50)
slowLength = input(200)
price = close
mafast = sma(price, fastLength)
maslow = sma(price, slowLength)
if (crossover(mafast, maslow))
strategy.entry("MA2CrossLE", strategy.long, comment="MA2CrossLE")
if (crossunder(mafast, maslow))
strategy.entry("MA2CrossSE", strategy.short, comment="MA2CrossSE") ```
答案 0 :(得分:0)
在末尾添加它。它可以检测未平仓交易数量的变化并从那时起计算条形:
strategy.close_all(when = barssince(change(strategy.opentrades)) == 2)