Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

Metatrader 4 Libraries | Stochastic Net | MQL5 Release

RobotFX curates the best open-source MetaTrader code to inspire your trading automation.

Image for Stochastic Net

If necessary, you can kill older neurons and so get rid of the "old stereotypes". To do it you should specify the lifetime of the neurons in bars (NL). When a new neuron is created the time of its creation is memorized and in future this mark will allow you to delete it. So the specification of the time factor is obligatory when inputting any kind of data to the net.

We use the DestroyNet function to delete the net. It is very simple. Do not forget to delete the unnecessary nets, otherwise they will stay living in the memory of the PC until the MetaTrader is closed.

int DestroyNet(string NN); // Deletes the <NetName> net, returns the number of remaining nets

Trade Renko charts automatically using the advanced Renko Expert Advisors. Clean trends, better entries. Discover them here.

I didn't implement the checking of the unicity of the names, and you can create a lot of nets of the same name. Nevertheless the nets will differ from each other with a serial number.

string NetName(int i); // Returns the name of the net by the specified serial number
int GetNetsNumber(); // Returns the total number of the nets in the library
int NetNumber(string NetName); // Returns the serial number of the <NetName> net in the library

You should train the net using the TrainNet function. The right answer is probably necessary for introducing the order. For example, we accept 1 for the BUY signals, and -1 for the SELL signals. As far as for the BUY and SELL different nets are used its is not taken into the account inside the net. We take the current time or the time of bar creation.

double TrainNet(string NetName,
double& iX[], // the inputted normalized vector of the InputVectorSize size
int T, // true answer
datetime sT); // time of vector creation

During the process of training you can determine if the net has recognized the pattern and modernized or it has added it as a new one. Use the SizeOfNet function for this purpose.

int SizeOfNet(string NetName); // Returns the number of the Gauss neurons

You should read the response using the GetPredict function. Its parameters are the same with the TrainNet. If you input the same vector as the net was thaught then the answer will be 1.

double GetPredict(string NetName, // Delivers the probability [0..1] of belonging of the input vector to the class
double& iX[],
datetime sT);

And these functions are necessary for the internal working of the net. Just use them as is shown in the example.

void Shrink(string NetName);
void WReset(string NetName);
void AEqual(string NetName);
void CW(string NetName);
void KillWeaks(string NetName);
void KillOlds(string NetName,datetime sT);

The examples of usage

The example of init() function code

Print("Net Created with number: ",CreateNet("OverBought",24,0.2,0.95,0.001,Period()*60*1000)); // Create a net for the Sell signals
Print("Net Created with number: ",CreateNet("OverSold",24,0.2,0.95,0.001,Period()*60*1000)); // Create a net for the Buy signals

//~~
Print("Initial training started ...");
for (int R = 1; R <= Repetitions; R++) // Process of the primary training
{ // on the history data
WReset("OverBought"); // Zeroise the values of the weights
InitialTraining("OverBought",HistoryBars,-1); // Run the training
Shrink("OverBought"); // Modernize the net
AEqual("OverBought"); // Set all weights equal to 1
CW("OverBought"); // Calculate the weights
KillWeaks("OverBought"); // Kill the weak neurons
CW("OverBought"); // Calculate the weights

WReset("OverSold"); // Zeroise the values of the weights
InitialTraining("OverSold",HistoryBars,1); // Run the training
AEqual("OverSold"); // Set all weights equal to 1
Shrink("OverSold"); // Modernize the net
CW("OverSold"); // Calculate the weights
KillWeaks("OverSold"); // Kill the weak neurons
CW("OverSold"); // Calculate the weights
FlushReport("OverSold",R); // Output the reports to the file

}

The example of the start() function code

KillOlds("OverBought",iTime(Symbol(),0,1)); // Kill the old neurons
PostTraining("OverBought",-1); // Continune training during the process
Shrink("OverBought"); // Modernize the net
AEqual("OverBought"); // Set all weights equal to 1
CW("OverBought"); // Calculate the weights
KillWeaks("OverBought"); // Kill the old neurons
CW("OverBought"); // Calculate the weights

KillOlds("OverSold",iTime(Symbol(),0,1));
PostTraining("OverSold",1); // Continue training during the process
AEqual("OverSold"); // Set all weights equal to 1
Shrink("OverSold"); // Modernize the net
CW("OverSold"); // Calculate the weights
KillWeaks("OverSold"); // Kill the old neurons
CW("OverSold"); // Calculate the weights

//--------------------------------------------------------------------
double OverBought = Predict("OverBought");
double OverSold = Predict("OverSold");
Out = (OverBought - OverSold)/(OverBought+OverSold);

The example of the deinit() function code

Print("Destroying net=>",Destroy ("OverBought")); // Kill what we have prolonged
Print("Destroying net=>",Destroy ("OverSold")); // Kill what we have prolonged

The working capacity of the net is successfully checked. The good results depend on the method of creation of the input vector. It is important to understand that the vectors, that are preliminary known to be answering the class conditions, should be inputted during the training. In a simple language, if a net is created for the SELL signals then the bar the training vector is taken from should have the SELL signal indeed. In other case the net will gather the contradictive signals and you won't get the desired result.

I invite you to discuss the following questions:

1. The forming of the input vector - At present I use 24 parameters (the difference between the moving averages), normalized to unity for the range of 24 bars. I.e. I calculate the 24x24 matrix. Then I normalize it and take the last values.

2. The determinations of the "right answer" - i.e. when the outputted vector is the signal. I use the AMA at present. So I get to know if there was a signal only after two bars.

That's all.

Level up your trading with professional RobotFX expert advisors and indicators. Visit robotfx.org for proven MT4/MT5 tools.

8514

Best MetaTrader Indicators + Profitable Expert Advisors