Code not working...

 

Whenever I backtest this script, It wont open any trades...why is this happening???


#include <Trade\Trade.mqh>;
CTrade trade;

int input length = 20;
float input stdv = 2.0;
double input tpPoints = 10;
double input slPoints = 40;


int boll = iBands(_Symbol,PERIOD_CURRENT,length,1,stdv,PRICE_CLOSE);
int positions = PositionsTotal();
bool noopentrades;
//----------------------------------------
int OnInit(){ 
   return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason){}
//----------------------------------------



void OnTick(){

double bbupper[], bblower[], bbmiddle[];

CopyBuffer(boll,BASE_LINE,1,2,bbmiddle);
CopyBuffer(boll,UPPER_BAND,1,2,bbupper);
CopyBuffer(boll,LOWER_BAND,1,2,bblower);

if(positions == 0)
  {
   noopentrades = true;
  }

if(PRICE_CLOSE >= bbupper[1] && noopentrades){

   double entry = SymbolInfoDouble(_Symbol,SYMBOL_BID);
    entry = NormalizeDouble(entry,_Digits);
    
    double tp = entry - tpPoints * _Point;
    tp = NormalizeDouble(tp,_Digits);
    
    double sl = entry + slPoints * _Point;
    sl = NormalizeDouble(sl,_Digits);

   trade.Buy(1,_Symbol,0,sl,tp);
  }

if(PRICE_CLOSE <= bblower[1] && noopentrades){

   double entry = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
    entry = NormalizeDouble(entry,_Digits);
    
    double tp = entry + tpPoints * _Point;
    tp = NormalizeDouble(tp,_Digits);
    
    double sl = entry - slPoints * _Point;
    sl = NormalizeDouble(sl,_Digits);
    
   trade.Sell(1,_Symbol,0,sl,tp);
  }

}
 
int boll = iBands(_Symbol,PERIOD_CURRENT,length,1,stdv,PRICE_CLOSE);
int positions = PositionsTotal();

Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
              external static variable - MQL4 programming forum #2 (2013)

Reason: