set variables in init function of indicator

 

Hi,

if I use the following indicator with USDCHF, I get different results, depending on whether the line marked with //! is uncommented or not.

Why?

#property indicator_chart_window

extern double margin = 0;
extern int    shift  = 0;

string symbol;
int timeframe;

double trade[];
double loc_margin;


int init() {

  if (symbol == "USDCHF") {

    loc_margin = margin / 100000;

  }

  else if (symbol == "USDJPY") {

    loc_margin = margin / 1000;

  }

  // loc_margin = margin / 100000; //!

  SetIndexBuffer(0, trade);
  return(0);


}


int start() {

  if (iAC(symbol, timeframe, shift) > 0 + loc_margin) {
    trade[0] = 1;
  }

  else if (iAC(symbol, timeframe, shift) < 0 - loc_margin) {
    trade[0] = -1;
  }

  else {
    trade[0] = 0;
  }

  return(0);
}
 

Assuming that you set a value for the extern "margin"

string symbol;

  if (symbol == "USDCHF") {

    loc_margin = margin / 100000;

  }

  else if (symbol == "USDJPY") {

    loc_margin = margin / 1000;

  }

loc_margin will not be assigned a value because symbol has not been assigned a value, so the only way that loc_margin can be given a value is by un-commenting-

 // loc_margin = margin / 100000; //!
 
Thanks for pointing that out.
Reason: