Fuzzy - Library for developing fuzzy models - Library for MetaTrader 5 - MT4/MT5 Resources




Dmitry Kalyuzhny. FuzzyNet project website - http://sourceforge.net/projects/fuzzynet/
Extract the archive to the terminal data folder .
The library code is located at
Sample test scripts can be found at
FuzzyNet is one of the most popular mathematical libraries for creating fuzzy models
The Microsoft.Net Fuzzy Logic Library (FuzzyNet) is an easy-to-use component that implements the Mamdani and Sugeno fuzzy inference systems.
Fuzzy networks include:
The following content was added when converting the library to MQL5:
Use the library
Notes: Mamdani-type inference systems can be configured at any stage after creation but before calling system calculation functions. If the system settings have not been changed since they were created, the system uses the default settings:
The transformation for the FuzzyNet library (v.1.2.0) is shown below.
To use this library, include the MamdaniFuzzySystem.mqh or SugenoFuzzySystem.mqh file, depending on the system you want to create.
The package also contains helper classes for implementing fuzzy rules.
Sugeno-type fuzzy variables are used when developing rules for Sugeno-type systems.
Using the FuzzyNet library in MQL5
Before writing a fuzzy system, you should have a clear understanding of its elements, including:
System development and calculation:
For Mamdani type systems:
MamdaniFuzzySystem *fuzzy_system=new MamdaniFuzzySystem();For Sugeno type systems:
SugenoFuzzySystem *fuzzy_system=new SugenoFuzzySystem();
Fuzzy variable *fuzzy_variable = new fuzzy variable ( const string name, constant double minute, constant double maximum);
fuzzy_variable.Terms().Add( new fuzzy term( const string name, new IMembershipFunction());
fuzzy_system.Input().Add(FuzzyVariable fuzzy_variable);
Sugeno variable *sugeno_variable= new Sugeno variable ( constant string name);A linear function that explains the linear combination of the input values is added to the Sugeno-type fuzzy variable instead of the fuzzy term. Arrays of names and coefficients are used as linear function arguments. The linear equation is formed based on this array, so it is important to respect the order of the elements in the array. The coefficient array length should be equal to the number of input values or one more than the input values. If the lengths are equal, the absolute term of the equation is equal to zero. If the array length exceeds this amount, the absolute term is equal to the last element value. All other array elements starting from the first array element are assigned to fuzzy input variables in the order in which they are entered into the system.
sugeno_varriable.Functions().Add(fuzzy_sytem.CreateSugenoFunction( const string name, const double &coeffs[]));
For Mamdani type systems:
fuzzy_system.Output().Add(FuzzyVariable fuzzy_variable);
For Sugeno type systems:
fuzzy_system.Output().Add(FuzzyVariable fuzzy_variable);
For Mamdani type systems:
MamdaniFuzzyRule *fuzzy_rule = fuzzy_system.ParseRule(const string Rule_text);
For Sugeno type systems:
SugenoFuzzyRule *fuzzy_rule = fuzzy_system.ParseRule(const string Rule_text);
For Mamdani type systems:
fuzzy_system.Rules().Add(MamdaniFuzzyRule fuzzy_rule);
For Sugeno type systems:
fuzzy_system.Rules().Add(SugenoFuzzyRule fuzzy_rule);
Dictionary_Obj_Double *p_od_in=new Dictionary_Obj_Double;This class implements the SetAll(CObject *key, const double value) method, which accepts two parameters - a fuzzy variable and a numeric value. This element is an input variable to the system.
p_od_in.SetAll(FuzzyVariable fuzzy_variable, constant double value );All other input values are filled in the same way. Create a list and add all values to it:
Directory * in = new CList; in . add (p_od_in);
Dictionary_Obj_Double *p_od_out= new Dictionary_Obj_Double; Directory * out = new C list;
Output=fuzzy_system.Calculate(in);After that, go out to list and store all calculated output values in the order they were entered into the system. We just need to receive them:
p_od_out=out.GetNodeAtIndex(int index); Double result=p_od_out.Value();Now, the result variable stores the system's calculated result exponent for the output value of the specified number in the input system.
Sample script
Prompt example (Mamdani)
Tips_Sample_Mamdani.mq5 Calculate the percentage you need to tip based on the quality of service and food.
Enter the input parameters:

Calculation result:

Cruise control sample (Sugeno)
The Cruise_Control_Sample_Sugeno.mq5 sample script is an example of a fuzzy regulator. It represents a car cruise control system that uses data on current deviation and the rate of deviation change to calculate the necessary acceleration so that the car reaches the desired speed.
Enter the input parameters:

Calculation result:

Attachment download
📎 dictionary.mqh (9.1 KB)
📎 fuzzyrule.mqh (17.77 KB)
📎 fuzzyterm.mqh (3.73 KB)
📎 fuzzyvariable.mqh (5.47 KB)
📎 genericfuzzysystem.mqh (11.88 KB)
📎 helper.mqh (7.18 KB)
📎 inferencemethod.mqh (7.06 KB)
📎 mamdanifuzzysystem.mqh (20.39 KB)
📎 membershipfunction.mqh (43.45 KB)
📎 ruleparser.mqh (36.52 KB)
📎 sugenofuzzysystem.mqh (12.69 KB)
📎 sugenovariable.mqh (11 KB)
📎 cruise_control_sample_sugeno.mq5 (7.65 KB)
📎 TestFuzzy.mq5 (26.37 KB)
📎tips_sample_mamdani.mq5 (5.98 KB)
Source: MQL5 #13697
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •