问题描述
我正在尝试根据入场时的摆动低点设置静态止损。但是当我尝试以下操作时,止损随着每个柱线不断变化,因为有新的最低点
SwingLowBars=20
longStop = lowest(low,SwingLowBars)[1]
longTake = strategy.position_avg_price + ((strategy.position_avg_price-longStop)*3)
我想要一个函数,在位置中的每根新蜡烛时,不断向 SwingLowBars 变量添加 +1,以便 longTake 保持静态并且在距离最低柱超过 20 个柱时不会改变
解决方法
您可以记住“var”变量中的 SL 值:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adolgov
//@version=4
strategy("My Strategy",overlay=true)
longCondition = crossover(sma(close,14),sma(close,28))
if (longCondition)
strategy.entry("My Long Entry Id",strategy.long)
SwingLowBars=20
longStop = lowest(low,SwingLowBars)[1]
var sl = float(na)
if strategy.position_size !=0 and strategy.position_size[1] == 0
sl := strategy.position_avg_price - ((strategy.position_avg_price-longStop)*3)
strategy.exit("x",stop = sl)