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

Programming Mode - Compound - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources

author EAcpu | 4 reads | 0 comments |

Programming patterns - Composite - library for MetaTrader 5

Programming patterns - Composite - library for MetaTrader 5

 /**********************************************************************
Programming pattern: Composite pattern combines objects into a tree structure to represent a part-whole hierarchy.
Composition allows clients to work uniformly on individual objects and combinations of objects.
/**/


Programming patterns - Composite - library for MetaTrader 5

 /************************************************************************/
class component // abstract (interface) { public :
virtual blank op() = 0 ;
virtual blank add(component*) = 0 ;
virtual blank remove(component*) = 0 ;
Virtual Component * GetChild( integer ) = 0 ;
                    component() {}
                    component( stringname ):mname(name) {} protected :
string name;
  }; /************************************************************************/
class leaf: people component { people :
void operation() { print (name);}
blank add(component*){}
blank delete(component*){}
  Component *GetChild( integer ) { return void ;}
                     leaves( stringname ):component(name){}
  }; /************************************************************************/
Class composite: public component { people :
  ObjectList node;
void operation() { print (name); integer c = nodes.Count(); for ( integer i = 0 ; i Blank Add(Component*c) {nodes+=c;}
Blank delete(component*c) {node-=c;}
  Component *GetChild( integer i) { return node[i];}
                    composite( stringname ):component(name){}
  }; /**/


/**********************************************************************
Applicability Use the Composite pattern when you want to represent a part-whole hierarchy of objects.
      You want clients to be able to ignore the difference between component objects and individual objects. The client will handle all objects in the composition structure evenly.
Actor Component declares the interface of the objects in the composition.
      Implement the default behavior of the interface common to all classes, as appropriate.
      Declare an interface for accessing and managing its subcomponents.
      (Optional) Define an interface for accessing the component's parent recursive structure, and implement it where appropriate.
  Leaf represents the leaf object in the composition. A leaf has no children.
      Defines the behavior of the original objects in the composition.
  Composite materials define the behavior of subcomponents.
      Store child components.
      Implement child-related operations in the Component interface.
  Clients operate the objects in the composition through the Component interface.
Cooperative clients use the Component class interface to interact with objects in the composite structure. If the recipient is a leaf, the request is processed directly. If the receiver is a composite, then it will typically forward the request to its subcomponents, possibly performing additional operations before and/or after the forwarding.
Consequences Composite pattern defines a class hierarchy consisting of primitive objects and composite objects.
      Primitive objects can be composed into more complex objects, which in turn can be composed, and so on recursively. Whenever client code requires a primitive object, it can also take a composite object.
      Make it simple for the client. Customers can handle composite structures and individual objects uniformly. Clients usually don't know (and shouldn't care) whether they are dealing with a leaf or a composite component. This simplifies client code because it avoids writing markup and case statement-style functions over classes that define combinations.
      Makes it easier to add new types of components. Newly defined composite or leaf classes automatically work with existing structures and client code.
      There is no need to change the client for the new component class.
      It will make your design too general. The disadvantage is that it is easy to add new components and the advantage is that it makes it more difficult to constrain the composition of components. Sometimes you want a composite to contain only certain components.      With Composite, you cannot rely on the type system to enforce these constraints for you. You must use runtime checking instead.
/**/


 /**********************************************************************
Composite mode usage example. client/****************************************************************/
blank start ()
  {
/*root*/ component*root = new composite( "root" ); //dynamic// = composite root("ROOT"); //auto /*part*/ component*branch1 = new composite( "branch1" );
  component * branch2 = new composite( "branch2" );
  component*leaf1 = new leaf( "leaf1" );
  component*leaf2 = new leaf( "leaf2" );
/*Build tree*/ Root.Add ( invalid ); //Error... Root.Add(branch1);
  root.add(branch2);
  branch1.Add(leaf1);
  branch1.add(leaf2);
  branch2.add(leaf2);
  branch2.add( new leaf( "leaf3" )); //Add section by reference (not var) root.Operation(); //View { string s; for ( integer i = 0 ; i < 13 ; i++) {s+= "-" ;} print (s);}
/*Delete the entire branch*/ root.delete(branch1);
  root.Operation(); //View /*Delete root directory*/ Delete root;
  } /************************************************************************
Output:
/**
  root branch 1
      Leaf 1
      Leaf 2
    Branch 2
      Leaf 2
      Leaf 3
  -------------
  root branch 2
      Leaf 2
      Leaf 3
/**/



Attachment download

📎 composite.mq5 (2.67 KB)

📎 composite.mqh (4.44 KB)

📎 objectlist.mqh (6.64 KB)

Source: MQL5 #29767

Programming Mode - Composite - Important points before using the MetaTrader 5 library

This page has been supplemented with structured instructions, focusing on the purpose of EA source code, MQL4/MQL5 platform compatibility, testing process and risk control. Before downloading or deploying, it is recommended to first determine whether it is consistent with your own variety, cycle and account environment.

Suitable for who to use

  • Users who want to quickly screen EA source code and are willing to do simulation disk verification first.
  • People who need to compare MT4/MT5 EAs, indicators, scripts or source code projects.
  • Traders who wish to record backtests, parameters and risk boundaries before placing a trade.

Testing and risk control suggestions

  • First verify the default parameters in the strategy tester or demo account, and then adjust the symbol, period, spread and trading period one by one.
  • Scalping, grid, martin, and high-frequency strategies should focus on observing maximum drawdowns, consecutive losses, slippage, and trading frequency.
  • It is not recommended to run any document directly on the real disk. At least observe the simulated disk performance for 2-4 weeks first.

FAQ

Can MT4 and MT5 be used interchangeably?
Usually cannot be used directly. EX4/MQ4 corresponds to MT4, EX5/MQ5 corresponds to MT5, and the source code needs to be compiled in the corresponding MetaEditor.

Is this resource guaranteed to be profitable?
cannot. EAs, indicators and scripts can only be used as trading tools, and the results depend on parameters, market environment, spreads, slippage and risk control.

What else can I continue to watch?
Forex EA download , foreign exchange indicator download , EA evaluation , MQL5 CodeBase .

English Search Notes

This page is also optimized for English search intent around Programming Patterns - Composite - MetaTrader 5 library . Related search terms include: MQL4 source code, MQL5 source code. Test any Forex EA, Expert Advisor, MT4/MT5 indicator, script or MQL source code with historical data and a demo account before live trading.

Verification code Refresh