does anyone know about 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 dont seem to work.

What i need is;

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

For each global variable to be deleted when that trade that stored the variable is removed in sl or tp.

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;
//+------------------------------------------------------------------+
//| 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, OrderTicket()); 
      
               }
             }
           }
         }
       }                 
}                                            

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

void Breakeven_buy()

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

you can test it.
#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 remember. Just check in the orderSelect loop if OrderStopLoss() >= OrderOpenPrice()
Also you should count down in the presence of EA's on other charts with multiple orders.
for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
    OrderSelect(pos, SELECT_BY_POS)             // Only my orders w/
&&  OrderMagicNumber() == Magic.Number          // my magic number
&&  OrderSymbol()      == Symbol() ){           // and period and symbol
 
WHRoeder:
Why remember. Just check in the orderSelect loop if OrderStopLoss() >= OrderOpenPrice()
Also you should count down in the presence of EA's on other charts with multiple orders.


Hi, I need a global variable for each individual trade to remember because i also have othet functions in my ea that control position of stoploss. So i have control of each trade if i know if a global variable exists for that trade or not.

 

You have control of each trade if the magic number matches the EA's.

If the terminal crashes, or power glitch reboots, etc, the global variables will not be written to disk and won't be valid on restart. Your EA fails.

If you need additional information you must use persistent storage (a file,) but you should be able to regenerate all data from the chart, open trades, and history.

 

I thought if a global variable is created it is stored for 4 weeks?? If i need to put this stuff on a file, how do i do that with the code i have posted. Igf its not much additional code, maybe tou willo show me.

Reason: