1

enter image description here

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 = "")
mkrieger1
  • 19,194
  • 5
  • 54
  • 65

2 Answers2

0

This is way too much code to help with any issue, but in general you can use the alert function to trigger any alerts without using the comment parameter of the strategy.*() functions. For example:

if longEntryCondition
    strategy.entry('Long', strategy.long, qty=contracts)
    alert(i_alertShortExit)
    alert(i_alertLongEntry)

mr_statler
  • 1,913
  • 2
  • 3
  • 14
  • Ok, you are right, I edited the code just to the part that is relevant. Thanks for your reply, I have tried the alert, but it keeps sending signals when trade is in opposite direction. In any case, I "believe"I may be half way there, I just want to be able to have the close short signal fire simultaniously as the long entry signal. If it works for short entry and close long, it should work the other way around, the think is I dont know how to fix that. – Giancarlo Bianchi Jan 26 '23 at 07:53
0

I think I did it!

Ok, I thihk I have found a solution, as akward as it seems, coding the orders this way, seem to do the trick. Is there a delay function between two parameters? to make this even better?

This is the code I used:

if isStartEndPeriodsAndTimeInRange // Long Conditions if longConditionCalc and i_tradeDirection != 'Short Only' and isInLongPosition == false strategy.close('Short', qty_percent=100, when=closeShortConditionCalc or longConditionCalc, comment = i_alertShortExit) 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.close('Long', qty_percent=100, comment = i_alertLongExit) 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 = "")