it was actually quite a bit of work. To do this using arrays only is probably a tough task. I used loops. Because it is so much code I leave it to you first to study it. In the Parameter window you can chose to just show System1 or System2 or System1&2 together. I set it up for futures trading but you can change the number of contracts in the code (line45). The display shows a yellow background if a trade in System2 occurs and a white when it occurs in System1. The numbers that are show are "number of contracts in the (partial) trade / total of contracts left in the trade".
SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );
mode = ParamList( "Systems Shown", "System1|System2|System1&2", 2 );
if( mode == "System1&2" )
{
Buy1 = RSI() < 30 ;
Sell1 = RSI() > 70 ;
Buy2 = Cross( C, MA( C, 20 ) );
Sell2 = Cross( MA( C, 20 ), C );
Plot( MA( C, 20 ), "", colorAqua, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
}
else
if( mode == "System1" )
{
Buy1 = RSI() < 30 ;
Sell1 = RSI() > 70 ;
Buy2 = 0;
Sell2 = 0;
}
else
if( mode == "System2" )
{
Buy1 = 0;
Sell1 = 0;
Buy2 = Cross( C, MA( C, 20 ) );
Sell2 = Cross( MA( C, 20 ), C );
Plot( MA( C, 20 ), "", colorAqua, styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
}
Buy = Sell = 0;
BuyPrice = SellPrice = C;
npos1 = npos2 = 0;
maxpos1 = 2; // maximum number of positions sys1
maxpos2 = 2; // maximum number of positions sys2
nposarray1 = nposarray2 = 0;
tposarray1 = tposarray2 = 0;
bp1 = bp2 = 0;
flag1 = flag2 = 0;
bb1 = bb2 = ss1 = ss2 = 0;
totalContracts1 = totalContracts2 = 0;
totalContracts = totalContractsArray = 0;
// set the number of contracts here
initialContracts1 = 1;
scaleInContracts1 = 1;
initialContracts2 = 1;
scaleInContracts2 = 1;
// array used for backtest
possize = 0;
for( i = 0; i < BarCount; i++ )
{
///////////// sys1 /////////////
// initial buy, this is the first long position of trade (no postion yet in system2)
if( Buy1[i] AND npos1 == 0 AND flag1 == 0 AND flag2 == 0 )
{
Buy[i] = 1;
bb1[i] = 1;
npos1 = 1;
nposarray1[i] = initialContracts1;
possize[i] = possize[i] + initialContracts1;
bp1 = C[i]; // the buyprice
flag1 = 1;
totalContracts1 = initialContracts1;
tposarray1[i] = totalContracts1;
totalContractsArray[i] = totalContracts1 + totalContracts2;
}
else
// buy is a scaleIn if already long in system2
if( Buy1[i] AND npos1 == 0 AND flag1 == 0 AND flag2 == 1 )
{
Buy[i] = sigScaleIn;
bb1[i] = sigScaleIn;
npos1 = 1;
nposarray1[i] = initialContracts1;
possize[i] = possize[i] + initialContracts1;
bp1 = C[i]; // the buyprice
flag1 = 1;
totalContracts1 = initialContracts1;
tposarray1[i] = totalContracts1;
totalContractsArray[i] = totalContracts1 + totalContracts2;
}
else
// scaleIn system1
if( C[i] < bp1 / 1.03 AND npos1 > 0 AND npos1 < maxpos1 AND flag1 == 1 )
{
Buy[i] = sigScaleIn;
bb1[i] = sigScaleIn;
npos1 = npos1 + 1;
nposarray1[i] = scaleInContracts1;
possize[i] = possize[i] + scaleInContracts1;
bp1 = C[i];
totalContracts1 = totalContracts1 + scaleInContracts1;
tposarray1[i] = totalContracts1;
totalContractsArray[i] = totalContracts1 + totalContracts2;
}
// scaleout if system2 is still active
if( Sell1[i] AND flag2 == 1 AND npos1 > 0 )
{
Buy[i] = sigScaleOut;
bb1[i] = sigScaleOut;
npos1 = 0;
nposarray1[i] = totalContracts1;
possize[i] = possize[i] + totalContracts1;
flag1 = 0;
totalContracts1 = 0;
tposarray1[i] = totalContracts1;
totalContractsArray[i] = totalContracts1 + totalContracts2;
}
else
// final sell of trade if there are no more position in system2
if( Sell1[i] AND flag2 == 0 AND npos1 > 0 )
{
Sell[i] = 1;
ss1[i] = 1;
npos1 = 0;
nposarray1[i] = totalContracts1;
possize[i] = possize[i] + totalContracts1; // last sell this number also can be set to 0
flag1 = 0;
totalContracts1 = 0;
tposarray1[i] = totalContracts1;
totalContractsArray[i] = totalContracts1 + totalContracts2;
}
///////////// sys2 /////////////
// initial buy, this is the first long position of trade (no postion yet in system1)
if( Buy2[i] AND npos2 == 0 AND flag2 == 0 AND flag1 == 0 )
{
Buy[i] = 1;
bb2[i] = 1;
npos2 = 1;
nposarray2[i] = initialContracts2;
possize[i] = possize[i] + initialContracts2;
bp2 = C[i]; // the buyprice
flag2 = 1;
totalContracts2 = initialContracts2;
tposarray2[i] = totalContracts2;
totalContractsArray[i] = totalContracts1 + totalContracts2;
}
else
// buy is a scaleIn if already long in system1
if( Buy2[i] AND npos2 == 0 AND flag2 == 0 AND flag1 == 1 )
{
Buy[i] = sigScaleIn;
bb2[i] = sigScaleIn;
npos2 = 1;
nposarray2[i] = initialContracts2;
possize[i] = possize[i] + initialContracts2;
bp2 = C[i]; // the buyprice
flag2 = 1;
totalContracts2 = initialContracts2;
tposarray2[i] = totalContracts2;
totalContractsArray[i] = totalContracts1 + totalContracts2;
}
else
// scaleIn system1
if( C[i] > bp2 * 1.03 AND npos2 > 0 AND npos2 < maxpos2 AND flag2 == 1 )
{
Buy[i] = sigScaleIn;
bb2[i] = sigScaleIn;
npos2 = npos2 + 1;
nposarray2[i] = scaleInContracts2;
possize[i] = possize[i] + scaleInContracts2;
bp2 = C[i];
totalContracts2 = totalContracts2 + scaleInContracts2;
tposarray2[i] = totalContracts2;
totalContractsArray[i] = totalContracts1 + totalContracts2;
}
// scaleout if system1 is still active
if( Sell2[i] AND flag1 == 1 AND npos2 > 0 )
{
Buy[i] = sigScaleOut;
bb2[i] = sigScaleOut;
npos2 = 0;
nposarray2[i] = totalContracts2;
possize[i] = possize[i] + totalContracts2;
flag2 = 0;
totalContracts2 = 0;
tposarray2[i] = totalContracts2;
totalContractsArray[i] = totalContracts1 + totalContracts2;
}
else
// final sell of trade if there are no more position in system1
if( Sell2[i] AND flag1 == 0 AND npos2 > 0 )
{
Sell[i] = 1;
ss2[i] = 1;
npos2 = 0;
nposarray2[i] = totalContracts2;
possize[i] = possize[i] + totalContracts2; // last sell this number also can be set to 0
flag2 = 0;
totalContracts2 = 0;
tposarray2[i] = totalContracts2;
totalContractsArray[i] = totalContracts1 + totalContracts2;
}
}
PlotShapes( IIf( bb1 == 1, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -15 );
PlotShapes( IIf( bb1 == 1, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( bb1 == sigScaleIn, shapeUpArrow, shapeNone ), colorlightblue, 0, L, -15 );
PlotShapes( IIf( bb1 == sigScaleIn, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( bb1 == sigScaleOut, shapeDownArrow, shapeNone ), colorOrange, 0, H, -15 );
PlotShapes( IIf( bb1 == sigScaleOut, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( ss1 == 1, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( ss1 == 1, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );
PlotShapes( IIf( bb2 == 1, shapeSmallUpTriangle, shapeNone ), colorDarkGreen, 0, L, -15 );
PlotShapes( IIf( bb2 == 1, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( bb2 == sigScaleIn, shapeSmallUpTriangle, shapeNone ), colorlightblue, 0, L, -15 );
PlotShapes( IIf( bb2 == sigScaleIn, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( bb2 == sigScaleOut, shapeSmallDownTriangle, shapeNone ), colorOrange, 0, H, -15 );
PlotShapes( IIf( bb2 == sigScaleOut, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( ss2 == 1, shapeSmallDownTriangle, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( ss2 == 1, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
ft = "Arial Black";
sz = 10;
c1 = colorWhite;
c2 = colorYellow;
for( i = fvb; i <= lvb; i++ )
{
if( bb1[i] == 1 )
{
PlotTextSetFont( "" + nposarray1[i] + "/" + totalContractsArray[i], ft, sz, i, L[i], colorGreen, c1, -sz * 4 );
}
if( bb1[i] == sigScaleIn )
{
PlotTextSetFont( "" + nposarray1[i] + "/" + totalContractsArray[i], ft, sz, i, L[i], colorGreen, c1, -sz * 4 );
}
if( bb1[i] == sigScaleOut )
{
PlotTextSetFont( "-" + nposarray1[i] + "/" + totalContractsArray[i], ft, sz, i, H[i], colorOrange, c1, sz * 3 );
}
if( ss1[i] == 1 )
{
PlotTextSetFont( "" + nposarray1[i] + "/" + totalContractsArray[i], ft, sz, i, H[i], colorRed, c1, sz * 3 );
}
if( bb2[i] == 1 )
{
if( bb1[i] == 0 )
PlotTextSetFont( "" + nposarray2[i] + "/" + totalContractsArray[i], ft, sz, i, L[i], colorGreen, c2, -sz * 4 );
else
PlotTextSetFont( "" + nposarray2[i] + "/" + totalContractsArray[i], ft, sz, i, L[i], colorGreen, c2, -sz * 6 );
}
if( bb2[i] == sigScaleIn )
{
if( bb1[i] == 0 )
PlotTextSetFont( "" + nposarray2[i] + "/" + totalContractsArray[i], ft, sz, i, L[i], colorGreen, c2, -sz * 4 );
else
PlotTextSetFont( "" + nposarray2[i] + "/" + totalContractsArray[i], ft, sz, i, L[i], colorGreen, c2, -sz * 6 );
}
if( bb2[i] == sigScaleOut )
{
if( bb1[i] == 0 )
PlotTextSetFont( "-" + nposarray2[i] + "/" + totalContractsArray[i], ft, sz, i, H[i], colorOrange, c2, sz * 3 );
else
PlotTextSetFont( "-" + nposarray2[i] + "/" + totalContractsArray[i], ft, sz, i, H[i], colorOrange, c2, sz * 5 );
}
if( ss2[i] == 1 )
{
if( ss1[i] == 0 )
PlotTextSetFont( "" + nposarray2[i] + "/" + totalContractsArray[i], ft, sz, i, H[i], colorRed, c2, sz * 3 );
else
PlotTextSetFont( "" + nposarray2[i] + "/" + totalContractsArray[i], ft, sz, i, H[i], colorRed, c2, sz * 5 );
}
}
SetTradeDelays( 0, 0, 0, 0 );
SetOption( "FuturesMode", True );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 2.37 );
SetPositionSize( possize, IIf( Buy == 1 OR Buy == sigScaleIn, spsShares, spsNoChange ) );
SetPositionSize( possize, IIf( Buy == sigScaleOut, spsShares, spsNoChange ) );
SetPositionSize( possize, IIf( Sell == 1, spsShares, spsNoChange ) );
Filter = Buy OR Sell;
AddColumn( bb1 == 1, "Buy System 1", 1.2, colorBlack, IIf( bb1 == 1, colorBrightGreen, colorDefault ) );
AddColumn( bb1 == sigScaleIn, "ScaleIn System 1", 1.2, colorBlack, IIf( bb1 == sigScaleIn, colorAqua, colorDefault ) );
AddColumn( bb1 == sigScaleOut, "ScaleOut System 1", 1.2, colorBlack, IIf( bb1 == sigScaleOut, colorOrange, colorDefault ) );
AddColumn( ss1 == 1, "Sell System 1", 1.2, colorBlack, IIf( ss1 == 1, colorRed, colorDefault ) );
AddColumn( bb2 == 1, "Buy System 2", 1.2, colorBlack, IIf( bb2 == 1, colorBrightGreen, colorDefault ) );
AddColumn( bb2 == sigScaleIn, "ScaleIn System 2" , 1.2, colorBlack, IIf( bb2 == sigScaleIn, colorAqua, colorDefault ) );
AddColumn( bb2 == sigScaleOut, "ScaleOut System 2", 1.2, colorBlack, IIf( bb2 == sigScaleOut, colorOrange, colorDefault ) );
AddColumn( ss2 == 1, "Sell System 2", 1.2, colorBlack, IIf( ss2 == 1, colorRed, colorDefault ) );
AddColumn( totalContractsArray, "totalContractsArray" );
"possize: "
+ possize;
here is how it looks like:
