I am setting up a Strategy to use with Wundertrading.
As these signal providers work, I need to send a duplicate signal to close any previous open position and then open a position in the opposite direction.
With the current code, I have managed to obtain a double signal for the close long, and short entry signal, but it does not work for close short, entry long. I suppose it is a matter of herarchy, but don't know how to resolve that issue.
What would be wonderful, is if with the same input, the close can exit market in the same candle the signal is received and the entry is done in the next candle.
This is the code I am currently using:
/// Trade State Management
isInLongPosition = strategy.position_size > 0
isInShortPosition = strategy.position_size < 0
/// Trade Execution
longConditionCalc = (longCondition and isADXFilterEnabledAndAboveThreshold and TFSlong and RSIlong and ZFSlong)
shortConditionCalc = (shortCondition and isADXFilterEnabledAndAboveThreshold and TFSshort and RSIshort and ZFSshort)
closeLongConditionCalc= (closeLongCondition and TFSexitlong and ZFSshort) or shortConditionCalc
closeShortConditionCalc= (closeShortCondition and TFSexitshort and ZFSlong) or longConditionCalc
if isStartEndPeriodsAndTimeInRange
// Long Conditions
if longConditionCalc and i_tradeDirection != 'Short Only' and isInLongPosition == false
strategy.entry('Long', strategy.long, qty=contracts, comment = i_alertLongEntry)
if closeLongConditionCalc and isInLongPosition == true
strategy.close('Long', qty_percent=100, comment = i_alertLongExit)
if slLongClose
strategy.close('Long', qty_percent=100, when=slLongClose, comment = "")
// Short Conditions
if shortConditionCalc and i_tradeDirection != 'Long Only' and isInShortPosition == false
strategy.entry('Short', strategy.short, qty=contracts, comment = i_alertShortEntry)
if closeShortConditionCalc and isInShortPosition == true
strategy.close('Short', qty_percent=100, when=closeShortConditionCalc or longConditionCalc, comment = i_alertShortExit)
if slShortClose
strategy.close('Short', qty_percent=100, when=slShortClose, comment = "")