Saturday, October 24, 2015

How to manually confirm a trade taken by the EA

Automated trading is great, you can have your strategy execute the transactions with near perfect precision at the right time, at the right level.
But what happens when you're not 100% confident on the rules of your strategy and you believe that you need your own discretion to choose which signals should turn into market orders and which should be ignored (due to aspects that cannot be put into mathematical formulas)?

MT4 programming can also allow you to create semi-automated Expert Advisors. This basically means that you are able to create an EA that runs continuously, checking the markets for a valid trade setup, but it does not place the orders without you confirming what is has found.
Whenever a valid setup is detected, a popup will appear, asking you if you want to continue by placing an order or ignore this signal and wait for a better one.
Here's how you can do this:



bool ManuallyConfirmed(int sign) // BUY - sign=1, SELL - sign=-1 
  {
   if(!_AskForManualConfirmation) return true;

   string s_type="BUY";
   if(sign<0) s_type="SELL";

   return Verify(WindowExpertName()+ " wants to open a "+s_type+" trade on "+Symbol()+", "+timeFrameToString(Period())+". Do you allow it?");
  }

This function takes an int parameter signaling the type of the trade (buy or sell), and returns true or false after the user has manually confirmed or denied the trade operation.

bool Verify(string message,string subject="Manual confirmation required")
  {
   return (MessageBox(message,subject,MB_YESNO)==IDYES);
  }

The fucntion Verify uses the MQL4 function MessageBos and returns true if the user confirmed (IDYES).

And once you have these 2 functions, all you need to do is call the ManuallyConfirmed function once you have a BUY or SELL signal, and only open the trade if it returns true.

      bool confirmed=ManuallyConfirmed(sign);
      if(confirmed)
        {
             // OPEN BUY or SELL here
         }