EA opens Many Trades, How to Open 1 buy & 1 sell?

 

hi Im very new mql5

**EA opens Many Trades, How to Open 1 buy & 1 sell

  I would like to open 1 buy & 1 sell. For some reason my EA opens many trades**
**here is my code**


***

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
burcu kıratlı:

hi Im very new mql5

**EA opens Many Trades, How to Open 1 buy & 1 sell

  I would like to open 1 buy & 1 sell. For some reason my EA opens many trades**
**here is my code**


***

Please insert the code correctly: when editing a message, press the button     Codeand paste your code into the pop-up window 
 
Vladimir Karputov #:
Please insert the code correctly: when editing a message, press the button     and paste your code into the pop-up window 
**EA opens Many Trades, How to Open 1 buy & 1 sell
  I would like to open 1 buy & 1 sell. For some reason my EA opens many trades**
**here is my code**

 
#property copyright "eMEK"
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\AccountInfo.mqh>

//--- variable
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
CSymbolInfo    m_symbol;                     // symbol info object
CAccountInfo   m_account;                    // account info wrapper

datetime time=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

   m_trade.SetExpertMagicNumber(13324);
   m_trade.SetDeviationInPoints(10);

//---
   return(INIT_SUCCEEDED);
  }



//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }



//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

   ema13_and_5();

  }

//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void PRINT()
  {

   Print("HELLO");

  }

//----------------------------------------------------------------------


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void ema13()
  {

//****
// EMA(13)

   ENUM_MA_METHOD     MAmethod = MODE_EMA;

   ENUM_APPLIED_PRICE MAprice=PRICE_TYPICAL;

   int h_ma13 = iMA(Symbol(),PERIOD_CURRENT,13,0,MAmethod,MAprice);

//****

   double  MA13Buffer[];

   SetIndexBuffer(0,MA13Buffer,INDICATOR_DATA);

   CopyBuffer(h_ma13,0,0,3,MA13Buffer);

//****


   double MA15_sinyal0= MA13Buffer[2]; //last bar 0
   double MA15_sinyal1= MA13Buffer[1]; //last bar 1
   double MA15_sinyal2= MA13Buffer[0]; //last bar 2

//Print("MA15_sinyal0:"+MA15_sinyal0);
//Print("MA15_sinyal1:"+MA15_sinyal1);
//Print("MA15_sinyal2:"+MA15_sinyal2);



  }



///*************

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void ema13_and_5()
  {

//****
// EMA(13)

   ENUM_MA_METHOD     MAmethod = MODE_EMA;

   ENUM_APPLIED_PRICE MAprice=PRICE_TYPICAL;

   int h_ma13 = iMA(Symbol(),PERIOD_CURRENT,13,200,MAmethod,MAprice);

//****

   double  MA13Buffer[];
   ArraySetAsSeries(MA13Buffer,true);
   SetIndexBuffer(0,MA13Buffer,INDICATOR_DATA);

   CopyBuffer(h_ma13,0,0,3,MA13Buffer);

//****

   double MA13_sinyal0= MA13Buffer[0]; //last bar 0
   double MA13_sinyal1= MA13Buffer[1]; //last bar 1
   double MA13_sinyal2= MA13Buffer[2]; //last bar 2

//Print("MA15_sinyal0:"+MA15_sinyal0);
//Print("MA15_sinyal1:"+MA15_sinyal1);
//Print("MA15_sinyal2:"+MA15_sinyal2);



//****
// EMA(5)

   int h_ma5 = iMA(Symbol(),PERIOD_CURRENT,5,50,MAmethod,MAprice);

//****

   double  MA5Buffer[];
   ArraySetAsSeries(MA5Buffer,true);
   SetIndexBuffer(0,MA5Buffer,INDICATOR_DATA);

   CopyBuffer(h_ma5,0,0,3,MA5Buffer);

//****


   double MA5_sinyal0= MA5Buffer[0]; //last bar 0
   double MA5_sinyal1= MA5Buffer[1]; //last bar 1
   double MA5_sinyal2= MA5Buffer[2]; //last bar 2

   //Print("MA5_sinyal0:"+MA5_sinyal0);
   //Print("MA5_sinyal1:"+MA5_sinyal1);
   //Print("MA5_sinyal2:"+MA5_sinyal2);


//********
   

   if(MA5_sinyal1>MA13_sinyal1 &&  MA5_sinyal2<MA13_sinyal2 && time!=iTime(_Symbol,PERIOD_CURRENT,0))
     {
      // BUY
      time=iTime(_Symbol,PERIOD_CURRENT,0);
      double price=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      double stoploss=price-300*_Point; // 300 pts sl
      double takeprofit=price+300*_Point; // 300 pts tp
      m_trade.Buy(0.01,_Symbol,price,stoploss,takeprofit,"");
     }


   if(MA5_sinyal1<MA13_sinyal1 && MA5_sinyal2>MA13_sinyal2 && time!=iTime(_Symbol,PERIOD_CURRENT,0))
     {
      // SELL
      time=iTime(_Symbol,PERIOD_CURRENT,0);
      double price=SymbolInfoDouble(_Symbol,SYMBOL_BID);
      double stoploss=price+300*_Point; // 300 pts sl
      double takeprofit=price-300*_Point; // 300 pts tp
      m_trade.Sell(0.01,_Symbol,price,stoploss,takeprofit,"");
     }



//********

  }
  
 

You have a huge mistake - you create an indicator handle at every tick.

REMEMBER: The MQL5 style implies that the indicator handle MUST be created ONLY ONCE and this must be done in OnInit !!!

 
How to start with MQL5
How to start with MQL5
  • 2020.03.05
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 
thank you are you able to solve the mistake I'm very confused now.
 
burcu kıratlı #:
thank you are you able to solve the mistake I'm very confused now.

iRSI simple advisor

(code: iRSI simple advisor)

Advisor works only on the new bar. The signal to open a position BUY: rsi on bar # 1> 'RSI Level UP'. Signal to open a SELL: rsi position on bar # 1 <'RSI Level DOWN'.

iRSI simple advisor

How to start with MQL5
How to start with MQL5
  • 2020.03.05
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 
either with time or with closing values.
closing values are prefered, easy to manipulate, create a global saveVar=0 and then a tickcheck to compare saveVar != from lastClosing
Then inside brackets saveVar = LasClosing
Thats it, keep it simple.
 
Pablo Jaguanharo Carvalho Pinheiro #:
either with time or with closing values.
closing values are prefered, easy to manipulate, create a global saveVar=0 and then a tickcheck to compare saveVar != from lastClosing
Then inside brackets saveVar = LasClosing
Thats it, keep it simple.

hi Thank you for your comment are you able to apply on my code ?