pnn-trainer

Author: Paco Hern�ndez G�mez
Price Data Components
Series array that contains close prices for each bar
0 Views
0 Downloads
0 Favorites
pnn-trainer
//+------------------------------------------------------------------+
//| Example training expert advisor                                  |
//|                                                  PNN 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 "PNN.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 = 18;

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

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

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

/**
 * Train the PNN
 */
int start() {
   
   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
    */
   for (int i = 0; i < 60; i++) {
      data[length][i] = (iClose(Symbol(), PERIOD_M15, i) - iClose(Symbol(), PERIOD_M15, i + 1)) / Point;
      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 ---