EA Error

 

This EA must Open 1 Market Order upon Initialization, and the fill a grid with Pending Orders.  SO now, upon Initiation, it opens multiple Market orders, and no Pendings. I get no error when I compile but it just does not do what I want. I am new to coding, if someone can just point to my mistake, I can find fix.


Thanks


//--External variables
extern int    MagicNumber       = 987654321;//Magic number
extern string EaComment         = "Grid Bomb";//Order comment
extern double StaticLot         = 0.01;//Static lots size
extern bool   MM                = false;//Money Management
extern int    Risk              = 2;//Risk %
extern double TakeProfit        = 50000.;//Take Profit in pips
extern double StopLoss          = 50000.;//Stop loss in pips
extern double PriceDistance     = 25.;//Distance from price in pips
extern double GridStep          = 35.;//Step between grid orders in pips
extern int    GridOrders        = 2;//Amount of grid orders
extern int    PendingExpiration = 72;//Pending expiration after xx hours
input ENUM_ORDER_TYPE   InpType =  ORDER_TYPE_BUY;   // Order Type

//--Internal variables
double
PriceB,
PriceS,
StopB,
StopS,
TakeB,
TakeS,
_points,
PT,
Lots,
MPP;
datetime
_e = 0;
int
Ticket  = 0;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   MPP=1;
   if((MarketInfo(Symbol(),MODE_DIGITS)==3)||(MarketInfo(Symbol(),MODE_DIGITS)==5))
      MPP=10;
   PT=MarketInfo(Symbol(),MODE_TICKSIZE)*MPP;
//--
   return(INIT_SUCCEEDED);{
   }      
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//If trade allowed and no positions exists by any chart - open a new Market Order and set of grid orders
   if(IsTradeAllowed() && !IsTradeContextBusy())
     
      if(MarketOrderCount()<1)  {
         OpenTrade();// Place 1 Trade
         }
         return;
       if(PosSelect()>0){
         GridPos(PriceDistance,TakeProfit,StopLoss);//Place grid
     }
    }
/////////////////////////////////////////////////////////    
// Market order count
int MarketOrderCount()
  {
//---
   int cnt=0;
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(!OrderSelect(i, SELECT_BY_POS))
         break;
      if(OrderSymbol()!=Symbol() && OrderMagicNumber()!=MagicNumber)
         continue;
      if((OrderCloseTime() == 0) && OrderMagicNumber()==MagicNumber)
        {
         if(OrderType() == OP_BUY||OrderType() == OP_SELL)
            cnt = 1;
         if(!(OrderType() == OP_BUY||OrderType() == OP_SELL))
            cnt = 1;
        }
     }
   return(cnt);
  }
///////////////////////////////////////////////////////////
//Open 1 trade function
void  OpenTrade() {
   
   double   price = (InpType==ORDER_TYPE_BUY)?
                       SymbolInfoDouble(Symbol(), SYMBOL_ASK) :          
                       SymbolInfoDouble(Symbol(), SYMBOL_BID);
            OrderSend(Symbol(), InpType, StaticLot, price, 0, 0 ,0, MagicNumber); {
        
                   }
      }
   ///////////////////////////////////////////////////////////
//Grid order send function
void GridPos(double _Dist,double _Take,double _Stop)
  {
   int i;
   _e=TimeCurrent()+PendingExpiration*60*60;
//--
   for(i=0; i<GridOrders; i++)
     {
      PriceB = NormalizeDouble(Bid-(_Dist*PT)-(i*GridStep*PT),Digits);
      TakeB  = PriceB + _Take * PT;
      StopB  = PriceB - _Stop * PT;
      double
      lotcheckA=CheckVolumeValue(LotSize());
      if((CheckMoneyForTrade(Symbol(),lotcheckA,OP_BUY))&&(IsNewOrderAllowed()))
         Ticket=OrderSend(Symbol(),OP_BUYLIMIT,lotcheckA,PriceB,3,StopB,TakeB,EaComment,MagicNumber,_e,Green);
      //--
      PriceS = NormalizeDouble(Ask+(_Dist*PT)+(i*GridStep*PT),Digits);
      TakeS  = PriceS - _Take * PT;
      StopS  = PriceS + _Stop * PT;
      double
      lotcheckB=CheckVolumeValue(LotSize());
      if((CheckMoneyForTrade(Symbol(),lotcheckB,OP_SELL))&&(IsNewOrderAllowed()))
         Ticket=OrderSend(Symbol(),OP_SELLLIMIT,lotcheckB,PriceS,3,StopS,TakeS,EaComment,MagicNumber,_e,Red);
     }
   if(Ticket<1)
     {
      Print("Order send error - errcode: ",GetLastError());
      return;
     }
   else
      Print("Grid placed successfully!");
  }
//////////////////////////////////////////////////////////
//Pending Count Pending Orders
int PosSelect()
  {
//---
   int poss=0;
   for(int k = OrdersTotal() - 1; k >= 0; k--)
     {
      if(!OrderSelect(k, SELECT_BY_POS))
         break;
      if(OrderSymbol()!=Symbol() && OrderMagicNumber()!=MagicNumber)
         continue;
      if((OrderCloseTime() == 0) && OrderMagicNumber()==MagicNumber)
        {
         if(OrderType() == OP_BUY||OrderType() == OP_SELL)
            poss = 1;
         if(!(OrderType() == OP_BUY||OrderType() == OP_SELL))
            poss = 1;
        }
     }
   return(poss);
  }
////////////////////////////////////////////////////////////
//Lots size calculation
double LotSize()
  {
   if(MM == true)
     {
      Lots = MathMin(MathMax((MathRound((AccountFreeMargin()*Risk/1000/100)
                                        /MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP)),
                             MarketInfo(Symbol(),MODE_MINLOT)),MarketInfo(Symbol(),MODE_MAXLOT));
     }
   else
     {
      Lots = MathMin(MathMax((MathRound(StaticLot/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP)),
                             MarketInfo(Symbol(),MODE_MINLOT)),MarketInfo(Symbol(),MODE_MAXLOT));
     }

   return(Lots);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool IsNewOrderAllowed()
  {

   int max_allowed_orders = (int)AccountInfoInteger(ACCOUNT_LIMIT_ORDERS);

   if(max_allowed_orders == 0)
      return true;

   int orders = OrdersTotal();

   return orders < max_allowed_orders;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CheckMoneyForTrade(string symb, double lots,int type)
  {
   double free_margin=AccountFreeMarginCheck(symb,type,lots);
   if(free_margin<0)
     {
      string oper=(type==OP_BUY)? "Buy":"Sell";
      Print("Not enough money for ", oper," ",lots, " ", symb, " Error code=",GetLastError());
      return(false);
     }
   return(true);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double CheckVolumeValue(double checkedvol)
  {
//--- minimal allowed volume for trade operations
   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
   if(checkedvol<min_volume)
      return(min_volume);

//--- maximal allowed volume of trade operations
   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
   if(checkedvol>max_volume)
      return(max_volume);

//--- get minimal step of volume changing
   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);
   int ratio=(int)MathRound(checkedvol/volume_step);
   if(MathAbs(ratio*volume_step-checkedvol)>0.0000001)
      return(ratio*volume_step);
   return(checkedvol);
  }
//+----------------End of Rolling Grid EA-------------------+
 
  1.   double   price = (InpType==ORDER_TYPE_BUY)?

    Invalid constant. Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL4 Reference

  2.       if(!OrderSelect(i, SELECT_BY_POS))
             break;
          if(OrderSymbol()!=Symbol() && OrderMagicNumber()!=MagicNumber)
             continue;
          if((OrderCloseTime() == 0) && OrderMagicNumber()==MagicNumber)

    Drop the second IF. You will never find a closed order (OCT!=0) in the loop.

  3.          if(OrderType() == OP_BUY||OrderType() == OP_SELL)
                cnt = 1;
             if(!(OrderType() == OP_BUY||OrderType() == OP_SELL))
                cnt = 1;
            }
    

    The function will always return one.

  4. JSJTradeworx: it just does not do what I want.

    Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
Thank you. Getting there