Open & Close button

 

Can I work  Open & Close button EA in Strategy Tester Visualization ? 

 Please help me use  Open & Close button in in Strategy Tester Visualization. Thank you.

 

Yes it is possible.

You must check the button state in OnTick() because OnChartEvent() doesn't work for EA's in strategy tester.

Post your code that you are having problems with. 

 
honest_knave:

Yes it is possible.

You must check the button state in OnTick() because OnChartEvent() doesn't work for EA's in strategy tester.

Post your code that you are having problems with. 

void OnTick()
  {
   buy_anniu("BUY",White,100,100,"buy");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   if(id==CHARTEVENT_OBJECT_CLICK)
     {  
      if(sparam=="BUY")
        {
         MqlTradeRequest request={0};
         MqlTradeResult  result={0};
         request.order=miss_B_ticket;
         request.action=TRADE_ACTION_DEAL;
         request.symbol=Symbol();
         request.type=ORDER_TYPE_BUY;
         request.volume=lots;
         request.deviation=100;
         request.price=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
         request.comment="xxxxx";
         request.magic=12345;

         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
        }
     }
  }
//------------------------------------------------------------------------------Buy

void buy_anniu(string name,color yanse,int x,int y,string text,int changdu=0)
  {
   cc.ObjectCreateMQL4(name,OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,name,OBJPROP_COLOR,yanse);
   ObjectSetInteger(0,name,OBJPROP_BGCOLOR,clrCrimson);
   ObjectSetInteger(0,name,OBJPROP_XDISTANCE,140);
   ObjectSetInteger(0,name,OBJPROP_YDISTANCE,40);
   if(changdu==0)
     {
      int as=StringLen(text);
      ObjectSetInteger(0,name,OBJPROP_XSIZE,as*40);
     }
   else
     {
      ObjectSetInteger(0,name,OBJPROP_XSIZE,changdu);
     }
   ObjectSetInteger(0,name,OBJPROP_YSIZE,60);
   ObjectSetInteger(0,name,OBJPROP_XSIZE,60);
   ObjectSetString(0,name,OBJPROP_FONT,"Microsoft JhengHei");
   ObjectSetString(0,name,OBJPROP_TEXT,text);
   ObjectSetInteger(0,name,OBJPROP_FONTSIZE,30);
   ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR,clrCrimson);
   ObjectSetInteger(0,name,OBJPROP_CORNER,0);
  }
//+------------------------------------------------------------------+

Yes, My button state isn't in OnTick(), how can I fix it and work in  strategy tester.

Please teach me ,thank you. 

 
 
jojocash:

Yes, My button state isn't in OnTick(), how can I fix it and work in  strategy tester.

Please teach me ,thank you. 

You need to check the state of the button (whether it is on or off) in OnTick(). 

Uncompiled / Untested:

int OnInit()
  {


   buy_anniu("BUY",White,100,100,"buy");
   return(INIT_SUCCEEDED);
  }

  
void OnTick()
  {
   if(ObjectGetInteger(0,"BUY",OBJPROP_STATE))
     {
      place_buy();
      ObjectSetInteger(0,"BUY",OBJPROP_STATE,false);
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   if(id==CHARTEVENT_OBJECT_CLICK)
     {  
      if(sparam=="BUY") place_buy();
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void place_buy()
  {
   MqlTradeRequest request={0};
   MqlTradeResult  result={0};
   request.order=miss_B_ticket;
   request.action=TRADE_ACTION_DEAL;
   request.symbol=Symbol();
   request.type=ORDER_TYPE_BUY;
   request.volume=lots;
   request.deviation=100;
   request.price=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   request.comment="xxxxx";
   request.magic=12345;

   if(!OrderSend(request,result))
      PrintFormat("OrderSend error %d",GetLastError());
   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
  }

//------------------------------------------------------------------------------Buy
void buy_anniu(string name,color yanse,int x,int y,string text,int changdu=0)
  {
   cc.ObjectCreateMQL4(name,OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,name,OBJPROP_COLOR,yanse);
   ObjectSetInteger(0,name,OBJPROP_BGCOLOR,clrCrimson);
   ObjectSetInteger(0,name,OBJPROP_XDISTANCE,140);
   ObjectSetInteger(0,name,OBJPROP_YDISTANCE,40);
   if(changdu==0)
     {
      int as=StringLen(text);
      ObjectSetInteger(0,name,OBJPROP_XSIZE,as*40);
     }
   else
     {
      ObjectSetInteger(0,name,OBJPROP_XSIZE,changdu);
     }
   ObjectSetInteger(0,name,OBJPROP_YSIZE,60);
   ObjectSetInteger(0,name,OBJPROP_XSIZE,60);
   ObjectSetString(0,name,OBJPROP_FONT,"Microsoft JhengHei");
   ObjectSetString(0,name,OBJPROP_TEXT,text);
   ObjectSetInteger(0,name,OBJPROP_FONTSIZE,30);
   ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR,clrCrimson);
   ObjectSetInteger(0,name,OBJPROP_CORNER,0);
  }
//+------------------------------------------------------------------+


 

 

Thank you. That’s impressive!

 The  button can work in  strategy tester.

Reason: