question on global variables

 

The following code moved stoploss to breakeven. It sets a global variable in doing so, then to remember that stoploss has moved to breakeven on that particular trade.

The code works when i have only one trade open, it will store the global variable and all functions work as they should in my ea. But when i have more that one order open the global variables only work for one trade. If one variable is stored for eg. trade (a) and then another variable is stored for trade (b). The variable for (b) replaces Variable (a). Its seems that one keeps replacing the other...

What i need is;

For a global variable to be stored PER each individual trade as stoploss is moved to breakeven.

To be able to trade multiple currencies at the same time and multiple positions at the same time, long and short and for the variables to be unique to each trade.

I WILL APPRECIATE THE HELP FOR ANYONE WILLING TO GIVE ME A HAND WITH THIS.

HERE IS THE CODE I HAVE SO FAR THAT WORKS WHEN THERE IS ONE POSITION OPEN ON ONE CURRENCY PAIR.

#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"


int MAGIC=2667756;

string Ticket1[200];
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
    
    Breakeven_buy();
    close();
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   
   Breakeven_buy();
   close();
   
//----
  return(0);
}
//+------------------------------------------------------------------+  
  void close()
   {
   
   int i;
   
   for (i = 0 ; i < OrdersTotal() ; i++)
            {  
          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
               
            if (OrderMagicNumber() == MAGIC)
               {
                  if(OrderSymbol()==Symbol())
                     {
                     
                    if(OrderType()==OP_BUY)
                    
                             {      
     
          if (iClose(NULL, 0, 0) >= iClose(NULL, 0, 1))
                  {
      OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice()),OrderTakeProfit(),0,CLR_NONE);
      GlobalVariableSet( Ticket1[i], OrderTicket()); 
      
               }
             }
           }
         }
       }                 
}                                            

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

void Breakeven_buy()

  {
  
  int i;
     
    for (i = 0 ; i < OrdersTotal() ; i++)
            {

Ticket1[i]=GlobalVariableName(i);


        OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
              {
              
          if (OrderMagicNumber() == MAGIC && OrderSymbol()==Symbol()) 
                {
           if(OrderType()==OP_BUY)
                 {       
            if (GlobalVariableGet(Ticket1[i])==OrderTicket())
                  { 
             if (OrderStopLoss() < OrderOpenPrice())
                   {
         OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice()),OrderTakeProfit(),0,CLR_NONE);  
         
                   }
                 }
               } 
             }
           }
         }
    
    }    
//+------------------------------------------------------------------+              
 
gavin:

The following code moved stoploss to breakeven. It sets a global variable in doing so, then to remember that stoploss has moved to breakeven on that particular trade. [...]

Why do you need a global variable? Why can't you just check whether a trade has already been processed using OrderOpenPrice() == OrderStopLoss() ?
 

save to a file, and read again if need, why need global variable?

global variables are used for exchange a little something between two EA.

and Ticket1 not have init values, and in GloVariableName(i); i means index of GloVariable number not OrdersTotal(),

GloVariable means a pair of name and value, GlobalVariableSet( Ticket1[i], OrderTicket()); value is OderTicket(), What is Ticket1[i] ? no content in it !

for (i = 0 ; i < OrdersTotal() ; i++)
{

Ticket1[i]=GlobalVariableName(i);

 
DxdCn:

save to a file, and read again if need, why need global variable?

global variables are used for exchange a little something between two EA.

and Ticket1 not have init values, and in GloVariableName(i); i means index of GloVariable number not OrdersTotal(),

GloVariable means a pair of name and value, GlobalVariableSet( Ticket1[i], OrderTicket()); value is OderTicket(), What is Ticket1[i] ? no content in it !

for (i = 0 ; i < OrdersTotal() ; i++)
{

Ticket1[i]=GlobalVariableName(i);


Basically i need to save the information one way or another. Can you modifiy my code to save to a file. Or point me in the direction so i can do it myself. I have limited coding experience.

Reason: