SYMBOL_TRADE_TICK_VALUE returns 0.0 on first run, then the correct value on next run

 

I'm using a script I found here to calculate volume for my trades and it works great on the second run, but always fails on the first run.

        double lots_maximum = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MAX);
         double lots_minimum = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MIN);
         double volume_step = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_STEP);
         double tick_size = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_SIZE);
         double tick_value = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_VALUE);

         double lots=0.0;
         int normalization_factor = 0;
           
   if(volume_step == 0.01) { normalization_factor = 2; }
   if(volume_step == 0.1)  { normalization_factor = 1; }
   

         // Get trade basic info
         double available_capital = fmin( fmin( AccountInfoDouble(ACCOUNT_EQUITY), AccountInfoDouble(ACCOUNT_BALANCE)), AccountInfoDouble(ACCOUNT_MARGIN_FREE));
         double MAX_LOTS=NormalizeDouble(available_capital/MaxLotsDivider,2);
         double amount_to_risk = available_capital * percentage_to_lose / 100;
         double sl_distance = MathAbs(entry_price - stop_loss_price); // Get the Abs since it might be a short (EP < SL)

             
         // Calculate steps and lots
         double money_step = sl_distance / tick_size * tick_value * volume_step;
         
   Print("tick_size : "+tick_size+" tick_value: " +tick_value+" volume_step: "+volume_step+" SL Dist: " +sl_distance+" money step: " +money_step);
    
         
         lots = NormalizeDouble(round(amount_to_risk / money_step) * volume_step,normalization_factor);

On the first run of this script after I open MT5, the Print statement shows this:

FN_BUY_STOP (XAUUSD.a,H4)    tick_size : 0.01 tick_value: 0.0 volume_step: 0.01 SL Dist: 8.769999999999527 money step: 0.0
Which returns an invalid number of Lots

But the second time I run it it shows the actual values.

FN_BUY_STOP (XAUUSD.a,H4)    tick_size : 0.01 tick_value: 1.5056839569374387 volume_step: 0.01 SL Dist: 8.769999999999527 money step: 13.204848302340627

I can't work out what's wrong with this code.

 
This is absolutely normal. The advisor must handle SymbolInfoDouble errors and not trade if SymbolInfoDouble fails. In the case of a script, just run it again.