Count bars since price reach a certain point - page 2

 

simoncs:

Each time Swingdate[0] changes

- calculate the number of bars that have crossed the Fib61 line

despite having read all the help docs, loops and arrays always screw me. let me know if this isn't clear and i will try to put together a visual example.
  1. Show the code where you update Swingdate[0]
  2. Show the code where you recalculate Fib61 line value. Show the code where you do the counting.
  3. Visual is unnecessary. You must learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.
  4. for(i=10; i>=0; i--) //amended from i>=0 to i>0
    {
       if(i>=iBarShift(Symbol(),ExtZZSwingTF,SwingDate[0]))continue;
    Why are you starting at 10 then then ignoring all i>=iBarshift? Start from there.
 

4. that was just the last iteration that i tried - i have tried too many examples to list all that code.

Another example of a loop - - I thought maybe that i could use...

 

for(i<=iBarShift(Symbol(),ExtZZSwingTF,SwingDate[0]); i=0;i++) 
{
  
   if(Trend==UpTrend) //will be buy trades
   RefreshRates();
      LowRetracePrice=iLow(NULL,ExtZZSwingTF,iBarShift(Symbol(),ExtZZSwingTF,i));
   
   
    
      if(ExtEntryLine==61)
         {
         if (LowRetracePrice < Fib61Line)
                
              {
              RetraceBarCount++;
              }
         }

 thinking that i make the start of the loop equal Swingdate[0] and then count forward to the current bar, but i don't think i constructed this properly.My thinking being each time there is a new Swingdate[0],start the loop at that bar, then count forward to the current bar, and increase RetraceBarCount only if the criteria is met.

as for the code where the Swingdate[0] and fib61 is reset...

Swingdate[0] being part of the SwingDate[Found] array is continually changing.

each time there is a new zigzag i reset a number of variables including the LowRetracePrice and the HighRetracePrice 

void Swings()
  {
   static datetime Time0;

   if(Time0==iTime(Symbol(),PERIOD_M5,0))
      return(0);           // if this is not a new bar then let's not do anything

   Time0=iTime(Symbol(),PERIOD_M5,0);

   int Found=2;
   int k=0;
   while(Found>=0)
     {
      if(iCustom(NULL,ExtZZSwingTF,"ZigZagOriginal",ExtZZDepth,ExtZZDeviation,ExtZZBackStep,0,k)!=0)
        {
         SwingValue[Found]=iCustom(NULL,ExtZZSwingTF,"ZigZagOriginal",ExtZZDepth,ExtZZDeviation,ExtZZBackStep,0,k); //report back the 2 swingvalues as set by found
         Found--;
         SwingDate[Found]=iTime(Symbol(),ExtZZSwingTF,k); // report back the 2 swing dates in the array as set by found
                                                          //SwingBar[Found]=iBars(Symbol(),ExtZZSwingTF); // report the bar numbers used in the swing

        }
      k++;
     }

   totalBars=Bars;    ///not sure that this is needed???

   if(prevZigZag!=SwingValue[1])
     {
      newZigZag=true;
      prevZigZag=SwingValue[1];
      OrderOpened = false;    ///reset orderopened upon new swing - so only one trade per swing
      OrderCount=0;           /// reset ordercount function upon new swing - so only one trade per swing
      BuyTicket = 0;
      SellTicket= 0;
      BarCheck=false;   /// reset barcheck function upon new swing - so only one trade per swing
      RetraceBarCount=0; // reset bar count upon new swing
      LowRetracePrice=0;
      HighRetracePrice=0;

     }
   if(newZigZag)
     {

      // define the uptrend and downtrend        

      if(SwingValue[0]<SwingValue[1])
        {
         Trend=UpTrend;
        }
      else Trend=DownTrend;

      //Define the Fib levels depending on the direction
      if(Trend==UpTrend)
        {
         Fib38Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* Fib38 - MathMax(SwingValue[0],SwingValue[1])),Digits);
         Fib50Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* 0.5 + MathMin(SwingValue[0],SwingValue[1])),Digits);
         Fib61Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* Fib61 - MathMax(SwingValue[0],SwingValue[1])),Digits);
         Fib73Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* Fib73 - MathMax(SwingValue[0],SwingValue[1])),Digits);
        }
      else if(Trend==DownTrend)
        {
         Fib38Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* Fib38 + MathMin(SwingValue[0],SwingValue[1])),Digits);
         Fib50Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* 0.5 + MathMin(SwingValue[0],SwingValue[1])),Digits);
         Fib61Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* Fib61 + MathMin(SwingValue[0],SwingValue[1])),Digits);
         Fib73Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* Fib73 + MathMin(SwingValue[0],SwingValue[1])),Digits);
        }
     }

  }
 

ihave amended as follows, so that the once per bar code is inside the loop,but the Retrace Bar count still seems to increment each new bar without considering the criteria.

any ideas?

 
simoncs: any ideas?
  1. Since you don't show all the code, I must assume Trend is an enumeration, U/D are enumeration values.
             Trend=UpTrend;
            }
          else Trend=DownTrend;
    If not make them so, or replace with:
    bool isUpTrend = SwingValue[0]<SwingValue[1];
  2. What part of "Show the code where you do the counting" was unclear?
  3. Your unreadable code
    if(Trend==UpTrend)
      {
       Fib38Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* Fib38 - MathMax(SwingValue[0],SwingValue[1])),Digits);
       Fib50Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* 0.5 + MathMin(SwingValue[0],SwingValue[1])),Digits);
       Fib61Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* Fib61 - MathMax(SwingValue[0],SwingValue[1])),Digits);
       Fib73Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* Fib73 - MathMax(SwingValue[0],SwingValue[1])),Digits);
      }
    else if(Trend==DownTrend)
      {
       Fib38Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* Fib38 + MathMin(SwingValue[0],SwingValue[1])),Digits);
       Fib50Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* 0.5 + MathMin(SwingValue[0],SwingValue[1])),Digits);
       Fib61Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* Fib61 + MathMin(SwingValue[0],SwingValue[1])),Digits);
       Fib73Line = NormalizeDouble(MathAbs(MathAbs(SwingValue[0] - SwingValue[1])* Fib73 + MathMin(SwingValue[0],SwingValue[1])),Digits);
      }
    }
    Simplified
    double  top     = MathMax(SwingValue[0],SwingValue[1]);
    double  bottom  = MathMin(SwingValue[0],SwingValue[1]);
    double  size    = top - bottom;
    if(Trend==UpTrend)
      {
       Fib38Line = MathAbs(size* Fib38 - top);
       Fib50Line = MathAbs(size* 0.5 + bottom);
       Fib61Line = MathAbs(size* Fib61 - top);
       Fib73Line = MathAbs(size* Fib73 - top);
      }
    else if(Trend==DownTrend)
      {
       Fib38Line = MathAbs(size* Fib38 + bottom);
       Fib50Line = MathAbs(size* 0.5 + bottom);
       Fib61Line = MathAbs(size* Fib61 + bottom);
       Fib73Line = MathAbs(size* Fib73 + bottom);
      }
    }
    Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
    Your MathAbs in the downTrend is redundant. The MathAbs in the uptrend is necessary because you calculate your lines backwards. The 38 retracement is top - size*0.38 (no MathAbs required.)
 

yes that is neater :)

but essentially my code was calculating the correct values each time, so i don't think this is the problem.

 

re the code that does the counting,i had provided snippets of it in previous posts, but here is the whole function. in this latest iteration, it seems to increment once per bar, but not taking into account the criteria.

void RetraceCheck()
{
RefreshRates();
static datetime Time0;

// ExtZZSwingTF - is a external variable for the timeframe  / ExtEntryLine is external variable
// "i" is declared as global variable at the mo, so i can use it in print statements
for(i=iBarShift(Symbol(),ExtZZSwingTF,SwingDate[0]);i>=0; i--) 
{

if(Time0==iTime(Symbol(),ExtZZSwingTF,0))
      return(0);           // if this is not a new bar then let's not do anything

   Time0=iTime(Symbol(),ExtZZSwingTF,0);
 

  
   if(Trend==UpTrend) //will be buy trades
   RefreshRates();
      LowRetracePrice=iLow(NULL,ExtZZSwingTF,iBarShift(Symbol(),ExtZZSwingTF,i));
   
    
    
      if(ExtEntryLine==50)
         {
         if (LowRetracePrice < Fib50Line)
                
              {
              RetraceBarCount++;
              }
         }
              
             
   
   

        

   else if(ExtEntryLine==61)

     {
      
        if (LowRetracePrice < Fib61Line)
                
              {
              RetraceBarCount++;
              }
              
           
     }
   
   if(Trend==DownTrend) //will be sell trades
   RefreshRates();
      HighRetracePrice=iHigh(NULL,ExtZZSwingTF,iBarShift(Symbol(),ExtZZSwingTF,i));
      
     
      
      if(ExtEntryLine==50)

         {
         if (HighRetracePrice > Fib50Line)
                
              {
              RetraceBarCount++;
              }
         }     
             

   else if(ExtEntryLine==61)
     {
         if (HighRetracePrice > Fib61Line)
                
              {
              RetraceBarCount++;
              }
     }     
   
     
     
    
}     
     
   return(RetraceBarCount);


}  
 
What is the third datatype for the function, what are you passing?
iBarShift(Symbol(),ExtZZSwingTF,i)
 
WHRoeder:
What is the third datatype for the function, what are you passing?

extern int       ExtZZSwingTF=3;


 switch(ExtZZSwingTF) //case statement so that the different periods can be used in optimization using the step
     {
      case 1: ExtZZSwingTF = 15;break;
      case 2: ExtZZSwingTF = 60;break;
      case 3: ExtZZSwingTF = 240;break;
      case 4: ExtZZSwingTF = 1440;break;
      case 5: ExtZZSwingTF = 10080;

     }
 
simoncs: extern int ExtZZSwingTF=3; ...
Learn to count! What is the third datatype for the function, what are you passing?
iBarShift(Symbol(),ExtZZSwingTF,i)
          ^First^^,^^^second^^^,^Third
 
WHRoeder:
Learn to count! What is the third datatype for the function, what are you passing?
didn't think you meant that one as thought you would already know that -  it's an integer as per in previous posts, just declared globally so it could be used in print statements.
 
Answer the question "What is the third datatype for the function"
Reason: