Explore Output Seems Wrong, While Trace Output is Correct

Having used AmiBroker for a lot of years (Thanks to @Tomasz and this forum), I should have been able to figure out and fix this problem, but I sadly have come to this group stuck.

I am using two Foreign symbols to create a signal to use with two other tickers in the watchlist (SPY, QQQ).

Up to the step
Ratio1 = (Sig1 - Sig2 ) ;
Calculates correctly, but when I try to recode to SignalA, it is not being handled correctly in the explore (and hence backtest not shown), but the results are as expected when I show it in a Trace.
I even tried to "stabilized" the data being passed using StaticVarSet & Get, but that made no difference.

Here is a stripped-down version of the code:

// Watchlist = SPY,QQQ

{_SECTION_BEGIN("backtestRotational Setup");
SetBacktestMode( backtestRotational ); 	// 
				
	SetOption("InitialEquity", 100000 );
	SetOption("AllowSameBarExit", False );
	SetOption("ActivateStopsImmediately", False );
	SetOption("AllowPositionShrinking", True );
	SetOption("FuturesMode", False );
	SetOption("InterestRate", 0 );
	SetOption("MinPosValue", 1 );
	SetOption("MinShares", .001 );
	SetOption("PriceBoundChecking", True );
	SetOption("ReverseSignalForcesExit", True ); 
	SetOption("UsePrevBarEquityForPosSizing", False );		
	SetOption("EveryBarNullCheck", False );
	SetOption("EarlyExitFee", 0 );
	SetOption("HoldMinBars", 0 );
	SetOption("HoldMinDays", 0 );
	SetOption("EarlyExitDays", 0 );
	SetOption("DisableRuinStop", False );
	if (Version() > 5.9) SetOption("WarningLevel", 1);
			
	SetOption( "CommissionMode", 3 ); /* 3 set commissions $ per share; 1=% of trade */
	SetOption( "CommissionAmount", .01 ); /* per share  + SLIPPAGE */
	SetOption ("AccountMargin", 100 );   
	SetOption("ExtraColumnsLocation", 1 );
	MaxPositions = Param("# of Positions", 1, 1, 5, 1);
	SetOption("MaxOpenPositions", MaxPositions);
	Symbol = Name();
_SECTION_END(); }

function TRACE(OutputString)
{
	global DBVOutput;

	if(DBVOutput) _TRACE(OutputString);
}
 
DBVOutput = ParamToggle("DebugView","OFF|ON", 1); 
TRACE("Now Running on: " + Name());
dt = DateTime();

if ( Status( "stocknum" ) == 0 OR Status("stocknum") == -1 )
	{ 
	StaticVarRemove( "*"    );
	}
	
	// Nxt day Close
	SetTradeDelays( 1,1,1,1);  // 1 day delay
	BuyPrice = CoverPrice = SellPrice = ShortPrice = Close;  // trade the close

	SigSymbol1 = "QQQE";
	SigSymbol2 = "RSP";

	Sig1c = Foreign( SigSymbol1, "C" );
	Sig2c = Foreign( SigSymbol2, "C" );

   Sig1=  ROC(ema(Sig1c, 100),5);
  Sig2 =  ROC(ema(Sig2c, 100),5); 	

	StaticVarSet("Sig1a" + Symbol, Sig1);
	StaticVarSet("Sig2a" + Symbol, Sig2);

 	OKDay = True;


  Sig1 = StaticVarGet("Sig1a" + Symbol);
  Sig2 = StaticVarGet("Sig2a" + Symbol);
  Ratio1 = (Sig1 - Sig2 ) ; 
 
 StaticVarSet("Ratio1a" + Symbol, Ratio1);

   Ratio1 = StaticVarGet("Ratio1a" + Symbol);
  SignalA = iif( Ratio1 >0,2,1);
  
 startdate = ParamDate( "Start date", "2017-01-31");
 enddate = ParamDate( "Start date", "2017-02-28");
//enddate = ParamDate( "End date", Now(1) );

dn = datenum();
datewindow = dn >= startdate and dn <= enddate;

for( i = 0; i < BarCount; i++ ) {
   if( datewindow[i] ) { 
   TRACE("Ticker " + Name() +  " Date: " + DateTimeToStr(dt[i], 1) + " Ratio1= " + Ratio1[i] + " SignalA=" + SignalA[i] )  ;
  } }
  
  Score[0] = 0;
  Score = iif( SignalA = 1 && Symbol== "QQQ", 100,
			iif( SignalA = 2 && Symbol== "SPY", 100, ref(Score,-1)));
  
	Filter =  1;
	
if (Status("action") == actionExplore)
{
AddColumn(C, "C",1.2);
AddColumn(SIG1, "SIG1",1.2);
AddColumn(Sig2, "Sig2",1.2);

AddColumn(Ratio1, "Ratio1", 1.2);
 AddColumn(SignalA, "SignalA", 1.1);
AddColumn(Score, "Score",1.0);
//AddColumn(PositionScore, "PositionScore",1.0);
 }

As an example, On Jan 31, 2017 , ratio was >0, so results should be a 2.
Trace shows it in the picture

However in Explore, the values shown are SPY 1 (wrong) and QQQ 0 (how does 0 get returned?).

Any and all help is deeply appreciated.

Kind Regards,
Scott

Your problem is here:

  Score = iif( SignalA = 1 && Symbol== "QQQ", 100,
			iif( SignalA = 2 && Symbol== "SPY", 100, ref(Score,-1)));

The first clause of the IIf() is meant to evaluate to a boolean value, but instead of using == (equality check) you used = (assignment). Change that, and everything seems to work.

Also, you can eliminate all the static variable stuff you added. It's not "stabilizing" anything, it's just cluttering up your code which makes it hard to read and slower to execute.

3 Likes

Thanks @mradtke ; 100% right - I was focused on the code step right before that, but setting both to the == fixed everything and wrong assignment screwed up earlier step. I do know how that works but I was blind from looking at the code too long.

Really appreciate it!
Cheers

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