Ngày tương ứng HHV thì đúng nhưng ngày LLV thì không đúng mà chưa hiểu lý do tại sao.( ngày LLV có giá đóng cửa 87.2 là ngày 4/10 trong khi nó báo là 2/11).
periods = Param( "Period", 20, 5, 200, 5 ); dt = DateTime(); hh = HHV( H, periods ); nwhh = hh > Ref( hh, -1 ); // Detect new points where hhv is different from previous one dthh = ValueWhen( nwhh, dt, 1 ); // Assign the date where the above condition was true ll = LLV( L, periods ); nwll = ll < Ref( ll, -1 ); // Same logic as hh dtll = ValueWhen( nwll, dt, 1 ); Filter = 1; AddColumn( H, "High", 1.2 ); AddColumn( hh, "HHV", 1.2 ); AddColumn( IIf( nwhh, 1, Null ), "New HHV", 1 ); AddColumn( dthh, "DT last HHV", formatDateTime, -1, -1, 150 ); AddColumn( L, "Low", 1.2 ); AddColumn( ll, "LLV", 1.2 ); AddColumn( IIf( nwll, 1, Null ), "New LLV", 1 ); AddColumn( dtll, "DT last LLV", formatDateTime, -1, -1, 150 ); SetSortColumns( -2 ); // see most recent dates at top LastHHVDate = LastValue( dthh ); LastLLVDate = LastValue( dtll ); _TRACE( "Last HHV date: " + DateTimeToStr( LastHHVdate ) ); _TRACE( "Last LLV date: " + DateTimeToStr( LastLLVdate ) );
Mình dùng đoạn code này, nhưng ngày của LLV không đúng trong khi ngày của HHV thì ok, không hiểu làm sao.
đoạn code mình post trên, mình đã chỉnh sửa ở 2 dòng từ code gốc của Pepe, bạn chạy lại thử xem có ra kết quả đúng không? nwhh = hh > Ref( hh, -1 ); // Detect new points where hhv is different from previous one và nwll = ll < Ref( ll, -1 ); // Same logic as hh bạn đọc về hàm ValueWhen của Amibroker thì sẽ hiểu vì sao nó chạy ra không đúng, và chỉnh sửa code lại để phù hợp với mục đích của bạn là được. ref link: http://www.amibroker.com/kb/2014/09/29/debugging-techniques-part-1-exploration/
Uhm, để mình kiểm tra lại xem sao. Mà mục đích của bạn là code cho Indicator hay code cho Automatic Analysis
Có lẽ đây là đoạn code mà bạn cần. Enjoy it! function GetDateTimeHHVorLLV(periods, type) { dtll = dthh = dt = DateTime(); switch(type){ case "HHV": for(i = BarCount; i>= BarCount - periods; i--){ hh = HHV(H, periods); nwhh = hh > Ref(hh, -1); dthh = ValueWhen( nwhh, dt, 1 ); dt = dthh; } break; case "LLV": for(i = BarCount; i>= BarCount - periods; i--){ ll = LLV(L, periods); nwll = ll < Ref(ll, -1); dtll = ValueWhen( nwll, dt, 1 ); dt = dtll; } break; } return dt; } periods = Param("Periods", 20, 10, 100); hh = HHV( H, periods ); nwhh = hh > Ref( hh, -1 ); // Detect new points where hhv is different from previous one //dthh = ValueWhen( nwhh, dt, 1 ); // Assign the date where the above condition was true ll = LLV( L, periods ); nwll = ll < Ref( ll, -1 ); // Same logic as hh //dtll = ValueWhen( nwll, dt, 1 ); dtHHV = GetDateTimeHHVorLLV(periods, "HHV"); dtLLV = GetDateTimeHHVorLLV(periods, "LLV"); Buy = Sell = Short = Cover = 0; Filter = 1; AddColumn( H, "High", 1.2 ); AddColumn( hh, "HHV", 1.2 ); AddColumn( IIf( nwhh, 1, Null ), "New HHV", 1 ); AddColumn(dtHHV, "dt_HHV", formatDateTime, -1, -1, 150 ); AddColumn( L, "Low", 1.2 ); AddColumn( ll, "LLV", 1.2 ); AddColumn( IIf( nwll, 1, Null ), "New LLV", 1 ); AddColumn(dtLLV, "dt_LLV", formatDateTime, -1, -1, 150); SetSortColumns(-2);
Bạn đọc thêm tại link này để hiểu cách AFL làm việc https://www.amibroker.com/guide/h_understandafl.html