Warm tip: This article is reproduced from serverfault.com, please click

algorithmic trading-Pine Script获取某个位置的小节数量

(algorithmic trading - Pine Script getting the number of bars in a position)

发布于 2020-12-23 22:34:23

我正在尝试根据进入时的低位波动设置静态止损。但是当我尝试以下操作时,止损不断变化,因为有新的最低低点

SwingLowBars=20

longStop = lowest(low, SwingLowBars)[1]

longTake = strategy.position_avg_price + ((strategy.position_avg_price-longStop)*3)

我想要一个函数,该函数在位置上每增加一个新蜡烛时就向SwingLowBars变量中添加+1,以便longTake保持静态,并且当最低柱超过20条时不会改变

Questioner
Samy
Viewed
0
Andrey D 2020-12-26 02:51:42

你可以将SL值存储在“ var”变量中:

// 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)