why won't this code work? - page 2

 
Well running that in the strategy tester gives me a zero_divide error and no trades at all.
 

If you call this function

//AVERAGE PRICE STRING:
double AveragePrice(string OrdType)
{
   double AveragePrice=0;
   double Count=0;
   int trade;
   for(trade=OrdersTotal()-1;trade>=0;trade--)
   {
      OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber) continue;
      if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
      {
     
         if (OrdType == "buy")
         {
            if(OrderType()==OP_BUY)
            {
               AveragePrice=AveragePrice+OrderOpenPrice()*OrderLots();
               Count=Count + OrderLots();
            }
         }

      }   
   }
   AveragePrice=NormalizeDouble(AveragePrice/Count, Digits);
      
  return(AveragePrice);    
}

when there are no orders present you crash the program with a zero divide.

It also doesn't check the OrderSelect.

Why are you re-checking for OrderSymbol and OrderMagicNumber after you have just rejected trades (with a continue) that do not meet those criteria?

 
dabbler:
Well running that in the strategy tester gives me a zero_divide error and no trades at all.
Now I tried with my modification, it opens a position, but gives a lot of OrderModify error 1. Before that, the stop points I set was too small, and it's result was error 130 and endless new position opening. This code is full of bugs, it is a wreck.
 

Fix ...

//AVERAGE PRICE STRING:
double AveragePrice(string OrdType)
{
   double AveragePrice=0;
   double Count=0;
   int trade;
   
   if( OrdersTotal()==0 )  // trap out a divide by zero error
      return (1.0);
      
   for(trade=OrdersTotal()-1;trade>=0;trade--)
   {
      OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber) continue;
      if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
      {
     
         if (OrdType == "buy")
         {
            if(OrderType()==OP_BUY)
            {
               AveragePrice=AveragePrice+OrderOpenPrice()*OrderLots();
               Count=Count + OrderLots();
            }
         }

      }   
   }
   AveragePrice=NormalizeDouble(AveragePrice/Count, Digits);
      
  return(AveragePrice);    
}

And it now trades.

BUT it is still not good code, and it is giving OrderSend error 4051 with invalid lots amounts.

 

s

dabbler:

If you call this function

when there are no orders present you crash the program with a zero divide.

It also doesn't check the OrderSelect.

Why are you re-checking for OrderSymbol and OrderMagicNumber after you have just rejected trades (with a continue) that do not meet those criteria?


sorry, i can not answer that as i did not write the code. i know it functions well without my added "Ask<Buy_Price" line. i only wanted to limit it from continuing to open orders as the price rose beyond a certain point. i am begining to think it is not possible.
 
cavesinarizona:
sorry, i can not answer that as i did not write the code. i know it functions well without my added "Ask<Buy_Price" line. i only wanted to limit it from continuing to open orders as the price rose beyond a certain point. i am begining to think it is not possible.

I would recommend that you dump that code and start from scratch. There is a lot of code and it is not well written -- and even if it were well written you wouldn't understand it!

It is better to start off simple and add things as you go. You will then really understand the code and be able to change it to do what you want. It will take a bit of time but you will get to your final goal much faster.

 
dabbler:

I would recommend that you dump that code and start from scratch. There is a lot of code and it is not well written -- and even if it were well written you wouldn't understand it!

It is better to start off simple and add things as you go. You will then really understand the code and be able to change it to do what you want. It will take a bit of time but you will get to your final goal much faster.


Thanks, sounds like good advice. thanks for your time and help.
Reason: