Risk Management: If already long then do not place new order

风格不统一 提交于 2020-01-05 08:23:18

问题


If the flag is already indicating long, there should not be a new flag indicating long. If flag does not indicate long evaluate the expression

longCondition = if (strategy.long) ? false: (rsi<30) and (close>moving_avg)

shortCondition = if (strategy.short) ? false: (rsi>70) and (close<moving_avg)

Processing script...

line 30: mismatched input 'shortCondition' expecting 'end of line without line continuation'


回答1:


I assume this is an indicator and not a strategy. Because you can configure how many entries you want to have in the same direction in a strategy with the pyramiding parameter. Default is 0, so if this is a strategy and you haven't changed the pyramiding parameter, it shouldn't be a problem.

For indicators, you can use a variable like this:

//@version=4
study("My Script", overlay=true)

var isLong = false
var isShort = false

rsi = rsi(close, 14)
moving_avg = ema(close, 9)

buySignal = not isLong and (rsi<50) and (close>moving_avg)    // Buy only if we are not already long
sellSignal = not isShort and (rsi>50) and (close<moving_avg)  // Sell only if we are not already short

if buySignal
    isLong := true
    isShort := false

if sellSignal
    isLong := false
    isShort := true

plotshape(series=buySignal, title="BUY", text="BUY", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=sellSignal, title="SELL", text="SELL", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)



来源:https://stackoverflow.com/questions/58383816/risk-management-if-already-long-then-do-not-place-new-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!