grid trading lot

 

Hello,

I am going to create a function of grid trading lot:

if trading direction is long and market price goes down, then multiple the trading lot according to previous order.

I think the rule is quite simple, but I always get error message of "invalid lots amount",

would you please give me some hints ?

Thanks a lot.


double grids(string direction,double in_price) {
   int ticketno,new_ticketno;
   double tmp , DiffPips, lastprice, lastlot;
   
   if(direction=="buy") {
      //get last order lot and price
      for(int i=0;i<=OrdersTotal();i++) {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==magicno) {
            new_ticketno=OrderTicket();
            if(new_ticketno>ticketno) {
               new_ticketno=ticketno;
               lastprice=OrderOpenPrice();
               lastlot = OrderLots();
            }   
         }
      }
         if(in_price < lastprice) { 
            tmp = MathAbs(in_price - lastprice) / iATR(Symbol(),0,in_period,0);
            DiffPips = NormalizeDouble(lastlot * tmp, 2);
         }
         else {
            DiffPips = lastlot;
         } 
   }
}

 
Chihming Tsao: I am going to create a function of grid trading lot:

if trading direction is long and market price goes down, then multiple the trading lot according to previous order. I think the rule is quite simple, but I always get error message of "invalid lots amount", would you please give me some hints ?

Please see the following link for the correct way to adjust volume/lot size:

Forum on trading, automated trading systems and testing trading strategies

How to calculate lots using multiplier according to number of opened orders?

Fernando Carreiro, 2017.09.01 21:57

Don't use NormalizeDouble(). Here is some guidance (code is untested, just serves as example):

// Variables for Symbol Volume Conditions
double
   dblLotsMinimum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN  ),
   dblLotsMaximum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX  ),
   dblLotsStep    = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP );
   
// Variables for Geometric Progression
double
   dblGeoRatio = 2.8,
   dblGeoInit  = dblLotsMinimum;
   
// Calculate Next Geometric Element
double
   dblGeoNext  = dblGeoInit * pow( dblGeoRatio, intOrderCount + 1 );
   
// Adjust Volume for allowable conditions
double
   dblLotsNext = fmin( dblLotsMaximum,                                     // Prevent too greater volume
                   fmax( dblLotsMinimum,                                   // Prevent too smaller volume
                     round( dblGeoNext / dblLotsStep ) * dblLotsStep ) );  // Align to Step value
Reason: