Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal


Long Short only EA based on CExpert - expert for MetaTrader 5

| English Русский 中文 Español Deutsch 日本語 Português

votes: 22
2014.07.31 11:40
2016.11.22 07:32

Current Expert Advisor allows to choose opening only long/short (or both) positions based on an extension of the CExpert class.

Two different files are provided:

  • LongShortExpertModified.mqh: This class extends default CExpert modifying the CheckOpen() and CheckReverse() methods in order to allow only the desired orders to be opened;
  • LongShortExpertMACD.mq5: This EA is a simple modification over the embedded ExpertMACD.mq5 class, including the developed modified expert in order to only allow the desired order type according to an input parameter, and is supplied to clarify the expert use.


This class modifies the default CExpert class behaviour allowing only a certain type of order according to the following enum:


This enum will be used as an input parameter for the required final EA to determine which type of orders will be allowed, and is used internally to open only the desired orders and to process order reversals only if both position types are allowed (BOTH_POSITION enum value).

To do so, CheckOpen() and CheckReverse() methods are rewritten:

class CLongShortExpertModified : public CExpert
   virtual bool      CheckOpen(void);
   virtual bool      CheckReverse(void);
   void SetAvailablePositions(ENUM_AVAILABLE_POSITIONS newValue){m_positions=newValue;};

CheckOpen() is modified to only check long or short positions according to m_positions value:

bool CLongShortExpertModified :: CheckOpen()
      case LONG_POSITION:
         return CheckOpenLong();         //check only new long positions
      case SHORT_POSITION:
         return CheckOpenShort();        //check only new short positions
         return CExpert::CheckOpen();    //default behaviour

CheckReverse() is modified to check position reversal only if both position types are allowed:

bool CLongShortExpertModified::CheckReverse()
      case LONG_POSITION:
      case SHORT_POSITION:
         return false;                    // no reversal is allowed
         return CExpert::CheckReverse(); //default behaviour


This class provides a specific EA example of the previous class use, based on the default ExpertMACD EA included in the MQL5 distribution.

First of all, the concrete Expert class must be included, and the corresponding input parameter added. Also, the external expert is associated with the subclass, instead of the default CExpert:

#include <Expert\LongShortExpertModified.mqh>
input ENUM_AVAILABLE_POSITIONS Inp_Allowed_Positions=BOTH_POSITION; //short / long / both positions allowed
CLongShortExpertModified ExtExpert;  //specific designed CExpert Subclass

After initiallizing the expert, the parameter must be set according to the input value:

      //--- failed
      printf(__FUNCTION__+": error initializing expert");
   // Specific parameter controlling which positions are allowed

No additional changes are required. The following figure shows the configuration parameters for the expert:

Long/Short only general Expert Advisor

Short Pending Order Short Pending Order

When you drag this script onto the chart, it will calculate the price where you drop the script and use this price to figure out if a Sell Stop or Sell Limit pending order should be placed.

Long Pending Order Long Pending Order

When you drag this script onto the chart, it will calculate the price where you drop the script and use this price to figure out if a Buy Stop or Buy Limit pending order should be placed.

Astro Indicators Astro Indicators

Show the aspect of two planets, the declination of planets or just the Body position.

FxTrend 25EMA FxTrend 25EMA

FxTrend 25EMA is based on the difference of the EMA 25 value during two different moments.