Please Help w/ code for number of open trades per Pair

 

I am tring to switch this code, so I can have 2 EAs running. One on EUR/USD and the other on USD/CHF.

I would like it to check all open orders and have a max of 3 open order for each currency. Could someone please assist.

I think it would have to do with the "total" lines...but I cant make it work.

I am sitting here :

int start()
{
int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
// to simplify the coding and speed up access
// data are put into internal variables


double LongTrend1=iMA(NULL,0,20,0,0,0,1);
double MidTrend1=iMA(NULL,0,5,0,0,0,1);
double Control1=iMA(NULL,0,100,0,0,0,1);
double LongTrend2=iMA(NULL,0,20,0,0,0,2);
double MidTrend2=iMA(NULL,0,5,0,0,0,2);
double Control2=iMA(NULL,0,100,0,0,0,2);
double SARCurrent=iSAR(NULL,0,0.02,0.2,1);
double Stoch0 = iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_MAIN,0);
double Stoch1 = iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_MAIN,1);
double Stoch2 = iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_MAIN,2);
double SAR = iSAR(NULL,0,0.02,0.2,0);


total=OrdersTotal();
if(total<3)
{
// no opened orders identified
{
if(CheckEntryTime==iTime(NULL,TimeFrame,0)) return(0); else CheckEntryTime = iTime(NULL,TimeFrame,0);
}
if(AccountFreeMargin()<(1*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}

 
mtandk0614:

I am tring to switch this code, so I can have 2 EAs running. One on EUR/USD and the other on USD/CHF.

I would like it to check all open orders and have a max of 3 open order for each currency. Could someone please assist.

I think it would have to do with the "total" lines...but I cant make it work.

I am sitting here :

int start()
{
int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
// to simplify the coding and speed up access
// data are put into internal variables


double LongTrend1=iMA(NULL,0,20,0,0,0,1);
double MidTrend1=iMA(NULL,0,5,0,0,0,1);
double Control1=iMA(NULL,0,100,0,0,0,1);
double LongTrend2=iMA(NULL,0,20,0,0,0,2);
double MidTrend2=iMA(NULL,0,5,0,0,0,2);
double Control2=iMA(NULL,0,100,0,0,0,2);
double SARCurrent=iSAR(NULL,0,0.02,0.2,1);
double Stoch0 = iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_MAIN,0);
double Stoch1 = iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_MAIN,1);
double Stoch2 = iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_MAIN,2);
double SAR = iSAR(NULL,0,0.02,0.2,0);


total=OrdersTotal();
if(total<3)
{
// no opened orders identified
{
if(CheckEntryTime==iTime(NULL,TimeFrame,0)) return(0); else CheckEntryTime = iTime(NULL,TimeFrame,0);
}
if(AccountFreeMargin()<(1*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}

change your 3 in this section : if(total<3)...change it to MaxTrades so it looks like this : if(total<MaxTrades)


now put extern int MaxTrades = 3; at the top of your code with the other external settings.


this will limit the EA to 3 open trade at a time...if you want it to open 3 for eur and 3 for swissy...then use a different magic number for the the EA's as they are attached to the separate charts.


cheers

 

MT

Something like this will count by magic number and order type

int OpenTradesForMNandType(int iMN, int iType)
{
// Counts orders by MagicNumber and OrderType 
int icnt, itotal, retval;

retval=0;
itotal=OrdersTotal();

   for(icnt=0;icnt<itotal;icnt++)
     {
      OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
       // check for opened position, symbol & MagicNumber
      if(OrderType()==iType)  
        {
         if(OrderMagicNumber()==iMN)
         {
           retval++;
        
           //Print("Orders opened : ",retval);
         }
        }
     }

return(retval);
}

Good Luck

-BB-

Reason: