Use foreign ticker to create a „sliding” buy signal

Hello everybody,
I would like to backtest a strategy which combines VIX and SP500 conditions (source:
The rules are as follows:

  1. Check if VIX daily close is above daily Bollinger band
  2. Check if VIX daily close is above weekly Bollinger band
  3. Buy SP500 on first candle which closes above previous day high


I managed to implement 1. and 2. but have no idea how to implement a “sliding” Close > Ref(Close, -1) condition.

Thanks in advance.

1 Like

What is the issue with C > ref(h,-1) ?

Hey Peter1,
thanks for your response. Sorry, Close > Ref(Close, -1) was a typo. Let me try to explain better

Here my full code.

// ### INDICATOR ###

Array = ParamField("Price Array",-1);

Period_Daily = Param("Period Daily", 20, 2, 300, 1 );
Width_Daily = Param("Width Daily", 2, 0, 10, 0.05 );
Color_Daily = ParamColor("Color Daily", colorBlue );
Style_Daily = ParamStyle("Style Daily");

Period_Weekly = Param("Period Weekly", 20, 2, 300, 1 );
Width_Weekly = Param("Width Weekly", 2, 0, 10, 0.05 );
Color_Weekly = ParamColor("Color Weekly", colorRed );
Style_Weekly = ParamStyle("Style Weekly", styleThick);

// Daily Time Frame
BBandTop_Daily = BBandTop(Array, Period_Daily, Width_Daily);
BBandBot_Daily = BBandBot(Array, Period_Daily, Width_Daily);

Plot( BBandTop_Daily, "BBTop Daily" + _PARAM_VALUES(), Color_Daily, Style_Daily ); 
Plot( BBandBot_Daily, "BBBot Daily" + _PARAM_VALUES(), Color_Daily, Style_Daily ); 

// Weekly Time Frame

WeeklyClose = TimeFrameCompress(Array, inWeekly);
BBandTop_Weekly = BBandTop(WeeklyClose, Period_Weekly, Width_Weekly);
BBandBot_Weekly = BBandBot(WeeklyClose, Period_Weekly, Width_Weekly);

BBandTop_Weekly = TimeFrameExpand(BBandTop_Weekly, inWeekly);
BBandBot_Weekly = TimeFrameExpand(BBandBot_Weekly, inWeekly);

Plot( BBandTop_Weekly, "BBTop Weekly" + _PARAM_VALUES(), Color_Weekly, Style_Weekly ); 
Plot( BBandBot_Weekly, "BBBot Weekly" + _PARAM_VALUES(), Color_Weekly, Style_Weekly );

// ### EXPLORATION ###

// SP-500 above yersterday's high
Filter_SP500 = Close > Ref(High,-1);

// VIX daily close above BBandTop Weekly and Daily


	Period_Daily = 20;
	Width_Daily = 2;
	Period_Weekly = 20;
	Width_Weekly = 2;

	VIX_Close_Daily = Close;
	VIX_BBandTop_Daily = BBandTop(Close,Period_Daily,Width_Daily);

	VIX_BBandTop_Weekly = BBandTop(Close,Period_Daily,Width_Daily);


Filter_VIX_BBandTop_Daily = Foreign("^VIX","Close") > VIX_BBandTop_Daily;
Filter_VIX_BBandTop_Weekly = Foreign("^VIX","Close") > TimeFrameExpand(VIX_BBandTop_Weekly,inWeekly);

//Hold(Filter_VIX_BBandTop_Daily, 2);
//Hold(Filter_VIX_BBandTop_Weekly, 2);

Filter = 1;
AddColumn( Close, "Close SP-500" );
AddColumn( VIX_CLose_Daily, "VIX_CLose_Daily" );
AddColumn( VIX_BBandTop_Daily, "VIX_BBandTop_Daily" );
AddColumn( VIX_BBandTop_Weekly, "VIX_BBandTop_Weekly" );
AddColumn( Filter_VIX_BBandTop_Daily, "Filter_VIX_BBandTop_Daily" );
AddColumn( Filter_VIX_BBandTop_Weekly, "Filter_VIX_BBandTop_Weekly" );

// ### SCAN & BACKTEST ###
//Buy = Filter_VIX_BBandTop_Daily AND Filter_VIX_BBandTop_Weekly;
Buy = Filter_SP500 AND Filter_VIX_BBandTop_Daily AND Filter_VIX_BBandTop_Weekly;

I first ran an exploration and can see that indicator values correspond with column output.
Additionally a buy condition without SP500 buy shows correct buy signals. In the time between 01.08.21 and 18.10.2021 I see four signals coming from VIX. In all four cases daily VIX close is above daily and weekly VIX filter.

Buy = Filter_VIX_BBandTop_Daily AND Filter_VIX_BBandTop_Weekly;


When I add SP500 buy condition. There is no signal at all. I think the reason is, that none of the four candles from above fulfil the additional condition Close > Ref(High, -1). If I look on the chart this makes perfectly sense as there is no close above previous high on 18.8, 19.08, 20.09 and 21.09..

Buy = Filter_SP500 AND Filter_VIX_BBandTop_Daily AND Filter_VIX_BBandTop_Weekly;


What I would like to achieve is, that the code will hold the first true from VIX filter until there is a buy for SP500.

I did more search in forum and found this thread, which describes something very similar but I can't figure out how to use Hold() or Flip() with BarsSince().

Any help is very appreciated!

1 Like

How long should your VIX condition be true? You do not want to wait for 1000 bars?
You could work with sum and a 10 day lookback (which I did not test!):
Buy = Filter_SP500 AND sum(Filter_VIX_BBandTop_Daily,10)>0 AND sum(Filter_VIX_BBandTop_Weekly,10)>0;


Hey Peter1,
it works great! :slightly_smiling_face:


1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.