Hi, I wonder if there is an elegant solution to tell my algorithm that it shall not open a long position when I already have an open short position.
You can do something quick like check if long triggered and not currently in a position like below:
if (longEntry and strategy.position_size==0)
I use this snipet of code to see if I’m in a position or not:
// *** Strategy Long/Short Call Calculation ***
longEntry = na
shortEntry = na
inLongPosition = na
inShortPosition = na
flat = na
inLongPosition := longEntry[1] ? true : shortEntry[1] ? false : inLongPosition[1]
inShortPosition:= shortEntry[1] ? true : longEntry[1] ? false : inShortPosition[1]
flat := not inLongPosition and not inShortPosition
Hope this helps answer your question… ;o)
I did it like this: (bs means barssince)
notLong = bsLongExit < bsLongEntry or bsStopLong < bsLongEntry
notShort = bsShortExit < bsShortEntry or bsStopShort < bsShortEntry
flat = notLong and notShort