pnn2-trainer

Author: Paco Hern�ndez G�mez
Price Data Components
Series array that contains the lowest prices of each barSeries array that contains the highest prices of each barSeries array that contains close prices for each bar
Indicators Used
Commodity channel index
0 Views
0 Downloads
0 Favorites
pnn2-trainer
//+------------------------------------------------------------------+
//| Example training expert advisor                                  |
//|                                                 PNN2 Trainer.mq4 |
//|                                             Paco Hernández Gómez |
//|                                    http://www.hernandezgomez.com |
//+------------------------------------------------------------------+
#property copyright "Paco Hernández Gómez"
#property link      "http://www.hernandezgomez.com"

// Import PNN library
#import "PNN2.ex4"
   void PNNInit();
   void PNNLoad();
   void PNNSave();
   void PNNAddVector(int class, double vector[]);
   int PNNClassifyVector(double vector[]);
#import

/**
 * In this system there is always an open position, either buy or sell.
 */
 
/**
 * Take profits at 18 pips.
 */
extern int LIMIT = 360; //18;

extern double NORMALIZER = 30000000;

/**
 * Take loses at 54 pips.
 */
extern int STOP_LOSS = 240; //54;

/**
 * Use an array to store the opened positions that are going to be used to train the PNN
 */
double data[0][63];

static datetime dtsv;

//////////////////////////////////////////

/**
 * Initialize the PNN
 */
int init() {
   PNNInit();
   ArrayResize(data, 0);
   return(0);

   //STOP_LOSS *= ( 0.0001 / Point );
   //LIMIT *= ( 0.0001 / Point );

   dtsv = Time[2];

}

////////////////////////////////////////////

/**
 * Train the PNN
 */
int start() {
   
   if ( dtsv >= Time[0] ) return;
   dtsv = Time[0] ;
   
   int length = ArrayRange(data, 0);

   /**
    * In each new period, add two new vectors to the training set (two new positions, one for buying and one for selling.
    */
   ArrayResize(data, length + 2);   

   /**
    * Build the two new vectors with the information of the trading system.
    * In this example, we are going to use last 60 periods (15 minutes) close price difference with prior period
    */
    
    
   double month_low  = iLow(Symbol(), PERIOD_MN1, 1) ;
   double month_high = iHigh(Symbol(), PERIOD_MN1, 1) ;
   double month_close= iClose(Symbol(), PERIOD_MN1, 1) ;

   double month_pv = ( month_low + month_high + month_close ) / 3 ;

   double week_low  = iLow (Symbol(), PERIOD_W1, 1) ;
   double week_high = iHigh(Symbol(), PERIOD_W1, 1) ;
   double week_close= iClose(Symbol(), PERIOD_W1, 1) ;

   double week_pv = ( week_low + week_high + week_close ) / 3 ;
   
   double day_low  = iLow(Symbol(), PERIOD_D1, 1) ;
   double day_high = iHigh(Symbol(), PERIOD_D1, 1) ;
   double day_close= iClose(Symbol(), PERIOD_D1, 1) ;

   double day_pv = ( day_low + day_high + day_close ) / 3 ;
   double day_r1 = 2 * day_pv - day_low ;
   double day_r2 = day_pv + ( day_high - day_low ) ;
   double day_r3 = 2 * day_pv + ( day_high - 2 * day_low ) ;
   
   double day_s1 = 2 * day_pv - day_high ;
   double day_s2 = day_pv - ( day_high - day_low ) ;
   double day_s3 = 2 * day_pv - ( 2 * day_high - day_low ) ;
   
   double day_mr1 = ( day_pv + day_r1 ) / 2 ;
   double day_mr2 = ( day_r2 + day_r1 ) / 2 ;
   double day_mr3 = day_mr2 + ( day_mr2 - day_r1 ) / 2 ;
   
   double day_ms1 = ( day_pv + day_s1 ) / 2 ;
   double day_ms2 = ( day_s2 + day_s1 ) / 2 ;
   double day_ms3 = day_ms2 - ( day_ms1 - day_s2 ) / 2 ;
   
   double hl = day_high + day_low ;
   double fib_r1 = day_pv + hl * 0.382 ;
   double fib_r2 = day_pv + hl * 0.618 ;
   double fib_r3 = day_pv + hl * 1.000 ;
   
   double fib_s1 = day_pv - hl * 0.382 ;
   double fib_s2 = day_pv - hl * 0.618 ;
   double fib_s3 = day_pv - hl * 1.000 ;

   double hi = iHigh(Symbol(), PERIOD_M15, 0);
   double lo = iLow(Symbol(), PERIOD_M15, 0);
   double cl = iClose(Symbol(), PERIOD_M15, 0);
   double price ;

   price = cl;
   if ( month_low > hi ) price = hi;
   else if ( month_low < lo ) price = lo;
   data[length][0] = ( month_low - price ) / ( Point * NORMALIZER );

   price = cl;
   if ( month_high > hi ) price = hi;
   else if ( month_high < lo ) price = lo;
   data[length][1] = ( month_high - price ) / ( Point * NORMALIZER );

   price = cl;
   if ( month_close > hi ) price = hi;
   else if ( month_close < lo ) price = lo;
   data[length][2] = ( month_close - price ) / ( Point * NORMALIZER );
   
   price = cl;
   if ( month_pv > hi ) price = hi;
   else if ( month_pv < lo ) price = lo;
   data[length][3] = ( month_pv - price ) / ( Point * NORMALIZER );


   price = cl;
   if ( week_low > hi ) price = hi;
   else if ( week_low < lo ) price = lo;
   data[length][4] = ( week_low - price ) / ( Point * NORMALIZER );

   price = cl;
   if ( week_high > hi ) price = hi;
   else if ( week_high < lo ) price = lo;
   data[length][5] = ( week_high - price ) / ( Point * NORMALIZER );

   price = cl;
   if ( week_close > hi ) price = hi;
   else if ( week_close < lo ) price = lo;
   data[length][6] = ( week_close - price ) / ( Point * NORMALIZER );
   
   price = cl;
   if ( week_pv > hi ) price = hi;
   else if ( week_pv < lo ) price = lo;
   data[length][7] = ( week_pv - price ) / ( Point * NORMALIZER );

   
   price = cl;
   if ( day_low > hi ) price = hi;
   else if ( day_low < lo ) price = lo;
   data[length][8] = ( day_low - price ) / ( Point * NORMALIZER );
   
   price = cl;
   if ( day_high > hi ) price = hi;
   else if ( day_high < lo ) price = lo;
   data[length][9] = ( day_high - price ) / ( Point * NORMALIZER );
   
   price = cl;
   if ( day_close > hi ) price = hi;
   else if ( day_close < lo ) price = lo;
   data[length][10] = ( day_close - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( day_pv > hi ) price = hi;  else if ( day_pv < lo ) price = lo;
   data[length][11] = ( day_pv - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( day_r1 > hi ) price = hi;  else if ( day_r1 < lo ) price = lo;
   data[length][12] = ( day_r1 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( day_r2 > hi ) price = hi;  else if ( day_r2 < lo ) price = lo;
   data[length][13] = ( day_r2 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( day_r3 > hi ) price = hi;  else if ( day_r3 < lo ) price = lo;
   data[length][14] = ( day_r3 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( day_s1 > hi ) price = hi;  else if ( day_s1 < lo ) price = lo;
   data[length][15] = ( day_s1 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( day_s2 > hi ) price = hi;  else if ( day_s2 < lo ) price = lo;
   data[length][16] = ( day_s2 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( day_s3 > hi ) price = hi;  else if ( day_s3 < lo ) price = lo;
   data[length][17] = ( day_s3 - price ) / ( Point * NORMALIZER );
   
   
   price = cl; if ( fib_r1 > hi ) price = hi;  else if ( fib_r1 < lo ) price = lo;
   data[length][18] = ( fib_r1 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( fib_r2 > hi ) price = hi;  else if ( fib_r2 < lo ) price = lo;
   data[length][19] = ( fib_r2 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( fib_r3 > hi ) price = hi;  else if ( fib_r3 < lo ) price = lo;
   data[length][20] = ( fib_r3 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( fib_s1 > hi ) price = hi;  else if ( fib_s1 < lo ) price = lo;
   data[length][21] = ( fib_s1 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( fib_s2 > hi ) price = hi;  else if ( fib_s2 < lo ) price = lo;
   data[length][22] = ( fib_s2 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( fib_s3 > hi ) price = hi;  else if ( fib_s3 < lo ) price = lo;
   data[length][23] = ( fib_s3 - price ) / ( Point * NORMALIZER );


   price = cl; if ( day_mr1 > hi ) price = hi;  else if ( day_mr1 < lo ) price = lo;
   data[length][24] = ( day_mr1 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( day_mr2 > hi ) price = hi;  else if ( day_mr2 < lo ) price = lo;
   data[length][25] = ( day_mr2 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( day_mr3 > hi ) price = hi;  else if ( day_mr3 < lo ) price = lo;
   data[length][26] = ( day_mr3 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( day_ms1 > hi ) price = hi;  else if ( day_ms1 < lo ) price = lo;
   data[length][27] = ( day_ms1 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( day_ms2 > hi ) price = hi;  else if ( day_ms2 < lo ) price = lo;
   data[length][28] = ( day_ms2 - price ) / ( Point * NORMALIZER );
   
   price = cl; if ( day_ms3 > hi ) price = hi;  else if ( day_ms3 < lo ) price = lo;
   data[length][29] = ( day_ms3 - price ) / ( Point * NORMALIZER );
   

   for (int i = 0 ; i < 10 ; i++) {
      data[length][i+30] = iCCI(NULL,PERIOD_M15,14,PRICE_CLOSE,i) / 100 ;
   }

   for ( i = 0; i < 20; i++) {
      data[length][i+40] = (iClose(Symbol(), PERIOD_M15, i) - iClose(Symbol(), PERIOD_M15, i + 1)) / ( Point * NORMALIZER );
   }

   for ( i = 0; i < 60; i++) {
      data[length + 1][i] = data[length][i];
   }
   


   /**
    * Add a buying positions (0)
    */
   data[length][60] = 0; // Compra
   data[length][61] = Ask; // Open position with Ask price (at market)
   data[length][62] = 0; // 0 indicates that this position is opened, 1 indicates that this position is closed
   
   /**
    * Add a selling position (1)
    */
   data[length + 1][60] = 1; // Venta
   data[length + 1][61] = Bid; // Open position with Bid price (at market)
   data[length + 1][62] = 0; // 0 indicates that this position is opened, 1 indicates that this position is closed

   /**
    * Iterate for all opened positions to see if they are winning positions or losing positions
    */
   for (i = 0; i < length; i++) {
      
      // If position is opened
      if (data[i][62] == 0) {
         
         double vector[60];
               
         // If position is a buying position
         if (data[i][60] == 0) {

            // If position is a winner position
            if ((Bid - data[i][61]) / Point >= LIMIT) {
               
               for (int j = 0; j < 60; j++) {
                  vector[j] = data[i][j];
               }

               // Add position to the PNN training vectors set and classify it as a buying position
               PNNAddVector(0, vector);

               // Mark the position as closed position
               data[i][62] = 1;
            }

            // If position is a loser position
            else if ((data[i][61] - Bid) / Point >= STOP_LOSS) {

               // Discard position and mark as closed
               data[i][62] = 1;
            }
         }
         
         // If position is a selling position
         else if (data[i][60] == 1) {
         
            // If position is a winner position
            if ((data[i][61] - Ask) / Point >= LIMIT) {
               
               for (j = 0; j < 60; j++) {
                  vector[j] = data[i][j];
               }

               // Add position to the PNN training vectors set and classify it as a selling position
               PNNAddVector(1, vector);

               // Mark the position as closed position
               data[i][62] = 1;
            }

            // If position is a loser position
            else if ((Ask - data[i][61]) / Point >= STOP_LOSS) {

               // Discard position and mark as closed
               data[i][62] = 1;
            }
         }
      }
   }

   return(0);
}

/**
 * Store the trained PNN in a file
 */
int deinit() {
   PNNSave();
}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---