Use the code style button </> in editor menu to format code.
thank you.
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:
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.
For short grid you need this other way round:
double nearestSellAboveBid=FindNearestOpenPriceAbove(Bid,MagicNumberSell,OP_SELL); double nearestSellBelowBid=FindNearestOpenPriceBelow(Bid,MagicNumberSell,OP_SELL);
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.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
see picture and
i wirte code :
ea not working.