sum of profits in one currency pair

 

hi guys, since I cant find it in the forum, maybe you can help me here.

I'm trading 10 pairs at the same time, so there are possibly 10 trades or so open. I want to know the profits of - lets say - EURUSD. so far I have:

double profit_in_currency()
   {
   int i, iOrders = OrdersTotal() - 1;      
   double sum_profits = 0;
  
   for(i = iOrders; i >= 0; i--)
   {
   OrderSelect( i, SELECT_BY_POS, MODE_TRADES );     
   if( Symbol() == OrderSymbol() &&
       OrderMagicNumber() == get_magic_number() )
       {
         sum_profits += OrderProfit();
       }        
   }
   
   return( sum_profits );
   }

the problem is that if I use the magic-number the EA doesnt work anymore and a friend told me I would have to use it. without the magicnumber it works fine on the backtest but doesnt work at all on the live test, it closes positions for no reason...

the get_magic_number function looks like this:


int get_magic_number()
{
int Magic = 0;

//buy
if( AutoMagic == TRUE && 
    is_long_in_pair() > 0 )
   {
      if(Symbol() == "AUDCADm" || Symbol() == "AUDCAD") Magic = 66601;
      if(Symbol() == "AUDCHFm" || Symbol() == "AUDCHF") Magic = 66602;
      if(Symbol() == "AUDJPYm" || Symbol() == "AUDJPY") Magic = 66603;
      if(Symbol() == "AUDNZDm" || Symbol() == "AUDNZD") Magic = 66604;
      if(Symbol() == "AUDUSDm" || Symbol() == "AUDUSD") Magic = 66605;
      if(Symbol() == "CADCHFm" || Symbol() == "CADCHF") Magic = 66606;
      if(Symbol() == "CADJPYm" || Symbol() == "CADJPY") Magic = 66607;
      if(Symbol() == "CHFJPYm" || Symbol() == "CHFJPY") Magic = 66608;
      if(Symbol() == "EURAUDm" || Symbol() == "EURAUD") Magic = 66609;
      if(Symbol() == "EURCADm" || Symbol() == "EURCAD") Magic = 66610;
      if(Symbol() == "EURCHFm" || Symbol() == "EURCHF") Magic = 66611;
      if(Symbol() == "EURGBPm" || Symbol() == "EURGBP") Magic = 66612;
      if(Symbol() == "EURJPYm" || Symbol() == "EURJPY") Magic = 66613;
      if(Symbol() == "EURNZDm" || Symbol() == "EURNZD") Magic = 66614;
      if(Symbol() == "EURUSDm" || Symbol() == "EURUSD") Magic = 66615;
      if(Symbol() == "GBPAUDm" || Symbol() == "GBPAUD") Magic = 66616;
      if(Symbol() == "GBPCADm" || Symbol() == "GBPCAD") Magic = 66617;
      if(Symbol() == "GBPCHFm" || Symbol() == "GBPCHF") Magic = 66618;
      if(Symbol() == "GBPJPYm" || Symbol() == "GBPJPY") Magic = 66619;
      if(Symbol() == "GBPNZDm" || Symbol() == "GBPNZD") Magic = 66620;
      if(Symbol() == "GBPUSDm" || Symbol() == "GBPUSD") Magic = 66621;
      if(Symbol() == "NZDCADm" || Symbol() == "NZDCAD") Magic = 66622;
      if(Symbol() == "NZDCHFm" || Symbol() == "NZDCHF") Magic = 66623;
      if(Symbol() == "NZDJPYm" || Symbol() == "NZDJPY") Magic = 66624;
      if(Symbol() == "NZDUSDm" || Symbol() == "NZDUSD") Magic = 66625;
      if(Symbol() == "USDCADm" || Symbol() == "USDCAD") Magic = 66626;
      if(Symbol() == "USDCHFm" || Symbol() == "USDCHF") Magic = 66627;
      if(Symbol() == "USDJPYm" || Symbol() == "USDJPY") Magic = 66628;
      if(Symbol() == "USDSGDm" || Symbol() == "USDSGD") Magic = 66629;
      if(Symbol() == "SGDJPYm" || Symbol() == "SGDJPY") Magic = 66630;
      if (Magic == 0) Magic = 666;
   }
   
//sell
if( AutoMagic && 
    is_short_in_pair() > 0 )
   {
      if(Symbol() == "AUDCADm" || Symbol() == "AUDCAD") Magic = 99901;
      if(Symbol() == "AUDCHFm" || Symbol() == "AUDCHF") Magic = 99902;
      if(Symbol() == "AUDJPYm" || Symbol() == "AUDJPY") Magic = 99903;
      if(Symbol() == "AUDNZDm" || Symbol() == "AUDNZD") Magic = 99904;
      if(Symbol() == "AUDUSDm" || Symbol() == "AUDUSD") Magic = 99905;
      if(Symbol() == "CADCHFm" || Symbol() == "CADCHF") Magic = 99906;
      if(Symbol() == "CADJPYm" || Symbol() == "CADJPY") Magic = 99907;
      if(Symbol() == "CHFJPYm" || Symbol() == "CHFJPY") Magic = 99908;
      if(Symbol() == "EURAUDm" || Symbol() == "EURAUD") Magic = 99909;
      if(Symbol() == "EURCADm" || Symbol() == "EURCAD") Magic = 99910;
      if(Symbol() == "EURCHFm" || Symbol() == "EURCHF") Magic = 99911;
      if(Symbol() == "EURGBPm" || Symbol() == "EURGBP") Magic = 99912;
      if(Symbol() == "EURJPYm" || Symbol() == "EURJPY") Magic = 99913;
      if(Symbol() == "EURNZDm" || Symbol() == "EURNZD") Magic = 99914;
      if(Symbol() == "EURUSDm" || Symbol() == "EURUSD") Magic = 99915;
      if(Symbol() == "GBPAUDm" || Symbol() == "GBPAUD") Magic = 99916;
      if(Symbol() == "GBPCADm" || Symbol() == "GBPCAD") Magic = 99917;
      if(Symbol() == "GBPCHFm" || Symbol() == "GBPCHF") Magic = 99918;
      if(Symbol() == "GBPJPYm" || Symbol() == "GBPJPY") Magic = 99919;
      if(Symbol() == "GBPNZDm" || Symbol() == "GBPNZD") Magic = 99920;
      if(Symbol() == "GBPUSDm" || Symbol() == "GBPUSD") Magic = 99921;
      if(Symbol() == "NZDCADm" || Symbol() == "NZDCAD") Magic = 99922;
      if(Symbol() == "NZDCHFm" || Symbol() == "NZDCHF") Magic = 99923;
      if(Symbol() == "NZDJPYm" || Symbol() == "NZDJPY") Magic = 99924;
      if(Symbol() == "NZDUSDm" || Symbol() == "NZDUSD") Magic = 99925;
      if(Symbol() == "USDCADm" || Symbol() == "USDCAD") Magic = 99926;
      if(Symbol() == "USDCHFm" || Symbol() == "USDCHF") Magic = 99927;
      if(Symbol() == "USDJPYm" || Symbol() == "USDJPY") Magic = 99928;
      if(Symbol() == "USDSGDm" || Symbol() == "USDSGD") Magic = 99929;
      if(Symbol() == "SGDJPYm" || Symbol() == "SGDJPY") Magic = 99930;
      if (Magic == 0) Magic = 999;
   }

return(Magic);
}
 

How does is_long_in_pair() know which is the currently selected order?

Would it not be easier to initilise the two magicnumbers used for the chart as global variables and then use those values throughout the code? You would then save processor time by not 'looking up' the the values everytime you want to use them.

 
Ickyrus:

How does is_long_in_pair() know which is the currently selected order?

Would it not be easier to initilise the two magicnumbers used for the chart as global variables and then use those values throughout the code? You would then save processor time by not 'looking up' the the values everytime you want to use them.


you are right, I changed that by now since is_long_in_pair() uses the magic number itself.

I dont understand why I have to use seperate magic numbers for buy and sell either, I dont see why I have to use magic numbers at all. when I ask for the currency symbol and if it is buy or sell it should work without any kind of "magic" number. I dont see the point yet.

I changed the code of get_magic_number() to:

int get_magic_number()
{

//buy
if( AutoMagic == TRUE )
   {
      if(Symbol() == "AUDCADm" || Symbol() == "AUDCAD") Magic = 66601;
      if(Symbol() == "AUDCHFm" || Symbol() == "AUDCHF") Magic = 66602;
      if(Symbol() == "AUDJPYm" || Symbol() == "AUDJPY") Magic = 66603;
      if(Symbol() == "AUDNZDm" || Symbol() == "AUDNZD") Magic = 66604;
      if(Symbol() == "AUDUSDm" || Symbol() == "AUDUSD") Magic = 66605;
      if(Symbol() == "CADCHFm" || Symbol() == "CADCHF") Magic = 66606;
      if(Symbol() == "CADJPYm" || Symbol() == "CADJPY") Magic = 66607;
      if(Symbol() == "CHFJPYm" || Symbol() == "CHFJPY") Magic = 66608;
      if(Symbol() == "EURAUDm" || Symbol() == "EURAUD") Magic = 66609;
      if(Symbol() == "EURCADm" || Symbol() == "EURCAD") Magic = 66610;
      if(Symbol() == "EURCHFm" || Symbol() == "EURCHF") Magic = 66611;
      if(Symbol() == "EURGBPm" || Symbol() == "EURGBP") Magic = 66612;
      if(Symbol() == "EURJPYm" || Symbol() == "EURJPY") Magic = 66613;
      if(Symbol() == "EURNZDm" || Symbol() == "EURNZD") Magic = 66614;
      if(Symbol() == "EURUSDm" || Symbol() == "EURUSD") Magic = 66615;
      if(Symbol() == "GBPAUDm" || Symbol() == "GBPAUD") Magic = 66616;
      if(Symbol() == "GBPCADm" || Symbol() == "GBPCAD") Magic = 66617;
      if(Symbol() == "GBPCHFm" || Symbol() == "GBPCHF") Magic = 66618;
      if(Symbol() == "GBPJPYm" || Symbol() == "GBPJPY") Magic = 66619;
      if(Symbol() == "GBPNZDm" || Symbol() == "GBPNZD") Magic = 66620;
      if(Symbol() == "GBPUSDm" || Symbol() == "GBPUSD") Magic = 66621;
      if(Symbol() == "NZDCADm" || Symbol() == "NZDCAD") Magic = 66622;
      if(Symbol() == "NZDCHFm" || Symbol() == "NZDCHF") Magic = 66623;
      if(Symbol() == "NZDJPYm" || Symbol() == "NZDJPY") Magic = 66624;
      if(Symbol() == "NZDUSDm" || Symbol() == "NZDUSD") Magic = 66625;
      if(Symbol() == "USDCADm" || Symbol() == "USDCAD") Magic = 66626;
      if(Symbol() == "USDCHFm" || Symbol() == "USDCHF") Magic = 66627;
      if(Symbol() == "USDJPYm" || Symbol() == "USDJPY") Magic = 66628;
      if(Symbol() == "USDSGDm" || Symbol() == "USDSGD") Magic = 66629;
      if(Symbol() == "SGDJPYm" || Symbol() == "SGDJPY") Magic = 66630;
      if (Magic == 0) Magic = 666;
   }

return(Magic)
}

and I made a global variable Magic which is initialised in the standard function:

int init()
{
   get_magic_number(); 
}

but it still doesnt work, the program closes the positions not following my rules, I need it to close when the orders of the traded pair is in profit, the code is like this:


double profit_in_currency()
   {
   int i, iOrders = OrdersTotal() - 1;        //defining running parameter for below
   double sum_profits = 0;
  
   for(i = iOrders; i >= 0; i--)
   {
   OrderSelect( i, SELECT_BY_POS, MODE_TRADES );     
      if( Symbol() == OrderSymbol() &&
          OrderMagicNumber() == Magic )
      {
          sum_profits += OrderProfit();
      }        
   }
   
   return( sum_profits );
   }

only if profit_in_currency()>0, the orders in the respective pair are closed. but on the forward test with multiple pairs it closes them arbitrarily...

I just checked on the forward test: the EA doesnt close the positions of only one currency but of all currencies - so there's still a little error with the magic numbers :)

 

There also might be an error in the function where you close the orders.

btw, i don't get the point of using a different magic number for each symbol...

 
zzuegg:

There also might be an error in the function where you close the orders.

btw, i don't get the point of using a different magic number for each symbol...


hehe welcome to the club,

the order close function is:


void close_all_open_orders() 
{
int iOrders = OrdersTotal() - 1, i;

if(TRUE)
  { 
     for(i = iOrders; i >= 0; i--) 
        {
           if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) && 
               OrderType() <= OP_SELL && 
               OrderSymbol() == Symbol() &&
               OrderMagicNumber() == Magic && 
               GetMarketInfo() && 
               !OrderClose( OrderTicket(), OrderLots(), Price[1-OrderType()], giSlippage ) ) 
           {
               Print( OrderError() );
               Sleep(3000);
           }
        }
   }
}
 
jdegenfellner:

I changed the code of get_magic_number() to:


int init()
{
   get_magic_number(); 
}

Should be

//varables declared outside of functions are global ie can be used by all functions

int Magic = 0 ;

int init()
{
  Magic= get_magic_number(); 
}
 

One reason for giving diferent magic no's to buy and sell could be to check that the sytem is still profitable for the sell as well being profitable for the buy alowing some flexibility in the way in which the EA works.

Magic numbers are assigned to EA's attached to charts. You can have several diferent EA's Working on one currency pair so in order to identfy how badly or how well that EA is doing then it the use of a magic number helps.

 

Hi, your Closeing all orders is wrong:

try this:

void close_all_open_orders() 
{
int iOrders = OrdersTotal() - 1, i;
  for(int i=iOrders;i>=0;i--){
    OrderSelect(i,SELECT_BY_POS);
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==Magic){
      if(OrderType()==OP_SELL||OrderType()==OP_BUY){
        OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), giSlippage );
      }
    }
  }
}

Your error was to put the OrderClose() expression inside the if( .... ) so you closed every order, not only those going trough the if() filter

 
zzuegg:

Hi, your Closeing all orders is wrong:

try this:

Your error was to put the OrderClose() expression inside the if( .... ) so you closed every order, not only those going trough the if() filter


that looks good I'll try that right away... but do I still need the

OrderSymbol()==Symbol()

in the first if ?

.

.

.

.

ok, just implemented it - let's see if it works this time... :)

.

.

.

.

ok, looks pretty good so far, I only have to implement something that deals with requotes...

 
it doesn't matter in which if you put that, but the actual OrderClose() must be inside the {}
 
zzuegg:
it doesn't matter in which if you put that, but the actual OrderClose() must be inside the {}

ok, it works now.

one can omit the "OrderSymbol() == Symbol() completely if magic numbers are correctly used.




thanks so much for your help guys!!!!

Reason: