Download MetaTrader 5

zero divide error on demo testing but not ST?

To add comments, please log in or register
Dominic Gilbert
1639
Dominic Gilbert  
//+------------------------------------------------------------------+
//| Order Entry function - Buy or Sell (pending orders)              |
//+------------------------------------------------------------------+

void OrderEntry(int direction)
{
   
   //Stop calculations  -------------------
   double ATR2 = iATR(NULL,low,ATR_Period,1);
   double MA2 = iMA(NULL,low,MA_Entry_Period,0,1,0,1);
   //Market Information -------------------  
   Lot_Step = MarketInfo(Symbol(), MODE_LOTSTEP); 
   ts = MarketInfo(Symbol(), MODE_TICKSIZE); 
   tv = MarketInfo(Symbol(), MODE_TICKVALUE);
   minlot = MarketInfo(Symbol(), MODE_MINLOT);
   freezelevel = MarketInfo(Symbol(), MODE_FREEZELEVEL);   
   //Risk applied per trade --------------
   risk_amount = AccountBalance( )*RiskPercent/100;
   risk_amount2 = AccountBalance( )*RiskPercent2/100;
   risk_amount3 = AccountBalance( )*RiskPercent3/100;
   risk_amount4 = AccountBalance( )*RiskPercent4/100;
         
//+-------------------------------------------------------------------------------------+
//| Order Buy Function                                                                  |
//+-------------------------------------------------------------------------------------+   

if(direction==0)
{//--Buy--// 
      
      //Get Buy Stop price
      double BuyStopPriceMath = MA2 - ATR2, 
             BuyStopPrice = NormalizeDouble(BuyStopPriceMath,Digits);
             Print("BuyStopPriceMath == ", MA," - ", ATR, " = ", BuyStopPrice);  
         
      //Get Take Profit price for FIRST TARGET
      if( Stored_BuyPrice > BuyStopPrice + Point || BuyStopPrice > Stored_BuyPrice + Point ) 
            {
             pips_to_bsl = Stored_BuyPrice - BuyStopPrice;
             Print("pips_to_bsl == ", Stored_BuyPrice," - ", BuyStopPrice, " = ", pips_to_bsl); 
            }
 
      double buy_tp_price1 = (pips_to_bsl*First_Target_Ratio) + Stored_BuyPrice, 
             buy_takeprofit_price1 = NormalizeDouble(buy_tp_price1,Digits), 
             btp1 = buy_takeprofit_price1;
             Print("buy_tp_price1 == ", pips_to_bsl," + ", Stored_BuyPrice, " = ", buy_takeprofit_price1); 
      
      //Get Take Profit price for SECOND TARGET
      double buy_tp_price2 =(pips_to_bsl*Second_Target_Ratio)+Stored_BuyPrice, 
             buy_takeprofit_price2 = NormalizeDouble(buy_tp_price2, Digits), 
             btp2 = buy_takeprofit_price2;
      
      //Get Take Profit price for THIRD TARGET
      double buy_tp_price3 =(pips_to_bsl*Third_Target_Ratio)+Stored_BuyPrice, 
             buy_takeprofit_price3 = NormalizeDouble(buy_tp_price3, Digits), 
             btp3 = buy_takeprofit_price3;   
      
      //Get Take Profit price for FOURTH TARGET
      double buy_tp_price4 =(pips_to_bsl*Fourth_Target_Ratio)+Stored_BuyPrice, 
             buy_takeprofit_price4 = NormalizeDouble(buy_tp_price4, Digits); 
      
      double btp4 = 0;
             if( buy_takeprofit_price4 > 0 ){btp4 = buy_takeprofit_price4;}
             if( buy_takeprofit_price4 < 0 ){btp4 = 0;}
             
             
      //What is loss for 1 lot?
      double loss_for_1_lot = pips_to_bsl / ts * tv ;
             Print("loss_for_1_lot == ", pips_to_bsl," / ", ts, " * ", tv, " = ", loss_for_1_lot);
      
      //Get Lot Size calculations for all FOUR Trades                   
      double LotSize_Buy  = MathFloor( risk_amount / loss_for_1_lot/ Lot_Step) * Lot_Step, 
             LotSize_Buy2 = MathFloor( risk_amount2 / loss_for_1_lot/ Lot_Step) * Lot_Step,
             LotSize_Buy3 = MathFloor( risk_amount3 / loss_for_1_lot/ Lot_Step) * Lot_Step, 
             LotSize_Buy4 = MathFloor( risk_amount4 / loss_for_1_lot/ Lot_Step) * Lot_Step;
             
             Print("LotSize_Buy == ", risk_amount," / ", loss_for_1_lot, " / ", Lot_Step, " ) * ", Lot_Step, " = ", LotSize_Buy);
             Print("LotSize_Buy2 == ", risk_amount2," / ", loss_for_1_lot, " / ", Lot_Step, " ) * ", Lot_Step, " = ", LotSize_Buy);
             Print("LotSize_Buy3 == ", risk_amount3," / ", loss_for_1_lot, " / ", Lot_Step, " ) * ", Lot_Step, " = ", LotSize_Buy);
             Print("LotSize_Buy4 == ", risk_amount4," / ", loss_for_1_lot, " / ", Lot_Step, " ) * ", Lot_Step, " = ", LotSize_Buy);
    
    RefreshRates();
//+------------------------------------------------------------------+
//---Buy---//
    if( minlot > LotSize_Buy ){LotSize_Buy = minlot;} 
    if( minlot > LotSize_Buy2 ){LotSize_Buy2 = minlot;} 
    if( minlot > LotSize_Buy3 ){LotSize_Buy3 = minlot;} 
    if( minlot > LotSize_Buy4 ){LotSize_Buy4 = minlot;}    
     
     
     if( OpenOrdersThisPair(Symbol()) == 0 && LotSize_Buy >= minlot )
         { 
         BuyTicketOrder1 = OrderSend(Symbol(),OP_BUY,LotSize_Buy,Ask ,3,
                                     0,0,NULL,MagicNumber1,0,Green);RefreshRates();
                                    
                                    if( OrderSelect(BuyTicketOrder1,SELECT_BY_TICKET)==true){
                                     if(OrderSymbol() == Symbol() && OrderType() == OP_BUY )
                                       bool ModifyBuy1 = OrderModify(BuyTicketOrder1,OrderOpenPrice(),BuyStopPrice,btp1,0,clrNONE);}

...
It says it found it the zero divide in the "LotSize_Buy" variable? Thing is, I never see it within ST? Just popped up now on the first demo trade....
Carl Schreiber
7224
Carl Schreiber  

Come on! Check yourself: loss_for_1_lot and Lot_Step!!

Dominic Gilbert
1639
Dominic Gilbert  
I don't see what's wrong with it? 

      double loss_for_1_lot = pips_to_bsl / ts * tv ;
2014.10
.14 09:20:04.797 2014.08.20 18:06  TF - v2.7.1 USDCAD,H1: loss_for_1_lot == 0.003310000000000146 / 1e-005.0 * 0.9122922254456551 = 301.9687266225252
LotSize_Buy4 = MathFloor( risk_amount4 / loss_for_1_lot/ Lot_Step) * Lot_Step;

2014.10.14 09:20:04.797 2014.08.20 18:06  TF - v2.7.1 USDCAD,H1: LotSize_Buy == 39.08068500000001 / 301.9687266225252 / 0.1 ) * 0.1 = 0.1
Dominic Gilbert
1639
Dominic Gilbert  
Forgive me, maths is not my strongest point. But I honestly cannot see how this formula doesn't work considering I am updating my EA which I have been using live for over a year now with no zero divide errors until now. Now that I place x4 trades instead of partially closing one... 
Dominic Gilbert
1639
Dominic Gilbert  
Is all I can think of? 

double loss_for_1_lot = pips_to_bsl / ( ts * tv ); // brackets?
Carl Schreiber
7224
Carl Schreiber  

Just place this before the line:

Print("LotSize_Buy == ", risk_amount," / ", loss_for_1_lot, " / ", Lot_Step, " ) * ", Lot_Step);

Then you should know - no?


Dominic Gilbert
1639
Dominic Gilbert  

Just checked FXCM MarketInfo and USDCAD Tick_Size = "0.0" where as other FX pairs return an actual ticksize... so that is my problem?

What the heck do I do about this?

2014.10.14 05:26:02.415 Account Information USDCAD,H1:  Tick Size is: 0 -- Tick Value is: 0.5568

 

Could I just use "Point" instead of ts like so:

 

      //What is loss for 1 lot?
      double loss_for_1_lot = pips_to_bsl / Point * tv ;
             Print("loss_for_1_lot == ", pips_to_bsl," / ", Point, " * ", tv, " = ", loss_for_1_lot);
Dominic Gilbert
1639
Dominic Gilbert  
Where as GBPUSD returns:

2014.10.14 05:11:13.359 TF - v2.7.1 - GBPUSD GBPUSD,H1:  Tick Size is: 1e-005.0 -- Tick Value is: 0.6262995716110931 
Carl Schreiber
7224
Carl Schreiber  

See you found out yourself!!

You're getting better ;)

"Just checked FXCM MarketInfo and USDCAD Tick_Size = "0.0""

Is USDCAD visual (available by your broker) in the MarketWatch?

Dominic Gilbert
1639
Dominic Gilbert  

What do you mean?

 "Is USDCAD visual (available by your broker) in the MarketWatch?" 

I have all the relevant markets open within Market Watch yeah. I've even gone show all but it doesn't change anything about the tick value on USDCAD. This is soooo strange!

Carl Schreiber
7224
Carl Schreiber  
Try SymbolInfoDouble("USDCAD",SYMBOL_TRADE_TICK_SIZE) and ask your broker may be the server is not correct?
123
To add comments, please log in or register