Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Series array that contains open prices of each barSeries 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
Miscellaneous
It writes information to fileIt writes information to fileIt writes information to fileIt writes information to file
0 Views
0 Downloads
0 Favorites
Range_v5
ÿþ//+------------------------------------------------------------------+

//|                                                        Range.mq4 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

#property indicator_chart_window

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

//|                                                                  |

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

/*  < - Delete this 

#include <WinUser32.mqh>

#import "user32.dll"

int RegisterWindowMessageW(string lpString);

int PostMessageW(int hWnd,int Msg,int wParam,int lParam);

#import

  Delete this - >*/

  

void _postMessage(int pHandle,int pMessage)

  {

   if(pHandle!=0)

     {

      //     PostMessageW(pHandle,WM_COMMAND,33324,0);  PostMessageW(pHandle,pMessage,2,1); //< - Delete comment 

     }

  }

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

//|                                                                  |

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

int OnInit()

  {

// MT4InternalMsg=RegisterWindowMessageW("MetaTrader4_Internal_Message");  //< - Delete comment



   if(Period()!=1)

     {

      Comment("The 'Range' indicator work only with M1.");

      return -1;

     }



   if(!IsDllsAllowed())

     {

      Comment("Please Allow DLL imports.");

      return -1;

     }



   int voidA[15],BarsCalculate=0,iChartBars=iBars(Symbol(),PERIOD_M1);



   Range=Pips*Point;

   handle=FileOpenHistory(Symbol()+IntegerToString(Frame)+".hst",FILE_BIN|FILE_WRITE|FILE_SHARE_READ|FILE_ANSI);

   if(handle < 0) return (-1);



   FileWriteInteger(handle,400,LONG_VALUE);

   FileWriteString(handle,"(C)opyright 2019, Roman",64);

   FileWriteString(handle,Symbol(),12);

   FileWriteInteger(handle,Frame,LONG_VALUE);

   FileWriteInteger(handle,Digits,LONG_VALUE);

   FileWriteArray(handle,voidA,0,15);

   FileFlush(handle);



   BarsCalculate=iChartBars>BarsHistory? BarsHistory==0 ? iChartBars--: BarsHistory--: iChartBars--;



   FileSeek(handle,0,SEEK_END);

   FileWriteInteger(handle,(int)iTime(Symbol(),PERIOD_M1,BarsCalculate),LONG_VALUE);

   FileWriteDouble(handle,iOpen(Symbol(),PERIOD_M1,BarsCalculate),DOUBLE_VALUE);

   FileWriteDouble(handle,iOpen(Symbol(),PERIOD_M1,BarsCalculate),DOUBLE_VALUE);

   FileWriteDouble(handle,iOpen(Symbol(),PERIOD_M1,BarsCalculate),DOUBLE_VALUE);

   FileWriteDouble(handle,iOpen(Symbol(),PERIOD_M1,BarsCalculate),DOUBLE_VALUE);

   FileWriteDouble(handle,1,DOUBLE_VALUE);

   FileFlush(handle);



   PrevLow=iOpen(Symbol(),PERIOD_M1,BarsCalculate);

   PrevHigh=iOpen(Symbol(),PERIOD_M1,BarsCalculate);



   for(int i=BarsCalculate; i>=0; i--)

      Candle(

             iTime(Symbol(),PERIOD_M1,i),

             iOpen(Symbol(),PERIOD_M1,i),

             iLow(Symbol(),PERIOD_M1,i),

             iHigh(Symbol(),PERIOD_M1,i),

             iClose(Symbol(),PERIOD_M1,i)

             );



   RefreshRates();

   if(AutoOpen)UpDateChart();

   return(INIT_SUCCEEDED);

  }

extern int Pips=50;           //Size Range in Pips

extern int Frame=2;           //Offline TimeFrame

extern int BarsHistory=20000; //Count Bars to Plot Range Chart

extern bool AutoOpen=true;    //Auto Open Offline Chart



int handle,MT4InternalMsg;

double Range,PrevLow,PrevHigh;

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

//|                                                                  |

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

void CloseOpenCandle(datetime Time_,double close_)

  {

   ModifyCandle(close_);

   FileSeek(handle,0,SEEK_END);

   FileWriteInteger(handle,(int)Time_,LONG_VALUE);

   FileWriteDouble(handle,NormalizeDouble(close_,Digits),DOUBLE_VALUE);

   FileWriteDouble(handle,NormalizeDouble(close_,Digits),DOUBLE_VALUE);

   FileWriteDouble(handle,NormalizeDouble(close_,Digits),DOUBLE_VALUE);

   FileWriteDouble(handle,NormalizeDouble(close_,Digits),DOUBLE_VALUE);

   FileWriteDouble(handle,1,DOUBLE_VALUE);

   FileFlush(handle);

   PrevLow=close_;

   PrevHigh=close_;

  }

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

//|                                                                  |

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

void ModifyCandle(double close_)

  {

   FileSeek(handle,-32,SEEK_END);

   FileWriteDouble(handle,NormalizeDouble(PrevLow,Digits),DOUBLE_VALUE);

   FileWriteDouble(handle,NormalizeDouble(PrevHigh,Digits),DOUBLE_VALUE);

   FileWriteDouble(handle,NormalizeDouble(close_,Digits),DOUBLE_VALUE);

   FileWriteDouble(handle,1,DOUBLE_VALUE);

   FileFlush(handle);

  }

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

//|                                                                  |

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

bool UpCandle(datetime Time_,double Close_)

  {

   if(PrevHigh-Close_>Range)

     {

      PrevLow=PrevHigh-Range;  CloseOpenCandle(Time_,PrevLow); return true;

     }



   if(Close_-PrevLow>Range)

     {

      PrevHigh=PrevLow+Range;  CloseOpenCandle(Time_,PrevHigh);  return true;

     }



   if(Close_>PrevHigh)PrevHigh=Close_;

   if(Close_<PrevLow)PrevLow=Close_;

   ModifyCandle(Close_);

   return false;

  }

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

//|                                                                  |

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

void Candle(datetime Time_,double Open_,double Low_,double High_,double Close_)

  {

   if(High_-Open_<Open_-Low_)

     {

      while(UpCandle(Time_,Open_));

      while(UpCandle(Time_,High_));

      while(UpCandle(Time_,Low_));

      while(UpCandle(Time_,Close_));

     }

   else

     {

      while(UpCandle(Time_,Open_));

      while(UpCandle(Time_,Low_));

      while(UpCandle(Time_,High_));

      while(UpCandle(Time_,Close_));

     }

   return;

  }

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

//|                                                                  |

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

void OnDeinit(const int reason)

  {

   if(handle>=0)

     {

      FileClose(handle);

      handle=-1;

     }

  }

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

//|                                                                  |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//---

   while(UpCandle(Time[0],Close[0]));

   if(AutoOpen)UpDateChart();

//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

//|                                                                  |

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

void UpDateChart()

  {

   if(CheckChart(Symbol(),Frame)==0)ChartOpen(Symbol(),Frame);

   _postMessage(WindowHandle(Symbol(),Frame),MT4InternalMsg);





  }

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

long CheckChart(string sym,int period)

  {

   long result=0,id=ChartFirst();

   while(id>=0)

     {

      if(ChartSymbol(id)==sym && ChartPeriod(id)==period && ChartGetInteger(id,CHART_IS_OFFLINE))

        {

         result=id;

         ChartSetInteger(id,CHART_AUTOSCROLL,true);

         ChartSetInteger(id,CHART_SHIFT,true);

         ChartNavigate(id,CHART_END);

         ChartRedraw(id);

         break;

        }

      id=ChartNext(id);

     }

   return(result);

  }

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