Download MetaTrader 5

EA not automatically increase lot size (although it worked in backtest)

To add comments, please log in or register
icedestyn
18
icedestyn  

I'm having problem in increasing lot size after winning (reverse martingale) .

The EA worked on backtest environment, but in real trading it's only placing same-lot-size-trades despite winning or losing.

I tried selecting the last ticket profit through SELECT_BY_POS as well as SELECT_BY_TICKET. None of them gave me the result I wanted.

Any help would be appreciated ! Thank u !

Here's my code :

//+------------------------------------------------------------------+

//|                                               HubbaHubbaVer1.mq4 |

//|                        Copyright 2017, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2017, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict


//

// INCLUDE FILE

//

#include <IndicatorValue.mqh>

#include <CandlePattern.mqh>

#include <StrategyTester.mqh>


extern double lot = 0.03f;

int lastOrder = -1;


int OnInit()

  {

   return(INIT_SUCCEEDED);

  }


void OnDeinit(const int reason)

  {

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

    if (//BUY CONDITION MET)

{

//also tried OrderSelect(OrderHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY)

               if (OrderSelect(lastOrder,SELECT_BY_TICKET)==TRUE)

                {

                  if (OrderProfit() > 0 && (float)NormalizeDouble(OrderLots(),2) < 0.04f)

// or  OrderLots() == 0.03 ; lot == 0.03  <== I tried these already

 {

                      lot = 0.05f;

                   }

                    else lot = 0.03f;

                }

  

               lastOrder =  OrderSend(Symbol(),OP_BUY,lot,Ask,3*Point,0,0,"EA",1234,0,clrRed);

               

              }

   

    else if(//SELL CONDITION MET)  

              {         

               if (OrderSelect(lastOrder,SELECT_BY_TICKET)==TRUE)

                {

                  if (OrderProfit() > 0 && (float)NormalizeDouble(OrderLots(),2) < 0.04f)

  {

                     lot = 0.05f;

                  }

                  else lot = 0.03f;

                }

  

lastOrder =  OrderSend(Symbol(),OP_SELL,lot,Bid,3*Point,0,0,"EA",1234,0,clrGreen);         

              }

        }

     }

  }


Martin Fischer
14907
Martin Fischer  
icedestyn:

I'm having problem in increasing lot size after winning (reverse martingale) .

The EA worked on backtest environment, but in real trading it's only placing same-lot-size-trades despite winning or losing.

I tried selecting the last ticket profit through SELECT_BY_POS as well as SELECT_BY_TICKET. None of them gave me the result I wanted.

Any help would be appreciated ! Thank u !



It looks like, that lastOrder is always -1.. Or?
You should position the brackets a little better. ;-)

Do you get ticket numbers? (Just try to print it...)
icedestyn
18
icedestyn  
Martin Fischer:


It looks like, that lastOrder is always -1.. Or?
You should position the brackets a little better. ;-)

Do you get ticket numbers? (Just try to print it...)
sorry, the brackets were auto-alignment, i didn't notice. 
As I mentioned, the EA also didn't work with OrderSelect by Pos. The lastOrder is just some last change I test to see whether it causes difference or not. I also got the ticket number as u asked :)
Thank u
Martin Fischer
14907
Martin Fischer  
icedestyn:
sorry, the brackets were auto-alignment, i didn't notice. 
As I mentioned, the EA also didn't work with OrderSelect by Pos. The lastOrder is just some last change I test to see whether it causes difference or not. I also got the ticket number as u asked :)
Thank u

For a better understanding: (Is it what you are thinking about?)

void OnTick()
{
   if (IsBuyCond()) // Method not included here
   {
      if (OrderSelect(lastOrder, SELECT_BY_TICKET, MODE_HISTORY))
      {
         if (OrderProfit() > 0 && OrderLots() < 0.04)
         {
            lot = 0.05;
         }
         else lot = 0.03;
      }
      lastOrder =  OrderSend(Symbol(), OP_BUY, lot, Ask, 3*Point, 0, 0, "EA", 1234, 0, clrRed);
   }
   else if (IsSellCond()) // Method not included here
   {         
      if (OrderSelect(lastOrder, SELECT_BY_TICKET, MODE_HISTORY))
      {
         if (OrderProfit() > 0 && OrderLots() < 0.04)
         {
            lot = 0.05;
         }
         else lot = 0.03;
      }
      lastOrder =  OrderSend(Symbol(), OP_SELL, lot, Bid, 3*Point, 0, 0, "EA", 1234, 0, clrGreen);         
   }
}

I can not test it, because I don't know, what your BUY CONDITION MET and SELL CONDITION MET are doing here.
I've replaced them with the funcions IsBuyCond() and IsSellCond().
You should check every "if" in the debug mode. Add breakpoints at the important lines and read out all variables.

icedestyn
18
icedestyn  
Martin Fischer:

For a better understanding: (Is it what you are thinking about?)

I can not test it, because I don't know, what your BUY CONDITION MET and SELL CONDITION MET are doing here.
I've replaced them with the funcions IsBuyCond() and IsSellCond().
You should check every "if" in the debug mode. Add breakpoints at the important lines and read out all variables.

yeah u got my idea, above code is exactly what i mean. I just discovered something new : the EA would increase the size if the last trade and new trade were on same symbol (such as EURUSD for these 2 trades). if there were other symbol trades between them then it won't work.
Martin Fischer
14907
Martin Fischer  
icedestyn:
yeah u got my idea, above code is exactly what i mean. I just discovered something new : the EA would increase the size if the last trade and new trade were on same symbol (such as EURUSD for these 2 trades). if there were other symbol trades between them then it won't work.

Maybe you've changed the symbol on the chart, where and while this EA is running. In this case the value of 'lastOrder' gets lost, because changing the symbol on the current chart reloads the EA on the chart.

icedestyn
18
icedestyn  
Martin Fischer:

Maybe you've changed the symbol on the chart, where and while this EA is running. In this case the value of 'lastOrder' gets lost, because changing the symbol on the current chart reloads the EA on the chart.

sorry but the EA runs on a VPS 24/7 and I don't touch it at all so I don't think it's the reason. anyway thanks for ur help :D
Martin Fischer
14907
Martin Fischer  

Hard to say, because nobody knows the rest of your code. ;-)
Try logging with "Print". (Example on IsSellCond())
(I did not test the following code. ;-) 

void OnTick()
{
   if (IsBuyCond()) // Method not included here
   {
      if (OrderSelect(lastOrder, SELECT_BY_TICKET, MODE_HISTORY))
      {
         if (OrderProfit() > 0 && OrderLots() < 0.04)
         {
            lot = 0.05;
         }
         else lot = 0.03;
      }
      lastOrder =  OrderSend(Symbol(), OP_BUY, lot, Ask, 3*Point, 0, 0, "EA", 1234, 0, clrRed);
   }
   else if (IsSellCond()) // Method not included here
   {  
      Print("Found sell condition.");       
      if (OrderSelect(lastOrder, SELECT_BY_TICKET, MODE_HISTORY))
      {
         Print("Selecting last order with ticket " + IntegerToString(lastOrder) + " ok.");
         if (OrderProfit() > 0 && OrderLots() < 0.04)
         {
            lot = 0.05;
         }
         else lot = 0.03;
      }
      lastOrder =  OrderSend(Symbol(), OP_SELL, lot, Bid, 3*Point, 0, 0, "EA", 1234, 0, clrGreen); 
      if (lastOrder == -1)
      { 
         Print("Failed to open new SELL order."); 
      }
      else
      {
         Print("Opened new SELL order. Ticket: " + IntegerToString(lastOrder) + ", lots: " + DoubleToStr(lot, 2));
      }
              
   }
}
To add comments, please log in or register