Download MetaTrader 5

EA - Neural Networks issue

To add comments, please log in or register
Write your articles using our editor. It is convenient!
Bogi_Loco85
7
Bogi_Loco85 2015.09.02 20:28 

Hi guys, can someone help me with the issue I am struggling with. Constantly while I am trying to perform test on my EA, I am receiving following errors:

- TestGenerator unmatched data error volume limit 3799 at 2015.08.07 17:00 exceeded)

- Cannot call Fann2MQL.dll:f2M_create_standard, dll is not allowed

- unresolved import funcion call

----> I have my FannMQL installed dlls in Libraries and mqhs in Includes


The code looks liek this could you please have a look on this:


#property tester_library "Fann2MQL.dll"
#property link      "https://www.mql5.com";
#property version   "1.00"
#property strict
#include <Fann2MQL.mqh>


//+------------------------------------------------------------------+
//| General trading variables                                        |
//+------------------------------------------------------------------+
  extern double lots = 0.01;
  extern double price = 0;
  extern int stoploss = 7;
  extern int takeprofit = 32;
  extern double trailingStopLevel = 5;

//+------------------------------------------------------------------+
//| Neural network variables                                         |
//+------------------------------------------------------------------+

  // Zaprojektowany schemat sieci neuronowej
  extern int neuralNetworkInputLayer = 3;
  extern int neuralNetworkFirstHiddenLayer = 3;
  extern int neuralNetworkSecondHiddenLayer = 2;
  extern int neuralNetworkOutputLayer = 1;
  int artificialNeuralNetwork;

  double neuralNetworkTrainingData[][4];
 

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
      createNeuralNetwork();
      neuralNetworkDataLoader();
   
//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
    f2M_destroy_all_anns();   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void start()
  {
//---
    
   double inputA, inputB, inputC;
   bool areWeGoingToMakeSomeTrade=false;
   
   inputA=iCCI("EURUSD",PERIOD_M30,3,PRICE_TYPICAL,0);
   inputB=iCCI("EURUSD",PERIOD_M30,3,PRICE_TYPICAL,1);
   inputC=iCCI("EURUSD",PERIOD_M30,3,PRICE_TYPICAL,2);
    
   prepareDataForNeuralNetwork("makeDecisionNeuralNetwork",inputA,inputB,inputC,0,areWeGoingToMakeSomeTrade);
   
   if(areWeGoingToMakeSomeTrade==true)
   {
     if(marketDirectionChecker(inputA,inputC))
     {
       buyOrder();
       trailiStopBuy(trailingStopLevel);
     }
     else if(!marketDirectionChecker(inputA,inputC))
     {
        sellOrder();
        trailingStopSell(trailingStopLevel);
     }
 
   }
   else
   {
     // something will happen:)
   }   
  }
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//| Trading functions                                                |
//+------------------------------------------------------------------+

void buyOrder()
{
  int ticket;
  ticket = OrderSend(Symbol(),OP_BUY,lots,Ask,0,Ask-stoploss*Point,Ask+takeprofit*Point,NULL,0,0);
}

//Sell order
void sellOrder()
{
  int ticket;
  ticket =OrderSend(Symbol(),OP_SELL,lots,Bid,0,Bid+stoploss*Point,Bid-takeprofit*Point,NULL,0,0);
 
}

// Method which moves the StopLoss level higher:)
void trailiStopBuy(double trailingStopLimit)
{
  for(int trade=0; trade<OrdersTotal(); trade++)
  { 
    if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
    {
       if(OrderType()==OP_BUY)
       {
         if(Ask-OrderOpenPrice()>trailingStopLimit*Point)
         {
           if(OrderStopLoss()<Ask-trailingStopLimit*Point) // tutaj trzeba mniej wiecej podac o ile ma nastapic zmiana
           {       
             tradeModifier(Ask+trailingStopLimit*Point);
           }   
         }
       }   
    }
  }
}

// Method which moves StopLoss level lower
void trailingStopSell(double trailingStopLimit)
{
  for(int trade=0; trade<OrdersTotal(); trade++)
  {
    if(OrderType()== OP_SELL)
    {
      if(Bid-OrderOpenPrice()<trailingStopLimit*Point)
      {
        if(OrderStopLoss()>Bid-trailingStopLimit*Point)
        {
           tradeModifier(Bid-trailingStopLimit*Point);
        } 
      }   
    }
  }
}

// Method which modify the StopLosses:)
void tradeModifier(double numberOfPoints)
{
  bool orderWasModified=true;
  orderWasModified = OrderModify(OrderTicket(),OrderOpenPrice(),numberOfPoints,OrderTakeProfit(),0,clrNONE);
}

bool marketDirectionChecker(double inputA, double inputC)
{
  bool ifItGoesUp=false; 
  if(inputA>inputC)
  {
    ifItGoesUp = true;
  }
  return ifItGoesUp;
}

// isTradeAllowedChecker - ma sprawdzac czy odpowiedni timeframe itd itp:)
// tradeLogger - ma wpisywac logi o trejdach i wartosciach na jkich podjal decyzje
// dodac pseudowyjatki?

//+------------------------------------------------------------------+
//| Neural network methods                                           |
//+------------------------------------------------------------------+

//Funkcja tworzy sieć neuronową
void createNeuralNetwork()
{
   artificialNeuralNetwork=f2M_create_standard(4,neuralNetworkInputLayer,neuralNetworkFirstHiddenLayer,neuralNetworkSecondHiddenLayer,neuralNetworkOutputLayer);
  
   // ustawienie funckji aktywacji dla poszczególnych warst
   f2M_set_act_function_hidden(artificialNeuralNetwork,FANN_SIGMOID_SYMMETRIC_STEPWISE);
   f2M_set_act_function_output(artificialNeuralNetwork,FANN_SIGMOID_SYMMETRIC_STEPWISE);
   
   // ustawienie wag w sieci neuronowej
   f2M_randomize_weights(artificialNeuralNetwork,-0.77, 0.77); 
   
   //Wypisanie ilosci neuronów wejściowych i wyjściowych
   Print("f2M_get_num_input(ann)",f2M_get_num_input(artificialNeuralNetwork));
   Print("f2M_get_num_output(ann)",f2M_get_num_output(artificialNeuralNetwork));
}

//Funkcja przygotowująca dane dla sieci neuronowej
void prepareDataForNeuralNetwork(string actionRequired, double inputA,
double inputB, double inputC, double output,bool tradeOrNotTrade)
{
  double inputVector[];
  double outputVector[];
  
  // zmiana wielkosci tablicy do wlasciwych rozmiarow
  ArrayResize(inputVector, f2M_get_num_input(artificialNeuralNetwork));
  ArrayResize(outputVector, f2M_get_num_output(artificialNeuralNetwork));
  
  inputVector[0]=inputA;
  inputVector[1]=inputB;
  inputVector[2]=inputC;
  
  outputVector[0]=output;
  
  if(actionRequired=="trainNeuralNetwork")
  {
   addTrainingDataToNeuralNetwork(inputVector,outputVector);
  }
  if(actionRequired=="makeDecisionNeuralNetwork")
  {
   neuralNetworkDecisionProvider(inputVector,tradeOrNotTrade);
   
  }
}

//Funkcja dodająca zestaw treningowy
void addTrainingDataToNeuralNetwork(double &inputArray[], double
&outputArray[])
{
  int j;
  ArrayResize(neuralNetworkTrainingData,1000);
     int bufferSize =
     ArraySize(neuralNetworkTrainingData)/(f2M_get_num_input(artificialNeuralNetwork)+f2M_get_num_output(artificialNeuralNetwork))-1;
   //register the input data to the main array
   for (j=0;j<f2M_get_num_input(artificialNeuralNetwork);j++) {
      neuralNetworkTrainingData[bufferSize][j] = inputArray[j];
   }
   for (j=0;j<f2M_get_num_output(artificialNeuralNetwork);j++) {
      neuralNetworkTrainingData[bufferSize][f2M_get_num_input(artificialNeuralNetwork)+j]
      = outputArray[j];
   }
   
   ArrayResize(neuralNetworkTrainingData,bufferSize+2);
  
 }
 
 //Funkcja podejmująca decyzję - FUNKCJA DO POPRAWY
 double neuralNetworkDecisionProvider(double &inputVector[], bool tradeOrNotTrade)
 {
  int out;
  double output;
  
  //wysylanie danych do neuronu
  out=f2M_run(artificialNeuralNetwork,inputVector);
  
  //sprawdza jaki jest wynik uzywajac funkcji f2M_get_output()
  output=f2M_get_output(artificialNeuralNetwork,0);
  
  if(output==1)
  {
    tradeOrNotTrade=true;  
  }
  
  return (output);
 }
 //Funkcja ucząca sieć neuronową FUNKCJA DO POPRAWY
 double teach() {
   int i,j;
   double MSE;
   double inputVector[];
   double outputVector[];
   ArrayResize(inputVector,f2M_get_num_input(artificialNeuralNetwork));
   ArrayResize(outputVector,f2M_get_num_output(artificialNeuralNetwork));
   int call;
   int bufferSize =
   ArraySize(neuralNetworkTrainingData)/(f2M_get_num_input(artificialNeuralNetwork)+f2M_get_num_output(artificialNeuralNetwork))-1;
   for (i=0;i<bufferSize;i++) {
      for (j=0;j<f2M_get_num_input(artificialNeuralNetwork);j++) {
         inputVector[j] = neuralNetworkTrainingData[i][j];
      }
      outputVector[0] = neuralNetworkTrainingData[i][3];
      //f2M_train() is showing the neurones only one example at a time.
      call = f2M_train(artificialNeuralNetwork, inputVector,
      outputVector);
   }
   // Once we have show them an example, 
   // we check if how good they are by checking their MSE. 
   // If it's low, they learn good!
   MSE = f2M_get_MSE(artificialNeuralNetwork);
   return(MSE);
}

// Funkcja przygotowuje i uczy sieć neuronową - FUNKCJA DO POPRAWY
void neuralNetworkDataLoader()
{
   bool notRealBoolean = false;
   double MSE;
   int maxTraining = 300;  
   double targetMSE = 0.1;    // MeanSquareError - wspolczynnik bledu dla sieci 
   // Przygotowywanie danych potrzebnych do nauki
   
   prepareDataForNeuralNetwork("trainNeuralNetwork",1,2,3,0,notRealBoolean);
   prepareDataForNeuralNetwork("trainNeuralNetwork",4,4,0,0,notRealBoolean);
   prepareDataForNeuralNetwork("trainNeuralNetwork",0,-11,-20,1,notRealBoolean);
   
   for (int i=0;i<maxTraining;i++) 
   {
      MSE = teach(); 
      if (MSE < targetMSE) { // if the MSE is lower than what we defined  (here targetMSE = 0.02)
     
         Print("training finished. Trainings ",i+1); // then we print in the console 
         
                                                     // how many training 
                                                     // it took them to
                                                     //understand
         i = maxTraining; // and we go out of this loop
      }
   }
   
   // we print to the console the MSE value once the training is completed
   Alert("MSE",f2M_get_MSE(artificialNeuralNetwork));
 
 }
Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
Stuart Browne
5816
Stuart Browne 2015.09.03 00:30  

Make sure you have allowed the use of DLLs in both MT options and the EA options/inputs

 

p.s. please use the SRC option in the toolbox when posting code, I've fixed it for you this time 

To add comments, please log in or register