Download MetaTrader 5

Problems in coding Martingale

To add comments, please log in or register
Millions of traders download codes from MQL5 library. Share your app with them!
Icarus
192
Icarus 2013.12.28 14:57 

Dears,

Im trying to implement a simple martingale strategy, that is, every time I have a deal with a loss, the next deal will have twice the volume of the previous.

I have no clue about why it is not working, I have tryed to work with positions, instead of deal, but got an error message.

Thanks in advance for the help! :)

Im trying this way:


if(HistoryDealGetDouble(mresult.deal,DEAL_PROFIT)<0) {mrequest.volume=HistoryDealGetDouble(mresult.deal,DEAL_VOLUME)*2;}
Alain Verleyen
Moderator
30707
Alain Verleyen 2013.12.28 15:21  
Icarus:

Dears,

Im trying to implement a simple martingale strategy, that is, every time I have a deal with a loss, the next deal will have twice the volume of the previous.

I have no clue about why it is not working, I have tryed to work with positions, instead of deal, but got an error message.

Thanks in advance for the help! :)

Im trying this way:


How do you select the history before using HistoryDealGetDouble() ?
Documentation on MQL5: Trade Functions / HistoryDealGetDouble
Documentation on MQL5: Trade Functions / HistoryDealGetDouble
  • www.mql5.com
Trade Functions / HistoryDealGetDouble - Documentation on MQL5
Icarus
192
Icarus 2013.12.28 16:16  
angevoyageur:
How do you select the history before using HistoryDealGetDouble() ?

Hi, angevoyageur!

Actually I was not doing that, lol   :P

After your comment I tryed this:


         ulong lastDeal=  mresult.deal; 
         
         if(HistoryDealGetDouble(lastDeal,DEAL_PROFIT)<0) {mrequest.volume=HistoryDealGetDouble(lastDeal,DEAL_VOLUME)*2;}
         
         OrderSend(mrequest,mresult);


Again it didnt work.

I really dont know if I dont know yet how to use the correct function to the problem, or if I should take a differente approach. For example, Im no sure if the deal number I should use, must be the deal number in the sell operation (which closes the position, assuming I have already a buy position), or the same number of the buy order. I tried both without success. :(

Alain Verleyen
Moderator
30707
Alain Verleyen 2013.12.28 17:12  
Icarus:

Hi, angevoyageur!

Actually I was not doing that, lol   :P

After your comment I tryed this:



Again it didnt work.

I really dont know if I dont know yet how to use the correct function to the problem, or if I should take a differente approach. For example, Im no sure if the deal number I should use, must be the deal number in the sell operation (which closes the position, assuming I have already a buy position), or the same number of the buy order. I tried both without success. :(

You have to use HistorySelect() or HistorySelectByPosition() before using HistoryDealGetDouble().

See second part of the following article for full explanation.

Orders, Positions and Deals in MetaTrader 5
Orders, Positions and Deals in MetaTrader 5
  • 2011.02.01
  • MetaQuotes Software Corp.
  • www.mql5.com
The ultimate goal of a trader is to extract profits through the means of trading operations on the financial markets. This article describes the terms and processes of the MetaTrader 5 trading platform, the knowledge of which is necessary for a proper understanding of the work of trade functions of the MQL5 language. Orders — are the trade...
Jose Miguel Soriano
4957
Jose Miguel Soriano 2013.12.30 23:42  
Icarus:

Dears,

Estoy tratando de poner en práctica una estrategia martingala simple, es decir, cada vez que tengo un trato con una pérdida, el siguiente acuerdo tendrá el doble del volumen de la anterior.

No tengo ni idea de por qué no está funcionando, He intentado trabajar con las posiciones, en vez de acuerdo, pero nos dieron un mensaje de error.

Gracias de antemano por la ayuda! :)

Im tratando de esta manera:


The simplest is to control the outcome of the open position: if negative doubles the previous lot. Before we define the variable volLot as global or at least visible to the module in which we work. For example... 

double result= resultNetoPosicion(simb);
if(result<0) volLot= volLot*2;
//--------------------------------- RESULTADO NETO DE POSICION ------------------------------------
double resultNetoPosicion(string simb)
  {
   double resp= 0;
   if(PositionSelect(simb)) resp= PositionGetDouble(POSITION_PROFIT)+
                                  PositionGetDouble(POSITION_SWAP)+
                                  PositionGetDouble(POSITION_COMMISSION);
   return(resp);
  }
Icarus
192
Icarus 2014.01.03 17:19  

angevoyageur and josemiguel1812,

sorry for the late answer, I was very busy due the hollydays.

Thank you guys SO MUCH for the answers!!! Im going to try to program both in this weekend, then I post the result. :)

Best wishes! 

Icarus
192
Icarus 2014.06.11 22:45  

Guys, after a long time away from forex, I finally started to implement the bloody martingale.

First I did for systems that use only stoploss and take profits and exit criteria. Like this:



   double ultimopreco = MinhaOperacao.mrate[0].close;
   double meustop = MinhaOperacao.mrequest.sl;
   double meutp = MinhaOperacao.mrequest.tp;



   if(ultimopreco < meustop)
  {
   Lote = Lote * 2;
  }
 
   if(ultimopreco > meutp)
   {
    Lote = 0.1;
   }

I simply put that lines before the function that send the order.

Translating what I did (for long positions): if the last price < stoploss (then, if it happened I should be stopped with a loss), then double the last lot used for the next order, when it comes;

if the last price > takeprofit, then, I should have closed the position with profit, then turn back the lot to the initial amount.


Im having hard time in figure out how to work with deals, instead of positions or orders. If one wants to get track of the last profit or loss, this should be the way to go, isnt it?

Josemiguel1812, thank you so much for your solution, Im trying to addapt. The thing is it only buys twice the volume. For instance, if you have 4 losses in a row, the system will buy 1 lot, 2, 2 and 2 lots.

Best wishes,

To add comments, please log in or register