Equity() affects plotted trailing stop line

Hello experts,
I can observe that Equity(1,0) and Equity(0,0) affects scaling in my code. Due Tomasz comment in online help this makes sense as, Equity(1,0) removes signals and Equity(0,0) not.

But in addition to this I can also observe that Equity(0,0), which allows scale in, also affects stop line plot in the chart.

Equity(1,0) -> red stop line, plot and sell signal match
01_Equity_1-0
02_Equity_1-0

Equity(0,0) -> blue stop line plot and sell signal don't match
03_Equity_0-0
04_Equity_0-0

To reproduce change Equity(1,0) to Equity(0,0) in line 60

//### Change Equity(1,0) or Equity(0,0)
Equity(1,0);
//###
// https://www.quanttrader.com/index.php/vix-spx-timing-strategie/KahlerPhilipp2015

// ### INDEX ###
TrendFilterIndex = BuyFilerIndex = 0;
TrendFilterIndex = Close > MA(Close,200); // Index above MA
BuyFilterIndex = Close > Ref(High,-1); // Close above previous day high

Buy_Signal_Index = TrendFilterIndex AND BuyFilterIndex;

// ### VIX ###

SetForeign("^VIX"); // Get VIX
	
	PeriodDaily = WidthDaily = 0;
	PeriodWeekly = WidthWeekly = 0;
	
	PeriodDaily = 20;
	WidthDaily = 2;
	PeriodWeekly = 20;
	WidthWeekly = 2;
	
	
	CloseDailyVIX = BolliTopDailyVIX = BolliTopWeeklyVIX = 0;
	
	CloseDailyVIX = Close;
	BolliTopDailyVIX = BBandTop(Close, PeriodDaily, WidthDaily);

	TimeFrameSet(inWeekly);
	BolliTopWeeklyVIX = BBandTop(Close, PeriodWeekly, WidthWeekly);

TimeFrameRestore();

BolliDailyFilterVIX = BolliWeeklyFilterVIX = Buy_Signal_VIX = 0;

BolliDailyFilterVIX = Foreign("^VIX","Close") > BolliTopDailyVIX;
BolliWeeklyFilterVIX = Foreign("^VIX","Close") > TimeFrameExpand(BolliTopWeeklyVIX,inWeekly);

Buy_Signal_VIX = BolliDailyFilterVIX * BolliWeeklyFilterVIX;

// ### INDEX + VIX ###
SlidingWindowSum = Buy_Signal_Sum = Buy_Signal_Sum = 0;

SlidingWindowSum = 10;
Buy_Signal_Sum = Sum(Buy_Signal_VIX, SlidingWindowSum) > 0 AND Buy_Signal_Index;
Buy_Signal_Flip = Ref(Flip(Buy_Signal_VIX,Buy_Signal_Index),-1) AND Buy_Signal_Index;

// ### BECKTEST ###
SetPositionSize(1, spsShares);
//Buy = Buy_Signal_Sum;
Buy = IIf(Buy_Signal_Flip, sigScaleIn, 0);

Sell = 0; // Trailing stop

// ### Trailing Stop ###

// Stop in %
StopValue = 6;
ApplyStop(stopTypeTrailing, stopModePercent, StopValue, 1, True);

//### Change Equity(1,0) or Equity(0,0)
Equity(1,0);
//###

InTrade = Flip(Buy,Sell);
SetOption("EveryBarNullCheck", True);
StopLine = IIf( InTrade, HighestSince( Buy, High ) * ( 1 - 0.01 * StopValue ), Null );
Plot(StopLine, "Trailing Stop", colorBlue, styleLine, 0, 0, 0, 0, 1); 
//PlotShapes(Buy*shapeUpArrow, colorGreen, 0, High);

// ATR Stop
//StopValue = 3*ATR(10);
//ApplyStop(stopTypeTrailing, stopModePoint, StopValue, 1, True, 1);
//Equity(0,0);
//InTrade = Flip(Buy,Sell);
//SetOption("EveryBarNullCheck", True);
//StopLine = IIf( InTrade, HighestSince( Buy, High - StopValue ), Null );
//Plot(StopLine, "Trailing Stop", colorRed, styleLine, 0, 0, 0, 0, 1); 
//PlotShapes(Buy*shapeUpArrow, colorGreen, 0, High);

// ### DEBUG ###
Filter = 1;

AddColumn( TrendFilterIndex, "TrendFilterIndex" );
AddColumn( BuyFilterIndex, "BuyFilterIndex" );
AddColumn( Buy_Signal_Index, "Buy_Signal_Index" );

AddColumn( CloseDailyVIX, "CloseDailyVIX" );
AddColumn( BolliTopDailyVIX, "BolliTopDailyVIX" );
AddColumn( TimeFrameExpand(BolliTopWeeklyVIX,inWeekly), "BolliTopWeeklyVIX" );

AddColumn( BolliDailyFilterVIX, "BolliDailyFilterVIX" );
AddColumn( BolliWeeklyFilterVIX, "BolliWeeklyFilterVIX" );
AddColumn( Buy_Signal_VIX, "Buy_Signal_VIX" );

AddColumn( Buy_Signal_Sum, "Buy_Signal_Sum" );
//AddColumn( Buy_Signal_Flip, "Buy_Signal_Flip" );

Thanks in advance!

Spend some time to figure out why this happens but couldn't resolve it. E.g. trailing stop example which uses loop instead ApplyStop causes the same issue.

My workaround is now to separate backtest and indicator code. I do this using #include feature.

#include "C:\Program Files\AmiBroker\Formulas\Custom\01 Strategies\VIX_SP500_Timing\Strategy_VIX_SP500_Timing_ScaleIn_v1.0.afl"

Equity(1,0); // Equity (1,0) needed for correct ploted plot line but no scale in
InTrade = Flip(Buy,Sell);
SetOption("EveryBarNullCheck", True);
StopLine = IIf( InTrade, HighestSince( Buy, High ) * ( 1 - 0.01 * StopLevel ), Null );
Plot(StopLine, "Trailing Stop", colorBlue, styleLine, 0, 0, 0, 0, 1); 
//PlotShapes(Buy*shapeUpArrow, colorGreen, 0, High);


// ATR Stop
//StopValue = 3*ATR(10);
//ApplyStop(stopTypeTrailing, stopModePoint, StopValue, 1, True, 1);
//Equity(0,0);
//InTrade = Flip(Buy,Sell);
//SetOption("EveryBarNullCheck", True);
//StopLine = IIf( InTrade, HighestSince( Buy, High - StopValue ), Null );
//Plot(StopLine, "Trailing Stop", colorRed, styleLine, 0, 0, 0, 0, 1); 
//PlotShapes(Buy*shapeUpArrow, colorGreen, 0, High);

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