Hi,
I’m trying to determine the price of 2 signals, but can’t get VALUEWHEN to return the earlier signal. I’ve indicated in the code what isn’t working.
In the attached screenshot:
- The green arrows are entry signals
- The red arrow is an exit signal
- The hover shows the price of the earliest entry signal
- The column "Signal" shows the value of Entry2 in the code below; it's the value of the latest entry signal
Thanks
Kim
MaxDays = 30 ; // Number of days trade allowed to run before checking whether to exit MaxDrop = 20 ; // Maximum percentage loss allowed before trade is exited MaxDropPct = MaxDrop / 100 * -1 ; // Convert to a negative percentage ShortMA1 = MA(BARS=10) ; ShortMA2 = MA(BARS=20) ; LongMA1 = MA(BARS=50) ; LongMA2 = MA(BARS=100) ; EntrySignal = ShortMA1 CrossesAbove ShortMA2 ; PrevSignal = OFFSET(ShortMA1 CrossesAbove ShortMA2, OFFSET=1) : // This doesn't work ExitSignal = LongMA1 CrossesBelow LongMA2 ; LastSignalDays = TIMESINCESIGNAL(Entry, UNIT=Days, ) ; PreviousSignalDays = TIMESINCESIGNAL(EntrySignal, OFFSET=1) ; LastExitDays = TIMESINCESIGNAL(ExitSignal) ; // Find earliest entry after a previous exit // Determine whether there has been successive entry signals without an exit EntryGap = PreviousSignalDays - LastSignalDays ; ExitGap = LastExitDays - LastSignalDays ; SuccessiveEntries = IF(ExitGap > EntryGap, 1, 0) ; // Store days from earliest entry signal DaysSinceSignal = IF(SuccessiveEntries, PreviousSignalDays, LastSignalDays) ; // Get value from each of the previous entries EntryPrice1 = VALUEWHEN(EntrySignal) ; EntryPrice2 = VALUEWHEN(EntrySignal[1]) ; // This doesn't work // Store price from earliest entry signal EntryPrice = IF(SuccessiveEntries, EntryPrice2, EntryPrice1 ) ; // Doesn't work because of EntryPrice2 CurrentResult = (Close() - EntryPrice) / Close() ; MaxDaysReached = IF(DaysSinceSignal > MaxDays, 1, 0) ; MaxDropReached = IF(CurrentResult < MaxDropPct, 1, 0) ; MaxDaysReached and MaxDropReached
PS This is related to an enhancement suggestion that Mathew said he’d look at, I’m just impatient!!
https://forum.optuma.com/topic/back-test-results-2/