Only Sell EA executing both BUY and SELL positions. PLEASE HELP!!!

Vengeance Seeker  

I've created an EA that uses the 3 and 13 EMA to ONLY execute SELL positions. When the 3 crosses bellow the 13, a sell position must be executed. When the 3 crosses above the 13, all positions of the CURRENT symbol should be closed. But my EA is opening both BUY and SELL positions - I'm struggling to understand why.

To those who viewed my first attempt at an EA - I hope this is an improvement :)

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

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   double Balance = AccountInfoDouble(ACCOUNT_BALANCE);
   double Equity = AccountInfoDouble(ACCOUNT_EQUITY);
//+----------------------------------------------------------------+  
//--- EMA cross over section                                       |
//+----------------------------------------------------------------+

//--- create first array
   double myMovingAverageArray1[],myMovingAverageArray2[];
   
//--- Define first EMA
   int movingAverageDefinition1 = iMA(_Symbol,_Period,3,0,MODE_EMA,PRICE_CLOSE);
   
//--- Define second EMA
   int movingAverageDefinition2 = iMA(_Symbol,_Period,13,0,MODE_EMA,PRICE_CLOSE);
   
//--- Sort price array1 from the current candle downwards
   ArraySetAsSeries(myMovingAverageArray1,true);
   
//--- Sort price arrayw from the current candle downwards
   ArraySetAsSeries(myMovingAverageArray2,true);
 
//--- Defined MA1, one line, current candle, 3 candles, store result.
   CopyBuffer(movingAverageDefinition1,0,0,3,myMovingAverageArray1);
   
//--- Defined MA2, one line, current candle, 3 candles, store result.
   CopyBuffer(movingAverageDefinition2,0,0,3,myMovingAverageArray2);
   
//--- check if the 3 EMA is above the 13 EMA 
   if((myMovingAverageArray1[0]<myMovingAverageArray2[0])
     && (myMovingAverageArray1[1]>myMovingAverageArray2[1]))
     trade.Sell(0.5,NULL,Bid,0,(Bid-50* _Point),NULL);
      
   if((myMovingAverageArray1[0]>myMovingAverageArray2[0])
     && (myMovingAverageArray1[1]<myMovingAverageArray2[1]))
     {
      CloseAllPositions();
     }
     
  }
  
 void CloseAllPositions()
   {
   for(int i=PositionsTotal()-1; i>=0;i--)
      {
       int ticket = PositionGetTicket(i);
       
       trade.PositionClose(i);
      }
   }
William Roeder  
void OnTick(){
   int movingAverageDefinition1 = iMA(_Symbol,_Period,3,0,MODE_EMA,PRICE_CLOSE);
   int movingAverageDefinition2 = iMA(_Symbol,_Period,13,0,MODE_EMA,PRICE_CLOSE);

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

Keith Watford  

Vengeance Seeker:

But my EA is opening both BUY and SELL positions - I'm struggling to understand why.

Are you confusing the closure of a sell position with a buy for a new buy position?

Vengeance Seeker  
William Roeder #:

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

Thank you. I implemented the code you gave me. The tester is still showing filled buy positions. Maybe I'm interpreting the tester wrong. Maybe its showing the closure of the sell. Like I've said before - I'm new to this side of MetaTrader.
Keith Watford  
Vengeance Seeker #:
I don't know maybe I'm interpreting the tester the wrong way. But its showing filled Buy positions there. Does the code look fine tho? I don't understand what that William is telling me to do 

I can't comment much about your code as you use

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

and I don't use that.

I can only say that you should create the handles for the indicators in OnInit as William has already pointed out.

In MT5 when you open a sell (In)

when it is closed it is closed by opening a buy(Out) and this may be confusing you.

I know that I found it a bit confusing when I started with MQL5 after coding with the much more user friendly MQL4.

Carl Schreiber  
Vengeance Seeker:

I've created an EA that uses the 3 and 13 EMA to ONLY execute SELL positions. When the 3 crosses bellow the 13, a sell position must be executed. When the 3 crosses above the 13, all positions of the CURRENT symbol should be closed. But my EA is opening both BUY and SELL positions - I'm struggling to understand why.

To those who viewed my first attempt at an EA - I hope this is an improvement :)

If your EA doesn't what you expect use the debugger to check the processing:

https://www.metatrader5.com/en/metaeditor/help/development/debug
https://www.mql5.com/en/articles/654
https://www.mql5.com/en/articles/35
https://www.mql5.com/en/articles/2041
https://www.mql5.com/en/articles/272
https://www.mql5.com/en/articles/150

You can solve your problems a lot faster :)

Code-Debugging - Programme entwickeln - MetaEditor Hilfe
  • www.metatrader5.com
MetaEditor hat einen eingebauten Debugger, mit dem Sie die Programmausführung Schritt für Schritt (durch einzelne Funktionen) ü...
Vengeance Seeker  
Keith Watford #:

I can't comment much about your code as you use

and I don't use that.

I can only say that you should create the handles for the indicators in OnInit as William has already pointed out.

In MT5 when you open a sell (In)

when it is closed it is closed by opening a buy(Out) and this may be confusing you.

I know that I found it a bit confusing when I started with MQL5 after coding with the much more user friendly MQL4.

Oooh! Now it makes sense! Thank you so much. The code runs perfectly then. Its profitable too. 

Just one last thing. 
A Sell stop loss is Bid-10 _Point or is it Bid+10_Point
Carl Schreiber  
Vengeance Seeker #:
Oooh! Now it makes sense! Thank you so much. The code runs perfectly then. Its profitable too. 

Just one last thing. 
A Sell stop loss is Bid-10 _Point or is it Bid+10_Point

You buy at Ask and close it (=sell) at Bid.

You sell at Bid and close it (=buy) at Ask.

For a sell SL is always above Ask+10.0*_Point (>min. distance) otherwise you'll get the error Invalid Stop.

Vengeance Seeker  
Carl Schreiber #:

You buy at Ask and close it (=sell) at Bid.

You sell at Bid and close it (=buy) at Ask.

For a sell SL is always above Ask+10.0*_Point (>min. distance) otherwise you'll get the error Invalid Stop.

Thanks. I  really appreciate it.
Reason: