need help with loop

Back to topics list  | 1 2 To post a new topic, please log in or register
avatar
Moderator
3406
GumRai 2015.07.26 20:59 #
 
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

avatar
33
3056527 2015.07.26 22:26 #
 

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 

 

avatar
313
JD4 2015.07.26 22:37 #
 

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++;
     }








Back to topics list   | 1 2  

To add comments, please log in or register