Price Data Components
Series array that contains open time of each bar
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
PlotGraph
/*
 * Filename:    PlotGrapth.mq4
 * Author:      sae2k
 * Date:        Mar 5, 2009
 *
 * Description: 
 *             Plot graph of function 
 */

#include <stdlib.mqh>
#include <WinUser32.mqh>

// Import PNN library
#import "PNN_parzen.ex4"
double PNN(int N, int d, double sigma, double test_example[d], double Examples[N][d]);
#import



#property indicator_chart_window
#property indicator_buffers   3
#property indicator_color1    Green
#property indicator_color2    Red
#property indicator_color3    Yellow

//---- buffers
double Buf[];
double Buf1[];
double Buf_current[];

double   pnn_trend_up[0][150];
double   pnn_trend_down[0][150];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(3);
   
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,indicator_color1);
   SetIndexBuffer(0,Buf);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1,indicator_color2);
   SetIndexBuffer(1,Buf1);
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1,indicator_color3);
   SetIndexBuffer(2,Buf_current);
   
   History_train_trend();
   //print_arr(pnn_trend_up);
   
   return(0);
}

  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   return(0);
}


datetime last_dt;

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int    shift, limit, lim, i;
      
   datetime dt = iTime(NULL,0,1);
   if( last_dt == dt) return(0);
   last_dt = dt;
   Comment(TimeToStr(dt, TIME_DATE|TIME_MINUTES));


   //create current vektor
   double vector[1][150];
   for (int j = 0; j < 50; j++) {
      double tmp1 = (iMA(NULL, 0, 50, 0, MODE_SMMA, PRICE_CLOSE, j) - iMA(NULL, 0, 50, 0, MODE_SMMA, PRICE_CLOSE, j+1)); // / Point;
      double tmp2 = (iMA(NULL, 0, 20, 0, MODE_SMMA, PRICE_CLOSE, j) - iMA(NULL, 0, 20, 0, MODE_SMMA, PRICE_CLOSE, j+1)); // / Point;
      double tmp3 = (iMA(NULL, 0, 10, 0, MODE_SMMA, PRICE_CLOSE, j) - iMA(NULL, 0, 10, 0, MODE_SMMA, PRICE_CLOSE, j+1)); // / Point;
      vector[j*3+0][0] = tmp1;
      vector[j*3+1][0] = tmp2;
      vector[j*3+2][0] = tmp3;
   }
   

   double x;
   double scale = 0.0000025;
   double a = -0.0005;      // íà÷àëî ãðàôèêà
   double b = 0.0005;       // êîíåö ãðàôèêà
    
   x = a;
   double len = (b - a) / scale;   
   for( shift = len; shift >= 0; shift--)
   {
      double buy = pdf(x, 0.0005, ArrayRange(pnn_trend_up, 0), ArrayRange(pnn_trend_up, 1), pnn_trend_up);
      double sell = pdf(x, 0.0005, ArrayRange(pnn_trend_down, 0), ArrayRange(pnn_trend_down, 1), pnn_trend_down);
      double pnn = pdf(x, 0.0005, 1, ArrayRange(vector, 1), vector);
      //Print("x=", DoubleToStr(x,8), " ", DoubleToStr(buy,8), " ", DoubleToStr(sell,8));
      
      Buf[shift] = 3.0 * buy;
      Buf1[shift] = 3.0 * sell;
      //Buf_current[shift] = 3.0 * pnn;
      
      x = x + scale;
   }
   
   // calculate probality current vector
   double prob_trend_up   = PNN(ArrayRange(pnn_trend_up, 0),  ArrayRange(pnn_trend_up, 1),  0.0005, vector, pnn_trend_up);
   double prob_trend_down = PNN(ArrayRange(pnn_trend_down, 0),  ArrayRange(pnn_trend_down, 1), 0.0005, vector, pnn_trend_down);

   Comment("UP ",DoubleToStr(prob_trend_up, 8), " DOWN ", DoubleToStr(prob_trend_down, 8));


   
   return(0);
}
//+------------------------------------------------------------------+




double pdf(double x, double sigma, int N, int d, double arr[][])
{
 
   double sum = 0.0;
   for( int i = 0; i < N; i++ ) 
   {
      double product = 0; 
      
      for( int j = 0; j < d; j++ )
         product += MathPow( (arr[i][j] - x), 2);
      product = (-1.0)*(product) / (2.0 * (sigma*sigma)); 
      product = MathExp(product); 
      sum += product;
   }
   sum /= N;
   
   
   return (sum);
}


int print_arr(double arr[][])
{
   int length = ArrayRange(arr, 0);
   int length1 = ArrayRange(arr, 1);
   for( int i = 0; i < length; i++){
      string str = "";
      for( int y = 0; y < length1; y++){
         str = StringConcatenate(str, DoubleToStr(arr[i][y], 5), " ");
      }
      Print(str);
   }
}


/*
 *     History
 *     
 */

int History_train_trend()
{

   int trend_up_count = 0;
   int trend_down_count = 0;  
   
   
   int n = 5000;        // number Bars
   
   int      count = 0;
   double   last_ma1 = -1.0;
   double   last_ma2 = -1.0;
   double   last_ma3 = -1.0;
   int      trend_up = 0;
   int      trend_down = 0;
   int      saved_bar = -1;
   for(int y = n; y > 1; y--)
   {
      double ma1 = iMA(NULL, 0, 50, 0, MODE_SMMA, PRICE_CLOSE, y);
      double ma2 = iMA(NULL, 0, 20, 0, MODE_SMMA, PRICE_CLOSE, y);
      double ma3 = iMA(NULL, 0, 10, 0, MODE_SMMA, PRICE_CLOSE, y);
      
      
      if( ma1 < ma2 && ma2 < ma3)   // trend UP
      {
         trend_up++;
         trend_down = 0;
         if( saved_bar == -1)
            saved_bar = y;
      }
      else if( ma1 > ma2 && ma2 > ma3)  // trend DOWN
      {  
         trend_down++;
         trend_up = 0;
         if( saved_bar == -1)
            saved_bar = y;
      }
      else
      {
         
         if( saved_bar > 0 && saved_bar > 50 && (trend_down > 50 || trend_up > 50))
         {
            
            Print("   TREND result at ", y, "(",saved_bar,")"," tDOWN = ", trend_down, " tUP = ", trend_up, 
                  " begin=", TimeToStr(iTime(Symbol(), 0, saved_bar), TIME_DATE|TIME_SECONDS)," end=",TimeToStr(iTime(Symbol(), 0, y), TIME_DATE|TIME_SECONDS));
                  
            // create many vektors
            for( int i = saved_bar - 50; i > y; i--){
               
               //create vector
               double vector[150];
               for (int j = 0; j < 50; j++) {
                  double tmp1 = (iMA(NULL, 0, 50, 0, MODE_SMMA, PRICE_CLOSE, i+j) - iMA(NULL, 0, 50, 0, MODE_SMMA, PRICE_CLOSE, i+j+1)); // / Point;
                  double tmp2 = (iMA(NULL, 0, 20, 0, MODE_SMMA, PRICE_CLOSE, i+j) - iMA(NULL, 0, 20, 0, MODE_SMMA, PRICE_CLOSE, i+j+1)); // / Point;
                  double tmp3 = (iMA(NULL, 0, 10, 0, MODE_SMMA, PRICE_CLOSE, i+j) - iMA(NULL, 0, 10, 0, MODE_SMMA, PRICE_CLOSE, i+j+1)); // / Point;
                  vector[j*3+0] = tmp1;
                  vector[j*3+1] = tmp2;
                  vector[j*3+2] = tmp3;
               }
               
               // add vektor to array
               int length;
               int z;
               if( trend_up > 0){
                  length = ArrayRange(pnn_trend_up, 0);
                  ArrayResize(pnn_trend_up, length + 1);
                  for( z = 0; z < ArrayRange(vector, 0); z++)
                     pnn_trend_up[length][z] = vector[z];
                  trend_up_count++;
               }
               
               if( trend_down > 0){
                  length = ArrayRange(pnn_trend_down, 0);
                  ArrayResize(pnn_trend_down, length + 1);
                  for( z = 0; z < ArrayRange(vector, 0); z++)
                     pnn_trend_down[length][z] = vector[z];               
                  trend_down_count++;
               }
            } 
         }
         
         trend_up = 0;
         trend_down = 0;
         saved_bar = -1;
      }
   }//for

   Print("History_train_trend: added buy vector: ", trend_up_count, ", added sell vector: ", trend_down_count);   

}

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 ---