Back Testing a manual trading system using an EA MQL4 Coding help request

#1
I am trying to get an MT4 EA I have written that opens and closes trades manually by clicking buttons on the chart to do the same within the strategy tester.

There are people that have done it but I can't find any examples of how to code it. If anyone is aware of some sample code to do this it would be greatly appreciated.
You can have excuses or you can have results, but you can't have both!


Re: Back Testing a manual trading system using an EA MQL4 Coding help request

#2
Found an example of what I need.

Code: Select all

#include<Controls/Button.mqh>

CButton buybutton;
CButton sellbutton;

extern int        magic          = 1234;            //Magic Number
extern double     Lots           = 0.01;             //Lot Size
extern double     Take_Profit    = 15;              //Take Profit
extern double     Stop_Loss      = 4.5;             //Stop Loss
extern int        BuyX           = 0;               //Buy Button Height Start
extern int        BuyY           = 500;             //Buy Button Width Start
extern int        BuyX1          = 0;               //Buy Button Height Finish
extern int        BuyY1          = 590;             //Buy Button Width Finish
extern int        SellX           = 0;              //Sell Button Height Start
extern int        SellY           = 600;            //Sell Button Width Start
extern int        SellX1          = 0;              //Sell Button Height Finish
extern int        SellY1          = 690;            //Sell Button Width Finish

double ticket1, ticket2;

   int P = 1;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(Digits == 5 || Digits == 3 || Digits == 1)P = 10;else P = 1; // To account for 5 digit brokers


   
   buybutton.Create(0,"buybutton",BuyX,BuyY,BuyX1,BuyY1,25);
   buybutton.Text("Buy");
   buybutton.FontSize(12);
   buybutton.Color(clrWhite);
   buybutton.ColorBackground(clrGreen);
   
   sellbutton.Create(0,"sellbutton",SellX,SellY,SellX1,SellY1,25);
   sellbutton.Text("Sell");
   sellbutton.FontSize(12);
   sellbutton.Color(clrWhite);
   sellbutton.ColorBackground(clrRed);
   

   return(0);
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
   {
//---
   if( IsVisualMode() )
   {
      long   lparam = 0;
      double dparam = 0.0;
      string sparam = "";
//---
      sparam = "buybutton";
      if( bool( ObjectGetInteger( 0, sparam, OBJPROP_STATE ) ) )
        OnChartEvent( CHARTEVENT_OBJECT_CLICK, lparam, dparam, sparam );
//---
      sparam = "sellbutton";
      if( bool( ObjectGetInteger( 0, sparam, OBJPROP_STATE ) ) )
        OnChartEvent( CHARTEVENT_OBJECT_CLICK, lparam, dparam, sparam );
   }
//---
   for(int i=OrdersTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==magic)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_BUY)
                  if(Ask>OrderOpenPrice())
                     if(OrderStopLoss()<Bid-(Stop_Loss*Point*P))
                        ticket1=OrderModify(OrderTicket(),OrderOpenPrice(),Bid - (Stop_Loss*Point*P),OrderTakeProfit(),0,clrNONE);
                        
                          
                        
          
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==magic)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_SELL)
                  if(Bid<OrderOpenPrice())
                     if(OrderStopLoss()>Ask+(Stop_Loss*Point*P))   
                        ticket2=OrderModify(OrderTicket(),OrderOpenPrice(),Bid + (Stop_Loss*Point*P),OrderTakeProfit(),0,clrNONE);
   }    
   }             
            

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   
   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="buybutton")
   {
      if(OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid - (Stop_Loss*Point*P),Bid + (Take_Profit*Point*P),NULL,magic,0,clrBlue)==-1)
         Print("did not place buy order");
      buybutton.Pressed(false);
   }
   
   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="sellbutton")
   {
      if(OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+(Stop_Loss*Point*P),Ask-(Take_Profit*Point*P),NULL,magic,0,clrRed)==-1)
         Print("did not place buy order");
      sellbutton.Pressed(false);
   }   
  }

You can have excuses or you can have results, but you can't have both!


Users viewing this forum: Admin, CommonCrawl [Bot] and 2 guests