Mix more than 1 system in 1 symbol, but trigger according to each system

Greeting!

This thread is a development of the previous thread like Different exits or Multiple sell tied to multiple buy - #8 by fxshrat.

it is a combination of running more than 1 system and trading scale, but there is a difference here. if buy position of system 1 is active even though the sell signal of system 1 has not appeared and system2 has a buy signal then the buy still can be made. so it is mix!

the framework is:
system 1(buy1), system 2(buy1), system 2(buy2), system 1(buy2), system 1(buy3), system 1(sell_all), system 2(sell_all).

other example with picture:
aaa

a similar thread has been mentioned here like created by @fxshrat https://forum.amibroker.com/t/multiple-sell-tied-to-multiple-buy/22244/8but seems different. what i have done so far is:

sellsystem1=.......; 
sellsystem2=.......;

buysystem1 = ruleOfsystem1;
buysystem2 = ruleOfsystem2;

Buy = buysystem1 OR buysystem2;
Sell = IIf(buysystem1, sellsystem1,
		IIf(buysystem2, sellsystem2));

I ask for your help and thank you

this is just scaling in and out. Many examples, see 1 here

1 Like

Thank you Ed,

Actually each of mysystems contains sigscalein, so my task is just to merge.

BuySystem_01 = IIf(FirstTrigger_System01, 1, IIf(SecondTrigger_System01, sigScaleIn, 0 ));
SellSystem_01 = Sell_rule_System01;

BuySystem_02 = IIf(FirstTrigger_System02, 1, IIf(SecondTrigger_System02, sigScaleIn, 0 ));
SellSystem_02 = Sell_rule_System02;

and I just thought that I should also use sigscalein + sigscaleout, thanks for the input ed @empottasch .
i have a problem to brake buysignal, usually use exrem, but I don't know how to write it in your code. so it is also have exrem each system.

xss

//https://forum.amibroker.com/t/multi-buy-sell-system-backtester-discrepancy/25576/7
//by @empottasch

SetTradeDelays( 0, 0, 0, 0 );
SetOption( "FuturesMode", True );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 2.37 );

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );

// system 1
Buy1 = Cross( C, MA( C, 20 ) );
Sell1 = Cross( MA( C, 20 ), C );
BuyPrice1 = SellPrice1 = C;

// system 2
Buy2 = Cross( StochK(), StochD() ) AND StochD() < 20;
Sell2 = Cross( StochD(), StochK() ) AND StochD() > 80;
BuyPrice2 = SellPrice2 = C;

Buy = Sell = 0;
BuyPrice = SellPrice = C;
npos = 0;
maxpos = 6; // maximum number of positions
nposarray = 0;

for( i = 0; i < BarCount; i++ )
{
    if( ( Buy1[i] OR Buy2[i] ) AND npos == 0 )
    {
        Buy[i] = 1;
        npos = 1;
        nposarray[i] = npos;
    }
    else
        if( ( Buy1[i] OR Buy2[i] ) AND npos > 0 AND npos < maxpos )
        {
            Buy[i] = sigScaleIn;
            npos = npos + 1;
            nposarray[i] = npos;
        }

    if( ( Sell1[i] OR Sell2[i] ) AND npos == 1 )
    {
        Sell[i] = 1;
        npos = 0;
        nposarray[i] = npos;
    }
    else
        if( ( Sell1[i] OR Sell2[i] ) AND npos > 1 )
        {
            Buy[i] = sigScaleOut;
            npos = npos - 1;
            nposarray[i] = npos;
        }
}

SetPositionSize( 1, spsShares );

PlotShapes( IIf( Buy == 1, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -15 );
PlotShapes( IIf( Buy == 1, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Buy == sigScaleIn, shapeUpArrow, shapeNone ), colorlightblue, 0, L, -15 );
PlotShapes( IIf( Buy == sigScaleIn, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Buy == sigScaleOut, shapeDownArrow, shapeNone ), colorOrange, 0, H, -15 );
PlotShapes( IIf( Buy == sigScaleOut, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );

bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
ft = "Arial Black";
sz = 10;

for( i = fvb; i <= lvb; i++ )
{
    if( Buy[i] == 1 )
    {
        PlotTextSetFont( "" + 1, ft, sz, i, L[i], colorGreen, colorWhite, -sz * 4 );
    }

    if( Buy[i] == sigScaleIn )
    {
        PlotTextSetFont( "" + nposarray[i], ft, sz, i, L[i], colorGreen, colorWhite, -sz * 4 );
    }

    if( Buy[i] == sigScaleOut )
    {
        PlotTextSetFont( "" + nposarray[i], ft, sz, i, H[i], colorOrange, colorWhite, sz * 3 );
    }

    if( Sell[i] == 1 )
    {
        PlotTextSetFont( "" + nposarray[i], ft, sz, i, H[i], colorRed, colorWhite, sz * 3 );
    }
}

Filter = Buy OR Sell;
AddColumn( Buy, "Buy" );
AddColumn( Sell, "Sell" );
AddColumn( nposarray, "Number Of Positions" );

looking at your chart I still think this should be treated as a single trade. So you first scale-in to 6 long positions and then you scale-out of these positions in 2 steps of 3. So when you exit the first batch of 3 positions you should use sigScaleOut in the Buy array and the last batch of 3 positions you should use the Sell array. So your trade would look like this (see below) in the Buy and Sell array. Then of course you also need to set the positionsize using SetPositionSize, something like

SetPositionSize( 1, IIf( Buy == 1 OR Buy == sigScaleIn, spsShares, spsNoChange ) );
SetPositionSize( 3, IIf( Buy == sigScaleOut, spsShares, spsNoChange ) );
SetPositionSize( 3, IIf( Sell == 1, spsShares, spsNoChange ) );
Buy Array Sell Array
1
sigScaleIn
sigScaleIn
sigScaleIn
sigScaleIn
sigScaleIn
sigScaleOut
1

each system has its own scalein style and also positionsize own style, each system is independent
styles for scalein and positionsize styles

this is the 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();

SetTradeDelays(0, 0, 0, 0);
SetOption("AllowPositionShrinking", True );
SetOption( "InitialEquity", 10000 );
SetOption( "MaxOpenPositions", 50 );
TickSize = 0 ;
BuyPrice = SellPrice = C ;

////////////////////////////////////////////////////////SYSTEM-01//////////////////////////////////////////////////
BuySYSTEM1    = RSI() < 30 ;
SellSYSTEM1   = RSI() > 70 ;
FirstScaleIn_System01  = 1;
SecondScaleIn_System01 = 1;

BarsSinceSell_System01          = BarsSince(SellSYSTEM1);
FirstEntry_System01 			= BuySYSTEM1;
InFirstPos_System01 			= Flip(FirstEntry_System01, SellSYSTEM1);
FirstTrigger_System01 			= ExRem(InFirstPos_System01, SellSYSTEM1);
BarsSinceFirstTrigger_System01  = BarsSince(FirstTrigger_System01);
FirstTriggerPrice_System01 		= IIf(BarsSinceFirstTrigger_System01 < BarsSinceSell_System01, Ref(C,-BarsSinceFirstTrigger_System01), 0 );


SecondEntry_System01 			=  C < (FirstTriggerPrice_System01/1.03) AND InFirstPos_System01 AND Ref(InFirstPos_System01,-1);
InSecondPos_System01			= Flip(SecondEntry_System01, SellSYSTEM1);
SecondTrigger_System01 			= ExRem(InSecondPos_System01, SellSYSTEM1);
BarsSinceSecondTrigger			= BarsSince(SecondTrigger_System01);
SecondTriggerPrice_System01 	= IIf(BarsSinceSecondTrigger < BarsSinceSell_System01, Ref(C,-BarsSinceSecondTrigger), 0);

if ( FirstScaleIn_System01 + SecondScaleIn_System01 = 1.0 ){

  BuySYSTEM1 = IIf( FirstTrigger_System01, 1, IIf( SecondTrigger_System01, sigScaleIn, 0));
  SetPositionSize(IIf( FirstTrigger_System01, FirstScaleIn_System01, 
                      IIf( SecondTrigger_System01, SecondScaleIn_System01,0 )), 
						IIf( BuySYSTEM1 > 0,spsShares, spsNoChange ) );
  SellSYSTEM1 = ExRem(SellSYSTEM1,BuySYSTEM1);
}

/////////////////////////////////////////////SYSTEM-02/////////////////////////////////////////////////////////////////////////
BuySYSTEM2 = Cross( C, MA( C, 20 ) );
SellSYSTEM2 = Cross( MA( C, 20 ), C );

FirstScaleIn_System02  = 2 ;
SecondScaleIn_System02 = 2;

BarsSinceSell_System02          = BarsSince(SellSYSTEM2);
FirstEntry_System02 		    = BuySYSTEM2;
InFirstPos_System02 			= Flip(FirstEntry_System02, SellSYSTEM2);
FirstTrigger_System02 			= ExRem(InFirstPos_System02, SellSYSTEM2);
BarsSinceFirstTrigger_System02  = BarsSince(FirstTrigger_System02);
FirstTriggerPrice_System02 		= IIf(BarsSinceFirstTrigger_System02 < BarsSinceSell_System02, Ref(C,-BarsSinceFirstTrigger_System02), 0 );

SecondEntry_System02 			=  C < (FirstTriggerPrice_System02*1.03) AND InFirstPos_System02 AND Ref(InFirstPos_System02,-1);
InSecondPos_System02			= Flip(SecondEntry_System02, SellSYSTEM2);
SecondTrigger_System02 			= ExRem(InSecondPos_System02, SellSYSTEM2);
BarsSinceSecondTrigger_System02	= BarsSince(SecondTrigger_System02);
SecondTriggerPrice_System02 		= IIf(BarsSinceSecondTrigger_System02 < BarsSinceSell_System02, Ref(C,-BarsSinceSecondTrigger_System02), 0);

if ( FirstScaleIn_System02 + SecondScaleIn_System02 = 1.0 ){

  BuySYSTEM2 = IIf( FirstTrigger_System02, 1, IIf( SecondTrigger_System02, sigScaleIn, 0));
  SetPositionSize(IIf( FirstTrigger_System02, FirstScaleIn_System02, 
                      IIf( SecondTrigger_System02, SecondScaleIn_System02,0 )), 
						IIf( BuySYSTEM2 > 0,spsShares, spsNoChange ) );
  SellSYSTEM2 = ExRem(SellSYSTEM2,BuySYSTEM2);
}

////////////////////////////////////////////////COMBINING///////////////////////////////////////////////////////////////
//https://forum.amibroker.com/t/multi-buy-sell-system-backtester-discrepancy/25576/7
//by @empottasch


// system 1
Buy1 = BuySYSTEM1;
Sell1 = SellSYSTEM1;

// system 2
Buy2 = BuySYSTEM2;
Sell2 = SellSYSTEM2;

Buy = Sell = 0;
BuyPrice = SellPrice = C;
npos = 0;
maxpos = 15; // maximum number of positions
nposarray = 0;

for( i = 0; i < BarCount; i++ )
{
    if( ( Buy1[i] OR Buy2[i] ) AND npos == 0 )
    {
        Buy[i] = 1;
        npos = 1;
        nposarray[i] = npos;
    }
    else
        if( ( Buy1[i] OR Buy2[i] ) AND npos > 0 AND npos < maxpos )
        {
            Buy[i] = sigScaleIn;
            npos = npos + 1;
            nposarray[i] = npos;
        }

    if( ( Sell1[i] OR Sell2[i] ) AND npos == 1 )
    {
        Sell[i] = 1;
        npos = 0;
        nposarray[i] = npos;
    }
    else
        if( ( Sell1[i] OR Sell2[i] ) AND npos > 1 )
        {
            Buy[i] = sigScaleOut;
            npos = npos - 1;
            nposarray[i] = npos;
        }
}


//////////////////////////////////////////////PLOTING//////////////////////////////////////////////////////////////////
PlotShapes( IIf( Buy == 1, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -15 );
PlotShapes( IIf( Buy == 1, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Buy == sigScaleIn, shapeUpArrow, shapeNone ), colorlightblue, 0, L, -15 );
PlotShapes( IIf( Buy == sigScaleIn, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Buy == sigScaleOut, shapeDownArrow, shapeNone ), colorOrange, 0, H, -15 );
PlotShapes( IIf( Buy == sigScaleOut, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );

bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
ft = "Arial Black";
sz = 10;

for( i = fvb; i <= lvb; i++ )
{
    if( Buy[i] == 1 )
    {
        PlotTextSetFont( "" + 1, ft, sz, i, L[i], colorGreen, colorWhite, -sz * 4 );
    }

    if( Buy[i] == sigScaleIn )
    {
        PlotTextSetFont( "" + nposarray[i], ft, sz, i, L[i], colorGreen, colorWhite, -sz * 4 );
    }

    if( Buy[i] == sigScaleOut )
    {
        PlotTextSetFont( "" + nposarray[i], ft, sz, i, H[i], colorOrange, colorWhite, sz * 3 );
    }

    if( Sell[i] == 1 )
    {
        PlotTextSetFont( "" + nposarray[i], ft, sz, i, H[i], colorRed, colorWhite, sz * 3 );
    }
}

Filter = Buy OR Sell;
AddColumn( Buy, "Buy" );
AddColumn( Sell, "Sell" );
AddColumn( nposarray, "Number Of Positions" );

PlotShapes (IIf (FirstTrigger_System01,  shapeSmallCircle, shapeNone), colorWhite, 0,c, 0 );
PlotShapes (IIf (SecondTrigger_System01,  shapeSmallCircle, shapeNone), colorWhite, 0,C, 0 );
PlotShapes (IIf (Sell1, shapeSmallCircle, shapeNone), colorRed, 0, C, 0);

PlotShapes (IIf (FirstTrigger_System01,  shapeSmallCircle, shapeNone), colorYellow, 0,c, 0 );
PlotShapes (IIf (SecondTrigger_System01,  shapeSmallCircle, shapeNone), colorYellow, 0,C, 0 );
PlotShapes (IIf (Sell2, shapeSmallCircle, shapeNone), colorBlue, 0, C, 0);

I tried running backtest but it didn't work.
I'm sorry maybe my explanation is not understandable, I'll try to explain again with pictures:

tempsnip

Thanks again edward!

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:
es

2 Likes

Hello Ed, I feel lucky you want to help me and Thanks very much, I really appreciate it. It took me some time to learn it, maybe I'll ask again if there's a problem. :blush:

yes it is quite a bit of code. Possibly it can be done more efficiently using re-usable blocks of code. Certainly when using the CBT but I have not used that in awhile. But without the CBT it is also possible like I showed. Just it is kind of tough if you for instance add a 3-rd or a 4-th system then you want to write it more efficiently using code that can be re-used for every added system.

If anyone cares to adjust my code to write a more efficient version, please go ahead.

1 more thing is that you scale-In is using a percentage. This works fine for instance using EOD data but if you use ATR then you get similar signals if you for instance switch to the hourly timeframe.

you can do this by for instance putting:

atrarray = Ref( 3 * ATR( 50 ), -1 );

at the top of the code and then when doing a scalein for system 1 you use:

            // scaleIn system1
            if( C[i] < ( bp1 - atrarray[i] )  AND npos1 > 0 AND npos1 < maxpos1 AND flag1 == 1 )
            {

and for system2 you use

            // scaleIn system2
            if( C[i] > ( bp2 + atrarray[i] ) AND npos2 > 0 AND npos2 < maxpos2 AND flag2 == 1 )
            {
1 Like

Your input is very helpful. I want to ask something but off topic, let me send a message if you don't mind

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