Writing an expert adviser in [ MQL4 ]

2019-02-20 13:45发布

问题:

So if I wanted an EA in MQL4 that took the open price and when current price was 10 pips below the open it places a buy order and when it was 10 pips above the open it sold. Only one order at a time and the open changed daily.

Q1: How could that run unstopped?

Q2: Would that even be profitable?

I know this is simple for some people to write but for me it's depressing.

回答1:

A1: the simplest part ...

The MQL4 Expert Advisor type-of-code's execution can be principally run unstopped, supposing the execution engine, the MetaTrader Terminal 4, is being run in a nonstop mode. While there still remain the weekends available for service & maintenance tasks, the EA code, per se can be operated infinitely long, state-fully with an automated re-entrant safe re-launch self-protection ( OnInit(){...} + OnDeinit(){...} ).

#property copyright "Copyright © 1987-2016 [MS]"
#property link      "nowhere.no"
#property version   "0.00"
#property strict

extern   int   minDist2XTO         = 10;     // A minimum Distance To XTO
         bool  aGlobalMUTEX_LOCKED = False;  // LOCK "only one order at a time"

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int      OnInit()
  {   // on-launch intialisation tasks go here:
         return( INIT_SUCCEEDED );
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void     OnDeinit( const int reason )
  {   // pre-exit sanitisation tasks go here:
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void     OnTick()
  {   // MAIN: this is being executed upon each anFxMarketEVENT's arrival

      // GoLONG();

      // GoSHORT();

  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double   OnTester()
  {   // back-testing utility calculation functions go here:
         double  retVal = 0.0;
         return( retVal );
  }
//+------------------------------------------------------------------+
void     GoLONG()
  {   // SELF-CONTROLLABLE ( might even get concurrently run in parallel to GoSHORT on dual resources )
      // -------------------------------------------------------------------------------------------------------
         static bool       aNewBarEVENT   = FALSE;
         static int        aLastBAR       = EMPTY,
                           aCurrBAR       = EMPTY;
         static double     GoLONG_LEVEL   = EMPTY;
      // -------------------------------------------------------------------------------------------------------
      // TEST A STATE-SHIFT

         RefreshRates();

                           aCurrBAR = iBars(_Symbol, PERIOD_D1 );
         if (  aLastBAR != aCurrBAR )
         {     aLastBAR  = aCurrBAR;   // .SET
               aNewBarEVENT = TRUE;    // .FLAG
            // ----------------------- // .RESET in-BAR-registers
               GoLONG_LEVEL =  NormalizeDouble( iOpen( _Symbol, PERIOD_D1, 0 )
                                              - minDist2XTO * _Point
                                                );
            // ----------------------
         }
         else
         {     aNewBarEVENT = FALSE;    // !FLAG
         }
         if ( !aGlobalMUTEX_LOCKED
            && Ask <= GoLONG_LEVEL
            )
         {  
         // XTO ...                     // .XTO
            if ( success ) aGlobalMUTEX_LOCK = True;
         }
    }
//+------------------------------------------------------------------+
void     GoSHORT()
  {   // SELF-CONTROLLABLE ( might even get concurrently run in parallel to GoLONG on dual resources )
         ...
         ..
         .
   } 

A2:

This question can be addressed quantitatively. Your trading idea can be tested and validated on the real-market data, so as to provide a reasonable amount of observations that either support or limit the expected performance expectations.

Without quantitative data from adequate TimeDOMAIN span of the market seasonality irregularities, the question cannot have a reasonably supported quantitative answer ( except for just an opinion ).

This goes far beyond a StackOverflow format of Q/A, but in principle back-testing ( with a support of an inbuilt mechanism of double OnTester(){...} post-processing facility ) and forward-testing ( with a support of demo-account forward runs ) are both being used in quant modelling, before any real trading strategy is ever exposed to execute any real market-risk.