Download MetaTrader 5

set variables in init function of indicator

To add comments, please log in or register
Your conversation partner speaks foreign language? Automatic translation will help you!
tarandus
32
tarandus 2016.10.09 14:32 

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);
}
Keith Watford
Moderator
9644
Keith Watford 2016.10.09 21:39  

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; //!
tarandus
32
tarandus 2016.10.10 08:25  
Thanks for pointing that out.
To add comments, please log in or register