Need help with struct variables..

 

I have the following code...

From the Main Program I am trying to call a function which would populate variable and arrays which I can use later in order to close specific orders.

I need some help, since I keep getting the "array out of range" error.

My purpose is to go once through all the Opentrades.. and populate the necessary variables once.

I would also like to know, if I need to use the elements in the arrays... how do I pass them to a function in order to either put stoplosses using the stored ticket numbers or close all trades using the stored ticket numbers in the arrays.

Any help is greatly appreciated.

Sample CODE BELOW;

struct Update 



{ 

  string Magic00_cm;

  string Magic01_cm;

  string Magic02_cm;

   

  double Pair1BuyTrades_Magic00;
  double Pair1SellTrades_Magic00;
  double Pair1BuyTrades_Magic01;
  double Pair1SellTrades_Magic01;
  double Pair1BuyTrades_Magic02;
  double Pair1SellTrades_Magic02;
  
  double Pair1BuyVolume_Magic00;
  double Pair1SellVolume_Magic00;
  double Pair1BuyVolume_Magic01;
  double Pair1SellVolume_Magic01;
  double Pair1BuyVolume_Magic02;
  double Pair1SellVolume_Magic02;
  
  double Pair1BuyProfit_Magic00;
  double Pair1SellProfit_Magic00;
  double Pair1BuyProfit_Magic01;
  double Pair1SellProfit_Magic01;
  double Pair1BuyProfit_Magic02;
  double Pair1SellProfit_Magic02;
  
  ulong Magic00SLBuyticket[];
  ulong Magic00SLSellticket[];
  ulong Magic00Alltickets[];
  
  ulong Magic01SLBuyticket[];
  ulong Magic01SLSellticket[];
  ulong Magic01Alltickets[];
  
  ulong Magic02SLBuyticket[];
  ulong Magic02SLSellticket[];
  ulong Magic02Alltickets[];
  
  double Pair1BuyHigh_Magic00;
  double Pair1BuyLow_Magic00;
  double Pair1SellHigh_Magic00;
  double Pair1SellLow_Magic00;
  double Pair1BuyHigh_Magic01;
  double Pair1BuyLow_Magic01;
  double Pair1SellHigh_Magic01;
  double Pair1SellLow_Magic01;
  double Pair1BuyHigh_Magic02;
  double Pair1BuyLow_Magic02;
  double Pair1SellHigh_Magic02;
  double Pair1SellLow_Magic02;
  
   
};  

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  { 
    
    Update sum;
    UpdateALL(sum);
 }

//+------------------------------------------------------------------+
//| Function: Create History Database for local lookup               |
//+------------------------------------------------------------------+

void UpdateALL (Update &sum)

{  
    sum.Pair1BuyTrades_Magic00 = 0;
    sum.Pair1BuyTrades_Magic01 = 0;
    sum.Pair1BuyTrades_Magic02 = 0;
    sum.Pair1SellTrades_Magic00 = 0;
    sum.Pair1SellTrades_Magic01 = 0;
    sum.Pair1SellTrades_Magic02 = 0;
    
    sum.Pair1BuyVolume_Magic00=0;
    sum.Pair1SellVolume_Magic00=0;
    sum.Pair1BuyVolume_Magic01=0;
    sum.Pair1SellVolume_Magic01=0;
    sum.Pair1BuyVolume_Magic02=0;
    sum.Pair1SellVolume_Magic02=0;
    
      sum.Pair1BuyProfit_Magic00=0;
      sum.Pair1SellProfit_Magic00=0;
      sum.Pair1BuyProfit_Magic01=0;
      sum.Pair1SellProfit_Magic01=0;
      sum.Pair1BuyProfit_Magic02=0;
      sum.Pair1SellProfit_Magic02=0;
    
    
    
    sum.Pair1BuyHigh_Magic00= 0;
    sum.Pair1BuyLow_Magic00 = 9999999;
    sum.Pair1SellHigh_Magic01= 0;
    sum.Pair1SellLow_Magic01 = 9999999;
    sum.Pair1SellHigh_Magic02= 0;
    sum.Pair1SellLow_Magic02 = 9999999;
    
    ArrayResize(sum.Magic00SLBuyticket,0);
    ArrayResize(sum.Magic00SLSellticket,0);
    ArrayResize (sum.Magic00Alltickets,0);
  
    ArrayResize(sum.Magic01SLBuyticket,0);
    ArrayResize(sum.Magic01SLSellticket,0);
    ArrayResize (sum.Magic01Alltickets,0);
  
    ArrayResize(sum.Magic02SLBuyticket,0);
    ArrayResize(sum.Magic02SLSellticket,0);
    ArrayResize (sum.Magic02Alltickets,0);
    
    
    
      int countM0Buy = 0;
      int countM0Sell = 0;
      int countM1Buy = 0;
      int countM1Sell = 0;
      int countM2Buy = 0;
      int countM2Sell = 0;
      int count00All = 0;
      int count01All = 0;
      int count02All = 0;
  
   for(int pos=0; pos<=PositionsTotal(); pos++)
   {
      ulong ticket = PositionGetTicket(pos);
      long MagicTemp = PositionGetInteger(POSITION_MAGIC);
      double PositionPrice = PositionGetDouble(POSITION_PRICE_OPEN);
      double Volume = PositionGetDouble(POSITION_VOLUME);
      string Cmt = PositionGetString(POSITION_COMMENT);
      ENUM_POSITION_TYPE PosType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
      double Profit = PositionGetDouble(POSITION_PROFIT)+PositionGetDouble(POSITION_SWAP)-Volume*Comms;
      
      
      if (MagicTemp == MagicNumber)
      { sum.Magic00_cm = Cmt;
      if (PosType == POSITION_TYPE_BUY) 
      {sum.Pair1BuyProfit_Magic00 += Profit;
       sum.Pair1BuyTrades_Magic00 +=1;
       sum.Pair1BuyVolume_Magic00 += Volume;
       if (PositionPrice > sum.Pair1BuyHigh_Magic00) sum.Pair1BuyHigh_Magic00 = PositionPrice;
       if (PositionPrice < sum.Pair1BuyLow_Magic00) sum.Pair1BuyLow_Magic00 = PositionPrice;
       if (Profit>0) {sum.Magic00SLBuyticket[countM0Buy] = ticket;countM0Buy +=1;}
       sum.Magic00Alltickets[count00All] = ticket;
       count00All+=1;
      } 
      if (PosType == POSITION_TYPE_SELL) 
      {sum.Pair1SellProfit_Magic00 += Profit;
       sum.Pair1SellTrades_Magic00 +=1;
       sum.Pair1SellVolume_Magic00 += Volume;
       if (PositionPrice > sum.Pair1SellHigh_Magic00) sum.Pair1SellHigh_Magic00 = PositionPrice;
       if (PositionPrice < sum.Pair1SellLow_Magic00) sum.Pair1SellLow_Magic00 = PositionPrice;
       if (Profit>0) {sum.Magic00SLSellticket[countM0Sell] = ticket;countM0Sell +=1;}
       sum.Magic00Alltickets[count00All] = ticket;
       count00All+=1;
      }  
      } 
      
      if (MagicTemp == MagicNumber+1)
      { sum.Magic01_cm = Cmt;
      if (PosType == POSITION_TYPE_BUY) 
      {sum.Pair1BuyProfit_Magic01 += Profit;
       sum.Pair1BuyTrades_Magic01 +=1;
       sum.Pair1BuyVolume_Magic01 += Volume;
       if (PositionPrice > sum.Pair1BuyHigh_Magic01) sum.Pair1BuyHigh_Magic01 = PositionPrice;
       if (PositionPrice < sum.Pair1BuyLow_Magic01) sum.Pair1BuyLow_Magic01 = PositionPrice;
       if (Profit>0) {sum.Magic01SLBuyticket[countM1Buy] = ticket;countM1Buy +=1;}
       sum.Magic01Alltickets[count01All] = ticket;
       count01All+=1;
      } 
      if (PosType == POSITION_TYPE_SELL) 
      {sum.Pair1SellProfit_Magic01 += Profit;
       sum.Pair1SellTrades_Magic01 +=1;
       sum.Pair1SellVolume_Magic01 += Volume;
       if (PositionPrice > sum.Pair1SellHigh_Magic01) sum.Pair1SellHigh_Magic01 = PositionPrice;
       if (PositionPrice < sum.Pair1SellLow_Magic01) sum.Pair1SellLow_Magic01 = PositionPrice;
       if (Profit>0) {sum.Magic01SLSellticket[countM1Sell] = ticket;countM1Sell +=1;}
       sum.Magic01Alltickets[count01All] = ticket;
       count01All+=1;
      }  
      }
      
      if (MagicTemp == MagicNumber+2)
      { sum.Magic02_cm = Cmt;
      if (PosType == POSITION_TYPE_BUY) 
      {sum.Pair1BuyProfit_Magic02 += Profit;
       sum.Pair1BuyTrades_Magic02 +=1;
       sum.Pair1BuyVolume_Magic02 += Volume;
       if (PositionPrice > sum.Pair1BuyHigh_Magic02) sum.Pair1BuyHigh_Magic02 = PositionPrice;
       if (PositionPrice < sum.Pair1BuyLow_Magic02) sum.Pair1BuyLow_Magic02 = PositionPrice;
       if (Profit>0) {sum.Magic02SLBuyticket[countM2Buy] = ticket;countM2Buy +=1;}
       sum.Magic02Alltickets[count02All] = ticket;
       count02All+=1;
      } 
      if (PosType == POSITION_TYPE_SELL) 
      {sum.Pair1SellProfit_Magic02 += Profit;
       sum.Pair1SellTrades_Magic02 +=1;
       sum.Pair1SellVolume_Magic02 += Volume;
       if (PositionPrice > sum.Pair1SellHigh_Magic02) sum.Pair1SellHigh_Magic02 = PositionPrice;
       if (PositionPrice < sum.Pair1SellLow_Magic02) sum.Pair1SellLow_Magic02 = PositionPrice;
       if (Profit>0){ sum.Magic02SLSellticket[countM2Sell] = ticket;countM2Sell +=1;}
       sum.Magic02Alltickets[count02All] = ticket;
       count02All+=1;
      }  
      } 

   }
   
} 
 
StarBuck99: I need some help, since I keep getting the "array out of range" error.

Of course, you do. Your arrays have no size.

    ArrayResize(sum.Magic00SLBuyticket,0);
    ArrayResize (sum.Magic00Alltickets,0);

      int countM0Buy = 0;  
      int count00All = 0;
⋮
       if (Profit>0) {sum.Magic00SLBuyticket[countM0Buy] = ticket;countM0Buy +=1;}

       sum.Magic00Alltickets[count00All] = ticket;

       count00All+=1;
 

Where do you resize those arrays before storing?