Open order function... feedback on how it's looking?

 

Hi all,

If you're willing to take a moment and look I'd like to get some feedback on if there's anything I missed that may cause problems unforseen to me down the road.

In particular, one thing I addressed in the MQL4 version was refreshing rates within the 2nd for loop, since I had a couple instances where my order didn't execute presumably because my open price became out of date. I think I've addressed that properly with SymbolInfoTick(Symbol(),last_tick)...? 

Feedback on that and anything else I may be missing would be appreciated. Thanks!

bool  OpenOrder(){
   int positionsRequired = 0;
   double riskAmountPerPosition = 0;
   double lotSize = 0;
   string accountCurrency = AccountInfoString(ACCOUNT_CURRENCY);
   double accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
   double riskAmount = accountBalance * sbPercent / 100.0;
   string slSizeText ;
   
   for(int i = 0; sbLots/maximumLot > i; i++)
   {
      positionsRequired = i + 1;
   }
   Print("Risk amount " + DoubleToString(riskAmount));
   Print("positionsRequired " + DoubleToString(positionsRequired));
   riskAmountPerPosition = riskAmount / positionsRequired; //normalize_lots(sandboxLotsize/positionsRequired);
   if(checkMaxSL() == -1) return(false);
   
   for(int i = 0; i < positionsRequired; i++) 
   {
      MqlTick last_tick;
      SymbolInfoTick(Symbol(),last_tick);
      double op = direction == 1 ? last_tick.ask : last_tick.bid;//SymbolInfoDouble(_Symbol,SYMBOL_ASK) : SymbolInfoDouble(_Symbol,SYMBOL_BID);

      slSize = StringFormat( "%.1f", MathAbs((slPrice - op) / pips2dbl / SymbolInfoDouble(Symbol(),SYMBOL_TRADE_CONTRACT_SIZE)* 10  * ptsDiv));
         
      lotSize = CalculateLotSize(op, riskAmountPerPosition);
      int slippage = int((SymbolInfoDouble(_Symbol,SYMBOL_ASK) - SymbolInfoDouble(_Symbol,SYMBOL_BID)) * 5 / _Point);
      
      ENUM_ORDER_TYPE_FILLING orderFillingType = 0;
      
      int nFilling = (int) SymbolInfoInteger( _Symbol, SYMBOL_FILLING_MODE ); // Get possible filling policy types
      if( ( nFilling & SYMBOL_FILLING_FOK ) == SYMBOL_FILLING_FOK ) // Decide on filling policy type
         orderFillingType = ORDER_FILLING_FOK;
      else
      {
         if( ( nFilling & SYMBOL_FILLING_IOC ) == SYMBOL_FILLING_IOC )
            orderFillingType = ORDER_FILLING_IOC;
      };
      
      MqlTradeRequest request={};
      MqlTradeResult result={};
      request.action = TRADE_ACTION_DEAL;// Market
      request.magic = MagicNumber;
      request.symbol = _Symbol;
      request.volume = lotSize;
      request.sl = slPrice;
      request.tp = tpPrice;
      request.type = orderType;
      request.price = op;
      request.deviation = slippage;
      request.comment = StringFormat("%.2f", slPrice) + "/" + slSize;
      request.type_filling = orderFillingType;
      
      if(OrderSend(request,result));
      else
      {
         Alert(StringFormat("%s error %u: %s. Retcode %u: %s. %u deal=%I64u  order=%I64u",__FUNCTION__, _LastError, errortext(_LastError), result.retcode, retcodeText(result.retcode), result.deal,result.order));
         PlaySound("expert.wav");
         return false;
      }
   }
   if(_LastError != 0) Print(__FUNCTION__ + " error: " + (string)_LastError + " " + errortext(_LastError));
   PlaySound("news.wav");
   
   return true;
}
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Price Constants - Indicator Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5