Work with WindowFirstVisibleBar, WindowBarsPerChart - automaticaly move graph - please help me

 

He everyone,

sorry my english. I have this problem. My idea what do I want do:

On first graph (ex. TF H1) I choose some bar and I want automatically move second graph (ex. TF M5). Second graph is moving that I would like seing time range H1 from first graph. I am choosing bar on first graph my own script with using function WindowTimeOnDropped(), this value (format datetime) is saving to global variable "global01". On second graph where is running my indicator (see attach code below) and it waits change value "global01". It works ok but (!!!) I have problem with identify moving second graph - function WindowFirstVisibleBar. This function always return the same value (before moving and after moving graph). Graph is moving, press key PgUp is functional.

I don´t know where is error...

This screen: listing function Alert() from my code

#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window

#import "user32.dll"
int GetParent(int hWnd);
int SendMessageA(int hWnd, int Msg, int wParam, int lParam);
int PostMessageA (int hWnd, int Msg, int wParam, int lParam);
#import

#define WM_MDIACTIVATE     0x222
#define WM_SCROLL          0x80F9
#define WM_COMMAND         0x0111
#define WM_KEYUP           0x0101
#define WM_KEYDOWN         0x0100
#define WM_CLOSE           0x0010
 
 
#define VK_PGUP            0x21
#define VK_PGDN            0x22
#define VK_HOME            0x24
#define VK_END             0x23
#define VK_DOWN            0x28
#define VK_PLUS            0xBB
#define VK_MINUS           0xBD

//extern string symbol = "GBPJPY"; 

int barsVisibleChart=0;
int barVisible=0;
int cnt=0;
int hwnd;
double _datetime=0;

int init()
{
  hwnd = WindowHandle(Symbol(), Period());  
  if (! GlobalVariableCheck( "global01")) GlobalVariableDel("global01");
  // return TRUE of global variable exist
  GlobalVariableSet( "global01", 0 );  // default global set to zero
}

int Window.activate(int hwnd) 
{
   int p = GetParent(hwnd);
   SendMessageA(GetParent(p), WM_MDIACTIVATE, p, 0);
}



void start() 
{
   if (_datetime!=GlobalVariableGet("global01") )
   {  cnt=0;
      _datetime=GlobalVariableGet("global01");
      
      barsVisibleChart=WindowBarsPerChart();
      barVisible=WindowFirstVisibleBar();
      Alert(  TimeToStr(Time[WindowFirstVisibleBar()], TIME_DATE|TIME_MINUTES), "    ",
              TimeToStr(Time[WindowFirstVisibleBar()]+barsVisibleChart*Period()*60, TIME_DATE|TIME_MINUTES)
             );  // bar visible of graph from -> to
      Window.activate(hwnd);
      PostMessageA(hwnd, WM_KEYDOWN, VK_PGUP, 0);
      PostMessageA(hwnd, WM_COMMAND, 33324  , 0);//Refresh Chart
      Sleep(400);
      Alert(  TimeToStr(Time[WindowFirstVisibleBar()], TIME_DATE|TIME_MINUTES), "    ",
              TimeToStr(Time[WindowFirstVisibleBar()]+barsVisibleChart*Period()*60, TIME_DATE|TIME_MINUTES)
             );  // bar visible of graph from -> to after sw press key PgUP
    }
      //Alert( TimeToStr(Time[WindowFirstVisibleBar()], TIME_DATE|TIME_MINUTES),"  ",WindowBarsPerChart() );
}
 
  1. It takes time for the chart to scroll, your 0.4 sec delay may be too small
  2. It takes time to get history, your 0.4 sec delay is way too small for that and the chart will NOT scroll until there IS history.
  3. Perhaps RefreshRates() and/or ObjectsRedraw() will update variables.
  4. Otherwise you will have to post and return and wait for a new tick. See posting a fake tick https://www.mql5.com/en/forum/128803/page2#373538 and perhaps post again.
  5. Concerning WindowBarsPerChart, Don't look into the future when you press the Chart Shift button.
    int     iVisible        =           WindowFirstVisibleBar(),
            iVisEnd         = MathMaxI( iVisible-WindowBarsPerChart(),0);// Shift
    Alert( TimeToStr(Time[iVisible]..., "   ", TimeToStr(Time[iVisEnd]...
    ////
    int     MathMaxI(int a, int b){
                            if(a>b) return(a);              return(b);             }
 
I don't know what you are up to, however why don't you just use Timeseries's iBarShift() ?
 
In relation to point 4. I think that is more than a simple tick, I think it also constitutes a RefreshRates(), if it is called many times a second your Broker will get upset . . .
 
onewithzachy:
I don't know what you are up to, however why don't you just use Timeseries's iBarShift() ?
He is trying to make one chart move automatically in response to one chart being moved manually . . so both charts stay synchronised in terms of what datetime they are showing . . . I think.
 
RaptorUK:
He is trying to make one chart move automatically in response to one chart being moved manually . . so both charts stay synchronised in terms of what datetime they are showing . . . I think.

I see ..., well if endy5 (the OP) keep Refresh Chart with WM_COMMAND on every tick, he better take a look at Task Manager to see if processor usage is not high.

BTW. that #property tell that this is a CI, last time I check, Sleep() will not work in CI. :(

 

Thank you all!

WHRoeder:

ad 1+2) I dont think if error is here

ad 3) I tried both function, but with the same result :-(

ad 4) thank you for this info

I tried re-write this indicator as script (code is below), script moving graph once right. Result: interesting ( at least for me): code is works full correctly !!! WindowFirstVisibleBar() return valid value.

Why? Where is different against indicator in code? I don´t see it... :-(

Thank you for help me.

#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//#property indicator_chart_window

#import "user32.dll"
int GetParent(int hWnd);
int SendMessageA(int hWnd, int Msg, int wParam, int lParam);
int PostMessageA (int hWnd, int Msg, int wParam, int lParam);
#import

#define WM_MDIACTIVATE     0x222
#define WM_SCROLL          0x80F9
#define WM_COMMAND         0x0111
#define WM_KEYUP           0x0101
#define WM_KEYDOWN         0x0100
#define WM_CLOSE           0x0010
 
 
#define VK_PGUP            0x21
#define VK_PGDN            0x22
#define VK_HOME            0x24
#define VK_END             0x23
#define VK_DOWN            0x28
#define VK_PLUS            0xBB
#define VK_MINUS           0xBD

//extern string symbol = "GBPJPY"; 

int barsVisibleChart=0;
int barVisible=0;
int cnt=0;
int hwnd;

int init()
{
  hwnd = WindowHandle(Symbol(), Period());  
}

int Window.activate(int hwnd) 
{
   int p = GetParent(hwnd);
   SendMessageA(GetParent(p), WM_MDIACTIVATE, p, 0);
}



void start() 
{
      barsVisibleChart=WindowBarsPerChart();
      barVisible=WindowFirstVisibleBar();
      Alert(  TimeToStr(Time[WindowFirstVisibleBar()], TIME_DATE|TIME_MINUTES), "    ",
              TimeToStr(Time[WindowFirstVisibleBar()]+barsVisibleChart*Period()*60, TIME_DATE|TIME_MINUTES)
             );  // bar visible of graph from -> to
      Window.activate(hwnd);
      PostMessageA(hwnd, WM_KEYDOWN, VK_PGUP, 0);
      PostMessageA(hwnd, WM_COMMAND, 33324  , 0);//Refresh Chart, uvedeno treba zde: https://forum.mql4.com/47179
      Alert(  TimeToStr(Time[WindowFirstVisibleBar()], TIME_DATE|TIME_MINUTES), "    ",
              TimeToStr(Time[WindowFirstVisibleBar()]+barsVisibleChart*Period()*60, TIME_DATE|TIME_MINUTES)
             );  // bar visible of graph from -> to after sw press key PgUP
      //Alert( TimeToStr(Time[WindowFirstVisibleBar()], TIME_DATE|TIME_MINUTES),"  ",WindowBarsPerChart() );
}
 
RaptorUK:
He is trying to make one chart move automatically in response to one chart being moved manually . . so both charts stay synchronised in terms of what datetime they are showing . . . I think.

Exactly, RaptorUK.
 

endy5:

RaptorUK:
He is trying to make one chart move automatically in response to one chart being moved manually . . so both charts stay synchronised in terms of what datetime they are showing . . . I think.

Exactly, RaptorUK.

iBarShift() then :D
 
onewithzachy:
iBarShift() then :D


Thank you for your effort help me. Maybe - I don´t understand your idea....

Function iBarShift() only return index bars. I need move automaticaly graph and I need know times press sw key PgUP. Therefore I need know which bar is visible as first in displacement graph. Do you understand me?

 
endy5:


Thank you for your effort help me. Maybe - I don´t understand your idea....

Function iBarShift() only return index bars. I need move automaticaly graph and I need know times press sw key PgUP. Therefore I need know which bar is visible as first in displacement graph. Do you understand me?

Yeah ... thanks to RaptorUK, I understood, and thank you endy5, coz you giving me some ideas. I let you know later :D