SecondCharts little doubt

 

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:
 

SOLVED!

Files:
 

Hi Rodrigo S. Franco

How can i have the chart for 5 seconds or 10 seconds?

Or added a coefficient? Please help about it.

thanks a lot

 

hi

i need this too. please help about it.

 

hi

how can i make it for history candle? and then live ?

 

Hi, Expert Advisor attached, you can set how numbers of seconds you want. Below, in the picture, Candlesticks of 6´´.

Greetings!!





Code:

//+------------------------------------------------------------------+
//|                                                 Second Chart.mq4 |
//|                   Copyright 2005-2024, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2024, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"

#include <WinUser32.mqh>
#include <stdlib.mqh>
#import "user32.dll"
int RegisterWindowMessageA(string lpString); 
#import

extern int SecondsBar=1;
extern int TimeFrame = 2;

bool EmulateOnLineChart = true;
bool StrangeSymbolName = false;
int HstHandle = -1, LastFPos = 0, MT4InternalMsg = 0;
string SymbolName;
int volumetime=iVolume(NULL,PERIOD_MN1,0);
int VolumeAnt=volumetime;
int inic=0;
int newbar=0;
int time=0;
//+------------------------------------------------------------------+
//| Init                                                             |
//+------------------------------------------------------------------+
int OnInit()
{EventSetMillisecondTimer(1);
        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 HIGH,LOW,OPEN,CLOSE;
        static datetime PrevTime;

        if(HstHandle < 0) {

                if(StrangeSymbolName) SymbolName = StringSubstr(Symbol(), 0, 6);
                else SymbolName = Symbol();
                OPEN = Close[0];
                CLOSE = Close[0];
                HIGH = Close[0];
                LOW = Close[0];
      PrevTime = TimeCurrent();
      volumetime=iVolume(NULL,PERIOD_MN1,0);
      VolumeAnt=volumetime;
        
      HstHandle = FileOpenHistory(SymbolName + TimeFrame + ".hst", FILE_BIN|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ);
        
                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("Second Chart: 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, 1, DOUBLE_VALUE);            // Volume                               
                FileFlush(HstHandle);
            
                UpdateChartWindow();
                
                return(0);
        }               
   
        FileSeek(HstHandle, LastFPos, SEEK_SET);

      if(inic==0)
      {
      PrevTime = TimeCurrent();
      double CloseAnt=Close[0];
                HIGH=Close[0];
                LOW=Close[0];
                OPEN=Close[0];
                CLOSE=Close[0];
      volumetime=iVolume(NULL,PERIOD_MN1,0);
      VolumeAnt=volumetime;
      volumetime=VolumeAnt-iVolume(NULL,PERIOD_MN1,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, 1, DOUBLE_VALUE);
        FileFlush(HstHandle);
                LastFPos = FileTell(HstHandle);
                UpdateChartWindow();                                                    
      inic=1;
      }

if(TimeCurrent()>=time+SecondsBar)
{                               
time=TimeCurrent();

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

      if(iVolume(NULL,PERIOD_MN1,0)!=VolumeAnt)
                {
                volumetime=iVolume(NULL,PERIOD_MN1,0)-VolumeAnt;
                OPEN=CLOSE;
      PrevTime = TimeCurrent();
                HIGH=MathMax(Close[0],OPEN);
                LOW=MathMin(Close[0],OPEN);
                CLOSE=Close[0];
                VolumeAnt=iVolume(NULL,PERIOD_MN1,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();                                                    
      }
      else
      {
                volumetime=0;
                OPEN=CLOSE;
      PrevTime = TimeCurrent();
                HIGH=Close[0];
                LOW=Close[0];
                CLOSE=Close[0];
                VolumeAnt=iVolume(NULL,PERIOD_MN1,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();                                                    
      }
      }   
      else if(iVolume(NULL,PERIOD_MN1,0)!=VolumeAnt)
      {      
                CLOSE = Close[0];
                HIGH = MathMax(HIGH,Close[0]);
                LOW = MathMin(LOW,Close[0]);
                volumetime=volumetime+iVolume(NULL,PERIOD_MN1,0)-VolumeAnt;
                VolumeAnt=iVolume(NULL,PERIOD_MN1,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);
}
//+------------------------------------------------------------------+
// F i n a n c e W a v e . c o m  ✓
Files: