PeriodInSeconds1.03

Author: IgorM
Miscellaneous
It writes information to fileIt writes information to fileIt writes information to fileIt issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
PeriodInSeconds1.03
ÿþ//+------------------------------------------------------------------+

//|                                          PeriodInSeconds1.00.mq4 |

//|                                                            IgorM |

//|                              https://www.mql5.com/ru/users/igorm |

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

#property copyright "IgorM"

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

#property version   "1.03"

#property description "=48:0B>@ ?>72>;O5B >B>1@060BL 3@0D8:8 A ;N1K< ?5@8>4>< 7040==K< 2 A5:C=40E"

#property description " "

#property description "The indicator allows you to display charts with any period specified in seconds"

#property strict

#property indicator_chart_window



#define NoUseDll // #15@8B5 MBC AB@>:C, 5A;8 E>B8B5 M<C;8@>20BL B8: =0 :0AB><=>< 3@0D8:5. "@51C5BAO ?>4:;NG5=85 .dll (1C45B 8A?>;L7>20=0 181;8>B5:0 Windows user32.dll)



#ifndef NoUseDll

#include <WinUser32.mqh>

#import "user32.dll"

uint RegisterWindowMessageW(string lpString);

#import

bool     TickEmulation;

#endif



input int   PeriodInSeconds   = 10;    //Period in seconds

input int   TimeFrame         = 4;     //TimeFrame Chart

input bool  OpenChartStart    = true;  //Auto Open Chart From Start Indicator

input bool  UseSystemTimer    = true;  //Use System Timer (false = New Tick)



int      MT4HandleWindow=0,HistoryHandle=-1;

uint     MT4Message;

ulong    FPos;

long     chart_id=0;

datetime NewBarTime=0;

MqlRates OHLC;

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

//| Custom indicator initialization function                         |

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

void HstWriteHeader(int handle,int i_period)

  {

   int      file_version=401;

   string   c_copyright="(C)opyright 2003, MetaQuotes Software Corp.";

   string   c_symbol=_Symbol;

   int      i_digits=_Digits;

   int      i_unused[13];

   ArrayInitialize(i_unused,0);

   FileWriteInteger(handle,file_version,LONG_VALUE);

   FileWriteString(handle,c_copyright,64);

   FileWriteString(handle,c_symbol,12);

   FileWriteInteger(handle,i_period,LONG_VALUE);

   FileWriteInteger(handle,i_digits,LONG_VALUE);

   FileWriteInteger(handle,0,LONG_VALUE);

   FileWriteInteger(handle,0,LONG_VALUE);

   FileWriteArray(handle,i_unused,0,13);

  }

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

int OnInit()

  {

   string FileName=_Symbol+(string)TimeFrame+".hst";

   if(HistoryHandle>0) FileClose(HistoryHandle);

   HistoryHandle=FileOpenHistory(FileName,FILE_BIN|FILE_READ|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_ANSI);

   if(HistoryHandle<0)

     {

      Alert("Error creating file ",FileName," - Error !",GetLastError());

      return(INIT_FAILED);

     }

   if(FileSize(HistoryHandle)==0) HstWriteHeader(HistoryHandle,TimeFrame); else Print("File found ",FileName," , record");

   FileSeek(HistoryHandle,0,SEEK_END);

   chart_id=0;

   InitChart();

   if(UseSystemTimer) EventSetTimer(1); else EventKillTimer();



#ifndef NoUseDll   

   if(IsDllsAllowed())

     {

      TickEmulation=true;

      MT4Message=RegisterWindowMessageW("MetaTrader4_Internal_Message");

      Print("-<C;OF8O B8:>2 @07@5H5=0");

     }

   else

     {

      TickEmulation=false;

      Print("0?@5B 2K7>20 DLL, M<C;OF8O B8:>2 >B<5=5=0");

     }

#endif



   return(INIT_SUCCEEDED);

  }

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

//|  Custom indicator deinitialization function                      |

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

void OnDeinit(const int reason)

  {

   if(HistoryHandle>0) FileClose(HistoryHandle);

   HistoryHandle=-1;

   EventKillTimer();

  }

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

//| Custom indicator iteration function                              |

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

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[])

  {

   if(HistoryHandle<0)

     {

      Comment("Indicator Stopped , error create history file!!!");

      if(UseSystemTimer) EventKillTimer();

      return(0);

     }

   if(!UseSystemTimer) SecondsChart();

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

   return(rates_total);

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

   RefreshRates();

   SecondsChart();

  }

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

//| Init func                                                        |

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

void InitChart()

  {

   double tickbid=SymbolInfoDouble(_Symbol,SYMBOL_BID);

   datetime tnow=TimeCurrent();

   InitBar(OHLC,tickbid,tnow);

   MqlDateTime t;

   TimeToStruct(tnow,t);

   t.sec=0;

   NewBarTime=StructToTime(t);

   while(tnow>NewBarTime) NewBarTime+=PeriodInSeconds;

   FPos=FileTell(HistoryHandle);

   FileWriteStruct(HistoryHandle,OHLC);

   FileFlush(HistoryHandle);

   if(OpenChartStart)

     {

      chart_id=CheckChart(_Symbol,TimeFrame,MT4HandleWindow);

      if(chart_id==0)

        {

         ChartOpen(_Symbol,TimeFrame);

         chart_id=CheckChart(_Symbol,TimeFrame,MT4HandleWindow);

        }

     }

   else PrintFormat("Open the chart [%s,M%d] , in the Menu  - File - Open Offline",_Symbol,TimeFrame);

  }

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

//|Main func                                                         |

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

void SecondsChart()

  {

   double tickbid=SymbolInfoDouble(_Symbol,SYMBOL_BID);

   datetime tnow = TimeCurrent();

   if(tnow>=NewBarTime)

     {

      if(tnow>NewBarTime)

        {

         while(tnow>=NewBarTime) NewBarTime+=PeriodInSeconds;

        }

      else NewBarTime+=PeriodInSeconds;

      FileWriteStruct(HistoryHandle,OHLC);

      FPos=FileTell(HistoryHandle);

      InitBar(OHLC,tickbid,tnow);

     }

   OHLC.high=fmax(OHLC.high,tickbid);

   OHLC.low=fmin(OHLC.low,tickbid);

   OHLC.close=tickbid;

   OHLC.tick_volume++;

   FileSeek(HistoryHandle,FPos,SEEK_SET);

   FileWriteStruct(HistoryHandle,OHLC);

   FileFlush(HistoryHandle);

   if(chart_id==0) chart_id=CheckChart(_Symbol,TimeFrame,MT4HandleWindow);

   else

     {

      ChartSetSymbolPeriod(chart_id,_Symbol,TimeFrame);

#ifndef NoUseDll

      if(TickEmulation) SendMessageW(MT4HandleWindow,MT4Message,2,1);

#endif

      ChartRedraw(chart_id);

     }

  }

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

//|                                                                  |

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

void InitBar(MqlRates &r,double price,datetime t)

  {

   r.open      = price;

   r.high      = price;

   r.low       = price;

   r.close     = price;

   r.tick_volume=1;

   r.time=t;

  }

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

//| ?>8A:  845=B8D8:0B>@0 3@0D8:0                                    |

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

long CheckChart(string sym,int period,int &wh)

  {

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

         wh=WindowHandle(sym,period);

         PrintFormat("0945=> >:=> 3@0D8:0: [%s,%d]",sym,period);

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