Grid Maker script

 
Here is a FIRST version of a script to place buy orders every GridSize ticks for GridSteps/2 below and above current price.

It will not put buy orders if there is already one or if there is a position open. Running it every 30 mins to 1 hour fills all the holes in the grid created by orders being closed.

For now, this only puts in longs. I dont see serious bugs in it but its my first mq4 script.

I will make it into an advisor so that it automatically fills holes, allow for longs and shorts (how do I access the parameters of the first page of show_inputs ?) and other variants of the grid.

Backtesting will be interesting!

I put this script here so that others may test it, comment, help me develop it, put in their ideas and experience...

If u are not aware of the trading idea behind this script, look at:

http://www2.oanda.com/cgi-bin/msgboard/ultimatebb.cgi?ubb=get_topic;f=15;t=002524

(MetaQuotes: is it ok for me to refer to other forums? )

Feel free to contact me if u want to help with this.

Here is the script (ps. this is a first version.. it is rough code.. it can make u lose money... I am not responsible for any direct or indirect use of this code):




//+------------------------------------------------------------------+
//|                                                     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.0beta"

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
extern double GridSteps = 40;          // total number of orders
extern double GridLong = 1;            // not used


//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//---- 
   int    doit, i, j,k, ticket, entermode, totalorders;
   double point, startrate, traderate;
 
 #property show_inputs              // shows the parameters - thanks Slawa... 
   
//----
   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
//   startrate = startrate + GridSize/2*point;                 // add half a mesh to be away from current rate
   
   for( i=0;i<GridSteps;i++)
   {
     traderate = startrate + i*point*GridSize;
     
     doit = 1;                                                 // we scan open orders to see if we already have one
     totalorders = OrdersTotal();
     for( j=0;j<totalorders;j++)                               // oops.. i must not test those that are not part of my grid.. 
      {
        OrderSelect(j, SELECT_BY_POS);
        if (MathAbs( OrderOpenPrice() - traderate) < point*GridSize)
            { 
               doit = 0;
            }
      }
     if (doit == 1)                                            // ok, i have no open orders
      {
         if ( traderate > Ask ) 
          { entermode = OP_BUYSTOP; } 
          else 
          { entermode = OP_BUYLIMIT ; } 
     
         ticket=OrderSend(Symbol(),entermode,Lots,traderate,0,0,traderate+point*GridSize,GridName,16384,0,Green);
      }
   }

   return(0);
  }
//+------------------------------------------------------------------+
Reason: