Watch how to download trading robots for free
Find us on Twitter!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Experts

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

Views:
12703
Rating:
(30)
Published:
2014.07.31 11:40
Updated:
2016.11.22 07:32
\MQL5\Include\Expert\
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

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.

LongShortExpertModified

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

enum ENUM_AVAILABLE_POSITIONS
  {
   LONG_POSITION,
   SHORT_POSITION,
   BOTH_POSITION
  };

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
  {
protected:
   ENUM_AVAILABLE_POSITIONS m_positions;
public:
                     CLongShortExpertModified(void);
                    ~CLongShortExpertModified(void);
   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()
  {
   switch(m_positions)
     {
      case LONG_POSITION:
         return CheckOpenLong();         //check only new long positions
      case SHORT_POSITION:
         return CheckOpenShort();        //check only new short positions
      default:
         return CExpert::CheckOpen();    //default behaviour
     }
  }

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

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

LongShortExpertMACD

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:

if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing expert");
      ExtExpert.Deinit();
      return(-1);
     }
   
   // Specific parameter controlling which positions are allowed
   ExtExpert.SetAvailablePositions(Inp_Allowed_Positions);  

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.