Daily Martingale code
Forums › ProRealTime English forum › ProOrder support › Daily Martingale code
- This topic has 18 replies, 6 voices, and was last updated 7 years ago by
Juan Salas.
-
-
12/26/2017 at 4:41 PM #56569
Dear Colleagues,
I am trying to put together a simple Daily Martingale Code.
The system start at 090000 every day a takes the price at that time as reference (levelZERO) for the rest of the day.
If the price goes up from levelZERO and pass the 10 pips up (level1up) opens a BUY STOP order with a SL=20 and PF=20
If the price goes down from levelZERO and pass the 10 pips down (level1down) opens a SELLSHORT STOP order with a SL=20 and PF=20
Every time our operation goes wrong and start again, the Martingale doubles the position size.
The idea is to create and area of 30 pips up and 30 pips down in which, if the price trespass those levels up or down, it is a winning operation (20 pips) and then no more trading for the day.
We all know the risk of the Martingale or any other systems based on the accumulation of orders, but since the operations are close daily and the range is 30 pips up and down from the starting price (levelZERO) at 090000, I felt very curious to see the long term results.
I have put together the code below, but it seems that after the first operations, it stops. I know there is a way to keep the STOP orders active in the following bars but I don’t remember how it goes. Everything I tried is not working, so I will leave the code here in case anyone finds the problem.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182// GRIDDIARIO20 5' DAX//----------------------------// PARAMETERSDEFPARAM CumulateOrders = falseDEFPARAM Flatbefore = 090000DEFPARAM Flatafter = 210000// DAILY operations. It resets the variable each new DAYIF INTRADAYBARINDEX = 0 THENnomoretrading = 0ENDIF// NO MORE TRADING per DAY if one operation is POSITIVE (20 pips)IF positionperf(1)>0 THENnomoretrading=1ENDIF// Positioningperdida1 = positionperf(1)<0perdida2 = positionperf(1)<0 and positionperf(2)<0perdida3 = positionperf(1)<0 and positionperf(2)<0 and positionperf(3)<0perdida4 = positionperf(1)<0 and positionperf(2)<0 and positionperf(3)<0 and positionperf(4)<0perdida5 = positionperf(1)<0 and positionperf(2)<0 and positionperf(3)<0 and positionperf(4)<0 and positionperf(5)<0perdida6 = positionperf(1)<0 and positionperf(2)<0 and positionperf(3)<0 and positionperf(4)<0 and positionperf(5)<0 and positionperf(6)<0IF INTRADAYBARINDEX = 0 THENpositionsize=1ENDIFIF perdida1 THENpositionsize=2ENDIFIF perdida2 THENpositionsize=4ENDIFIF perdida3 THENpositionsize=8ENDIFIF perdida4 THENpositionsize=16ENDIFIF perdida5 THENpositionsize=32ENDIFIF perdida6 THENpositionsize=32ENDIF// STOP LOSS and PROFITsSET STOP pLOSS loss1SET TARGET pPROFIT profit1profit1 = 20loss1 = 20// Distancia GRIDdisgrid1 = 10// Determinición niveles GRIDIF time=090000 AND nomoretrading=0 THENlevelZERO = closeENDIFlevel1up = levelZERO + disgrid1*pipsize//level2up = level0 + disgrid2*pipsizelevel1down = levelZERO - disgrid1*pipsize//level2down = level0 - disgrid2*pipsize// ORDERS //////////////////////////////////////////////////////////////////////////////////////////////////////////IF nomoretrading=0 THENBUY positionsize CONTRACT AT level1up STOPSELLSHORT positionsize CONTRACT AT level1down STOPENDIFThanks,
Juan
12/26/2017 at 4:47 PM #56572Before anyone point it out, I know that the Martingale code is a little unconventional and there is another one here in the library shorter and simplified, but I understand this much better and it is easier for me to change it and modify it in case I want to create a reverse or other type of Martingale.
12/26/2017 at 11:02 PM #56592I am not sure but could it be that you open the trades with stoporders that causes the problem? (using stoporders and positionperf)
Its an interesting approach, i have tried something similar but never did get it to work, ( but i am not any good at coding)
12/26/2017 at 11:30 PM #56593Another thing i been thinking about but i doubt it can be done with proorder?
First of all you need a account with hedging allowed
If you start with a box of 30 points or a inside bar or whatever
and quit if profit is 60 points
then you open 1 buy at boxhigh (no stop) and if it reach boxlow before take profit
then you open 2 sell (no stop)
and next 3 buy and so on
you just keep all trades open and hedge so that you have only 1 extra open buy or sell and wait for take profit and close all
most of the time you only need 1 buy or maybe one sell open before TP
of course TP could be 1:1 but then you need to double size each time
the thing is you lower the risk and the margin is lower when hedging
and less slippage because less stoporders
12/26/2017 at 11:48 PM #56594Try the following in replacement of line 14-17 of your system :
12345678// NO MORE TRADING per DAY if one operation is POSITIVE (20 pips)diffTiBi = barindex - tradeindexdiffTiBi1 = barindex - tradeindex(2)c1 = positionperf(1) > 0IF (c1 and date[diffTiBi] = date and (not onmarket)) OR (c1 and date[diffTiBi1] = date and onmarket) THENnomoretrading=1ENDIFHowever, the result looks like a conventional Martingale system : long periods with nice gains and a few catastrophic events that destroy everything.
1 user thanked author for this post.
12/27/2017 at 2:48 AM #56599Hi Verdi and Eric,
Thanks so much for your suggestions and for your piece of code. It works fine, and pretty much what I expected of a Martingale: Nice periods of steady gain and sudden drops.
Nevertheless, it is not quite what I want. I had the idea of doing the daily martingale. It means that at the end of the day the accumulation is over and next day start when positionsize=1. I realised that many of the drops are due to the accumulation of contracts through different days, which is very plausible. On the contrary, if we reduced the martingale to just one day, the probability of failing more than 4-5 consecutive operations (critical number) in one day, when a simple grid is rare.
I have tried to reset the number of contracts every day by:
IF time=010000 THEN
positionsize=1
ENDIF
It doesn’t work.
Any suggestions??
Thanks,
Juan
12/27/2017 at 3:21 AM #56600Same problem as before : positionperf(x) keeps counting the performance of every position, whenever it was. So, if positionperf(6)<0 occured, say 6 days ago, it will still reset positionsize to a higher value than 1. You need to find a way that only the performance of positions of the same day (not of the days before) are counted for the calculation of positionsize, just as I did it in my short code.
“IF (positionperf(1) > 0 and date[diffTiBi] = date and (not onmarket)) OR (positionperf(1) > 0 and date[diffTiBi1] = date and onmarket)” only counts the positionperf of positions that were closed on the same day.
1 user thanked author for this post.
12/27/2017 at 2:35 PM #56641Hi Verdi,
Thanks, I will try it this afternoon. If this work, it is going to be very useful for me in the future, because it is a problem/condition that I have been having in my last codes and didn’t know how to solve it.
Let you of the results later,
Thanks,
Juan
12/27/2017 at 6:57 PM #5667612/27/2017 at 7:19 PM #56679Hi JR1976,
I am using 5 min, but the smaller the better, although you will have to get a decent backtest due to the limited past.
I have changed the orders at market, since does not interfere with the reading of the Martingale. The result attached are not bad, but like any Martingale subject to any sudden drawdown. Anyway, I wanted to try and see the results since I am exploring different ways to approach strategies, different than pure Technical Analysis.
Regards,
Juan
P.S.- I am having your code “Riding the trend/SP500” on REAL, and it is going fairly well. Thanks.
1 user thanked author for this post.
12/27/2017 at 7:50 PM #5668512/27/2017 at 7:51 PM #566861 user thanked author for this post.
12/27/2017 at 8:01 PM #56689Martingale will fail, whatever conditions or filters you’ll add in your strategy. The backtests’ results (or equity curve looks) depends mainly of when you start the strategy, it is all clear in the picture that verdi55 has attached. Anyway, this is a very good coding exercise 🙂
12/27/2017 at 8:40 PM #56695Hi Verdi,
Thanks for the 200k. No doubt about what Nicolas said. I would have never put this in REAL because any day may be the last one (literally), but I am coding and studying new different types of grid strategies, and part of this code is going to be very useful for the future.
Regards,
Juan
12/29/2017 at 9:22 PM #56842 -
AuthorPosts
Find exclusive trading pro-tools on