Help a newb please!

 

Ok so I stared experimenting with EA's and testing and so on.

I understand the basics of programming and realize that if this was written in OOP style it would be a bit easier to navigate. However, I just wanted to start out and see if I could get an EA going with all the parameters I'm after. It's been a ride.

The following code works for everything using 2 EMA cross as a buy signal. However, I've been fighting with the hedging portion of the EA and trying to close a position. Going back and forth I finally get a invalid fill 10030 code when attempting to close the position for a reversal cross of the EMAs.


Please help and thank you


Error

2017.07.09 16:55:08.501    2016.02.19 02:00:59   failed market sell 0.10 EURCHF, close #8 buy 0.10 EURCHF 1.10166 [Unsupported filling mode]
2017.07.09 16:55:08.501    2016.02.19 02:00:59   CTrade::OrderSend: market sell 0.10 position #8 EURCHF [invalid fill]
2017.07.09 16:55:08.501    2016.02.19 02:00:59   PositionClose() method failed. Return code=10030. Code description: invalid fill

:


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


input int      SL=450;
input int      TP=750;
input int      EMASlowPeriod=48;
input int      EMAFastPeriod=13;
input int      Magic_Number=12345;
input double   Lot=0.1;

int slowMAhandle;
int fastMAhandle;
double fEMA[];
double sEMA[];
int STPL;
int TKPFT;
double barclose;
double Ask;
bool buyCondition_1;
bool buyCondition_2;
bool sellCondition;


int OnInit()
  {

//--- order filling mode, the mode allowed by the server should be used
   trade.SetTypeFilling(ORDER_FILLING_IOC);


   return(0);
  }

void OnTick()
  {
  
  slowMAhandle=iMA(_Symbol,_Period,EMASlowPeriod,0,MODE_EMA,PRICE_CLOSE);
  
  fastMAhandle=iMA(_Symbol,_Period,EMAFastPeriod,0,MODE_EMA,PRICE_CLOSE);
   
   
    if(slowMAhandle<0 || fastMAhandle<0)
      {
  
    Alert("Error Creating Handles For Inidicators - Error:",GetLastError(),"!!");
      }


   
   ArraySetAsSeries(sEMA,true);

   ArraySetAsSeries(fEMA,true);
   

   CopyBuffer(fastMAhandle,0,0,3,fEMA);
   CopyBuffer (slowMAhandle,0,0,3,sEMA);
   
   double fastMAhandle=fEMA[0];
   double slowMAhandle=sEMA[0];

   Comment ("Fast EMA: ",fastMAhandle,""
   "Slow EMA: ",slowMAhandle);
   
   Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   
   buyCondition_1 = (fEMA[0] > sEMA[0]);
   buyCondition_2 = (fEMA[1]<=sEMA[1]);
   sellCondition = (fEMA[0]<sEMA[0]) && (fEMA[1]>=sEMA[1]);
   

   
   if(PositionsTotal()<=0)
      {
      if(buyCondition_1 && buyCondition_2)
        {
               
          if(!trade.Buy(Lot, NULL, Ask,(Ask-SL * _Point),(Ask+TP * _Point),NULL)) //Request is completed or order placed
           {
            Print("Trade Failed",trade.ResultRetcode(),". Error:",trade.ResultRetcodeDescription());
           }
          else
           {
            Print("Trade Successful")   ;     
            return;
       }
    } 
}



      if(sellCondition)
         {
         if(!trade.PositionClose(_Symbol))
            {//--- failure message
      Print("PositionClose() method failed. Return code=",trade.ResultRetcode(),
            ". Code description: ",trade.ResultRetcodeDescription());
     }
   else
     {
      Print("PositionClose() method executed successfully. Return code=",trade.ResultRetcode(),
            " (",trade.ResultRetcodeDescription(),")");
     }
         

}

}
 

Try this option:

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
...
   if(IsFillingTypeAllowed(Symbol(),SYMBOL_FILLING_FOK))
      m_trade.SetTypeFilling(ORDER_FILLING_FOK);
   else if(IsFillingTypeAllowed(Symbol(),SYMBOL_FILLING_IOC))
      m_trade.SetTypeFilling(ORDER_FILLING_IOC);
   else
      m_trade.SetTypeFilling(ORDER_FILLING_RETURN);
...
//+------------------------------------------------------------------+ 
//| Checks if the specified filling mode is allowed                  | 
//+------------------------------------------------------------------+ 
bool IsFillingTypeAllowed(string symbol,int fill_type)
  {
//--- Obtain the value of the property that describes allowed filling modes 
   int filling=(int)SymbolInfoInteger(symbol,SYMBOL_FILLING_MODE);
//--- Return true, if mode fill_type is allowed 
   return((filling & fill_type)==fill_type);
  }
 
Vladimir Karputov:

Try this option:

Wow, I'd been looking for a forum post to describe setting the function automatically! Thanks so much!