Follow Move

Author: Copyright © 2017, Vladimir V. Tkach
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each barSeries array that contains open prices of each barSeries array that contains close prices for each barSeries array that contains open time of each bar
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
Follow Move
ÿþ//+------------------------------------------------------------------+

//|                                Copyright 2017, Vladimir V. Tkach |

//+------------------------------------------------------------------+

#property version "1.0"

#property copyright "Copyright © 2017, Vladimir V. Tkach"

#property description "Exper follows by the price movement between Start and End hours."

#property description "Several deals could be opened."

#property description "Opened deals will be closed by timer."

#property link      "https://www.mql5.com/ru/users/net/news"

#property strict

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

enum yes_no

  {

   yes=0,   //yes

   no=1,    //no 

  };

  

extern int

hstart=0,     //Start hour

hend=3,       //End hour

dura=21,      //Deal duration (hours)

max=5,         //Maximum deals

slip=50;       //Slippage (pips)



input yes_no rev=0;  //Use reverse



extern double

lot=0.01;      //Lot size



extern int

magic=113322;  //Magic number



int 

bar,

dir;



double

pstart,

pend;



//+------------------------------------------------------------------+

//|Initialisation function                                           |

//+------------------------------------------------------------------+

int OnInit()

  {

//---

   bar=(int)iTime(Symbol(),PERIOD_H1,1);

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//|Deinitialisation function                                         |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//---

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

//---

  pstart=0.0; 

  pend=0.0;

  double O=2.0, C=2.0, H=1.0, L=1.0, //weights for prices

  dev=O+C+H+L, price, price2;

  

  int i=0;

  while(pstart==0 || pend==0)

  {

      //averaged price

      price=(O*iOpen(Symbol(),PERIOD_H1,i)+C*iClose(Symbol(),PERIOD_H1,i)+H*iHigh(Symbol(),PERIOD_H1,i)+L*iLow(Symbol(),PERIOD_H1,i))/dev;

      

      if(TimeHour(iTime(Symbol(),PERIOD_H1,i))==hstart && pstart==0.0 && pend!=0.0) pstart=price;

      if(TimeHour(iTime(Symbol(),PERIOD_H1,i))==hend && pend==0.0) pend=price;

      

   i+=1;

   if(i>100) return; //break cicle if some error in data

  }

  

  dir=0; price2=Ask; //signal for buy

  if( (pstart>pend && rev==1) || (pstart<pend && rev==0) ) {dir=1; price2=Bid;} //no, signal for sell

  

  if(DealsAmount()<max && Hour()==hend && hend!=hstart && bar!=(int)iTime(Symbol(),PERIOD_H1,0)) 

   {  

      if(!OrderSend(Symbol(),dir,lot,price2,slip,0,0,"",magic,0)) Print("Opening order error: "+(string)GetLastError());

      bar=(int)iTime(Symbol(),PERIOD_H1,0);

   }

  if(OrdersTotal()!=0) CheckAge();

  

  return;

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void CheckAge()

  {

   int tot=OrdersTotal();

   double price3;



   for(int i=tot-1; i>-1; i--)

     {

      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;

      if(OrderMagicNumber()!=magic || OrderSymbol()!=Symbol()) continue;

      if((int)(TimeCurrent()-OrderOpenTime())<dura*60*60) continue; //if deal old

      

      price3=Ask;

      if(OrderType()==0) price3=Bid;

      

      if(!OrderClose(OrderTicket(),OrderLots(),price3,slip)) Print("Closing order error: "+(string)GetLastError());

     }



   return;

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

int DealsAmount()

  {

   int h=0;



   for(int i=0; i<OrdersTotal(); i++)

     {

      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;

      if(OrderMagicNumber()!=magic || OrderSymbol()!=Symbol()) continue;

      h++;

     }

   return(h);

  }

//+------------------------------------------------------------------+

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