What's wrong with this code ?!!!

 

Hi

I've written a very simple code to buy if a cross between MAs occurs.  But the error is "Invalid order type". All the parameters could be found like ask and Bstl but it could not open position.


this is m_Trade

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

can u help me please.

void  MACross_Class::CheckBuy()

  {


    if(MA_Fast_1 <= MA_Slow_1 && MA_Fast_0+20*_Point >= MA_Slow_0)
      {
  
          double  ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);

         double   Bstl = NormalizeDouble(ask - (_StopLoss*_Point),_Digits); // S.L
         double   Btkp = NormalizeDouble(ask + (_TakeProfit*_Point),_Digits); // Take profit

         m_Trade.Buy(_Lots,_symbol,ask,Bstl,Btkp,"Buy Position");
       

      }


  }
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...
 

1) check the use of ask and bid. buys you open at Ask, and close at bid.

2) you do not show any definition of _symbol -- so I can only make a guess that this is NOT defined correctly.

EDIT: try changing the _symbol to _Symbol. See if that fixes it. After -- you have fixed the ask and bid.
 

Hi

Check also the Lots value you use perhaps this is some wrong number not allowed by the broker. Also you can use _Symbol instead of this _symbol you defined you use current ask price from current symbol so the name of the symbol for Buy should also be _Symbol (current chart) not some other value. To be perfectly correct you should also check if you can place order with those sl/tp levels checking stoplevel and freeze level or check required margin for the lots you used, but those are not so important now when you have invalid type order issue. But remember about this in the future.

Have a nice day👍📊