uninit reason 0 error

 
Hi, I'm trying to initialize my script but it is failed with an error message that it was uninitialized with a reason 0. I looked into the documentation and find the description doesn't tell me anything about the problem. Someone know how to fix this?
 
Luandre Ezra: I'm trying to initialize my script but it is failed with an error message that it was uninitialized with a reason 0. I looked into the documentation and find the description doesn't tell me anything about the problem.
  1. We have no idea whether you mean code or an actual script.
  2. Perhaps you should read the manual.
    Constant Value Description
    REASON_PROGRAM 0 Expert Advisor terminated its operation by calling the ExpertRemove() function
              Uninitialization Reason Codes - Named Constants - Constants, Enumerations and Structures - MQL4 Reference
  3. What part of "called … function" is unclear to you?
 
Luandre Ezra:
Hi, I'm trying to initialize my script but it is failed with an error message that it was uninitialized with a reason 0. I looked into the documentation and find the description doesn't tell me anything about the problem. Someone know how to fix this?

You are reading something wrong.

A script cannot un-initialize before it is initialized.

A script is loaded, then initialized, executed and then removed from the chart ( with "Uninit reason 0").

 
William Roeder:
  1. We have no idea whether you mean code or an actual script.
  2. Perhaps you should read the manual.
  3. What part of "called … function" is unclear to you?

A Script, I'm trying to run in a script before run it as an EA. I have read the documentation but it doesn't tell me anything why that's happened, that's the part where I'm unclear. 

 
Keith Watford:

You are reading something wrong.

A script cannot un-initialize before it is initialized.

A script is loaded, then initialized, executed and then removed from the chart ( with "Uninit reason 0").

0       08:20:21.469    Script Test USDCHF,H1: loaded successfully
0       08:20:21.485    Test USDCHF,H1: initialized
0       08:20:21.486    Test USDCHF,H1: Alert: 
0       08:20:21.486    Test USDCHF,H1: uninit reason 0
0       08:20:21.525    Script Test USDCHF,H1: removed

That's the log.

void OnStart()
  {
   Alert("");
   double bbUpperBand2 = iBands(NULL,0,20,4,0,PRICE_CLOSE,MODE_UPPER,0);
   double bbUpperBand = iBands(NULL,0,20,1,0,PRICE_CLOSE,MODE_UPPER,0);
   double bbMidBand = iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_MAIN,0);
   double bbLowerBand = iBands(NULL,0,20,1,0,PRICE_CLOSE,MODE_LOWER,0);
   double bbLowerBand2 = iBands(NULL,0,20,4,0,PRICE_CLOSE,MODE_LOWER,0);

     
   //Alert(MarketInfo(NULL, MODE_STOPLEVEL)); //check minimum stop level

   if(Ask < bbLowerBand){//Buying
      Alert("Price is below bbLowerBand, sending buy order");
      double stopLossPrice = bbLowerBand2;
      double takeProfitPrice = bbMidBand;
      Alert("Entry price = " + Ask);
      Alert("Stop loss price = " + NormalizeDouble(stopLossPrice,Digits));
      Alert("Take profit price = " + NormalizeDouble(takeProfitPrice,Digits));
      
      //syntax for calculate optimal lot size
      double lotSize = OptimalLotSize(0.02,Ask,stopLossPrice);
      Alert("Optimal Lot Size: " + lotSize);
      

      //syntax for buy order
      int orderID = OrderSend(NULL,OP_BUYLIMIT,lotSize,Ask,1,stopLossPrice,takeProfitPrice);
      //syntax to check if EA working properly or not and give message to user
      if (orderID < 0){
         Alert("Order Rejected, error code: " + GetLastError());
      }else{
         Alert("Order Accepted");
      }

   }else if (Bid > bbUpperBand){//Shorting
      Alert("Price is above bbUpperBand, sending short order");
      double stopLossPrice = bbUpperBand2;
      double takeProfitPrice = bbMidBand;
      Alert("Entry price = " + Bid);
      Alert("Stop loss price = " + NormalizeDouble(stopLossPrice,Digits));
      Alert("Take profit price = " + NormalizeDouble(takeProfitPrice,Digits));

      double lotSize = OptimalLotSize(0.02,Bid,stopLossPrice);
      Alert("Optimal Lot Size: " + lotSize);
      

      int orderID = OrderSend(NULL,OP_SELLLIMIT,lotSize,Bid,1,stopLossPrice,takeProfitPrice);   
      if (orderID < 0){
         Alert("Order Rejected, error code: " + GetLastError());
      }else{
         Alert("Order Accepted");
      }

   }
   
  }

That's my code. The script only run the first alert that has no text. and the rest is not initialized at all.

 
Ok guys, I already find the culprit. The problem is I'm using NormalizedDouble function in the alert instead in the initialized variable section.
 
Luandre Ezra:

That's my code. The script only run the first alert that has no text. and the rest is not initialized at all.

Please post code that compiles.

 double lotSize = OptimalLotSize(0.02,Ask,stopLossPrice);

You have 2 conditions

if(Ask < bbLowerBand)
{}

and

else if (Bid > bbUpperBand)
{}

If neither is true, you will get nothing else but the first alert executed.

 
okay now I really now what's wrong. It's not the code that is not working. The broker set a minimum stop loss of 40 pips. When I'm running the code for short time frame such as H1 below, the condition (stop loss to take profit less than 40 pips) cannot be meet  that's why the system rejected the script. Thanks for the help @Keith Watford
 
Luandre Ezra:
okay now I really now what's wrong. It's not the code that is not working. The broker set a minimum stop loss of 40 pips. When I'm running the code for short time frame such as H1 below, the condition (stop loss to take profit less than 40 pips) cannot be meet  that's why the system rejected the script. Thanks for the help @Keith Watford

The system did not reject the script.

Conditions were not right for the code to be executed.

 
Luandre Ezra: That's the log.

Start began. It Alerted. It returned and script exited (0);

Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?

 
Luandre Ezra:
okay now I really now what's wrong. It's not the code that is not working. The broker set a minimum stop loss of 40 pips. When I'm running the code for short time frame such as H1 below, the condition (stop loss to take profit less than 40 pips) cannot be meet  that's why the system rejected the script. Thanks for the help @Keith Watford
I Know that Script , I had same problem, what you have to do is increase the Slippage in the Include.mqh that your script should be calling and that's it!!
Reason: