Dear Nicolas,
I am pleased to share the following strategy.
The trade is triggered by the MACD histo / TF – 2mn / Instrument: DAX, 1eur.
I stand ready for your kind feedback.
IMPORTANT: Great forum, thx to Nicolas, Maz, Ale, Maverick 😉 , and everyone else for your ideas and bit of codes !
DJ
//
// Définition des paramètres du code
DEFPARAM CumulateOrders = False
// Cumul des positions désactivé
// Annule tous les ordres en attente et ferme toutes les positions à 0:00, puis empêche toute création d'ordre avant l'heure "FLATBEFORE".
DEFPARAM FLATBEFORE = 091500
// Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"
DEFPARAM FLATAFTER = 220000
// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position avant l'heure spécifiée
noEntryBeforeTime = 091500
timeEnterBefore = time >= noEntryBeforeTime
// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position après l'heure spécifiée
noEntryAfterTime = 220000
timeEnterAfter = time < noEntryAfterTime
// Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiés
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Conditions pour ouvrir une position acheteuse
indicator1 = MACDline[16,26,9](close)
c1 = (indicator1 > 0)
indicator2 = MACD[16,26,9](close)
c2 = (indicator2 CROSSES OVER 0)
IF (c1 AND c2) AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
BUY 1 CONTRACT AT MARKET
ENDIF
//trailing stop
trailingstop = 28
//resetting variables when no trades are on market
if not onmarket then
MAXPRICE = 0
priceexit = 0
endif
//case LONG order
if longonmarket then
MAXPRICE = MAX(MAXPRICE,close) //saving the MFE of the current trade
if MAXPRICE-tradeprice(1)>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then
priceexit = MAXPRICE-trailingstop*pointsize //set the exit price at the MFE - trailing stop price level
endif
endif
//exit on trailing stop price levels
if onmarket and priceexit>0 then
EXITSHORT AT priceexit STOP
SELL AT priceexit STOP
endif
SET STOP ploss 120
SET TARGET pPROFIT 200
Thanks Inertia. I moved the topic to ProOrder section and add the code with the correct button to insert code 🙂
It’s great you made the walk forward analysis! So the only setting you optimized is the “trailingstop” variable? Any change to get a short version to see how it goes?
Thank you Nicolas. Yes for the “trailingstop”, and replaced the MACD 12 26 9 by 16 26 9…Is that the correct way to do a WF ?
Attached the short version…How does this sounds like ?
DJ
//Dax 15mn / short only
// Définition des paramètres du code
DEFPARAM CumulateOrders = False // Cumul des positions désactivé
// Annule tous les ordres en attente et ferme toutes les positions à 0:00, puis empêche toute création d'ordre avant l'heure "FLATBEFORE".
DEFPARAM FLATBEFORE = 091500
// Annule tous les ordres en attente et ferme toutes les positions à l'heure "FLATAFTER"
DEFPARAM FLATAFTER = 214500
// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position avant l'heure spécifiée
noEntryBeforeTime = 091500
timeEnterBefore = time >= noEntryBeforeTime
// Empêche le système de placer des ordres pour entrer sur le marché ou augmenter la taille d'une position après l'heure spécifiée
noEntryAfterTime = 214500
timeEnterAfter = time < noEntryAfterTime
// Empêche le système de placer de nouveaux ordres sur les jours de la semaine spécifiés
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Conditions pour ouvrir une position en vente à découvert
indicator1 = MACD[12,26,9](close)
c1 = (indicator1 CROSSES UNDER 0)
indicator2 = MACDline[12,26,9](close)
c2 = (indicator2 < 0)
indicator3 = ExponentialAverage[2000](close)
c3 = (close < indicator3)
indicator4 = ExponentialAverage[2000](close)
indicator5 = ExponentialAverage[2000](close)
c4 = (indicator4 < indicator5[1])
IF (c1 AND c2 AND c3 AND c4) AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
//trailing stop
trailingstop = 28
//resetting variables when no trades are on market
if not onmarket then
MAXPRICE = 0
priceexit = 0
endif
//case LONG order
if longonmarket then
MAXPRICE = MAX(MAXPRICE,close) //saving the MFE of the current trade
if MAXPRICE-tradeprice(1)>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then
priceexit = MAXPRICE-trailingstop*pointsize //set the exit price at the MFE - trailing stop price level
endif
endif
//exit on trailing stop price levels
if onmarket and priceexit>0 then
EXITSHORT AT priceexit STOP
SELL AT priceexit STOP
endif
SET STOP ploss 62//
SET TARGET pPROFIT 250//
Dear Nicolas,
Attached is the best I can do: => Stop Loss set to 45 and No target (No Limits !!)
OK?
Thanks for sharing! Is it really possible to have both trailing an stop loss in the same code?
Hi T-rader.
Yes at first. Then it works roughly like a OCO order. Once the value of the trailing stop gain in profit, the original stop will be cxl’d to be moved to the trailing value.
Hope this is clear. 😉
https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
Tx.
I made videos in French to explain the WFA. If you made a backtest for the whole history with optimized variables of one of your OOS period, I think you did not understand how it should work 🙂
Anyway, I think we should be careful with the result of the WF test you made for a main reason, most of the Out Of Sample results have huge WFE (>200%).
Why did you made the test of the short version on a 15 minutes timeframe? While the long one is designed for the 2 minutes one?
Thank you Nicolas. WF’s feedback much appreciated. I’ll have a look asap.
Short version: It doesn’t work well with the TF 2mn. The 15TF works much better for this kind of signal.
The idea would be to start both strategies at once and one may partially cover the losses of the other.
But, agree, it is not a perfect TF match Long Short…
Based on your experience, don’t you think that these 2 strategies, despite their different TF deserve a try ?
Tx again for your time and kind help on it.
DJ
Well, the spread is not the same after 170000, you should reduce the schedules of the strategy or add at least 1 point to the spread in backtests.
I just realized that your short version can’t work effectively because your trailing stop function doesn’t include the short orders but only the long ones!
OMG 🙁 – but thank you…
1 pts added (total of 2).
Attached the versions with and without trailing stop.
Same amount of draw downs in both case … My choice goes logically to the most profitable one.
I will WF it and put them in a demo account see how it goes.
Thank you Nicolas.
The trailing stop version for short orders can be easily found in forums or directly into comments of the blog post related to it.