# Libraries: FuzzyNet Fuzzy Logic Library

100954
2015.09.02 17:29

### FuzzyNet is one of the most popular mathematical libraries for creating fuzzy models

Fuzzy Logic Library for Microsoft.Net (FuzzyNet) is an easy to use component that implements Mamdani and Sugeno fuzzy inference systems.

FuzzyNet includes:

The following additions have been made when converting the library into MQL4:

• 8 new membership functions.
• 4 new defuzzification methods for Mamdani-type systems.

Author: MetaQuotes Software Corp.

28244
2015.09.23 02:01

I wrote the following code to test this fuzzy lib,

```//+------------------------------------------------------------------+
//|                                                         Test.mq4 |
//|                                     http://rasoulfx.blogspot.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Connecting libraries                                             |
//+------------------------------------------------------------------+
#include <Math\FuzzyNet\MamdaniFuzzySystem.mqh>
//--- input parameters
input double   InpV1 = 5.0;
input double   InpV2 = 1.5;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//--- Mamdani Fuzzy System
MamdaniFuzzySystem *fsTest=new MamdaniFuzzySystem();
FuzzyVariable *fvV1=new FuzzyVariable("V1",0.0,10.0);
FuzzyVariable *fvV2=new FuzzyVariable("V2",0.0,10.0);
//--- Create first input variables for the system
fvV1.Terms().Add(new FuzzyTerm("Small", new TriangularMembershipFunction(-10, 0, 5)));
fvV1.Terms().Add(new FuzzyTerm("Medium", new TriangularMembershipFunction(3, 5, 7)));
fvV1.Terms().Add(new FuzzyTerm("Large", new TriangularMembershipFunction(5, 10, 40)));
//--- Create second input variables for the system
fvV2.Terms().Add(new FuzzyTerm("Small", new TriangularMembershipFunction(-1.0, 0, 1.0)));
fvV2.Terms().Add(new FuzzyTerm("Medium", new TriangularMembershipFunction(0.1, 1.0, 1.25)));
fvV2.Terms().Add(new FuzzyTerm("Large", new TriangularMembershipFunction(1.25, 1.5, 10.0)));
//--- Create Output
FuzzyVariable *fvOutput=new FuzzyVariable("Output",0.0,1.0);
fvOutput.Terms().Add(new FuzzyTerm("Out1", new TriangularMembershipFunction(-0.5, 0.0, 0.75)));
fvOutput.Terms().Add(new FuzzyTerm("Out2", new TriangularMembershipFunction(0.5, 0.75, 1.0)));
//--- Create three Mamdani fuzzy rule
MamdaniFuzzyRule *rule1 = fsTest.ParseRule("if (V1 is Small) or (V1 is Large) then Output is Out1");
MamdaniFuzzyRule *rule2 = fsTest.ParseRule("if (V2 is Small) or (V2 is Large) then Output is Out1");
MamdaniFuzzyRule *rule3 = fsTest.ParseRule("if (V1 is Medium) and (V2 is Medium) then Output is Out2");
//--- Add three Mamdani fuzzy rule in system

CList *inList=new CList;
Dictionary_Obj_Double *p_od_V1=new Dictionary_Obj_Double;
Dictionary_Obj_Double *p_od_V2=new Dictionary_Obj_Double;
p_od_V1.SetAll(fvV1, InpV1);
p_od_V2.SetAll(fvV2, InpV2);

CList *result;
Dictionary_Obj_Double *p_od_Output;
result=fsTest.Calculate(inList);
p_od_Output=result.GetNodeAtIndex(0);
delete inList;
delete result;
delete fsTest;
}
//+------------------------------------------------------------------+```

but I receive the below error message in Experts tab of my terminal:

```2015.09.23 01:56:43.849 Script Test CADJPY,H1: removed
2015.09.23 01:56:43.849 Test CADJPY,H1: uninit reason 1
2015.09.23 01:56:43.849 invalid pointer access in 'List.mqh' (374,7)
2015.09.23 01:56:43.839 Test CADJPY,H1 inputs: InpV1=5.0; InpV2=1.5;

```

and when I copy and paste above code into OnTick() function of an EA, I get the following error:

`2015.09.24 00:09:14.912 2014.01.01 23:00  invalid pointer access in 'GenericFuzzySystem.mqh' (225,25)`

Any idea what I'm doing wrong here?

Edit:  I run above script in MetaTrader 5, and it works just fine!

Moderator
182454
2015.09.24 09:03
Thank you for your message. The issue has been fixed and files have been updated.
6670
2015.09.24 11:48

I wrote the following code to test this fuzzy lib,

but I receive the below error message in Experts tab of my terminal:

and when I copy and paste above code into OnTick() function of an EA, I get the following error:

Any idea what I'm doing wrong here?

Edit:  I run above script in MetaTrader 5, and it works just fine!

May be this question is insolent to you! In this case I beg your pardon and just ignore it!

But if you are developing an EA would you mind to post your code using the fuzzy libs here.

Of course you can exchange your indicators and and entry and exit strategy or you can use an existing here posted EA

like this one: https://www.mql5.com/en/code/913?

EA_MARSI
• 2012.06.12
• Alexander Puzikov
• www.mql5.com
The Expert Advisor is based on EMA_RSI_VA indicator.
28244
2015.09.24 13:16
calli:

May be this question is insolent to you! In this case I beg your pardon and just ignore it!

But if you are developing an EA would you mind to post your code using the fuzzy libs here.

Of course you can exchange your indicators and and entry and exit strategy or you can use an existing here posted EA

like this one: https://www.mql5.com/en/code/913?

Hello Calli,

I'm upgrading RFX Breakout Trader EA with using this fuzzy library. Since it is a product in the market, I cannot post the source code.

Best,
Rasoul

28244
2015.09.24 16:04
MetaQuotes Software Corp.:
Thank you for your message. The issue has been fixed and files have been updated.
Thank you for your prompt response. The problem is now solved, and it works fine :-)