Combining short/long strategies into 1 strategy issue

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #226672 quote
    Snålänningen
    Participant
    Junior

    Hi!

    I’m trying to combine a short and a long strategy in the same algo. However, the combined strategy backtest is different, compared to if I backtest the long and short strategies separately. If the long stategy gains 100$ and the short also gains 100$ during the same period, I would expect the combined to give a gain of 200$. This is not the case right now, so I wonder if something is wrong in my code or if I am missing something? Appreciate any help, thanks!

     

    DefParam CumulateOrders = True
    defparam preloadbars = 10000
    
    noEntryBeforeTime = 150000
    noEntryAfterTime = 220000
    
    // LONG
    
    C1 = Close > Average[210](Close)
    C2 = Close < Average[2](Close)
    
    
    
    IF Not LongOnMarket and C1 and C2 and time >= noEntryBeforeTime and time <= noEntryAfterTime then
    Buy 1 contract at Market
    SET STOP %LOSS 0.5
    SET TARGET %PROFIT 0.5
    EndIf
    
    
    // SHORT
    
    CS1 = Close < Average[210](Close)
    CS2 = Close > Average[2](Close)
    CS3 = Close < Average[5000](close)
    CS4 = MACD[12,26,9](close) < 20
    
    
    IF Not ShortOnMarket and CS1 and CS2 and CS3 and CS4 and time >= noEntryBeforeTime and time <= noEntryAfterTime then
    Sellshort 1 contract at Market
    SET STOP %LOSS 0.5
    SET TARGET %PROFIT 0.5
    
    EndIf
    shortOnly.png shortOnly.png longonly.png longonly.png combined.png combined.png
    #226676 quote
    PeterSt
    Participant
    Master

    Hi,

    Without judging your code : this is perfectly normal. This is because if a Long trade is running, it will occupy the “space” for the Short(s). Thus, both can not run at the same time.
    It can also easily happen that the Long + Short separately, bring only 80 instead of at least 100 what you might expect (let alone 200).

    Of course the code itself is related – e.g. how you optimized it. And although that could have been worked towards the both Long and Short together, nothing would be wrong with doing that.
    I hope it is clear a little ?

    Regards,
    Peter

    Snålänningen thanked this post
    #226677 quote
    PeterSt
    Participant
    Master

    But also your code is not correct;

    You allow for both Long and Short at the same time. This means that a Short will cancel out a Long. This can happen in the one (bar-)call of the code, but also across bars (several bars later).
    If you include the condition “If Not OnMarket” for both cases, the problem across bars won’t be there. If you then also put the lot in an If/Else (so it will be *or* buy Long *or* sell short), the in the one (bar-) call cancellation will also not happen and all is good.

    The many more trades for both together vs. each on its own, is indicative for “error”.

    When you solved this, you will still not end at 200+. 😉

    Snålänningen thanked this post
    #226678 quote
    phoentzs
    Participant
    Master

    The first thing I would do is change the “True” to “False”… then I would test whether a direct change between long and short helps, often this reduces the drawdown. Regardless, I would prefer bigger winners than losses, definitely in short trades. Depends on the instrument and the time frame.

    Snålänningen thanked this post
    #226679 quote
    fifi743
    Participant
    Master

    You can do two algo’s, one long and one short, but it depends on your broker.
    Here is a code for the long with 2 in spread

    DefParam CumulateOrders = true
    defparam preloadbars = 10000
    
    noEntryBeforeTime = 153000
    noEntryAfterTime = 220000
    
    // LONG
    
    C1 = Close > Average[210](Close)
    C2 = Close < Average[2](Close)
    if opentime=220000 then
    F=open
    endif
    
    
    IF Not LongOnMarket and C1 and C2 and F>close and high[40]>close and opentime >= noEntryBeforeTime and opentime <= noEntryAfterTime then
    Buy 1 contract at Market
    SET STOP %LOSS 0.2
    SET TARGET %PROFIT 2
    EndIf
    
    
    // SHORT
    
    CS1 = Close < Average[210](Close)
    CS2 = Close > Average[2](Close)
    CS3 = Close < Average[5000](close)
    CS4 = MACD[12,26,9](close) < 20
    
    
    IF Not ShortOnMarket and CS1 and CS2 and CS3 and CS4 and time >= noEntryBeforeTime and time <= noEntryAfterTime then
    //Sellshort 1 contract at Market
    //SET STOP %LOSS 0.5
    //SET TARGET %PROFIT 0.5
    
    EndIf
    
    Snålänningen thanked this post
    Capture-decran-2024-01-21-212020.png Capture-decran-2024-01-21-212020.png Capture-decran-2024-01-21-212048.png Capture-decran-2024-01-21-212048.png
    #226682 quote
    JS
    Participant
    Senior
    Hi,

    The PRT “back test module” does not accept long and short positions at the same time…

    (so, you can’t back test your combined system (properly), hence the differences…)

    Snålänningen thanked this post
    #226713 quote
    Snålänningen
    Participant
    Junior
    Thanks so much for all answers! Adding the “If Not OnMarket” instead of “if short onmarket/long onmarket” solved the issue and made the code behave as expected! Thanks!
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.

Combining short/long strategies into 1 strategy issue


General Trading: Market Analysis & Manual Trading

New Reply
Author
Summary

This topic contains 6 replies,
has 5 voices, and was last updated by Snålänningen
2 years ago.

Topic Details
Forum: General Trading: Market Analysis & Manual Trading
Language: English
Started: 01/21/2024
Status: Active
Attachments: 5 files
Logo Logo
Loading...