DALE ! FIND Nth INSTANCE OF A RULE THEN RETURN THE VALUE OF AN INDICATOR AT THAT DATE ! Coded by: Richard Denning 4/23/09 ! www.TradersEdgeSystems.com ! MetaStock or AmiBroker function - duplicate the output in AIQ of the following function: ! ValueWhen(formula, Indicator, instance). ! Example that is coded below: ! formula = close crosses above a 10 day simple moving average ! indicator = high price ! instance = 1st time, 2nd time, etc. ! In AIQ we can not create functions or subroutines. All code must be in each EDS file and ! we can not refer to code that is in another file so we can't call a function that is in another ! EDS file multiple times to get different results (e.g. 1st time, 2nd time) but we can code up ! a UDF that will give the same resutls. It is just that we have to code each instance separately ! and we must code the 1st instance to get the 2nd instance etc. ! In the following example, 5 instances have been coded. If additional instances are needed, ! just continue the logic. ! The "ListAll" report displays the value of the first five instances of the high of the bar when the ! close of the bar crosses from below to above the simple 10 day moving average. ! To get other rules, just copy and paste this code to a new EDS file and then change the ! rule marked "! FORMULA-CHANGE TO SUIT" to a different rule of your choosing. ! also change "! INDICATOR-CHANGE TO SUIT" to whatever indicator value you ! wish to find on the rule dates and instances. RULE if C > SMA10 and valrule(C < SMA10,1). ! FORMULA-CHANGE TO SUIT INDICATOR is [high]. ! INDICATOR-CHANGE TO SUIT C is [close]. SMA10 is simpleavg(C,10). OSD is OffSetToDate(month(),day(),year()). ! FIRST INSTANCE: RULE_1stDte is scanany(RULE,200,0). ! DATE OF FIRST INSTANCE RULE_1stOS is scanany(RULE,200,0) then OSD. ! OFFSET IN BARS TO FIRST INSTANCE Val_1st is valresult(INDICATOR,^RULE_1stOS). ! VALUE OF INDICATOR ON FIRST INSTANCE ! SECOND INSTANCE: RULE_2ndDte is scanany(RULE,200,^RULE_1stOS+1). RULE_2ndOS is scanany(RULE,200,^RULE_1stOS+1) then OSD. Val_2nd is valresult(INDICATOR,^RULE_2ndOS, 0). ! THIRD INSTANCE: RULE_3rdDte is scanany(RULE,200,^RULE_2ndOS+1). RULE_3rdOS is scanany(RULE,200,^RULE_2ndOS+1) then OSD. Val_3rd is valresult(INDICATOR,^RULE_3rdOS, 0). ! FORTH INSTANCE: RULE_4thDte is scanany(RULE,200,^RULE_3rdOS+1). RULE_4thOS is scanany(RULE,200,^RULE_3rdOS+1) then OSD. Val_4th is valresult(INDICATOR,^RULE_4thOS, 0). ! FIFTH INSTANCE: RULE_5thDte is scanany(RULE,200,^RULE_4thOS+1). RULE_5thOS is scanany(RULE,200,^RULE_4thOS+1) then OSD. Val_5th is valresult(INDICATOR,^RULE_5thOS, 0). ListAll if 1.  Rule Library CCodeViewListAll CReportViewSymbol<Val_1st8 RULE_1stDteO RULE_1stOSIVal_2nd; RULE_2ndDteS RULE_2ndOSSVal_3rd= RULE_3rdDteO RULE_3rdOSJVal_4th? RULE_4thDteT RULE_4thOSPVal_5th> RULE_5thDteN RULE_5thOSOdAףAE?̂AEA= AE`AWAEA= ApEE@>E@H>pEA?PEAH>0EAAFSI 'AE@ffAEPAAxEpA@EA)\ AEBAG)\AE33AE`AAEAAElBGAPEBAGIIGAEAA@EAzAEABxE8BBEhBAGM C@E?333@EAz4@E0AI@EPA(L@EAAGNq=FBE@,BHEA= BEAHB0E\B= BEhBAGOAE?@E`A@EARA0E\BQ AEhBAGPAEAXEAAEAףAEAAE,BAGYS@E?33@E`Aq=z@EA)\@EBw@EBAHII(?EA33?xE8Bz?PELB?0EB33?EBAHL33AE`ApAEAAE B(AHEPBAEhBAHR?E?E@@>E@A ?EA?0E\BAHS\@E?@E`A̬@`EAQ@EAQ@E,BAHTG@EAQ?E`A33?EAq=?EA?xE8BAIGR?E?)\?EA>EA= >EAz?EhBAIMCף@E)\@E@)\@EA33@EPA33@(EAAINq=AE?ףAE@@EA((AhE@BQ(AHEPBAINVR@E?,@`EA@HEA{ApEBq=A8EBAIQp@E Aq=@XEA{@HEAQ@0EAAE BAIRRjAE?LAE`AQA0EA