Expert Advisor trading on Open Prices

 

For various reasons (of which the time it takes to run an optimization in the ST is the most important) i need to code my EA to trade on Open Prices when running on a live account so that I can do my optimizations under the "Open Prices" model in MT4. I already code all my indicators with a shift of "1" for a start.

The problem I need help with is how to determine the price at which to open a trade. In MQL4 the OrderSend function opens trades at market price and so the price stipulated must be either "Ask" or "Bid".

But when I only want to open the trade at Open Price (e.g. iOpen,NULL,0,0) how do I code my EA to open a trade only when the bar opens? Do I use pending trades? Or is there a better way?

Thank you for any assistance in this matter.

 
Ernest Klokow:

For various reasons (of which the time it takes to run an optimization in the ST is the most important) i need to code my EA to trade on Open Prices when running on a live account so that I can do my optimizations under the "Open Prices" model in MT4. I already code all my indicators with a shift of "1" for a start.

The problem I need help with is how to determine the price at which to open a trade. In MQL4 the OrderSend function opens trades at market price and so the price stipulated must be either "Ask" or "Bid".

But when I only want to open the trade at Open Price (e.g. iOpen,NULL,0,0) how do I code my EA to open a trade only when the bar opens? Do I use pending trades? Or is there a better way?

Thank you for any assistance in this matter.

you can only ever open trades at the current bid or ask.

if you use pending orders and the 'open prices only' feature of ST then unless the pending price equals an open price (how would you know that in advance?) you will get extra slippage

so in your case you would be best detecting the new bar and then placing your order at the current bid or ask at that moment

 

Thank you for that advice! Much appreciated!

I have some MQL5 codethat can simulate Open Price trading on a live account. Can anybody tell me if there are any changes required to use this code to run successfully in a live MT4 account?

enum ENUM_BAR_PROCESSING_METHOD
   {
      PROCESS_ALL_DELIVERED_TICKS,               //Process All Delivered Ticks
      ONLY_PROCESS_TICKS_FROM_NEW_M1_BAR,        //Only Process Ticks From New M1 Bar
      ONLY_PROCESS_TICKS_FROM_NEW_TRADE_TF_BAR   //Only Process Ticks From New Bar in Trade TF
   };
int OnInit()
      //################################
      //Determine which bar we will used (0 or 1) to perform processing of data
      //################################
      
      if(BarProcessingMethod == PROCESS_ALL_DELIVERED_TICKS)                        //Process data every tick that is 'delivered' to the EA
         iBarToUseForProcessing = 0;                                                //The rationale here is that it is only worth processing every tick if you are actually going to use bar 0 from the trade TF, the value of which changes throughout the bar in the Trade TF                                          //The rationale here is that we want to use values that are right up to date - otherwise it is pointless doing this every 10 seconds
      
      else if(BarProcessingMethod == ONLY_PROCESS_TICKS_FROM_NEW_M1_BAR)            //Process trades based on 'any' TF, every minute.
         iBarToUseForProcessing = 0;                                                //The rationale here is that it is only worth processing every minute if you are actually going to use bar 0 from the trade TF, the value of which changes throughout the bar in the Trade TF
         
      else if(BarProcessingMethod == ONLY_PROCESS_TICKS_FROM_NEW_TRADE_TF_BAR)      //Process when a new bar appears in the TF being used. So the M15 TF is processed once every 15 minutes, the TF60 is processed once every hour etc...
         iBarToUseForProcessing = 1;                                                //The rationale here is that if you only process data when a new bar in the trade TF appears, then it is better to use the indicator data etc from the last 'completed' bar, which will not subsequently change. (If using indicator values from bar 0 these will change throughout the evolution of bar 0) 
   
      Print("EA USING " + EnumToString(BarProcessingMethod) + " PROCESSING METHOD AND INDICATORS WILL USE BAR " + IntegerToString(iBarToUseForProcessing));
 
      //Perform immediate update to screen so that if out of hours (e.g. at the weekend), the screen will still update (this is also run in OnTick())
      
      if(!MQLInfoInteger(MQL_TESTER))
         OutputStatusToScreen(); 
         
   return(INIT_SUCCEEDED);
void OnTick()
  {
//---
    TicksReceivedCount++;
         
      //########################################################
      //Control EA so that we only process at required intervals (Either 'Every Tick', 'Open Prices' or 'M1 Open Prices')
      //########################################################
      
      bool ProcessThisIteration = false;     //Set to false by default and then set to true below if required
      
      if(BarProcessingMethod == PROCESS_ALL_DELIVERED_TICKS)
         ProcessThisIteration = true;
      
      else if(BarProcessingMethod == ONLY_PROCESS_TICKS_FROM_NEW_M1_BAR)    //Process trades from any TF, every minute.
      {
         if(TimeLastTickProcessed != iTime(Symbol(), PERIOD_M1, 0))
         {
            ProcessThisIteration = true;
            TimeLastTickProcessed = iTime(Symbol(), PERIOD_M1, 0);
         }
      }
         
      else if(BarProcessingMethod == ONLY_PROCESS_TICKS_FROM_NEW_TRADE_TF_BAR) //Process when a new bar appears in the TF being used. So the M15 TF is processed once every 15 minutes, the TF60 is processed once every hour etc...
      {
         if(TimeLastTickProcessed != iTime(Symbol(), TradeTimeframe, 0))      // TimeLastTickProcessed contains the last Time[0] we processed for this TF. If it's not the same as the current value, we know that we have a new bar in this TF, so need to process 
         {
            ProcessThisIteration = true;
            TimeLastTickProcessed = iTime(Symbol(), TradeTimeframe, 0);
         }
      }

      //#############################
      //Process Trades if appropriate
      //#############################

      if(ProcessThisIteration == true)
      {
         TicksProcessedCount++;

         ProcessTradeClosures();
         ProcessTradeOpens();
         
         Alert("PROCESSING " + Symbol() + " ON " + EnumToString(TradeTimeframe) + " CHART");
      }
      
      //############################################
      //OUTPUT INFORMATION AND METRICS TO THE SCREEN (DO NOT OUTPUT ON EVERY TICK IN PRODUCTION, FOR PERFORMANCE REASONS - DONE HERE FOR ILLUSTRATIVE PURPOSES ONLY)
      //############################################
      
      if(!MQLInfoInteger(MQL_TESTER))
         OutputStatusToScreen();
  void ProcessTradeClosures()
   {
      double localBuffer[];
      ArrayResize(localBuffer, 3);
      
      //Use CopyBuffer here to copy indicator buffer to local buffer...
      
      ArraySetAsSeries(localBuffer, true);
      
      double currentIndValue  = localBuffer[iBarToUseForProcessing];
      double previousIndValue = localBuffer[iBarToUseForProcessing + 1];
   }
   
   void ProcessTradeOpens()
   {
      double localBuffer[];
      ArrayResize(localBuffer, 3);
      
      //Use CopyBuffer here to copy indicator buffer to local buffer...
      
      ArraySetAsSeries(localBuffer, true);
      
      double currentIndValue  = localBuffer[iBarToUseForProcessing];
      double previousIndValue = localBuffer[iBarToUseForProcessing + 1];
   }
   
   void OutputStatusToScreen()
   {      
      double offsetInHours = (TimeCurrent() - TimeGMT()) / 3600.0;
      
      string OutputText = "\n\r";
     
      OutputText += "MT5 SERVER TIME: " + TimeToString(TimeCurrent(), TIME_DATE|TIME_SECONDS) + " (OPERATING AT UTC/GMT" + StringFormat("%+.1f", offsetInHours) + ")\n\r\n\r";
      
      OutputText += Symbol() + " TICKS RECEIVED:   " + IntegerToString(TicksReceivedCount) + "\n\r";  
      OutputText += Symbol() + " TICKS PROCESSED:   " + IntegerToString(TicksProcessedCount) + "\n\r";
      OutputText += "PROCESSING METHOD:   " + EnumToString(BarProcessingMethod) + "\n\r";
      OutputText += EnumToString(TradeTimeframe) + " BAR USED FOR PROCESSING INDICATORS / PRICE:   " + IntegerToString(iBarToUseForProcessing) + "\n\r";
      OutputText += "SYMBOL BEING TRADED:   " + Symbol() + "\n\r"; 
      OutputText += "TRADING TIMEFRAME:   " + EnumToString(TradeTimeframe) + "\n\r\n\r";
      
      Comment(OutputText);
    }  
Reason: