Download MetaTrader 5

[Help]Martingale function

To add comments, please log in or register
Did you like the article? Share it with your friends!
Achmad Hidayat
14766
Achmad Hidayat 2012.08.03 23:08 

Dear all,

I have difficulty to write this martingale function from MQL4 to MQL5. Please help me to convert this script to MQL5.

double Calculate_Lots()
{
   double lots=Lots;
   double MinLots=NormalizeDouble(MarketInfo(Symbol(),MODE_MINLOT),2);
   double MaxLots=NormalizeDouble(MarketInfo(Symbol(),MODE_MAXLOT),2);
   Lots=NormalizeDouble(AccountBalance()*Risk/100000,2); else Lots=Lots;
   
   Loss = 0;
   int round = MathAbs(MathLog(MinLots) / MathLog(10.0)) + 0.5;
   int total = OrdersHistoryTotal();
   for (int i = 0; i < total; i++) 
   {
      OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) 
      {
         if (OrderProfit() > 0) 
         {
            lots = Lots;
            Loss = 0;
         } else {
            lots = lots * Multiplier;
            Loss++;
         }
      }
      lots = NormalizeDouble(lots, round); else lots = Lots;
   }
   else
   lots=Lots;
   if (lots < MinLots) lots = MinLots;
   if (lots > MaxLots) lots = MaxLots;
   return (lots);
}
onewithzachy
2271
onewithzachy 2012.08.04 00:28  
achidayat:

Dear all,

I have difficulty to write this martingale function from MQL4 to MQL5. Please help me to convert this script to MQL5.

Hi achidayat,

Please don't ask for free code anymore, you asked that a lot around here.  Show the MQL5 code that you have written so far and we'll be more vthan happy to help you coding it.

 

 

Achmad Hidayat
14766
Achmad Hidayat 2012.08.04 01:50  
onewithzachy:

Hi achidayat,

Please don't ask for free code anymore, you asked that a lot around here.  Show the MQL5 code that you have written so far and we'll be more vthan happy to help you coding it.

 

 

Ok I'am sorry and thank you for your warning. I've tried to convert it to mql5, but not working properly :

double Calculate_Lots() {
   double lots=Lots;
   double Loss;
   double MinLots=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);
   double MaxLots=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);

   lots=AccountInfoDouble(ACCOUNT_BALANCE)*Risk/100000; else lots=Lots;   

   Loss = 0;   
   int deals=HistoryDealsTotal();
   for(int i=0;i<deals;i++)
   {
      ulong ticket=HistoryDealGetTicket(i);
      ENUM_DEAL_ENTRY entry_type=(ENUM_DEAL_ENTRY)HistoryDealGetInteger(ticket,DEAL_ENTRY);
      if(entry_type==DEAL_ENTRY_OUT)
      if (HistoryDealGetDouble(ticket,DEAL_PROFIT)>0)
         {
            lots = Lots;
            Loss = 0;
         } else {
            lots = lots * Multiplier;
            Loss++;
         }
      }
      lots = NormalizeDouble(lots, round); else lots = Lots;
   }
   else
   lots=Lots;
   if (lots < MinLots) lots = MinLots;
   if (lots > MaxLots) lots = MaxLots;
   return (lots);
}
onewithzachy
2271
onewithzachy 2012.08.06 00:19  
achidayat:

Ok I'am sorry and thank you for your warning. I've tried to convert it to mql5, but not working properly :

Hi achidayat,

My apology for late reply, I was busy and I think I gonna be busy again. I don't recommend using Martingale for trading. From what I've read for so many years many loses came from this type of trading, its a trading technique that more close to gamble than to actual trading.

However let's talk about the code, both mql4 and mql5 code are poorly written. There are error in its syntax and its logic also does not makes any sense, it actually even worse than Martingale.

Let me clear what this code is trying to achieve, whe are we multiply the lot size, is it when the previous trade is loss or profit ?.


Achmad Hidayat
14766
Achmad Hidayat 2012.08.06 07:57  
onewithzachy:

Hi achidayat,

My apology for late reply, I was busy and I think I gonna be busy again. I don't recommend using Martingale for trading. From what I've read for so many years many loses came from this type of trading, its a trading technique that more close to gamble than to actual trading.

However let's talk about the code, both mql4 and mql5 code are poorly written. There are error in its syntax and its logic also does not makes any sense, it actually even worse than Martingale.

Let me clear what this code is trying to achieve, whe are we multiply the lot size, is it when the previous trade is loss or profit ?.


Of course multiply the lot size when previous trade loss.

In MT4, this code work properly.

onewithzachy
2271
onewithzachy 2012.08.06 10:15  
achidayat:

Of course multiply the lot size when previous trade loss.

In MT4, this code work properly.

Dear achidayat.

In MT4 its not even compiled, and the logic is an error.

 

Pawel Wojnarowski
685
Pawel Wojnarowski 2012.08.06 10:33  
onewithzachy:

Dear achidayat.

In MT4 its not even compiled, and the logic is an error.

 

Martingale (increase lot size on losses) is based on something totally opposite to verified, much more secure position sizing approach (reduce lot size on losses, increase lot size on wins). What most traders do not understand is that it is not the indicator or other signal source that is the most important, but the proper risk management and position sizing.

For more refer to http://www.amazon.com/Trade-Your-Way-Financial-Freedom/dp/007147871X/ref=la_B000APL8YW_1_1?ie=UTF8&qid=1344241808&sr=1-1 - plus I would especially recommend playing Van Tharp's "Secrets of the Masters" trading simulation game. It really opens your eyes with regard to trading.

Trade Your Way to Financial Freedom
Trade Your Way to Financial Freedom
  • reviews: 98
  • 24.06 USD
  • www.amazon.com
“Sound trading advice and lots of ideas you can use to develop your own trading methodology.”-Jack Schwager, author of Market Wizards and The New Market Wizards This trading masterpiece has been fully updated to address all the concerns of today's market environment. With...
Achmad Hidayat
14766
Achmad Hidayat 2012.08.06 17:03  
onewithzachy:

Dear achidayat.

In MT4 its not even compiled, and the logic is an error.

 

I mean this code that work and compiled properly on MT4 :

double GetLots() 
{
   double lots=MyLots;
   double MinLots=NormalizeDouble(MarketInfo(Symbol(),MODE_MINLOT),2);
   double MaxLots=NormalizeDouble(MarketInfo(Symbol(),MODE_MAXLOT),2);
   if (MM) MyLots=NormalizeDouble(AccountBalance()*Risk/LotSize,LotsDigit); else MyLots=Lots;
   
   if (UseMartingale)
   {
      Loss = 0;
      int round = MathAbs(MathLog(MinLots) / MathLog(10.0)) + 0.5;
      int total = OrdersHistoryTotal();
      for (int i = 0; i < total; i++) 
      {
         OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) 
         {
            if (OrderProfit() > 0) 
            {
               lots = MyLots;
               Loss = 0;
            } else {
               lots = lots * Multiplier;
               Loss++;
            }
         }
      }
      if (Loss<=MaxLossOrder && Loss>0) lots = NormalizeDouble(lots, round); else lots = MyLots;
   }
   else
   lots=MyLots;
   if (lots < MinLots) lots = MinLots;
   if (lots > MaxLots) lots = MaxLots;
   return (lots);
}
onewithzachy
2271
onewithzachy 2012.08.07 00:27  
achidayat:

I mean this code that work and compiled properly on MT4 :

Hi achidayat,

Yes, thank you for posting a slightly different code that almost compile, may as well give those variable a type so it's really compile.

It compile but it does not run properly, this is the correct one, The return of OrderSelect() is true or false and we must check it

the logic code to search for the last order also dead wrong, That code won't select last order anyway. 

We can use  datetime open_time as global variable.

I hope you read what Enigma71fx wrote over there 

Enigma71fx 2012.08.06 10:33 

 Martingale (increase lot size on losses) is based on something totally opposite to verified, much more secure position sizing approach (reduce lot size on losses, increase lot size on wins). What most traders do not understand is that it is not the indicator or other signal source that is the most important, but the proper risk management and position sizing.  ...

 Martingale is not a proper risk management and position sizing. Here's some reading on forex Martingale http://www.investopedia.com/articles/forex/06/martingale.asp

 static datetime open_time;
   if (UseMartingale)
      {
      Loss = 0;
      int round = MathAbs(MathLog(MinLots) / MathLog(10.0)) + 0.5;
      int total = OrdersHistoryTotal();
      for (int i = 0; i < total; i++) 
         {
         if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == true
             && OrderSymbol() == Symbol() 
             && OrderMagicNumber() == Magic) 
            {
            if (OrderOpenTime() > open_time)
              {
              open_time = OrderOpenTime();
              if (OrderProfit() > 0) 
                 {
                 lots = MyLots;
                 Loss = 0;
                 } else {
                 lots = lots * Multiplier;
                 Loss++;
                 }
              }
            }
         }
      if (Loss<=MaxLossOrder && Loss>0) lots = NormalizeDouble(lots, round); else lots = MyLots;
      }

 

Forex Trading The Martingale Way
Forex Trading The Martingale Way
  • Kathy Lien
  • www.investopedia.com
Would you be interested in a trading strategy that is practically 100% profitable? Most traders will probably reply with a resounding, "Yes!" Amazingly, such a strategy does exist and dates all the way back to the 18th century. This strategy is based on probability theory, and if your pockets are deep enough, it has a near-100% success rate...
Achmad Hidayat
14766
Achmad Hidayat 2012.08.07 13:39  
onewithzachy:

Hi achidayat,

Yes, thank you for posting a slightly different code that almost compile, may as well give those variable a type so it's really compile.

It compile but it does not run properly, this is the correct one, The return of OrderSelect() is true or false and we must check it

the logic code to search for the last order also dead wrong, That code won't select last order anyway. 

We can use  datetime open_time as global variable.

I hope you read what Enigma71fx wrote over there 

 Martingale is not a proper risk management and position sizing. Here's some reading on forex Martingale http://www.investopedia.com/articles/forex/06/martingale.asp

 

Thank you for your correction. Then how to convert to mq5? Sorry for my request.
To add comments, please log in or register