Combining short/long strategies into 1 strategy issue
Forums › ProRealTime English forum › General trading discussions › Combining short/long strategies into 1 strategy issue
- This topic has 6 replies, 5 voices, and was last updated 9 months ago by SweTrade.
-
-
01/21/2024 at 6:32 PM #226672
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!
Test code12345678910111213141516171819202122232425262728293031323334DefParam CumulateOrders = Truedefparam preloadbars = 10000noEntryBeforeTime = 150000noEntryAfterTime = 220000// LONGC1 = Close > Average[210](Close)C2 = Close < Average[2](Close)IF Not LongOnMarket and C1 and C2 and time >= noEntryBeforeTime and time <= noEntryAfterTime thenBuy 1 contract at MarketSET STOP %LOSS 0.5SET TARGET %PROFIT 0.5EndIf// SHORTCS1 = Close < Average[210](Close)CS2 = Close > Average[2](Close)CS3 = Close < Average[5000](close)CS4 = MACD[12,26,9](close) < 20IF Not ShortOnMarket and CS1 and CS2 and CS3 and CS4 and time >= noEntryBeforeTime and time <= noEntryAfterTime thenSellshort 1 contract at MarketSET STOP %LOSS 0.5SET TARGET %PROFIT 0.5EndIf01/21/2024 at 6:43 PM #226676Hi there sandelius,
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,
Peter1 user thanked author for this post.
01/21/2024 at 6:56 PM #226677But 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+. 😉
1 user thanked author for this post.
01/21/2024 at 7:44 PM #226678The 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.
1 user thanked author for this post.
01/21/2024 at 9:21 PM #226679You 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 spread123456789101112131415161718192021222324252627282930313233343536DefParam CumulateOrders = truedefparam preloadbars = 10000noEntryBeforeTime = 153000noEntryAfterTime = 220000// LONGC1 = Close > Average[210](Close)C2 = Close < Average[2](Close)if opentime=220000 thenF=openendifIF Not LongOnMarket and C1 and C2 and F>close and high[40]>close and opentime >= noEntryBeforeTime and opentime <= noEntryAfterTime thenBuy 1 contract at MarketSET STOP %LOSS 0.2SET TARGET %PROFIT 2EndIf// SHORTCS1 = Close < Average[210](Close)CS2 = Close > Average[2](Close)CS3 = Close < Average[5000](close)CS4 = MACD[12,26,9](close) < 20IF 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.5EndIf1 user thanked author for this post.
01/21/2024 at 9:41 PM #22668201/22/2024 at 11:50 AM #226713 -
AuthorPosts
Find exclusive trading pro-tools on