help for code for fine minimum variable

To add comments, please log in or register
Hadi Ein Jafari
687
Hadi Ein Jafari  

I need to have a code for the smallest variable of the four variables, and if the value of each is zero, then comment on the next variable.



GlobalVariableGet("SL"+Symbol()+"240")

GlobalVariableGet("SL"+Symbol()+"1440")

GlobalVariableGet("SL"+Symbol()+"10080")

GlobalVariableGet("SL"+Symbol()+"43200")

Seng Joo Thio
1209
Seng Joo Thio  
Hadi Ein Jafari:

I need to have a code for the smallest variable of the four variables, and if the value of each is zero, then comment on the next variable.

GlobalVariableGet("SL"+Symbol()+"240")

GlobalVariableGet("SL"+Symbol()+"1440")

GlobalVariableGet("SL"+Symbol()+"10080")

GlobalVariableGet("SL"+Symbol()+"43200")

Here's one way (I packaged as a self-contained script for convenience) - highlighted parts contain the main comparison, the remaining codes are supportive:

const ENUM_TIMEFRAMES iaTFs[] = {PERIOD_H4,PERIOD_D1,PERIOD_W1,PERIOD_MN1};
const double daTFVs[] = {1.5,2.4,0,3};

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   int iNumOfTFs = ArraySize(iaTFs);

   // Mockup - initializing GlobalVariables with values.
   for (int i=0; i<iNumOfTFs; i++)
      GlobalVariableSet(ComposeGVName(i), daTFVs[i]);
   
   // Actual test proper
   double dValues[];
   int iSmallestIdx = -1;
   ArrayResize(dValues, iNumOfTFs);

   for (int i=0; i<iNumOfTFs; i++)
   {
      dValues[i] = GlobalVariableGet(ComposeGVName(i));
      if (dValues[i]>0 && (iSmallestIdx==-1 || dValues[i]<dValues[iSmallestIdx]))
         iSmallestIdx = i;
   }
   
   if (iSmallestIdx!=-1)
      Print ("GlobalVariable ", ComposeGVName(iSmallestIdx), " contains the smallest value: ", DoubleToString(dValues[iSmallestIdx],2));
   else
      Print ("Not found!!!");
}

string ComposeGVName(int iTFIdx)
{
   string sResult = "";

   if (iTFIdx>=0 && iTFIdx<ArraySize(iaTFs))
      sResult = "SL"+_Symbol+IntegerToString(PeriodSeconds(iaTFs[iTFIdx])/60);
   
   return sResult;
}
To add comments, please log in or register