Chart Event For MT4 Backtester - page 2

To add comments, please log in or register
ddmmmyyyy B
14
ddmmmyyyy B  
Fernando Carreiro:

Yes it works on MQL4 but it is only a code sample/snippet. That is why I stated in my post: "something like this"! You have to adapt it according to your own nee

Fernando Carreiro:

Yes it works on MQL4 but it is only a code sample/snippet. That is why I stated in my post: "something like this"! You have to adapt it according to your own needs!


This is the codes for the bottons. Would you be able to guide me where to put your codes please? What value should I put instead of "idResetButtonObject". Thank you in advance for your time and assistance.


#property strict
//--- input parameters
input int         MagicNumber=99;
input double      LotSize=1;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   //if(IsTesting())
     {
      string name;
      string heading[4]={"Buy","Sell","Stop","TP"};
      int xc=5;
      int yc=30;
      for(int i=0;i<2;i++)
        {
         name=heading[i];
         ObjectCreate(0,name,OBJ_BUTTON,0,0,0);
         ObjectSetText(name,name,10,"Arial",clrBlue);
         ObjectSetInteger(0,name,OBJPROP_XDISTANCE,xc);
         ObjectSetInteger(0,name,OBJPROP_YDISTANCE,yc);
         yc+=20;
        }
      for(int i=2;i<4;i++)
        {
         name=heading[i];
         ObjectCreate(0,name,OBJ_LABEL,0,0,0);
         ObjectSetText(name,name,10,"Arial",clrBlue);
         ObjectSetInteger(0,name,OBJPROP_XDISTANCE,xc);
         ObjectSetInteger(0,name,OBJPROP_YDISTANCE,yc);
         ObjectSetInteger(0,name,OBJPROP_SELECTED,false);
         yc+=20;
        }
      name="EditSL";
      ObjectCreate(0,name,OBJ_EDIT,0,0,0);
      ObjectSetText(name,DoubleToStr(0,Digits),10,"Arial",clrRed);
      ObjectSetInteger(0,name,OBJPROP_XDISTANCE,xc+50);
      ObjectSetInteger(0,name,OBJPROP_YDISTANCE,70);
      ObjectSetInteger(0,name,OBJPROP_XSIZE,60);
      ObjectSetInteger(0,name,OBJPROP_YSIZE,20);
      name="EditTP";
      ObjectCreate(0,name,OBJ_EDIT,0,0,0);
      ObjectSetText(name,DoubleToStr(0,Digits),10,"Arial",clrRed);
      ObjectSetInteger(0,name,OBJPROP_XDISTANCE,xc+50);
      ObjectSetInteger(0,name,OBJPROP_YDISTANCE,90);
      ObjectSetInteger(0,name,OBJPROP_XSIZE,60);
      ObjectSetInteger(0,name,OBJPROP_YSIZE,20);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      string name="Buy";
      if(ObjectGetInteger(0,name,OBJPROP_STATE)==true)
        {
         ObjectSetInteger(0,name,OBJPROP_STATE,false);
         double sl=StrToDouble(ObjectGetString(0,"EditSL",OBJPROP_TEXT));
         double tp=StrToDouble(ObjectGetString(0,"EditTP",OBJPROP_TEXT));
         int ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,50,sl,tp,NULL,MagicNumber,0,clrNONE);
        }
      name="Sell";
      if(ObjectGetInteger(0,name,OBJPROP_STATE)==true)
        {
         ObjectSetInteger(0,name,OBJPROP_STATE,false);
         double sl=StrToDouble(ObjectGetString(0,"EditSL",OBJPROP_TEXT));
         double tp=StrToDouble(ObjectGetString(0,"EditTP",OBJPROP_TEXT));
         int ticket=OrderSend(Symbol(),OP_SELL,LotSize,Ask,50,sl,tp,NULL,MagicNumber,0,clrNONE);
        }
     }
//---
 
  }


Fernando Carreiro
4958
Fernando Carreiro  
ddmmmyyyy B:

Sorry, but that is completely the wrong approach!

The whole point of this thread and my work-around is the fact that the OnChartEvent() is NOT called during the back-tests in the Strategy Tester. My work-around is to be used during the OnTick() event as I have shown in my post.

ddmmmyyyy B
14
ddmmmyyyy B  
Fernando Carreiro:

Sorry, but that is completely the wrong approach!

The whole point of this thread and my work-around is the fact that the OnChartEvent() is NOT called during the back-tests in the Strategy Tester. My work-around is to be used during the OnTick() event as I have shown in my post.


I've got you. Thanks for your reply. 
Diego
27
Diego  
Fernando Carreiro:

I know this is a an old thread, but I recently needed to debug some of my code that implements "buttons" to control certain aspects of an EA I was coding and had need for it to work in the Strategy Tester.

The solution I came up with was to check the button states on every incoming tick when the EA was in Visual Mode.

In other words, something like this:

Fernando: MVP! 

You saved my brain!

cheers

Diego

12
To add comments, please log in or register