Ordersend() not working as expected - page 2

 

did some rework on your code so please try this one....not tested ;-)

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
extern double TakeProfit=5.5;//Take in pips not points
extern double StopLoss=4.0;//Stop in pips not points
extern int FastEma=5;
extern int SlowEma=21;
extern double LotSize=0.1;
extern int MagicNumber=1234567;
double pips;
double ticksize;
int ticket,digits;



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   ticksize = MarketInfo(Symbol(),MODE_POINT);
   digits=(int)MarketInfo(Symbol(),MODE_DIGITS);

   if((digits==5||digits==3))
      pips = ticksize*10;
   else
      pips=ticksize;

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(CheckPos()==0)
      Signal();
   return;
  }
//---
void Signal()
  {
   double PreviousFast = iMA(Symbol(), 0, FastEma, 0, MODE_EMA, PRICE_CLOSE, 2),
          CurrentFast = iMA(Symbol(), 0, FastEma, 0, MODE_EMA, PRICE_CLOSE, 1),
          PreviousSlow = iMA(Symbol(), 0, SlowEma, 0, MODE_EMA, PRICE_CLOSE, 2),
          CurrentSlow = iMA(Symbol(), 0, SlowEma, 0, MODE_EMA, PRICE_CLOSE, 1);
   if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow)
      ticket=OrderSend("NZDUSD",OP_BUY,LotSize,Ask,10,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),"Myorder",MagicNumber,0,Green);
   Print("stoploss-BuyOrder: ",Ask-(StopLoss*pips));
   Print("Ask in BuyOrder: ",Ask);
   if(ticket<1)
     {
      Print("Error opening SELL order : ",GetLastError());
      return;
     }
   else
      Print("BUY order opened : ",DoubleToStr(OrderOpenPrice(),Digits));


   if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow)
      ticket=OrderSend("NZDUSD",OP_SELL,LotSize,Bid,10,Bid+(StopLoss*pips),Bid-(TakeProfit*pips),"Mysorder",MagicNumber,0,Red);
   Print("stoploss-SellOrder: ",Bid+(StopLoss*pips));
   Print("Bid in SellOrder: ",Bid);
   if(ticket<1)
     {
      Print("Error opening SELL order : ",GetLastError());
      return;
     }
   else
      Print("SELL order opened : ",DoubleToStr(OrderOpenPrice(),Digits));
  }
//+------------------------------------------------------------------+
int CheckPos()
  {
   int pos=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 && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         if(OrderType() == OP_BUY)
            pos = 1; //Long position
         if(OrderType() == OP_SELL)
            pos = -1; //Short positon
        }
     }
   return(pos);
  }
//+------------------------------------------------------------------+
 
Kenneth Parling:

did some rework on your code so please try this one....not tested ;-)

This has the same errors that I pointed out in a previous post

   double PreviousFast = iMA(Symbol(), 0, FastEma, 0, MODE_EMA, PRICE_CLOSE, 2),
          CurrentFast = iMA(Symbol(), 0, FastEma, 0, MODE_EMA, PRICE_CLOSE, 1),
          PreviousSlow = iMA(Symbol(), 0, SlowEma, 0, MODE_EMA, PRICE_CLOSE, 2),
          CurrentSlow = iMA(Symbol(), 0, SlowEma, 0, MODE_EMA, PRICE_CLOSE, 1);
   if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow)
      ticket=OrderSend("NZDUSD",OP_BUY,LotSize,Ask,10,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),"Myorder",MagicNumber,0,Green);

   //The following code is executed regardless of whether there has been an attempt to open an order.
   Print("stoploss-BuyOrder: ",Ask-(StopLoss*pips));
   Print("Ask in BuyOrder: ",Ask);
   if(ticket<1)
     {
      Print("Error opening SELL order : ",GetLastError());
      return;
     }
   else
      Print("BUY order opened : ",DoubleToStr(OrderOpenPrice(),Digits));
 
Keith Watford:

This has the same errors that I pointed out in a previous post

did not work on those small type errors and it's easy to retype with correct corresponding order type and they do not impact on the OrderSend functions just the printouts, mainly i did a Signal function and a position check and that's the main important functions i had in mind. Here i try to help and instead just complains.....well well have a nice day

 
Keith Watford:

First the obvious stuff, you are missing {} (I have put them in and highlighted them). This means that you will get the error reports even when there was no attempt to open an order. That is why last error is 0.

Also you are trying to open a buy and then reporting that a sell has opened

Thanks a ton Keith! It was the missing braces under the if loop. I tested in a short time frame and was able to get it working. 

Thanks everyone for helping me out. Appreciate your time and effort.

Regards,

Aj

 
Kenneth Parling:

did not work on those small type errors and it's easy to retype with correct corresponding order type and they do not impact on the OrderSend functions just the printouts, mainly i did a Signal function and a position check and that's the main important functions i had in mind. Here i try to help and instead just complains.....well well have a nice day

I am sorry that you think that I was complaining. I wasn't, I have no reason to.

My comment was intended more for the OP and just to point out that the errors that I had referred to previously had not been fixed in your code.

Reason: