some questions about adding text by "A" on K line chart and time counting. - page 2

 
WHRoeder:
No it will not. Test it.


I have test as below:

A:

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

datetime  time_begin=D'2013.11.14 0:00';
bool is_first=true;

int init()
  {
if(is_first==true) {time_begin=TimeCurrent(); is_first=false;}
   return(0);
  }

int deinit()
  {
   return(0);
  }

int start()
  {
  Print("time_begin-1384412220= ",time_begin-1384412220);
  return(0);
  }

A's result of changing time frame is :

B:

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

extern datetime  time_begin=D'2013.11.14 0:00';
bool is_first=true;

int init()
  {
if(is_first==true) {time_begin=TimeCurrent(); is_first=false;}
   return(0);
  }

int deinit()
  {
   return(0);
  }

int start()
  {
  Print("time_begin-1384412220= ",time_begin-1384412220);
  return(0);
  }

B's result of changing time frame is:



So...........

 
vx0532:


Just now I have test the difference between A and B according to your points.

What you said is correct but not all, because I find that as below:

1. A has match my purpose very well, but B can't. when B has been attached to chart(suppose: extern datetime time_current=2013.11.14 0:00 in B), if I go to other pair or other time frame chart, "time_current" will be 2013.11.14 0:00 again. so i think there must be other difference between A and B.

2. So I think "extern" has only one effect which can allow us assign its variables before EA's attached.

time_current will be whatever the user sets it to until this line is executed in init() . . .

time_current = TimeCurrent();

. . . what is the point in allowing the user to set it when you then instantly change it ?

 
vx0532:


I have test as below:

Test it properly . . .

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

extern datetime  time_begin=D'2013.11.14 0:00';
bool is_first=true;

int init()
   {
   Print("init from extern, time_begin : ", TimeToStr(time_begin, TIME_DATE|TIME_MINUTES) );
   if(is_first==true) {time_begin=TimeCurrent(); is_first=false;}
   Print("init from TimeCurrent, time_begin : ", TimeToStr(time_begin, TIME_DATE|TIME_MINUTES) );
   return(0);
   }

int deinit()
  {
   return(0);
  }

int start()
  {
  Print("time_begin-1384412220= ",time_begin-1384412220);
  return(0);
  }
 
RaptorUK:

Test it properly . . .


Ok, I have copied your code to test and get the result as below:

1. This EA is loaded in M5 chart, time_begin is 2013.11.14 00:00 and then changed to 2013.11.15 01.21;

2. Chart is changed to M15 from M5, time_begin is 2013.11.14 00:00 again, so time_begin has changed after initializing.

3. If there is no "extern" before "datetime time_begin", time_begin will be never changed when chart is changed to M15 from M5.

 
vx0532: 3. If there is no "extern" before "datetime time_begin", time_begin will be never changed when chart is changed to M15 from M5.
Based on the output I agree. Changing timeframes resets externals but not globals.
 
WHRoeder:
Based on the output I agree. Changing timeframes resets externals but not globals.


Just to be clear, Changing timeframes in an EA resets externals but not globalscope/statics.

But in an indicator,Changing timeframes resets externals, globalscope and statics.

 
GumRai: indicator,Changing timeframes resets externals, globalscope and statics.
extern datetime  time_begin=D'2013.11.14 0:00';
bool is_first=true;
int init(){
   Print("init from extern, time_begin : ", TimeToStr(time_begin, TIME_DATE|TIME_MINUTES) );
   if(is_first==true) {time_begin=TimeCurrent(); is_first=false;}
   Print("init from TimeCurrent, time_begin : ", TimeToStr(time_begin, TIME_DATE|TIME_MINUTES) );
If globals and statics are not reset the if(is_first==true) will be false the second time and you should see
init from TimeCurrent, time_begin : X
init from extern, time_begin : X
Only the first time will you see
init from TimeCurrent, time_begin : Y
init from extern, time_beg : X
Which is exactly what you posted Only externals are reset.
 
WHRoeder:
If globals and statics are not reset the if(is_first==true) will be false the second time and you should see
init from TimeCurrent, time_begin : X
init from extern, time_begin : X
Only the first time will you see
init from TimeCurrent, time_begin : Y
init from extern, time_beg : X
Which is exactly what you posted Only externals are reset.


I didn't post that, so I don't understand your response, you have quoted my post and added code to it.

Are you saying that only externals are reset when changing tf in both EAs and indicators?

 

About below A and B, it is very clear until now : the two variables, 'isFirst' and 'time_current', can never been changed when shift time frame in A, but done in B; another difference is that we can't set the two variables' value when EA is being attached in A, but we can do it in B.

Then another question is that: how shall we do if we want to set variables' value when EA is being attached like B and the variables can never been changed when shift time frame like in A? "static" can resolve this problem?

A:

bool isFirst = true;
datetime  time_current;
int init(){
   if(isFirst) 
   { isFirst = false; 
    time_current=TimeCurrent();
    }

B:

extern bool isFirst = true;
extern datetime  time_current;
int init(){
   if(isFirst) 
   { isFirst = false; 
    time_current=TimeCurrent();
    }


 
GumRai:
I didn't post that, so I don't understand your response, you have quoted my post and added code to it.
Are you saying that only externals are reset when changing tf in both EAs and indicators?
  1. RaptorUK's added code that you were supposed to try. You hadn't. vx0532 posted the result for you. Did you bother to read and understand either post?
  2. What part of "Only externals are reset" was unclear?
Reason: