Download MetaTrader 5

need help with loop - page 2

To add comments, please log in or register
Keith Watford
Moderator
10033
Keith Watford  
3056527:

To reduce the lines is indeed what i want.

And i am sorry that i wasn't clear about that. 

Your code can be drastically reduced by using an array

First though, what is the point of all the ok1, ok2 etc? They are intermediate variables that are not used anywhere else

//--- input parameters
extern int magicnumber=1202;

double BuyArray[71]; //Array index is 0 to 70
string GV_Name="newvaluezynga";
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   for(int x=0;x<70;x++)
     {
      BuyArray[x]=GlobalVariableGet(GV_Name+(string)x);
     }

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int BuyCnt=0;
   int SellCnt=0;
   int BuyStopCnt=0;
   int SellStopCnt = 0;
   int BuyLimitCnt = 0;
   int SellLimitCnt= 0;

   int cnt=OrdersTotal();
   for(int i=0; i<cnt; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSymbol()!=Symbol()) continue;
      if(OrderMagicNumber()!=magicnumber) continue;
      int type= OrderType();
      if(type == OP_BUY) BuyCnt++;
      if(type == OP_SELL) SellCnt++;
      if(type == OP_BUYSTOP) BuyStopCnt++;
      if(type == OP_SELLSTOP) SellStopCnt++;
      if(type == OP_BUYLIMIT) BuyLimitCnt++;
      if(type == OP_SELLLIMIT) SellLimitCnt++;
     }

   if(BuyCnt==0)
     {
      //Code to open initial order
      double value;// Calculated value that needs to be stored in the array and Global Variable
      BuyArray[1]=value;
      GlobalVariableSet(GV_Name+(string)1,value);
     }
   else
     {
      OpenBuy(BuyCnt+1);
     }
//---

  }
//+------------------------------------------------------------------+
void OpenBuy(int index)
  {
   double previous_value=BuyArray[index-1];
    //Code to open order
   double value;// Calculated value that needs to be stored in the array and Global Variable
   BuyArray[index]=value;
   GlobalVariableSet(GV_Name+(string)index,value);

  }
//+------------------------------------------------------------------+

 I didn't check, but I think that all trades after the initial trade follow the same logic, so only 1 function is required

796790
91
796790  

Gumrai

thank you for your time.

This is new for me working with array so i try to let the program to run.

 

again thanks a lot 

JD4
1101
JD4  

For some of this code block, where you are trying to see order type, you can also use switch/case logic.

for(int i=0; i<cnt; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSymbol()!=Symbol()) continue;
      if(OrderMagicNumber()!=magicnumber) continue;
      int type= OrderType();
      if(type == OP_BUY) BuyCnt++;
      if(type == OP_SELL) SellCnt++;
      if(type == OP_BUYSTOP) BuyStopCnt++;
      if(type == OP_SELLSTOP) SellStopCnt++;
      if(type == OP_BUYLIMIT) BuyLimitCnt++;
      if(type == OP_SELLLIMIT) SellLimitCnt++;
     }
12
To add comments, please log in or register