How to build a period condition into a current function

 

Hi, All

Please help me with a coding question. I would need to modify one of my current indi to quarterly level and yearly (the current one works well on weekly and monthly levels).

For quarterly -- new levels are only calculated if it's in month of Jan/Apr/Jul/Sept (ie the beginning of the month), and the lines are plotted through to the end of the quarter;

For yearly -- new levels are only calculated if it's in month of Jan (ie the beginning of the year) and the lines are plotted through to the end of the year.

In essence, it's similar to quarterly and yearly pivots, but I have some extra calculations (not using range only of the last year, so the indi has available options to choose how many periods to include in the calculation of the range), so can't use any existing yearly indicators out there. For example, if I want to use previous 3 years' range, it would be the range of previous 36 monthly candles, if 5 years, 60 monthly candles etc. So I can't simply use last_year_high/low/close.

The main part of the code that does the calculation is as follows. I guess it needs a While loop somewhere, but my coding knowledge is too basic, I don't know how to handle this. Please could anyone help modify the code? 


Many thanks in advance!

for (i=limit; i>=0;i--)
   
  
   double CandleNum[5000];

   
   {
      
      // If the pivot day changes...
      if( TimeFrame == 1 )
      {
         // Determine High & Low for the previous Period
         CandleNum[i] = iBarShift( NULL, PERIOD_D1, Time[i], false);
         PastHigh[i] = iHigh(NULL, PERIOD_D1, iHighest( NULL, PERIOD_D1, MODE_HIGH, DailyPeriod, CandleNum[i]+1 ) ); // Pivot Day high
         PastLow[i] = iLow(NULL, PERIOD_D1, iLowest( NULL, PERIOD_D1, MODE_LOW, DailyPeriod, CandleNum[i]+1 ) );     // Pivot Day low
         PastClose[i] = iClose( NULL, PERIOD_D1, CandleNum[i]+1 ); 
         Comment("");
         // Pivot calculations
         Pivot[i] = ( PastHigh[i] + PastLow[i] + PastClose[i] ) / 3;    // Pivot point
         Range[i] = PastHigh[i] - PastLow[i];
         Res1[i] = Pivot[i] + SR1*Range[i];//2 * Pivot[i] - PastLow[i];                     // R1
         
         Res3[i] = Pivot[i] + SR3*Range[i];//Res1[i] + Range[i];                            // R3
         Sup1[i] = Pivot[i] - SR1*Range[i];//2 * Pivot[i] - PastHigh[i];                    // S1
                             // S2
         Sup3[i] = Pivot[i] - SR3*Range[i];//Sup1[i] - Range[i];                            // S3
         
         if(Res1[i]!=Res1[i+1]) Res1[i+1] = EMPTY_VALUE;
         if(Res2[i]!=Res2[i+1]) Res2[i+1] = EMPTY_VALUE;
         if(Res3[i]!=Res3[i+1]) Res3[i+1] = EMPTY_VALUE;
         if(Sup1[i]!=Sup1[i+1]) Sup1[i+1] = EMPTY_VALUE;
         if(Sup2[i]!=Sup2[i+1]) Sup2[i+1] = EMPTY_VALUE;
         if(Sup3[i]!=Sup3[i+1]) Sup3[i+1] = EMPTY_VALUE;
      }
 
Please can anyone take a look and give some suggestions?
 
Alex :

Halo semua

Tolong bantu saya dengan pertanyaan coding. Saya perlu merasakan salah satu indi saya saat ini ke tingkat triwulanan dan tahunan (yang sekarang bekerja dengan baik pada tingkat pertama dan bulanan).

Untuk kuartalan - level baru saja dihitung jika berada di bulan Jan / Apr / Jul / Sept (yaitu awal bulan), dan garis diplot sampai akhir kuartal;

Untuk tingkat tahunan - baru hanya dihitung jika berada di bulan Januari (yaitu awal tahun) dan garis diplot sampai akhir tahun.Untuk tingkat tahunan - baru hanya dihitung jika berada di bulan Januari (yaitu awal tahun) dan garis diplot sampai akhir tahun.

Intinya, ini mirip dengan pivot kuartalan dan tahunan, namun saya memiliki beberapa perhitungan ekstra (tidak menggunakan rentang hanya tahun lalu, jadi indi memiliki pilihan yang tersedia untuk memilih berapa periode yang harus disertakan dalam perhitungan kisaran), sehingga dapat gunakan indikator tahunan yang ada di luar sana. Misalnya, jika saya ingin menggunakan rentang 3 tahun sebelumnya, itu akan menjadi kisaran 36 lilin bulanan sebelumnya, jika 5 tahun, 60 lilin bulanan dll. Jadi, saya tidak bisa begitu saja menggunakan last_year_high / low / close.

Bagian utama dari kode yang melakukan perhitungan adalah sebagai berikut. Kurasa perlu loop Sementara di suatu tempat, tapi pengetahuan pengkodeanku terlalu mendasar, aku tidak tahu bagaimana menangani ini. Tolong ada yang bisa membantu memodifikasi kode? 


Banyak terima kasih sebelumnya!

 
4731055 :
 Double CandleNum [5000];
Reason: