Grid maker 1.1 - page 3

 
Hi again

thanks for yuor answer ill add you when i get home
 
I find that this code has problems at times keeping the grid perfect (at least when I run it as a pullback not a breakout grid). I've seen holes form and not get updated and I've seen the grid not fully form at times with all 10 orders. Also, it seems to not like update time settings that are very short and I don't understand why. It seems that 15 minutes is way too long to update a grid (more like 15 seconds!) so this is a major problem. I would appreciate knowing if you have experienced any of this. Great job however on getting this going at all. Nice work.
 
soma,

u are right, there was a bug that someone pointed out to me and seems to be fixed. here is the new version. I also changed the interval to 1 min - but I think 15 to 30 mins is better.. it will avoid a few counter trades when the market moves real fast.

regards,

//+------------------------------------------------------------------+
//|                                                     MakeGrid.mq4 |
//|                                            Copyright © 2005, hdb |
//|                                       http://www.dubois1.net/hdb |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, hdb"
#property link      "http://www.dubois1.net/hdb"
//#property version      "1.4beta"

extern string GridName = "Grid";       // identifies the grid. allows for several co-existing grids
extern double Lots = 0.1;              // 
extern double GridSize = 6;            // pips between orders - grid or mesh size
extern double GridSteps = 12;          // total number of orders to place
extern double TakeProfit = 6 ;         // number of ticks to take profit. normally is = grid size but u can override
extern double StopLoss = 0;            // if u want to add a stop loss. normal grids dont use stop losses
extern double UpdateInterval = 1;      // update orders every x minutes
extern bool   wantLongs = true;        //  do we want long positions
extern bool   wantShorts = true;       //  do we want short positions
extern bool   wantBreakout = true;     // do we want longs above price, shorts below price
extern bool   wantCounter = true;      // do we want longs below price, shorts above price
extern bool   limitEMA34 = false;      // do we want longs above ema only, shorts below ema only
extern double LastUpdate = 0;          // counter used to note time of last update
extern double GridMaxOpen = 0;         // maximum number of open positions
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
 #property show_inputs                  // shows the parameters - thanks Slawa...    
 if ( TakeProfit <= 0 )                 // 
   { TakeProfit = GridSize; }
//----
   return(0);
  }
//+------------------------------------------------------------------------+
//| tests if there is an open position or order in the region of atRate    |
//|     will check for longs if checkLongs is true, else will check        |
//|     for shorts                                                         |
//+------------------------------------------------------------------------+

bool IsPosition(double atRate, double inRange, bool checkLongs )
  {
  
     int totalorders = OrdersTotal();
     for(int j=0;j<totalorders;j++)                                // scan all orders and positions...
      {
        OrderSelect(j, SELECT_BY_POS);
        if ( OrderSymbol()==Symbol() && OrderComment() == GridName )  // only look if mygrid and symbol...
         {  int type = OrderType();
            if (MathAbs( OrderOpenPrice() - atRate ) < inRange) // dont look for exact price but price proximity (less than gridsize)
              { if ( ( checkLongs && ( type == OP_BUY || type == OP_BUYLIMIT  || type == OP_BUYSTOP ) )  || (!checkLongs && ( type == OP_SELL || type == OP_SELLLIMIT  || type == OP_SELLSTOP ) ) )
                 { 
                    return(true); 
                 }
              }
         }
      } 

   return(false);
  }
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//---- 
   int    i, j,k, ticket, entermode, totalorders;
   bool   doit;
   double point, startrate, traderate;
 
//----
  if (MathAbs(CurTime()-LastUpdate)> UpdateInterval*60)           // we update the first time it is called and every UpdateInterval minutes
   {
   LastUpdate = CurTime();
   Print("Updating");
   point = MarketInfo(Symbol(),MODE_POINT);
   startrate = ( Ask + point*GridSize/2 ) / point / GridSize;    // round to a number of ticks divisible by GridSize
   k = startrate ;
   k = k * GridSize ;
   startrate = k * point - GridSize*GridSteps/2*point ;          // calculate the lowest entry point
   
   double EMA34=iMA(NULL,0,34,0,MODE_EMA,PRICE_CLOSE,0);
   
   for( i=0;i<GridSteps;i++)
   {
     traderate = startrate + i*point*GridSize;
     
     if ( wantLongs && (!limitEMA34 || traderate > EMA34))
       {
         if ( IsPosition(traderate,point*GridSize,true) == false )           // test if i have no open orders close to my price: if so, put one on
          {
            double myStopLoss = 0;
             if ( StopLoss > 0 )
               { myStopLoss = traderate-point*StopLoss ; }
               
             if ( traderate > Ask ) 
              { entermode = OP_BUYSTOP; } 
              else 
              { entermode = OP_BUYLIMIT ; } 
              
              if ( ((traderate > Ask ) && (wantBreakout)) || ((traderate <= Ask ) && (wantCounter)) ) 

              { 
                  ticket=OrderSend(Symbol(),entermode,Lots,traderate,0,myStopLoss,traderate+point*TakeProfit,GridName,16384,0,Green); 
   
             }
          }
       }

     if ( wantShorts && (!limitEMA34 || traderate < EMA34))
       {
         if (IsPosition(traderate,point*GridSize,false)== false )           // test if i have no open orders close to my price: if so, put one on
          {
             myStopLoss = 0;
             if ( StopLoss > 0 )
               { myStopLoss = traderate+point*StopLoss ; }
             if ( traderate > Bid ) 
              { entermode = OP_SELLLIMIT; } 
              else 
              { entermode = OP_SELLSTOP ; } 
              
              if ( ((traderate < Bid ) && (wantBreakout)) || ((traderate >= Bid ) && (wantCounter)) ) 
                { ticket=OrderSend(Symbol(),entermode,Lots,traderate,0,myStopLoss,traderate-point*TakeProfit,GridName,16384,0,Red); }
          }
       }

    }
   }
   return(0);
  }
//+------------------------------------------------------------------+
 
Thanks, hdb. But perhaps I don't understand exactly how you think of grids because I can't see a reason why updating too soon would be bad. Missing an entry would keep the grid from it's full potential. When I run it manually, I update orders the second anything changes. I run pullback grids not breakout grids so perhaps that is where the difference lies, but with pullbacks you profit from choppiness and so keeping the grid free of holes is ideal.
 
soma,

yes, maybe u are right.. so the best would be to update frequently but only a few grid slots? needs testing though. I have no strong opinion.

reagrds,
 
Well, i was waiting for a favourable moment to post an update of the grid - since its performane has really been lousy recently.
The week after my last update, I was on vacation and the grid was stopped. Last week, the grid was running most of the time.
I switched it off a few times when avail margin was zero and set 'longs only' on most of the majors.
Today was a super comeback day.

Balance : 93 (+10 since 10 days ago)
Margin used : +15k
Avail mrg : +28k
unreal. p&l : -49k (-11 since 10 days ago)
Balance : 44k

Net is 1k worse off than 10 days ago and 5k below starting balance.

The really hard part in all this is trying to limit the drawdown. I really have to find ways of doing this!
 
I think the only way to avoid the drawdown is to use discretionary methods
i.e to know when to stop adding orders and when to like you did stop adding say short positions.

Started with 100K balance after starting off badly by using to large a lot size and being forced to close out loads of positions.

Balance: 97 594.19 Balance about 2 weeks ago 83k
Free Margin: 6 016.88
Floating P/L: -38 960.45 was as bad as -56k
Margin: 52 616.86

the last couple of days had been really slow margin level was as low as 93%

but now all the majors seemed to have mad a turn around for the time being
margin level is back up to about 130%
 
Agreed, darkstonexa, I think u may be right - but i am testing a few automated variations based om ema and macd...I am not too optimistic though.

A friend also has one with a stoploss running.. and it is struggling to stay afloat! That really does solve the drawdown problem though!

In the meantime, i cleaned up my positions - all positions with a strong negative carry interest and well in loss ( a few 100 pips) were closed.

So my grids now are ready for another hit on the head (margin bact to 430%) !
 
well what a nice 2 days we had... yesterday saw my grid +5k in the green (equity was 54k, I started with 49k ) after weeks of pretty catastrophic trading.

Lucky we can try on demo accounts!

Now that I am back in a 'reasonable' position, i have set all the currency pairs to do the following:
1) trade only in the direction of positive carry interest (unless the interest is close to 0, then do it both ways)
2) longs only above 34 ema, shorts only below 34 ema
3) for 2 way currencies, use macd on 5 min chart to decide if long or short

We will see what this gives!

ps. I wish the back testing worked!
 
Hello hdb,

I have taken your advisor on EURUSD, trading both sides as breakout.

Now I would like to ask you, if you already run in the following problem:

After a while, I have more than one order on some of the same grid positions. Sometimes there are two, but I have also seen 5 and more of them.

As far as I understand the code, only one should be open at a gridposition at a time.

I have messed around converting all doubles to int before comparing because in MQL2 I have found a similar problem when comparing double values.

Did you encounter something similar?

With regards,

cori
Reason: