VALUEWHEN of an OFFSET

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
Is this something that just isn't possible as VALUEWHEN doesn't have an OFFSET facility?

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!! :slight_smile:
https://forum.optuma.com/topic/back-test-results-2/

Hi Kim,

Offsets are used with bar values not signals, so an offset of 1 will get the value of the bar before the signal, not the previous signal.

Here’s a work around using the NonZero function, where variable P2 gets the bardate of the previous signal, and EntryPrice2 get the close value on that bardate i.e. $12.42 in the case of AERI:

ShortMA1 = MA(BARS=10) ; 
ShortMA2 = MA(BARS=20) ; 

EntrySignal = ShortMA1 CrossesAbove ShortMA2 ; 
P1 = BARDATE();
//Get bardate of previous signal;
P2 = LAST(BARDATE(NONZERO(EntrySignal), OFFSET=1));
PrevSignal = P1 == P2;
EntryPrice2 = ValueWhen(PrevSignal);
EntryPrice2

Capture

Hi again, an FYI.

The script works perfectly on a chart, but the back tester always sees the latest signal as of today rather than the latest signal as of the point in time being back tested. Oh well, looks like I’ll have to wait for the new back tester.

Thanks again

Kim