Help me grid code

To add comments, please log in or register
Thanat Thitithammaphong
489
Thanat Thitithammaphong  

see picture and 

i wirte code :

if(Ask!=FindOrderOpenPriceByType(MagicNumberBuy,OP_BUY) && (Ask-(GridStep*Point)>=FindOrderOpenPriceByType(MagicNumberBuy,OP_BUY) &&

   Ask+(GridStep*Point)<=FindOrderOpenPriceByType(MagicNumberBuy,OP_BUY)))
   int buy4=OrderSend(Symbol(),OP_BUY,MinLots,Ask,0,0,0,comment,MagicNumberBuy,0,clrGreen); 



//--------------------------------------------------------------------------------------

double FindOrderOpenPriceByType(int magic,int input_type)

{  

  for(int func_i=OrdersTotal()-1;func_i>=0;func_i--)

   {

      if(OrderSelect(func_i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderType()==input_type && OrderMagicNumber()==magic)

      return OrderOpenPrice();

   }



return -1;

}

//----------------------------------------------------------------------

ea not working.

Files:
yy.png 24 kb
lippmaje
594
lippmaje  
Use the code style button </> in editor menu to format code.
Thanat Thitithammaphong
489
Thanat Thitithammaphong  
lippmaje:
Use the code style button </> in editor menu to format code.

thank you.

lippmaje
594
lippmaje  

Up trend:

You must look for nearest open order price below Ask. If distance is greater grid step, open buy order.

Down trend:

You must look for nearest open order price above Ask. If distance is greater grid step, open buy order.

double nearestBuyAboveAsk=FindNearestOpenPriceAbove(Ask,MagicNumberBuy,OP_BUY);
double nearestBuyBelowAsk=FindNearestOpenPriceBelow(Ask,MagicNumberBuy,OP_BUY);

if(nearestBuyAboveAsk==-1 || nearestBuyBelowAsk==-1) {
   // to do: what if not exist?
}
else if(Ask-(GridStep*Point)>=nearestBuyBelowAsk && Ask+(GridStep*Point)<=nearestBuyAboveAsk) {
   int buy4=OrderSend(Symbol(),OP_BUY,MinLots,Ask,0,0,0,comment,MagicNumberBuy,0,clrGreen); 
   ...
}

//--------------------------------------------------------------------------------------

double FindNearestOpenPriceAbove(double price,int magic,int ordertype) {
  double min=99999;
  double openprice=-1;

  for(int i=OrdersTotal()-1;i>=0;i--) {
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderType()==ordertype && OrderMagicNumber()==magic) {
        double dist=OrderOpenPrice()-price;
        if(dist>=0 && min>dist) { min=dist; openprice=OrderOpenPrice(); }
     }
  }
  return openprice;
}

double FindNearestOpenPriceBelow(double price,int magic,int ordertype) {
  double min=99999;
  double openprice=-1;

  for(int i=OrdersTotal()-1;i>=0;i--) {
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderType()==ordertype && OrderMagicNumber()==magic) {
        double dist=price-OrderOpenPrice();
        if(dist>=0 && min>dist) { min=dist; openprice=OrderOpenPrice(); }
     }
  }
  return openprice;
}
Thanat Thitithammaphong
489
Thanat Thitithammaphong  
Thanat Thitithammaphong:

thank you.

lippmaje:

Up trend:

You must look for nearest open order price below Ask. If distance is greater grid step, open buy order.

Down trend:

You must look for nearest open order price above Ask. If distance is greater grid step, open buy order.

ea working

thank you.

lippmaje
594
lippmaje  

For short grid you need this other way round:

double nearestSellAboveBid=FindNearestOpenPriceAbove(Bid,MagicNumberSell,OP_SELL);
double nearestSellBelowBid=FindNearestOpenPriceBelow(Bid,MagicNumberSell,OP_SELL);
Thanat Thitithammaphong
489
Thanat Thitithammaphong  
double nearestBuyAboveAsk=FindNearestOpenPriceAbove(Ask,MagicNumberBuy,OP_BUY);
double nearestBuyBelowAsk=FindNearestOpenPriceBelow(Ask,MagicNumberBuy,OP_BUY);

if(nearestBuyAboveAsk==-1 || nearestBuyBelowAsk==-1) {
  nearestBuyAboveAsk=FindNearestOpenPriceAbove(Ask,MagicNumberBuy,OP_BUY);
  nearestBuyBelowAsk=FindNearestOpenPriceBelow(Ask,MagicNumberBuy,OP_BUY);
}
 else if(Ask-(GridStep*Point)>=nearestBuyBelowAsk && Ask+(GridStep*Point)<=nearestBuyAboveAsk){ 
   int buy4=OrderSend(Symbol(),OP_BUY,MinLots,Ask,0,0,0,comment,MagicNumberBuy,0,clrGreen); 
 }  


double nearestSellAboveBid=FindNearestOpenPriceAbove(Bid,MagicNumberSell,OP_SELL);
double nearestSellBelowBid=FindNearestOpenPriceBelow(Bid,MagicNumberSell,OP_SELL);

if(nearestSellAboveBid==-1 || nearestSellBelowBid==-1) {
   nearestSellAboveBid=FindNearestOpenPriceAbove(Bid,MagicNumberSell,OP_SELL);
   nearestSellBelowBid=FindNearestOpenPriceBelow(Bid,MagicNumberSell,OP_SELL);
}
else if(Bid-(GridStep*Point)>=nearestSellBelowBid && Bid+(GridStep*Point)<=nearestSellAboveBid) {
   int sell4=OrderSend(Symbol(),OP_SELL,MinLots,Bid,0,0,0,comment,MagicNumberSell,0,clrRed); 
  }
lippmaje:

For short grid you need this other way round:

thank you.

my ea working.

To add comments, please log in or register