Download MetaTrader 5

SecondCharts little doubt

To add comments, please log in or register
rofranco
68
rofranco  

Hi

I am trying to make Second-to-Second Chart.

That´s all fine less that the candle formed intro each second is drawn but on the final of the second it isn´t recorded, only the close price remains.

What is wrong? Could someone give a light, please?

//+------------------------------------------------------------------+
#include <WinUser32.mqh>
#include <stdlib.mqh>
//+------------------------------------------------------------------+
#import "user32.dll"
        int RegisterWindowMessageA(string lpString); 
#import
//+------------------------------------------------------------------+
extern int TimeFrame = 2;      // What time frame to use for the offline tick chart
bool EmulateOnLineChart = true;
bool StrangeSymbolName = false;
//+------------------------------------------------------------------+
int HstHandle = -1, LastFPos = 0, MT4InternalMsg = 0;
string SymbolName;
int volumetime=0;
int seconds=100;

//+------------------------------------------------------------------+
//| Init                                                             |
//+------------------------------------------------------------------+
int OnInit()
{
EventSetMillisecondTimer(10);
        return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| DeInit                                                           |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
        EventKillTimer();

        if(HstHandle >= 0) {
                FileClose(HstHandle);
                HstHandle = -1;
        }
        Comment("");
        return(0);
}
//+------------------------------------------------------------------+
void UpdateChartWindow() {
        static int hwnd = 0;
 
        if(hwnd == 0) {
                hwnd = WindowHandle(SymbolName, TimeFrame);
                if(hwnd != 0) Print("Chart window detected");
        }

        if(EmulateOnLineChart && MT4InternalMsg == 0) 
                MT4InternalMsg = RegisterWindowMessageA("MetaTrader4_Internal_Message");

        if(hwnd != 0) if(PostMessageA(hwnd, WM_COMMAND, 0x822c, 0) == 0) hwnd = 0;
        if(hwnd != 0 && MT4InternalMsg != 0) PostMessageA(hwnd, MT4InternalMsg, 2, 1);

        return;
}
//+------------------------------------------------------------------+
void OnTimer()
  {
        static double CurVolume, HIGH,LOW,OPEN,CLOSE;
        static datetime PrevTime;
   int n=0;     
        //+------------------------------------------------------------------+
        // This is only executed ones, then the first tick arives.
        if(HstHandle < 0) {
                // Init

                if(StrangeSymbolName) SymbolName = StringSubstr(Symbol(), 0, 6);
                else SymbolName = Symbol();
                OPEN = Close[0];
                LOW = Close[0];
                HIGH = Close[0];
                CLOSE = Close[0];
                PrevTime = Time[Bars-1];
        
                // create / open hst file               
      HstHandle = FileOpenHistory(SymbolName + TimeFrame + ".hst", FILE_BIN|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ);
        
                // write hst file header
                int HstUnused[13];
                FileWriteInteger(HstHandle, 400, LONG_VALUE);                   // Version
                FileWriteString(HstHandle, "", 64);                                     // Copyright
                FileWriteString(HstHandle, SymbolName, 12);                     // Symbol
                FileWriteInteger(HstHandle, TimeFrame, LONG_VALUE);     // Period
                FileWriteInteger(HstHandle, Digits, LONG_VALUE);                // Digits
                FileWriteInteger(HstHandle, 0, LONG_VALUE);                     // Time Sign
                FileWriteInteger(HstHandle, 0, LONG_VALUE);                     // Last Sync
                FileWriteArray(HstHandle, HstUnused, 0, 13);                    // Unused
                //
        
                LastFPos = FileTell(HstHandle);   // Remember Last pos in file
                //
                        
                Comment("SecondCharts: Open Offline ", SymbolName, ",M", TimeFrame, " to view chart");
                
                FileWriteInteger(HstHandle, PrevTime, LONG_VALUE);              // Time
                FileWriteDouble(HstHandle, OPEN, DOUBLE_VALUE);         // Close
                FileWriteDouble(HstHandle, LOW, DOUBLE_VALUE);
                FileWriteDouble(HstHandle, HIGH, DOUBLE_VALUE);
                FileWriteDouble(HstHandle, CLOSE, DOUBLE_VALUE);
                FileWriteDouble(HstHandle, volumetime, DOUBLE_VALUE);           // Volume                               
                FileFlush(HstHandle);
            
                UpdateChartWindow();
                
                return(0);
                // End historical data / Init           
        }               
        //----------------------------------------------------------------------------
        // HstHandle not < 0 so we always enter here after history done
        // Begin live data feed
   
        FileSeek(HstHandle, LastFPos, SEEK_SET);

        //-------------------------------------------------------------------------                                     
      if(TimeSeconds(TimeCurrent())!=seconds)
      {
                OPEN = Close[0];
                CLOSE = Close[0];
                HIGH = Close[0];
                LOW = Close[0];
      PrevTime = TimeCurrent();

      volumetime++;                                                     
                FileWriteInteger(HstHandle, PrevTime, LONG_VALUE);
                FileWriteDouble(HstHandle, OPEN, DOUBLE_VALUE);
                FileWriteDouble(HstHandle, LOW, DOUBLE_VALUE);
                FileWriteDouble(HstHandle, HIGH, DOUBLE_VALUE);
                FileWriteDouble(HstHandle, CLOSE, DOUBLE_VALUE);
                FileWriteDouble(HstHandle, volumetime, DOUBLE_VALUE);
        FileFlush(HstHandle);
                LastFPos = FileTell(HstHandle);   // Remeber Last pos in file                                                                                   
                                        
                UpdateChartWindow();                                                    
      seconds=TimeSeconds(TimeCurrent());
           }

        else 
        {
                CLOSE = Close[0];
                HIGH = MathMax(HIGH,Close[0]);
                LOW = MathMin(LOW,Close[0]);
        
                FileWriteInteger(HstHandle, PrevTime, LONG_VALUE);
                FileWriteDouble(HstHandle, OPEN, DOUBLE_VALUE);
                FileWriteDouble(HstHandle, LOW, DOUBLE_VALUE);
                FileWriteDouble(HstHandle, HIGH, DOUBLE_VALUE);
                FileWriteDouble(HstHandle, CLOSE, DOUBLE_VALUE);
                FileWriteDouble(HstHandle, volumetime, DOUBLE_VALUE);
        FileFlush(HstHandle);
                                                
                UpdateChartWindow();                                                    
        }

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

Files:
rofranco
68
rofranco  

SOLVED!

Files:
To add comments, please log in or register