Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Only long and short EA based on CExpert - MetaTrader 5 Expert - MT4/MT5 Resources

author EAcpu | 5 reads | 0 comments |

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

The current Expert Advisor allows the option of opening only long/short (or both) positions depending on the extension of the trading period. C expert class.

Two different files are provided:

LongShortExpertModification

This class modifies the default CExpert class behavior to only allow a certain type of order to be specified based on the following enumeration:

 Enum ENUM_AVAILABLE_POSITIONS
  {
  long position,
  short position,
  BOTH_POSITION
  };

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

For this purpose, the CheckOpen() and CheckReverse() methods are overridden:

 Class CLongShortExpert Modified: Folks C Expert { protected :
  ENUM_AVAILABLE_POSITIONS m_positions; people :
                    CLongShortExpertmodify( blank );
                    ~CLongShortExpertModified( blank );
virtual boolean CheckOn( blank );
virtual boolean checkInverse( void );
Blank SetAvailablePositions(ENUM_AVAILABLE_POSITIONS newValue){m_positions=newValue;};
  };

CheckOpen() was modified to only check long or short positions based on the m_positions value:

 Boolean CLongShortExpertModified::CheckOpen()
  {
transition (m_position)
    {
Case long position:
Return checkOpenLong(); //Only check new long position case SHORT_POSITION:
Return check OpenShort(); //Only check new short positions Default :
Return CExpert::CheckOpen(); //Default behavior }
  }

CheckReverse() was modified to check position reversals only when both position types are allowed:

 Boolean CLongShortExpertModified::CheckReverse()
  {
transition (m_position)
    {
Case long position:
Case SHORT_POSITION:
Returns false ; // Not allowed to reverse default :
Return CExpert::CheckReverse(); //Default behavior }
  }

MACD, long and short expert

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

First, the concrete Expert class must be included and the corresponding input parameters added. Additionally, external Experts are associated with subclasses rather than the default CExpert:

 #Include <Expert\LongShortExpertModified.mqh> //[...]
Input ENUM_AVAILABLE_POSITIONS Inp_Allowed_Positions=BOTH_POSITION; //Short/long/Both positions are allowed
//[...] ExtExpert modified by CLongShortExpert; //Specifically designed CExpert subclass

After initializing the EA, parameters must be set according to the input values:

 if (!ExtExpert.Init( symbol (), period (),Expert_EveryTick,Expert_MagicNumber))
    {
// - - failed print function ( __function__ + ": error initializing expert" );
      ExtExpert.Deinit();
return (- 1 );
    }
  
// Specific parameters that control which positions are allowed ExtExpert.SetAvailablePositions(Inp_Allowed_Positions);

No additional changes are required. The following figure shows the configuration parameters of Expert:

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


Attachment download

📎 longshortexpertmodified.mqh (3.25 KB)

📎longshortexpertmacd.mq5 (6.51 KB)

Source: MQL5 #1896

Verification code Refresh