how to open one position for each market pair? Can somebody help me with the code?

To add comments, please log in or register
Its_Victor
12
Its_Victor  

Hi there, Complete beginner here...
so a few weeks ago I'm trying to make a simple Crossover MA EA using MQL5, everything seems fine and I got no error message, but after I try it on a demo account, I start having a problem.

so I want to make my EA open only one position for each market/pair, but can open several positions if there is a signal on another pair.

for example, let's say it's open a buy position on EUR/USD, I don't want my EA to open the same position again on the same pair, instead, only open a new position if there is a signal on another market (for example USD/JPY).

For now, what my EA does is opening 5 different entry on the same signal, even when there is another signal on another pair.

here is the code, I hope you guys can help me out with this, Thanks, and sorry for bad English   

#include <Trade\Trade.mqh> 

//create an instance of CTrade
   CTrade trade;

input int SmallMovingAverage=100;
input int BigMovingAverage=200; 


void OnTick()
  {
   // calculate the ask price
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
  
   // calculate the Bid Price
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
      //create a string
      string signal="";
      
      //create an Array for several prices
      double SmallMovingAverageArray [],BigMovingAverageArray[]; 
      
      //define the properties of the Small Moving Average
      int SmallMovingAverageDefinition = iMA (_Symbol,_Period,SmallMovingAverage,0,MODE_SMA,PRICE_CLOSE);
       
       //define the properties of the Small Moving Average
      int BigMovingAverageDefinition = iMA (_Symbol,_Period,BigMovingAverage,0,MODE_SMA,PRICE_CLOSE);
 
      // Defined EA, one line current candle, 3 candle, store result
      CopyBuffer(SmallMovingAverageDefinition,0,0,3,SmallMovingAverageArray);
      
      // Defined EA, one line current candle, 3 candle, store result
      CopyBuffer(BigMovingAverageDefinition,0,0,3,BigMovingAverageArray);
      
      // if BigMovingAverage > SmallMovingAverage
      if (BigMovingAverageArray[1] > SmallMovingAverageArray[1])
      
      // if BigMovingAverage < SmallMovingAverage before
      if (BigMovingAverageArray[2] < SmallMovingAverageArray[2])
      {
       signal= "buy";
      }
      
      //if BigMovingAverage < SmallMovingAverage
      if (BigMovingAverageArray[1] < SmallMovingAverageArray[1])
      
      // if BigMovingAverage > SmallMovingAverage before
      if (BigMovingAverageArray[2] > SmallMovingAverageArray[2])
      {
       signal="sell";
      }
      
      // Trade Sell
      if (signal=="sell" && PositionsTotal()<5)
         trade.Sell(0.10,NULL,Bid,(Bid+250 *_Point),(Bid-250 *_Point),NULL);
     
     // Trade Buy
     if (signal=="buy" && PositionsTotal ()<5)
         trade.Buy(0.10,NULL,Ask,(Ask-250*_Point),(Ask+250 *_Point),NULL);
         
     // Chart Output
     Comment ("the signal is now:",signal);   
  }

Vladimir Karputov
Moderator
215421
Vladimir Karputov  

You make a grave mistake! You create an indicator handle AT EVERY TICK! The indicator handle HAS TO BE CREATED ONCE (the best place for this is OnInit).

An example of correctly creating handles: Creating an iMA indicator handle, getting indicator values

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...
Its_Victor
12
Its_Victor  
Vladimir Karputov:

You make a grave mistake! You create an indicator handle AT EVERY TICK! The indicator handle HAS TO BE CREATED ONCE (the best place for this is OnInit).

An example of correctly creating handles: Creating an iMA indicator handle, getting indicator values

ouch... my bad, so I just need to move all the code inside the OnTick?
Vladimir Karputov
Moderator
215421
Vladimir Karputov  
Its_Victor :
ой ... мой плохой, так что мне просто нужно переместить весь код внутри OnTick?

Check out my example first. The indicator handle MUST BE CREATED ONCE (in OnInit). And the trading functions are over, they are needed in OnTick.

The main thing to remember: the handle of the indicator SHOULD BE CREATED ONCE (in OnInit).

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
To add comments, please log in or register