EA on 3 pairs - magical problems

 

Hi,

I finished my script and launched it on 3 pairs on demo account to see how it would work.

I loaded same script except just changed the magic number, so trades don't get mixed.

Immediately the script should have opened a trade on each of the pair (so I should have 3 live trades going), but instead it only opened one and that one was messed up. I tried it again and again, same problem.


I think the problem is inthis:

if(OrdersHistoryTotal()==0 && hour1==true)      //when there are no orders in history we enter buy

How do I make that part of code Magic Number specific? So that the script doesn't just check whatever is in history but looks for the trades with the magic number which are on it?

Thank you guys!

 
if(OrdersHistoryTotal()==0 ...

What if you closed an order last week ? It will be in the history.

If you want to check if an order with your magic number is in the history use OrderSelect() with MODE_HISTORY in a loop to look at each order in the history for one with your magic number.

 
Thank you SDC that's what I need
 

Guys,

I been breaking my head over this, I'm a bit conused. I don't think Magic Numbers is what I need. Could you please just point me in the right direction (I'l code it myself but I don't know what approach to take):


Basically I have 1 EA which I plan to run on 15 pairs.

The EA works in such a way that it looks at what trades have already been done (i.e. up to last 30 trades, it starts with the last trade and checks it, then it goes to i-2, i-3, etc) (OderHistory) and then based on that it decides what to do next.

The EA's parameters will change based on what pair I run it on (eg: tp/sl will change etc).

Question:

What is the best approach to use so that the EA actually works on multiple pairs? (i'l code it myself but if you could at least point me somewhere please)

 
niko:

What is the best approach to use so that the EA actually works on multiple pairs? (i'l code it myself but if you could at least point me somewhere please)


It is difficult to offer any advice with so little information and such a general question.
 
niko:

Guys,

I been breaking my head over this, I'm a bit conused. I don't think Magic Numbers is what I need. Could you please just point me in the right direction (I'l code it myself but I don't know what approach to take):


Basically I have 1 EA which I plan to run on 15 pairs.

The EA works in such a way that it looks at what trades have already been done (i.e. up to last 30 trades, it starts with the last trade and checks it, then it goes to i-2, i-3, etc) (OderHistory) and then based on that it decides what to do next.

The EA's parameters will change based on what pair I run it on (eg: tp/sl will change etc).

Question:

What is the best approach to use so that the EA actually works on multiple pairs? (i'l code it myself but if you could at least point me somewhere please)

Show us the code and someone will help you if the needed help is not too much of the trouble.
 

Thanks guys,

I made the code myself hence it might not be up to best conventions (I knew no programming till just a few months ago). I will add the getlasterror part to it later as well.

It needs to be tailored to each pair.

I've read various ways to make a code multi-pair on this forum (eg: 1) attach it to a chart, 2) code into the code multiple pair execution, 3) create a separate file).

I would rather make it 'attachable to the chart'.

But as you see the part I get stuck with is 'OrdersHistory' as I'm confused how to make that part work if I attach it to any chart.

Here it is:

ps: any other tips on how to improve this code (so it runs more efficiently, to avoid future errors, etc) I would greatly appreciate too!

//+------------------------------------------------------------------+
//|                                                   mm3byone  .mq4 |
//|                                                          Nikolai |
//|                                                          nikolai |
//+------------------------------------------------------------------+
#property copyright "Nikolai"
#property link      "nikolai"

double pips;
extern int TakeProfit = 24;
extern int StopLoss = 20;

extern double LotSize= 0.1;

extern double Loss1=2;
extern double Win1=0.5;
extern double Loss2=2;
extern double Win2=0.5;


extern int MagicNumber = 777;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if (ticksize == 0.00001 || ticksize == 0.001)
   pips = ticksize*10;
   else pips =ticksize;   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
if(OrdersHistoryTotal()==0)
   {
      if(OrdersTotal()==0)
      buy();
   }
   
   if (last1orderloss()==true && last2orderloss()==true && last3orderloss()==false) 
   {
   if (lastorderbuy()==false)
   buy2();
   
   else
   
   sell2();
   } 
   
if (last1orderloss()==true && last2orderloss()==false) 
   {
   if (lastorderbuy()==false)
   buy1();
   
   else
   
   sell1();
   } 
   
   if (last1orderwin()==true && last2orderwin()==true && last3orderwin()==false)
   {
   if (lastorderbuy()==false)
   buy22();
   
   else
   
   sell22();
   }

if (last1orderwin()==true && last2orderwin()==false)
   {
   if (lastorderbuy()==false)
   buy11();
   
   else
   
   sell11();
   }
   
   else
   
   if (lastorderbuy()==false)
   {
   buy();
   }
  
  
   
   sell();
  
  


//----
   return(0);
  }
//+------------------------------------------------------------------+
bool lastorderbuy()
{
   int i1=OrdersHistoryTotal()-1;
   if(OrderSelect(i1,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderType()==OP_BUY)
   
   return (true);
}


//+------------------------------------------------------------------+
bool last1orderloss()
{
   int i1=OrdersHistoryTotal()-1;
   if(OrderSelect(i1,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderClosePrice()==OrderStopLoss())
      
   return (true);
}
  
bool last2orderloss()
{
   int i2=OrdersHistoryTotal()-2;
   if(OrderSelect(i2,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderClosePrice()==OrderStopLoss())
      
   return (true);
}

bool last3orderloss()
{
   int i3=OrdersHistoryTotal()-3;
   if(OrderSelect(i3,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderClosePrice()==OrderStopLoss())
      
   return (true);
}

bool last1orderwin()
{
   int i1=OrdersHistoryTotal()-1;
   if(OrderSelect(i1,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderClosePrice()==OrderTakeProfit())
      
   return (true);
}

bool last2orderwin()
{
   int i2=OrdersHistoryTotal()-2;
   if(OrderSelect(i2,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderClosePrice()==OrderTakeProfit())
      
   return (true);
}

bool last3orderwin()
{
   int i3=OrdersHistoryTotal()-3;
   if(OrderSelect(i3,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderClosePrice()==OrderTakeProfit())
      
   return (true);
}

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

bool buy()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-StopLoss*pips,Ask+TakeProfit*pips,NULL,MagicNumber,0,Green);
GetLastError();
return(true);
}   

bool sell()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+StopLoss*pips,Bid-TakeProfit*pips,NULL,MagicNumber,0,Red);
GetLastError();
return(true);
}  
//+-----------------------------------------------------------------------------------------------------------+//

bool buy1()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_BUY,LotSize*Loss1,Ask,3,Ask-StopLoss*pips,Ask+TakeProfit*pips,NULL,MagicNumber,0,Green);
GetLastError();
return(true);
}

bool sell1()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_SELL,LotSize*Loss1,Bid,3,Bid+StopLoss*pips,Bid-TakeProfit*pips,NULL,MagicNumber,0,Red);
GetLastError();
return(true);
}  

//+-----------------------------------------------------------------------------------------------------+//
bool buy2()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_BUY,LotSize*Loss2,Ask,3,Ask-StopLoss*pips,Ask+TakeProfit*pips,NULL,MagicNumber,0,Green);
GetLastError();
return(true);
}

bool sell2()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_SELL,LotSize*Loss2,Bid,3,Bid+StopLoss*pips,Bid-TakeProfit*pips,NULL,MagicNumber,0,Red);
GetLastError();
return(true);
}  

//+-----------------------------------------------------------------------------------------------------+//
bool buy22()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_BUY,LotSize*Win2,Ask,3,Ask-StopLoss*pips,Ask+TakeProfit*pips,NULL,MagicNumber,0,Green);
GetLastError();
return(true);
}

bool sell22()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_SELL,LotSize*Win2,Bid,3,Bid+StopLoss*pips,Bid-TakeProfit*pips,NULL,MagicNumber,0,Red);
GetLastError();
return(true);
}  

//+-----------------------------------------------------------------------------------------------------+//
bool buy11()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_BUY,LotSize*Win1,Ask,3,Ask-StopLoss*pips,Ask+TakeProfit*pips,NULL,MagicNumber,0,Green);
GetLastError();
return(true);
}

bool sell11()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_SELL,LotSize*Win1,Bid,3,Bid+StopLoss*pips,Bid-TakeProfit*pips,NULL,MagicNumber,0,Red);
GetLastError();
return(true);
}  

//+-----------------------------------------------------------------------------------------------------+//
 
*ps: it works if I attach it to a single chart, it doesn't work when I attach it to multiple charts
 
niko:
*ps: it works if I attach it to a single chart, it doesn't work when I attach it to multiple charts
//+------------------------------------------------------------------+
//|                                                   mm3byone  .mq4 |
//|                                                          Nikolai |
//|                                                          nikolai |
//+------------------------------------------------------------------+
#property copyright "Nikolai"
#property link      "nikolai"

double pips;
extern int TakeProfit = 24;
extern int StopLoss = 20;

extern double LotSize= 0.1;

extern double Loss1=2;
extern double Win1=0.5;
extern double Loss2=2;
extern double Win2=0.5;


extern int MagicNumber = 777;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if (ticksize == 0.00001 || ticksize == 0.001)
   pips = ticksize*10;
   else pips =ticksize;   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
if(OrdersHistoryTotal()==0)
   {
      if(OrdersTotal()==0)
      buy();
   }
   
   if (last1orderloss()==true && last2orderloss()==true && last3orderloss()==false) 
   {
   if (lastorderbuy()==false)
   buy2();
   
   else
   
   sell2();
   } 
   
if (last1orderloss()==true && last2orderloss()==false) 
   {
   if (lastorderbuy()==false)
   buy1();
   
   else
   
   sell1();
   } 
   
   if (last1orderwin()==true && last2orderwin()==true && last3orderwin()==false)
   {
   if (lastorderbuy()==false)
   buy22();
   
   else
   
   sell22();
   }

if (last1orderwin()==true && last2orderwin()==false)
   {
   if (lastorderbuy()==false)
   buy11();
   
   else
   
   sell11();
   }
   
   else
   
   if (lastorderbuy()==false)
   {
   buy();
   }
  
  
   
   sell();
  
  


//----
   return(0);
  }
//+------------------------------------------------------------------+
bool lastorderbuy()
{
   int i1=OrdersHistoryTotal()-1;
   if(OrderSelect(i1,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderType()==OP_BUY)
   
   return (true);
}


//+------------------------------------------------------------------+
bool last1orderloss()
{
   int i1=OrdersHistoryTotal()-1;
   if(OrderSelect(i1,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderClosePrice()==OrderStopLoss())
      
   return (true);
}
  
bool last2orderloss()
{
   int i2=OrdersHistoryTotal()-2;
   if(OrderSelect(i2,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderClosePrice()==OrderStopLoss())
      
   return (true);
}

bool last3orderloss()
{
   int i3=OrdersHistoryTotal()-3;
   if(OrderSelect(i3,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderClosePrice()==OrderStopLoss())
      
   return (true);
}

bool last1orderwin()
{
   int i1=OrdersHistoryTotal()-1;
   if(OrderSelect(i1,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderClosePrice()==OrderTakeProfit())
      
   return (true);
}

bool last2orderwin()
{
   int i2=OrdersHistoryTotal()-2;
   if(OrderSelect(i2,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderClosePrice()==OrderTakeProfit())
      
   return (true);
}

bool last3orderwin()
{
   int i3=OrdersHistoryTotal()-3;
   if(OrderSelect(i3,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderClosePrice()==OrderTakeProfit())
      
   return (true);
}

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

bool buy()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-StopLoss*pips,Ask+TakeProfit*pips,NULL,MagicNumber,0,Green);
GetLastError();
return(true);
}   

bool sell()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+StopLoss*pips,Bid-TakeProfit*pips,NULL,MagicNumber,0,Red);
GetLastError();
return(true);
}  
//+-----------------------------------------------------------------------------------------------------------+//

bool buy1()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_BUY,LotSize*Loss1,Ask,3,Ask-StopLoss*pips,Ask+TakeProfit*pips,NULL,MagicNumber,0,Green);
GetLastError();
return(true);
}

bool sell1()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_SELL,LotSize*Loss1,Bid,3,Bid+StopLoss*pips,Bid-TakeProfit*pips,NULL,MagicNumber,0,Red);
GetLastError();
return(true);
}  

//+-----------------------------------------------------------------------------------------------------+//
bool buy2()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_BUY,LotSize*Loss2,Ask,3,Ask-StopLoss*pips,Ask+TakeProfit*pips,NULL,MagicNumber,0,Green);
GetLastError();
return(true);
}

bool sell2()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_SELL,LotSize*Loss2,Bid,3,Bid+StopLoss*pips,Bid-TakeProfit*pips,NULL,MagicNumber,0,Red);
GetLastError();
return(true);
}  

//+-----------------------------------------------------------------------------------------------------+//
bool buy22()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_BUY,LotSize*Win2,Ask,3,Ask-StopLoss*pips,Ask+TakeProfit*pips,NULL,MagicNumber,0,Green);
GetLastError();
return(true);
}

bool sell22()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_SELL,LotSize*Win2,Bid,3,Bid+StopLoss*pips,Bid-TakeProfit*pips,NULL,MagicNumber,0,Red);
GetLastError();
return(true);
}  

//+-----------------------------------------------------------------------------------------------------+//
bool buy11()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_BUY,LotSize*Win1,Ask,3,Ask-StopLoss*pips,Ask+TakeProfit*pips,NULL,MagicNumber,0,Green);
GetLastError();
return(true);
}

bool sell11()
{
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_SELL,LotSize*Win1,Bid,3,Bid+StopLoss*pips,Bid-TakeProfit*pips,NULL,MagicNumber,0,Red);
GetLastError();
return(true);
}  

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

do you see why ??

 
I think I know what is going on because it seems it is like what I have been through last time. Still, let the senior show us their magic.
 

But if I change that to if(OrdersTotal()==2 (or more), it will still not work properly?

Because these bits:

bool last1orderwin()
{
   int i1=OrdersHistoryTotal()-1;
   if(OrderSelect(i1,SELECT_BY_POS,MODE_HISTORY)==true)
   if (OrderClosePrice()==OrderTakeProfit())
      

Needs to be specific to the symbol in question (this is my assumption, I don't know, just asking)?

Those bits just look into the general 'history pool', without checking for specific currency pair in there, I think that's the problem (and thats what I don't know how to solve)?

Reason: