(MT5) Need help with setting up orders in Grid

 

Hello I am learning mql5 language. I have designed a code to setup pending orders in gird when a candle is bigger than the predefined range. If candle is bullish then place 1 sell trade and then set a specific amount of sell limit orders based on open price of the Sell trade and opposite for buys.

When anyone of the trade reaches the TP I want to close all orders and positions. Please check my code and help me improve it.

#include<Trade\Trade.mqh>
CTrade trade;

input double RiskPercent      = 0.5;
input double CandleSize       = 1.0;
input double TPPips           = 5.0;
input double MaxOrders        = 5;
input double OrderDistance    = 5.00;
input double VolumeMultiplier = 2.0;

  double candlesizepips;    
  double takeprofitpips;    
  double orderdistancepips; 
  double tppricebuy;        
  double tppricesell;


void OnTick()
  {
  double Equity   = AccountInfoDouble(ACCOUNT_EQUITY); 
  double Balance  = AccountInfoDouble(ACCOUNT_BALANCE);
  double Bid      = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
  double Ask      = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
  
   candlesizepips    = NormalizeDouble((CandleSize*_Point*10),_Digits);
   takeprofitpips    = NormalizeDouble((TPPips*_Point*10),_Digits);
   orderdistancepips = NormalizeDouble((OrderDistance*_Point*10),_Digits);
   tppricebuy        = NormalizeDouble((Ask+takeprofitpips),_Digits);
   tppricesell       = NormalizeDouble((Bid-takeprofitpips),_Digits);
  
  double High[];
  double Low[];
  double Open[];
  double Close[];
  
  ArraySetAsSeries(High,true);
  ArraySetAsSeries(Low,true);
  ArraySetAsSeries(Open,true);
  ArraySetAsSeries(Close,true);
  
  CopyHigh(_Symbol,_Period,0,100,High);
  CopyLow(_Symbol,_Period,0,100,Low);
  CopyOpen(_Symbol,_Period,0,100,Open);
  CopyClose(_Symbol,_Period,0,100,Close);
  
  MqlRates PriceInfo[];
  ArraySetAsSeries(PriceInfo,true);
  int PriceData =CopyRates(_Symbol,_Period,0,100,PriceInfo);
  
  static datetime timeStampLastCheck;
  datetime timeStampCurrentCandle;
  timeStampCurrentCandle = PriceInfo[0].time;
  
  double currentcandlesize = NormalizeDouble((High[0]-Low[0]),_Digits);
  double lastcandlesize    = NormalizeDouble((High[1]-Low[1]),_Digits);
  
  double pipvalue = NormalizeDouble((10*(SymbolInfoDouble(NULL,SYMBOL_TRADE_TICK_VALUE))),2);
  double minimumlotsize = NormalizeDouble((SymbolInfoDouble(NULL,SYMBOL_VOLUME_MIN)),2);
  double maximumlotsize = NormalizeDouble((SymbolInfoDouble(NULL,SYMBOL_VOLUME_MAX)),2);
  double contractsize = NormalizeDouble((SymbolInfoDouble(NULL,SYMBOL_TRADE_CONTRACT_SIZE)),2);
  
  double LS = (Balance*(RiskPercent/100))/(TPPips*pipvalue);
  double LotSize = NormalizeDouble(LS,2);
  
  if(LotSize < minimumlotsize)
  {
   LotSize = minimumlotsize;
  }
  
  if(LotSize > maximumlotsize)
  {
   LotSize = maximumlotsize;
  }
  
 
  
  if(currentcandlesize > candlesizepips && Close[0] < Open[0] && timeStampCurrentCandle!=timeStampLastCheck && PositionsTotal() == 0)
  {
   int ticket = trade.Buy(LotSize,NULL,Ask,0,tppricebuy,NULL);
   timeStampLastCheck=timeStampCurrentCandle; 
  
   if(ticket <= 0)
   {
    Alert("Trade rejected"," ", "Reason:",GetLastError());
   }
   else
   {
    Print(" Buy Trade sucessful");    
   }
  }
 
  if(currentcandlesize > candlesizepips && Close[0] > Open[0] && timeStampCurrentCandle!=timeStampLastCheck && PositionsTotal() == 0)
  {
   int ticket = trade.Sell(LotSize,NULL,Bid,0,tppricesell,NULL);
   
   timeStampLastCheck=timeStampCurrentCandle; 
   Alert(ticket);
   if(ticket <= 0)
   {
    Alert("Trade rejected"," ", "Reason:",GetLastError());
   }
   else
   {
    Print(" Sell Trade sucessful");    
   }
  }
  
  if(currentcandlesize > candlesizepips)
  {
   candlesizepips = currentcandlesize;
  }
  
  if(PositionsTotal() > 0 && OrdersTotal() <= MaxOrders)
{
 if((OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_LIMIT) || (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY))
 {
  double latesttradeopenprice;
  double latesttradelots;
  double lastorderopenprice;
  double lastorderlots;
  double nexttradeopenprice;
  double nexttradelots;
  double nextorderopenprice;
  double nextorderlots;
   for(int i=MaxOrders; i>=0; i--)
 {
  int ticket=PositionGetTicket(i);
   if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY && OrdersTotal()==0 && PositionSelectByTicket(ticket) == )
   { 
     latesttradeopenprice = NormalizeDouble((PositionGetDouble(POSITION_PRICE_OPEN)),_Digits);
     latesttradelots      = NormalizeDouble((PositionGetDouble(POSITION_VOLUME)),2);
     nexttradeopenprice   = NormalizeDouble((latesttradeopenprice - orderdistancepips),_Digits);
     nexttradelots        = NormalizeDouble((latesttradelots * VolumeMultiplier),2);
     
     if(PositionsTotal() == 1)
     {
      trade.BuyLimit(nexttradelots,nexttradeopenprice,NULL,0,latesttradeopenprice,0,0,NULL);
     }
  }
  }
   for(int i=MaxOrders; i>=0; i--)
 {
  int ticket=OrderGetTicket(i);
  if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_LIMIT && PositionsTotal() >= 1)
   {
    lastorderopenprice = NormalizeDouble((OrderGetDouble(ORDER_PRICE_OPEN)),_Digits);
    lastorderlots      = NormalizeDouble((OrderGetDouble(ORDER_VOLUME_CURRENT)),2);
    nextorderopenprice = NormalizeDouble((lastorderopenprice - orderdistancepips),_Digits);
    nextorderlots      = NormalizeDouble((lastorderlots * VolumeMultiplier),2);
    
    if(OrdersTotal() > 0 && OrdersTotal() < (MaxOrders))
    {
     trade.BuyLimit(nextorderlots,nextorderopenprice,NULL,0,lastorderopenprice,0,0,NULL);
    }
   }
  }
 }
   
 
 if((OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_LIMIT) || (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL))
 {
  double latesttradeopenprice;
  double latesttradelots;
  double lastorderopenprice;
  double lastorderlots;
  double nexttradeopenprice;
  double nexttradelots;
  double nextorderopenprice;
  double nextorderlots;
  
 for(int i=MaxOrders; i>=0; i--)
 {
  int ticket=PositionGetTicket(i);
  if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL && OrdersTotal()==0)
   { 
     latesttradeopenprice = NormalizeDouble((PositionGetDouble(POSITION_PRICE_OPEN)),_Digits);
     latesttradelots      = NormalizeDouble((PositionGetDouble(POSITION_VOLUME)),2);
     nexttradeopenprice   = NormalizeDouble((latesttradeopenprice + orderdistancepips),_Digits);
     nexttradelots        = NormalizeDouble((latesttradelots * VolumeMultiplier),2);
     
     if(PositionsTotal() == 1)
     {
      trade.SellLimit(nexttradelots,nexttradeopenprice,NULL,0,latesttradeopenprice,0,0,NULL);
     }
    }
    }
 for(int i=MaxOrders; i>=0; i--)
 {
  int ticket=OrderGetTicket(i);
  if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_LIMIT && PositionsTotal() >= 1)
   {
    lastorderopenprice = NormalizeDouble((OrderGetDouble(ORDER_PRICE_OPEN)),_Digits);
    lastorderlots      = NormalizeDouble((OrderGetDouble(ORDER_VOLUME_CURRENT)),2);
    nextorderopenprice = NormalizeDouble((lastorderopenprice + orderdistancepips),_Digits);
    nextorderlots      = NormalizeDouble((lastorderlots * VolumeMultiplier),2);
   
    if(OrdersTotal() > 0 && OrdersTotal() < (MaxOrders))
    {
     trade.SellLimit(nextorderlots,nextorderopenprice,NULL,0,lastorderopenprice,0,0,NULL);
    }
    }
  }
 }
 }


 }  
//+------------------------------------------------------------------+



void Closeallbuypostions()
{
 for(int i=PositionsTotal(); i>=0; i--)
 {
  int ticket=PositionGetTicket(i);
  
  int PositionDirection=PositionGetInteger(POSITION_TYPE);
  
  if(PositionDirection==POSITION_TYPE_BUY)
  
  trade.PositionClose(ticket);
 }
}

void Closeallsellpostions()
{
 for(int i=PositionsTotal(); i>=0; i--)
 {
  int ticket=PositionGetTicket(i);
  
  int PositionDirection=PositionGetInteger(POSITION_TYPE);
  
  if(PositionDirection==POSITION_TYPE_SELL)
  
  trade.PositionClose(ticket);
 }
}   

void Closeallbuyorders()
{
 for(int i=OrdersTotal(); i>=0; i--)
 {
  int ticket=OrderGetTicket(i);
  
  int OrderDirection=OrderGetInteger(ORDER_TYPE);
  
  if(OrderDirection==ORDER_TYPE_BUY_LIMIT)
  
  trade.OrderDelete(ticket);
 }
}

void Closeallsellorders()
{
 for(int i=OrdersTotal(); i>=0; i--)
 {
  int ticket=OrderGetTicket(i);
  
  int OrderDirection=OrderGetInteger(ORDER_TYPE);
  
  if(OrderDirection==ORDER_TYPE_SELL_LIMIT)
  
  trade.OrderDelete(ticket);
 }
}
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be obtained using functions Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The identifier of...
 

Immediately a mistake.

Read help:

OrderGetInteger

Returns the requested order property, pre-selected using OrderGetTicket or OrderSelect. Order property must be of the datetime, int type. There are 2 variants of the function.


PositionGetInteger

The function returns the requested property of an open position, pre-selected using PositionGetSymbol or PositionSelect. The position property should be of datetime, int type. There are 2 variants of the function.

Documentation on MQL5: Trade Functions / OrderGetInteger
Documentation on MQL5: Trade Functions / OrderGetInteger
  • www.mql5.com
Returns the requested order property, pre-selected using OrderGetTicket or OrderSelect. Order property must be of the datetime, int type. There are 2 variants of the function. 2. Returns true or false depending on the success of the function. If successful, the value of the property is...
Reason: