Hi everyone,
The name of the strategy is obviously ironic and conceptually wrong.
Anyway, I practiced writing this strategy, but it doesn’t work because I have two error messages on lines 4 and 7. I would ask you to help me fix the code and make any changes you think are necessary if you believe it can be improved.
Specifically, the strategy follows the market trend when the 50-period EMA is above the 100-period EMA, the ADX is above 20, the RSI is above 50, the MACD is above zero, the Stochastic is above 20, and the price is above the 20-period Bollinger moving average. In this case, a long position is opened with a market order, while the stop loss is set at twice the 14-period ATR below the market price and the profit target is set at three times the 14-period ATR above the market price.
When the market is choppy, the strategy adopts a more cautious trading strategy. When the price crosses the Bollinger range, a long or short position is opened . In this case, the stop loss is set at twice the 14-period ATR above or below the market price and the profit target is set at three times the 14-period ATR above or below the market price.
At least , I believe that what I have written outlines these rules. Sorry, but I am learning while having fun.
Edo
// Define Parameters
DEFPARAM CumulateOrders = False
DEFPARAM PreloadBars = 500
DEFPARAM OrderCancelDelay = 1
// Define Indicators
Indicator1 = EMA[50](close)
Indicator2 = EMA[100](close)
Indicator3 = ADX[14](14)
Indicator4 = RSI[14](close)
Indicator5 = Bollinger[20](close)
Indicator6 = MACD[12,26,9](close)
Indicator7 = Stochastic[14,3,3](close)
Indicator8 = ATR[14](close)
// Trend Following Strategy
If Indicator1 > Indicator2 and Indicator3 > 20 and Indicator4 > 50 and Indicator6 > 0 and Indicator7 > 20 and close > Indicator5.Middle then
BUY 1 CONTRACT AT MARKET
SET STOP LOSS 2 * Indicator8 BELOW MARKET
SET TARGET PROFIT 3 * Indicator8 ABOVE MARKET
// Choppy Market Strategy
ElseIf Indicator5.Upper > Indicator5.Lower and Indicator6 < 0 and Indicator7 < 80 then
If close crosses above Indicator5.Upper Then
BUY 1 CONTRACT AT MARKET
SET STOP LOSS 2 * Indicator8 BELOW MARKET
SET TARGET PROFIT 3 * Indicator8 ABOVE MARKET
ElseIf close crosses below Indicator5.Lower Then
SELL SHORT 1 CONTRACT AT MARKET
SET STOP LOSS 2 * Indicator8 ABOVE MARKET
SET TARGET PROFIT 3 * Indicator8 BELOW MARKET
EndIf
EndIf
Hi Edo,
OrderCancelDelay is not a “system parameter” (I have no idea how to call these, set with DefParam), hence an error message on that one.
Then probably some message on line 7 because line 4 has to be corrected first.
What you intended with line 4 ? … no idea. 🙂 What was the idea about this line ?
The name of the title is perfect. Now get it working !
haha
Peter
Hi Peter,
eheh you’re right: the name of the strategy is perfect!…but , I say, the strategy itself… not exactly the same!
Anyhow, in line 4 the value of “1” in this case should mean that the order will be cancelled after one bar, so if the order is not filled immediately, it will be cancelled in the next bar. As per your suggestion, I deleted line 4 but error in line 7 remains..
ciao
Edo
Ah, I only now see it myself … EMA is not an existing function. Look here on how to do it : https://www.prorealcode.com/documentation/average/
in line 4 the value of “1” in this case should mean that the order will be cancelled after one bar
If that is what you want, then let your code start with Sell At Market. Or better :
If OnMarket then
Sell at Market // For a Long position.
Endif
OrderCancelDelay, Ema, Bollinger and ATR are not valid supported keywords.
OrderCancelDelay does not exist.
EMA must be replaced with Exponentialaverage[periods](source) or Average[periods,1](source).
Bollinger needs to be either BollingerUP[periods](source) or BollingerDown[periods](source).
ATR should be replaced with AverageTrueRange[periods](source).
Hello everyone,
I was finally able to finish my first code. I tested it on BTCUSD and I’m attaching a screenshot.
Thank you in advance to those who want to try to improve it, if it’s worth it, by sharing here any enhancements.
Edo
// Define Parameters
DEFPARAM CumulateOrders = False
DEFPARAM PreloadBars = 500
// Define Indicators
Indicator1 = Exponentialaverage[100](close)
Indicator2 = Exponentialaverage[150](close)[100]
Indicator3 = ADXR[14]
Indicator4 = RSI[14](close)
Indicator5 = BollingerUp[20](close)
Indicator6 = BollingerDown[20](close)
Indicator7 = BollingerBandWidth[20](close)
Indicator8 = MACD[12,26,9](close)
Indicator9 = Stochasticd[14,3,5](close)
Indicator10 = AverageTrueRange[14](close)
// Trend Following Strategy
If Indicator1 > Indicator2 and Indicator3 > 20 and Indicator4 > 50 and Indicator8 > 0 and Indicator9 > 20 and close > Indicator7 then
BUY 100 CONTRACT AT MARKET
SET STOP TRAILING 30 * Indicator10
// Choppy Market Strategy
ELSIF Indicator5 > Indicator6 and Indicator8 < 0 and Indicator9 < 80 then
If close CROSSES OVER Indicator5 Then
BUY 100 CONTRACT AT MARKET
SET STOP TRAILING 30 * Indicator10
ELSIF close CROSSES UNDER Indicator6 Then
SELLSHORT 50 CONTRACT AT MARKET
SET STOP TRAILING 30 * Indicator10
ENDIF
ENDIF
Congratulations ! Very well done.
Keep in mind that backtesting is an art in itself. For example, being Long on Bitcoin over that period of time does not lose easily. Hence, try the same over the past one year and it could be more difficult.
Still, well done because you applied your theories and they worked out. Looks easy but is not easy at all.
Hi Edoardo,
I’ve just seen your code and I’d like to backtest it.
Before that I’m asking you why, at line 7, you wrote [100]. What does it mean ?
Thanks for your reply and ………….. HAPPY EASTER TO EVEROBODY
Hi MaoRai54, and thank you for wanting to test my strategy.
If you can improve it, that’s great. The 100 on line 7 is a typo, feel free to delete it.
Happy Easter to you too!
Edo
Keep in mind that backtesting is an art in itself. For example, being Long on Bitcoin over that period of time does not lose easily. Hence, try the same over the past one year and it could be more difficult.
yes backtest is an Art, You can take almost any strategy and you will see that it will always be positive at some point, and that will be comforting to you and because of that you may do a lot of stupid things, You always have to look and look hard and understand what’s going on,
A little theory that might help you and it’s very simple, if for example your strategy is a Long strategy, then once you think your strategy is good you can try to backtest it on some periods in the past where you can see visually that the trend is down, if your Long strategy ends up with zero gains in a downtrend then I think you might have a winning strategy,
Be careful not to optimize your strategy lot for the bear market in question otherwise it is cheating
about your line :
DEFPARAM OrderCancelDelay = 1
If your goal is to buy only after the condition candle (Candle 1) then this is already the case with the ProRealTime code your order will be cancelled at the next candle (Candle 3) if it is not filled at the previous candle (Candle 2)