I have over simplified the actual strategy for the sake of this forum. The loop is needed for a more complex version.
Here is the simplified version's code:
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
InitialCap = Param("InitialCapital",100000,100000,10000000,1000);
DoCounts = Param("DoCounts",0,1,1,1);
ROC_Threshold = Param("ROC_Threshold",0,-100,100,1);
if ( Status("stocknum") == 0)
{
if (DoCounts)
{
wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum ) ;
StaticVarRemove( "score*" );
StaticVarRemove( "~AboveCnt*" );
StaticVarRemove( "~BelowCnt*" );
StaticVarRemove( "~CrxAboveCnt*" );
StaticVarRemove( "~CrxBelowCnt*" );
StaticVarRemove( "~SymbolCt*" );
for ( n = 0; ( Symbol = StrExtract( List, n ) ) != ""; n++ )
{
SetForeign(Symbol);
myScore = Nz(ROC(Close, 15), -9999);
StaticVarSet ( "score" + symbol, myScore );
ActiveSymbol = myScore > -9999;
StaticVarAdd( "~SymbolCt", ActiveSymbol );
ScoreAbove = myScore > ROC_Threshold AND ActiveSymbol;
ScoreBelow = myScore <= ROC_Threshold AND ActiveSymbol;
ScoreCrxAbove = myScore > ROC_Threshold AND Ref(myScore, -1) <= ROC_Threshold AND ActiveSymbol;
ScoreCrxBelow = myScore <= ROC_Threshold AND Ref(myScore, -1) > ROC_Threshold AND ActiveSymbol;
StaticVarAdd( "~AboveCnt", ScoreAbove );
StaticVarAdd( "~BelowCnt", ScoreBelow );
StaticVarAdd( "~CrxAboveCnt", ScoreCrxAbove );
StaticVarAdd( "~CrxBelowCnt", ScoreCrxBelow );
RestorePriceArrays();
}
}
}
myROC = ROC(Close, 15);
ROCscore = Nz(StaticVarGet ( "score" + Name() ), -9999);
AboveCnt = StaticVarGet( "~AboveCnt" );
BelowCnt = StaticVarGet( "~BelowCnt" );
CrxAboveCnt = StaticVarGet( "~CrxAboveCnt" );
CrxBelowCnt = StaticVarGet( "~CrxBelowCnt" );
SymbolCt = StaticVarGet( "~SymbolCt" );
Diff = CrxBelowCnt - CrxAboveCnt;
printf("======================" + "\n");
printf("myROC: " + NumToStr(myROC, 1.3) + "\n");
printf("ROCscore: " + NumToStr(ROCscore, 1.3) + "\n");
printf("SymbolCt: " + NumToStr(SymbolCt, 1.0) + "\n");
printf("AboveCnt: " + NumToStr(AboveCnt, 1.0) + "\n");
printf("BelowCnt: " + NumToStr(BelowCnt, 1.0) + "\n");
printf("CrxAboveCnt: " + NumToStr(CrxAboveCnt, 1.0) + "\n");
printf("CrxBelowCnt: " + NumToStr(CrxBelowCnt, 1.0) + "\n");
printf("Diff: " + NumToStr(Diff, 1.0) + "\n");
Buy = 0;
BuyPrice = 0;
Sell = 0;
SellPrice = 0;
Short = 0;
Cover = 0;
mydatetime = DateTimeToStr(GetCursorXPosition());
bi = BarIndex();
dn = DateNum();
tn = TimeNum();
dow = DayOfWeek();
SelectedDN = SelectedValue(dn);
SelectedTN = SelectedValue(tn);
SetBarsRequired( sbrAll, sbrAll );
SetOption("MaxOpenPositions", 9999);
SetOption("AllowPositionShrinking", True);
SetOption("InitialEquity", InitialCap);
SetOption("AllowSameBarExit",false);
SetBacktestMode( backtestRegularRaw );
myPosSize = IIf(BelowCnt > 0, -100/BelowCnt, 0);
PositionSize = myPosSize;
for (i = 0; i < BarCount; i++)
{
if (i == 0 )
{
mp = 0;
BarsInTrade = 0;
MarkEntryPrc = 0;
}
else
if (i > 1 AND i < BarCount - 1)
{
// Exit first if we have a position
if ( mp == 1 )
{
BarsInTrade += 1;
if (BarsInTrade >= 1)
{
if ( mp == 1 )
{
Sell[i] = 1;
SellPrice[i] = Close[i];
mp = 0;
BarsInTrade = 0;
MarkEntryPrc = 0;
}
}
}
// And then entry next
if (mp == 0)
{
if (myROC[i] <= ROC_Threshold)
{
Buy[i] = 1;
BuyPrice[i] = Close[i];
mp = 1;
MarkEntryPrc = Close[i];
}
}
}
}
printf(mydatetime + "\n");
printf("------------------" + "\n" );
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorBlue, 0, BuyPrice, Offset=-15);
PlotShapes(IIf(sell, shapeDownArrow, shapeNone),colorYellow, 0, SellPrice, Offset=-15);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorPink, 0, ShortPrice, Offset=-15);
PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0, CoverPrice, Offset=-15);
see in the graphic below how it is not taking trades as the chart does:
