Author: Erics
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open ordersIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
PNNeric
//+------------------------------------------------------------------+
//|                                                 PNNdayCandle.mq4 |
//|                                          Copyright © 2007, Erics |
//|                                                  erics.fx@tut.by |
//+------------------------------------------------------------------+
#property copyright "Erics"
#property link "mailto:erics.fx@tut.by"

/**
* Second dimention of the array should be constant.
*/
#define MAX_INPUT_SIZE 1000

/**
* Input vector size.
*/
extern int InputSize = 36;

/**
* Amount of stored patterns.
*/

extern int AmountOfPatterns = 400;

/**
* Standard deviation.
*/
extern double sigma = 0.03;

/**
* Trading treshold.
*/
extern double TradingTreshold = 0.0;

/**
* Number of bars used for forcast.
*/
extern int AmoutOfForecastBars = 10;

/**
* Patterns.
*/
double w[][MAX_INPUT_SIZE];

/**
* Pattern class.
*/
int b[];

/**
* PNN input vector.
*/
double x[];

/**
* PNN available classes.
*/
double Class[2];

/**
* PNN output neurons.
*/
double Neurons[2];

/**
* Take action flags.
*/
bool Buy, Sell, CloseBuy, CloseSell;

/**
* The moment in time when to start next training.
*/
int TrainingStartTime = 0;

void init(){
   ArrayResize(w, AmountOfPatterns);
   ArrayResize(b, AmountOfPatterns);
   ArrayResize(x, InputSize);
}

void deinit(){
}

int start(){
/*
* Input vector size is limited by the size of array.
*/
   if(InputSize > MAX_INPUT_SIZE){
      Comment( "*** ERROR - Input size vector is too big! ***" );
      return(0);
   }

/*
* If there is no enough data just stop break.
*/
   if(Bars < AmountOfPatterns+AmoutOfForecastBars){
      Comment( "No enough chart bars!" );
      return(0);
   }

/*
* Do calculations if there is a new bar on the chart.
*/
   if(TimeCurrent() > Time[0]){
      Comment("No new bar for calculations! Server time: ", TimeCurrent(), " Chart start time:", Time[0]);
      return(0); 
   }

/*
* Training fragment.
*/
   if(TrainingStartTime < Time[0]) {
      Comment( "Current time is: ", Time[0], " Next training time will be: ", TrainingStartTime);
      Neurons[0] = 0;
      Neurons[1] = 0;

/*
* Collect history of bars before specific moment in time.
*/
     for(int bar=0; bar<AmountOfPatterns; bar++) {
         for(int i=0; i<InputSize; i++){
         w[bar][i] = Input(i, bar+AmoutOfForecastBars);
         }

/*
* Class 1 if + close and class 0 if - close
*/
         b[bar] = (Close[bar]-Open[bar+AmoutOfForecastBars-1]) > 0;
         Neurons[ b[bar] ]++;
     }
   TrainingStartTime = Time[0] + Period()*60*AmoutOfForecastBars;
   }

/*
* Input classification.
*/
   for(i=0; i<InputSize; i++){
   x[i] = Input(i, 1);
   }

/*
* Radial function.
*/
   double Yi, d;
   Class[0] = 0.0; 
   Class[1] = 0.0;

   double D = 2.0*MathPow(sigma, 2);
   for(int p=0; p<AmountOfPatterns; p++) {
      for(Yi=0.0,d=0.0,i=0; i<InputSize; i++){
      d += MathPow(w[p][i]-x[i], 2.0);
   }

   Yi = MathExp(-d/D);

/*
* Use pattern class information.
*/
   Class[ b[p] ] += Yi;
   }

/*
* Output calculation.
*/
   if(Neurons[0] > 0){ 
      Class[0] /= Neurons[0];
   }
   if(Neurons[1] > 0){
      Class[1] /= Neurons[1];
   }

   double out = (Class[1]-Class[0]) / (Class[1]+Class[0]);

/*
* 0<OUT<=1 = Class1, -1<=OUT<=0 = Class0
*/
   int TotalNumberOfOrders = CalculateCurrentOrders( Symbol() );

/*
* Trading decision.
*/
   Sell = False;
   Buy = False;
   CloseSell = False;
   CloseBuy = False;

/*
* Trade only if treshold is good enough.
*/
   if(out < -TradingTreshold){
   Sell = True;
   }
   if(out > TradingTreshold){ 
   Buy = True;
   }

/*
* Close ineffective orders.
*/
   if(out > 0){
      CloseSell = True;
   }
   if(out < 0){
      CloseBuy = True;
   }

   if(TotalNumberOfOrders > 0){
      CheckForClose();
   }

   if(TotalNumberOfOrders < 1){
      CheckForOpen();
   }
   return(0);
 }

double Input(int input, int shift) {
return( Close[shift+input] );
}

//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen(){
double lot = 0.1;
double sl = 0;
double tp = 0;

RefreshRates();

if(Sell == true){
if(tp != 0){
tp = Bid - tp*Point;
}

if(sl != 0){
sl = sl*Point + Ask;
}

OrderSend(Symbol(), OP_SELL, lot, Bid, 0, sl, tp, "", 0, 0, Red);
} 

if(Buy == true){
if(tp != 0){
tp = tp*Point + Ask;
}

if(sl != 0){
sl = Bid - sl*Point;
}

OrderSend(Symbol(), OP_BUY, lot, Ask, 0, sl, tp, "", 0, 0, Green);
} 
}

//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose(){ 
RefreshRates();

for(int i=0; i<OrdersTotal(); i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false){
Print(GetLastError());
break;
}

if(OrderType()==OP_BUY && CloseBuy){
OrderClose(OrderTicket(), OrderLots(), Bid, 3, White);
}

if(OrderType()==OP_SELL && CloseSell){
OrderClose(OrderTicket(), OrderLots(), Ask, 3, White);
}
}
}

int CalculateCurrentOrders(string symbol){
int buys = 0;
int sells = 0;

for(int i=0; i<OrdersTotal(); i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false){
break;
}

if(OrderSymbol()==Symbol() /*&& OrderMagicNumber()==MAGICMA*/){
if(OrderType()==OP_BUY || OrderType()==OP_BUYSTOP){
buys++;
}

if(OrderType()==OP_SELL || OrderType()==OP_SELLSTOP){
sells++;
}
}
}

return(buys+sells);
}

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