Daily Martingale code

Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #56569 quote
    Juan Salas
    Participant
    Master

    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.

    // GRIDDIARIO20 5' DAX
    //----------------------------
    
    // PARAMETERS
    DEFPARAM CumulateOrders = false
    DEFPARAM Flatbefore = 090000
    DEFPARAM Flatafter = 210000
    
    // DAILY operations. It resets the variable each new DAY
    IF INTRADAYBARINDEX = 0 THEN
    nomoretrading = 0
    ENDIF
    
    // NO MORE TRADING per DAY if one operation is POSITIVE (20 pips)
    IF positionperf(1)>0 THEN
    nomoretrading=1
    ENDIF
    
    // Positioning
    
    perdida1 = positionperf(1)<0
    perdida2 = positionperf(1)<0 and positionperf(2)<0
    perdida3 = positionperf(1)<0 and positionperf(2)<0 and positionperf(3)<0
    perdida4 = positionperf(1)<0 and positionperf(2)<0 and positionperf(3)<0 and positionperf(4)<0
    perdida5 = positionperf(1)<0 and positionperf(2)<0 and positionperf(3)<0 and positionperf(4)<0 and positionperf(5)<0
    perdida6 = positionperf(1)<0 and positionperf(2)<0 and positionperf(3)<0 and positionperf(4)<0 and positionperf(5)<0 and positionperf(6)<0
    
    IF INTRADAYBARINDEX = 0 THEN
    positionsize=1
    ENDIF
    
    IF perdida1 THEN
    positionsize=2
    ENDIF
    
    IF perdida2 THEN
    positionsize=4
    ENDIF
    
    IF perdida3 THEN
    positionsize=8
    ENDIF
    
    IF perdida4 THEN
    positionsize=16
    ENDIF
    
    IF perdida5 THEN
    positionsize=32
    ENDIF
    
    IF perdida6 THEN
    positionsize=32
    ENDIF
    
    // STOP LOSS and PROFITs
    SET STOP pLOSS loss1
    SET TARGET pPROFIT profit1
    
    profit1 = 20
    loss1 = 20
    
    // Distancia GRID
    disgrid1 = 10
    
    
    // Determinición niveles GRID
    IF time=090000 AND nomoretrading=0 THEN
    levelZERO = close
    ENDIF
    
    level1up = levelZERO + disgrid1*pipsize
    //level2up = level0 + disgrid2*pipsize
    
    level1down = levelZERO - disgrid1*pipsize
    //level2down = level0 - disgrid2*pipsize
    
    // ORDERS //////////////////////////////////////////////////////////////////////////////////////////////////////////
    IF nomoretrading=0 THEN
    BUY positionsize CONTRACT AT level1up STOP
    SELLSHORT positionsize CONTRACT AT level1down STOP
    ENDIF
    

    Thanks,

    Juan

    #56572 quote
    Juan Salas
    Participant
    Master

    Before 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.

    #56592 quote
    Eric
    Participant
    Master

    I 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)

    #56593 quote
    Eric
    Participant
    Master

    Another 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

    #56594 quote
    verdi55
    Participant
    Veteran

    Try the following in replacement of line 14-17 of your system :

    // NO MORE TRADING per DAY if one operation is POSITIVE (20 pips)
    diffTiBi = barindex - tradeindex
    diffTiBi1 = barindex - tradeindex(2)
    c1 = positionperf(1) > 0
    
    IF (c1 and date[diffTiBi] = date and (not onmarket)) OR (c1 and date[diffTiBi1] = date and onmarket) THEN
    nomoretrading=1
    ENDIF

    However, the result looks like a conventional Martingale system : long periods with nice gains and a few catastrophic events that destroy everything.

    Juan Salas thanked this post
    #56599 quote
    Juan Salas
    Participant
    Master

    Hi 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

    #56600 quote
    verdi55
    Participant
    Veteran

    Same 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.

    Juan Salas thanked this post
    #56641 quote
    Juan Salas
    Participant
    Master

    Hi 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

    #56676 quote
    JR1976
    Participant
    Veteran

    HI ,

    timeframe 1 min?

    #56679 quote
    Juan Salas
    Participant
    Master

    Hi 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.

    JR1976 thanked this post
    Screen-Shot-2017-12-27-at-19.11.57.png Screen-Shot-2017-12-27-at-19.11.57.png ZZZ-GRIDDIARIO20-5-DAX-market.itf
    #56685 quote
    verdi55
    Participant
    Veteran

    This is my try with 200.000 bars :

    #56686 quote
    verdi55
    Participant
    Veteran

    Oh, the attachments…[attachment file=56687]

    Juan Salas thanked this post
    DAX-5-Minuten.png DAX-5-Minuten.png
    #56689 quote
    Nicolas
    Keymaster
    Master

    Martingale 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 🙂

    #56695 quote
    Juan Salas
    Participant
    Master

    Hi 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

    #56842 quote
    juanj
    Participant
    Master

    Have you had a look at this one: https://www.gridtrendmultiplier.com/

    Have bookmarked it some time ago in order to code something similar when I have some time but never seem to get to it.

Viewing 15 posts - 1 through 15 (of 19 total)
  • You must be logged in to reply to this topic.

Daily Martingale code


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Juan Salas @juan-salas Participant
Summary

This topic contains 18 replies,
has 6 voices, and was last updated by Juan Salas
8 years, 2 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 12/26/2017
Status: Active
Attachments: 3 files
Logo Logo
Loading...