Objective is to track the relative performance of two symbols (SPY + QQQ), and set a flag indicating whichever one is current doing better. We then do not allow that flag to be changed for 22 bars, after which we set the flag to whichever again is stronger, which might mean no change in flag state or a switch. Once 22 bars have passed we continue to check every bar for the potential change.
I rarely use loops (not very good with them) and attempted to solve this with normal afl array formulas, but failed, so I tried with a loop. I am trying to use the barsinSPY + barsinQQQ variables to track and count # of bars they have been in that state, but I somehow screwed up those counters.
I spend a good amount of time with manual/forum attempting to adapt examples, but not successfully.
Your help is greatly appreciated – I should have been able to do this without bothering you. Code and current explore output enclosed.
_SECTION_BEGIN("Relative Str SPY vs QQQ Index Filter");//
{
SPYTick = ParamStr( "SPY ETF", "SPY" );
SPYcl = Foreign(SPYTick, "C"); // fc = Foreign( symbol, "C" );
QQQTick = ParamStr( "QQQ ETF", "QQQ" );
QQQcl = Foreign(QQQTick, "C");
SPYEMA = Roc(SPYcl,10); //
QQQEMA = Roc(QQQcl,10);
SwitchIndsignal = iif( SPYEMA >= QQQEMA,1 , 2);
_SECTION_END();
}
Position = 2;
barsinSPY[ 0 ]=0;
barsinQQQ[ 0 ] =0;
for( i = 0; i < BarCount; i++ )
{
if (position[ i ]==1 ) // currently in SPY
{
if (SwitchIndsignal[ i ]==2 AND barsinSPY[ i ] >22) // chg to QQQ
{
Position[ i ] = 2;
barsinSPY[ i ] = 0;
barsinQQQ[ i ] = 1;
}
else if (SwitchIndsignal[ i ]==1)
{ barsinSPY [ i ]= barsinSPY[ i-1]+1 ; }
}
if (position[ i ]==2) // currently in QQQ
{
if (SwitchIndsignal[ i ]==1 AND barsinQQQ[ i ] >22) // chg to SPY
{
Position[ i ] = 1;
barsinSPY[ i ] = 1;
barsinQQQ[ i ] = 0;
}
else barsinQQQ++; // Trying different was to increast counter here
}
}
Filter=1;
AddColumn(SPYEMA , "SPYEMA", 1.1);
AddColumn(QQQEMA , "QQQEMA", 1.1);
AddColumn(SwitchIndsignal , "SwitchIndsignal", 1.0);
AddColumn(Position , "Position", 1.0);
AddColumn(barsinSPY , "barsinSPY", 1.0);
AddColumn(barsinQQQ , "barsinQQQ", 1.0);type or paste code here
Thank you for post Henri.
This is not intended to trade these symbols, rather it was intended as an indicator into another more complex rotation system trading other assets; using the state of this variable is a valuable risk on/risk off vote.
Your post gave me an idea - I might translate this into a quick traditional rotation system and use static var set to store the output for later system.
As I was struggling with the looping approach, to meet my objective of creating an indicator of relative index strength while restricting frequency of changing "signals", I switched to using a simple rotation code structure. The results in the trade history are exactly as intended, however, short of creating a custom CBT code base to capture/store an array of these trades, I am at a loss how to create a staticvarset with trade results. The existing variables do not capture the information I need, but the final open positions do have what I need.
Specifically, I want to access "open_position" , and store it to a staticvar so that I can use it as an indicator flag in other independent models.
Guidance/suggestions on this is again appreciated!
Enclosed is a simplified version of the rotation code:
SetBacktestMode( backtestRotational);
SetOption("HoldMinBars", 22 );
SetOption("HoldMinDays", 32 );
SetOption("MaxOpenPositions", 1);
_SECTION_BEGIN("Relative Str SPY vs QQQ Index Filter");//
{ RelStrLB =14;
ROC14 = Roc(c,RelStrLB);
RocScore = ROC14 + 100; // Making positive so no shorts
_SECTION_END();
}
_SECTION_BEGIN("trade");
OKDay=1;
PositionScore = IIf(OKDay, RocScore, scoreNoRotate) ;
_SECTION_END();
OK, maybe this won't work - let me clarify further. The system switches between SPY & QQQ, using combination of the relative strength plus minholding days to result in the position history I want to capture. I need to access the historical array of daily holdings to get the signal (SPY vs QQQ) for that day as an indicator for another system.
I don't think the Foreign( "~~~EQUITY", "I") captures the actual symbol in array.
Any suggestions on how to capture/store that open position history is appreciated!