Why is my position value =0

Here is my code . i am using gbpusd @ 5 min charts.

_SECTION_BEGIN( "RSI" );
SetBarsRequired( -2, -2 ); // require all bars.
dt = DateTime() ;
Buy = Sell = Cover = Short = False;
_rsi = RSI( 2 );

_SECTION_END();
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", colorBlack ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
Plot( ema200 = MA( C, 200 ), "200 ma" , colorBlue, ParamStyle( "Style" ) );


Plot( ema5 = eMA( C, 5 ), "5ema" , colorYellow, ParamStyle( "Style" ) );
Plot( don10 = Ref( LLV( L, 10 ), -1 ), "dn10" , colorPink, styleLine );
r = ATR( 14 );
gotsignal = C<ema5 AND C>ema200 AND _rsi <= 6 AND L <= don10;
PlotShapes( gotsignal*shapeHollowSquare, IIf( L <= don10, colorLightBlue, colorYellow ), Graph0, l );
opentrade = False;
quantity = 0;
tradebar = 0;
entryDate = Null;
entryprice = 0;
currentpnl = 0;
risk = 0;
tradenumber = 0;
jump = 2;

for( i = 1; i < BarCount - 1; i++ )

{
    if( gotsignal[i ]  AND opentrade == False )
    {
        opentrade = True;
        Buy[i] = True;
        BuyPrice[i] = Open[i];
        entryprice = BuyPrice[i];
        quantity = 2;
        tradebar = i;
        currentpnl = 0;

        entryDate = DateTimeToStr( dt[i + 1] , 3 )  ;
        tradenumber += 1;
        jump = IIf( jump == 2, 3, 2 );


        PlotText( "entry Signal #" + tradenumber, i, Status( "axisminy" ) + jump * r[i], colorLightBlue, coloryellow, yoffset = 0 ) ;

    }

    if( i == tradebar + 1 )
    {
        risk = don10[i] - r[i];
    }

    if( opentrade AND i > tradebar AND quantity > 0 )
    {
        if( quantity == 2 AND _rsi[i - 1] > 70 ) // profit targets
        {
            Buy[ i ] = sigScaleOut;
            SellPrice[i] = Open[i];
            quantity = 1;

            currentpnl = Open[i] - entryprice;
            PlotText( "Scaleout(" +  tradenumber + ")"   + currentpnl , i, Status( "axisminy" ) + 5 * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;
        }

        //  if( quantity == 2 AND  L[i] < don10[i] -r[i]) // stops
        if( quantity == 2 AND  L[i] < risk ) // stops
        {
            Sell[ i ] = true;
            SellPrice[i] = Close[i];
            quantity = 0;
            opentrade = False;
            currentpnl = SellPrice[i] - entryprice;
            PlotText( "stoploss(" +  tradenumber + ")"     + currentpnl, i, Status( "axisminy" ) + 4 * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;
        }

        if( quantity == 1 AND  L[i] < don10[i] ) // trailing stops.
        {
            Sell[ i ] = true;
            sellPrice = Open[i];
            quantity = 0;
            opentrade = False;
            currentpnl = ( Sellprice[ i ] - entryprice );
            PlotText( "trail(" +  tradenumber + ")"   + currentpnl, i, Status( "axisminy" ) + 6.5 * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;
        }
    }


}

i did a detailed report and now totally lost for ideas. the backtester report shows position value =0 but the detailed report shows that i have purchased 7896 shares. what is going on and how do i fix it?

one pattern i notice is that i get positin value =0 when i have a scale out. I dunno why.

It appears that you are scaling out of the entire position before exiting the trade. The Position Value column in the trade list shows the position value after the last entry, scale-in, or scale-out. It does not simply show the value at trade entry.

1 Like

I don't get it. the scaleout exits most of the position when I expect it to only exit half position.
this is what I use in my code to manage it. can I set the position size per bar in my loop?
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position

Are you calling SetPositionSize more than once? It would be helpful if you would post all of your code.

And yes, the PositionSize variable is an array, and can have a different value in each element of the array whether you set it directly via assignment or indirectly by using SetPositionSize

Here is the full code

SHT = 1;
LNG = 2;
direction = LNG ;
_SECTION_BEGIN( "RSI" );
SetBarsRequired( -2, -2 ); // require all bars.
dt = DateTime() ;
Buy = Sell = Cover = Short = False;
_rsi = RSI( 2 );

_SECTION_END();
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", colorBlack ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
Plot( ema200 = MA( C, 200 ), "200 ma" , colorBlue, ParamStyle( "Style" ) );


Plot( ema5 = eMA( C, 5 ), "5ema" , colorYellow, ParamStyle( "Style" ) );

if( direction == LNG )
{
    Plot( don10l = Ref( LLV( L, 10 ), -1 ), "dn10l" , colorPink, styleLine );




    donh20 = HHV( H, 20 );
    r = ATR( 14 );
    gotsignal = C<ema5 AND C>ema200 AND _rsi <= 6 AND L <= don10l;
//PlotShapes( gotsignal*shapeHollowSquare, IIf( L <= don10l, colorLightBlue, colorYellow ), Graph0, l );
    opentrade = False;
    quantity = 0;
    tradebar = scaleoutbar = trailexitbar = stopexitbar = 0;
    entryDate = Null;
    entryprice = 0;
    currentpnl = 0;
    risk = 0;
    tradenumber = 0;
    jump = 2;
    tradeScaleoutPrice = tradeSellPrice = tradeBuyPrice = 0;
    myOpen = Prec( Open, 4 );

    for( i = 1; i < BarCount - 1; i++ )

    {
        if( gotsignal[i ]  AND opentrade == False )
        {
            opentrade = True;
            Buy[i] = True;
            BuyPrice[i] = Open[i];


            quantity = 2;
            tradebar = i;

            currentpnl = 0;

            entryDate = DateTimeToStr( dt[i + 1] , 3 )  ;
            tradenumber += 1;
            jump = IIf( jump == 2, 3, 2 );




        }

        if( i == tradebar + 1 )
        {
            scaleoutbar = trailexitbar = stopexitbar = 0;
            risk = don10l[i] - r[i];
            tradeBuyPrice[i] = entryprice = myOpen[i]; // remembmer that the buy price is at open of next day in amibroker.
            PlotText( "entry#" + tradenumber + "[" + entryprice + "]" , i, Status( "axisminy" ) + jump * r[i], colorLightBlue, coloryellow, yoffset = 0 ) ;
        }

        if( i == scaleoutbar + 1 )
        {
            tradeScaleoutPrice[i] = myOpen[i]; //remembmer that the sell  price is at open of next day in amibroker.
            currentpnl = myOpen[i] - entryprice;
            PlotText( "Scaleout(" +  tradenumber + ")"   + currentpnl  + "[" +  myOpen[i] + "]", i, Status( "axismaxy" ) - 2 * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;
        }

        if( i == stopexitbar + 1 )
        {
            currentpnl = myOpen[i] - entryprice; //remembmer that the sell  price is at open of next day in amibroker.
            tradeSellPrice[i] = myOpen[i];
            PlotText( "stoploss(" +  tradenumber + ")"     + currentpnl + "[" +  myOpen[i] + "]", i, Status( "axismaxy" ) - jump * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;

        }

        if( i == trailexitbar + 1 )
        {
            tradeSellPrice[i] = myOpen[i]; //remembmer that the sell  price is at open of next day in amibroker.
            currentpnl =   myOpen[i] - entryprice  ;
            PlotText( "trail(" +  tradenumber + ")"   +  currentpnl + "[" +  myOpen[i] + "]", i, Status( "axismaxy" ) - 4 * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;

        }

        if( opentrade AND i > tradebar AND quantity > 0 )
        {
            if( quantity == 2 AND _rsi[i - 1] > 70 ) // profit targets
            {
                Buy[ i ] = sigScaleOut;
                SellPrice[i] = Open[i];
                quantity = 1;
                scaleoutbar = i;

            }

            //  if( quantity == 2 AND  L[i] < don10l[i] -r[i]) // stops
            if( quantity == 2 AND  L[i] < risk ) // stops
            {
                Sell[ i ] = true;
                SellPrice[i] = Open[i];
                quantity = 0;
                opentrade = False;
                stopexitbar = i;

            }

            if( quantity == 1 AND( L[i] < don10l[i] ) OR( L[i] < entryprice ) ) // trailing stops.
            {
                Sell[ i ] = true;
                sellPrice = Open[i];
                quantity = 0;
                opentrade = False;
                trailexitbar = i;

            }
        }


    }

    SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position
    PlotShapes( tradeBuyPrice * shapeUpArrow, colorLightBlue, tradeBuyPrice );
    PlotShapes( tradeScaleoutPrice * shapeSmallCircle, colorLightOrange, tradeScaleoutPrice );
    PlotShapes( tradeSellPrice * shapedownArrow, colorLightOrange, tradeSellPrice );
}

if( direction == SHT )
{
    Plot( don10s = Ref( hhv( h, 10 ), -1 ), "dn10s" , colorPink, styleLine );




    donl20 = llv( l, 20 );
    r = ATR( 14 );
    gotsignal = C > ema5 AND C<ema200 AND _rsi >= 94 AND h >= don10s;
//PlotShapes( gotsignal*shapeHollowSquare, IIf( L <= don10l, colorLightBlue, colorYellow ), Graph0, l );
    opentrade = False;
    quantity = 0;
    tradebar = scaleoutbar = trailexitbar = stopexitbar = 0;
    entryDate = Null;
    entryprice = 0;
    currentpnl = 0;
    risk = 0;
    tradenumber = 0;
    jump = 2;
    tradeScaleoutPrice = tradeCoverPrice = tradeShortprice = 0;
    myOpen = Prec( Open, 4 );

    for( i = 1; i < BarCount - 1; i++ )

    {
        if( gotsignal[i ]  AND opentrade == False )
        {
            opentrade = True;
            Short[i] = True;
            ShortPrice[i] = Open[i];


            quantity = 2;
            tradebar = i;

            currentpnl = 0;

            entryDate = DateTimeToStr( dt[i + 1] , 3 )  ;
            tradenumber += 1;
            jump = IIf( jump == 2, 3, 2 );




        }

        if( i == tradebar + 1 )
        {
            scaleoutbar = trailexitbar = stopexitbar = 0;
            risk = don10s[i] + r[i];

            tradeShortPrice[i] = entryprice = myOpen[i]; // remembmer that the buy price is at open of next day in amibroker.
            PlotText( "entry#" + tradenumber + "[" + entryprice + "]" , i, Status( "axisminy" ) + jump * r[i], colorLightBlue, coloryellow, yoffset = 0 ) ;
        }

        if( i == scaleoutbar + 1 )
        {
            tradeScaleoutPrice[i] = myOpen[i]; //remembmer that the sell  price is at open of next day in amibroker.
            currentpnl =  entryprice - myOpen[i];
            PlotText( "Scaleout(" +  tradenumber + ")"   + currentpnl  + "[" +  myOpen[i] + "]", i, Status( "axismaxy" ) - 2 * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;
        }

        if( i == stopexitbar + 1 )
        {
            currentpnl =  entryprice - myOpen[i]; //remembmer that the sell  price is at open of next day in amibroker.
            tradeCoverPrice[i] = myOpen[i];
            PlotText( "stoploss(" +  tradenumber + ")"     + currentpnl + "[" +  myOpen[i] + "]", i, Status( "axismaxy" ) - jump * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;

        }

        if( i == trailexitbar + 1 )
        {
            tradeCoverPrice[i] = myOpen[i]; //remembmer that the sell  price is at open of next day in amibroker.
            currentpnl =  entryprice - myOpen[i];
            PlotText( "trail(" +  tradenumber + ")"   +  currentpnl + "[" +  myOpen[i] + "]", i, Status( "axismaxy" ) - 4 * r[i], IIf( currentpnl > 0, colorBlue, colorred ), coloryellow, yoffset = 0 ) ;

        }

        if( opentrade AND i > tradebar AND quantity > 0 )
        {
            if( quantity == 2 AND _rsi[i - 1] < 30 ) // profit targets
            {
                Cover[ i ] = sigScaleOut;
                CoverPrice[i] = Open[i];
                quantity = 1;
                scaleoutbar = i;

            }

            //  if( quantity == 2 AND  L[i] < don10l[i] -r[i]) // stops
            if( quantity == 2 AND  h[i] > risk ) // stops
            {
                Cover[ i ] = true;
                CoverPrice[i] = Open[i];
                quantity = 0;
                opentrade = False;
                stopexitbar = i;

            }

            if( quantity == 1 AND( h[i] > don10s[i] ) )   // trailing stops.

                //  if( quantity == 1 AND( h[i] > don10s[i] ) OR(h[i] > entryprice ) ) // trailing stops.
            {
                cover[ i ] = true;
                coverPrice = Open[i];
                quantity = 0;
                opentrade = False;
                trailexitbar = i;

            }
        }


    }

    PlotShapes( tradeShortPrice * shapedownArrow, colorLightBlue, tradeShortPrice );
    PlotShapes( tradeScaleoutPrice * shapeSmallCircle, colorLightOrange, tradeScaleoutPrice );
    PlotShapes( tradeCoverPrice * shapeupArrow, colorLightOrange, tradeCoverPrice );
    SetPositionSize( 50, spsPercentOfPosition * ( short == sigScaleOut ) ); // scale out 50% of position
}


SetCustomBacktestProc( "" );


if( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();

    bo.Backtest( 1 ); // run default backtest procedure
    tradeid = 1;

    // iterate through closed trades first
    for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
    {
        trade.AddCustomMetric( "Trade #", tradeid );
        // bo.RawTextOutput("Trade #" + tradeid);
        tradeid = tradeid + 1;
    }

    bo.ListTrades();
}


// rules buy when gotsignal
// sell half when i hit 70
// trail stop at 10 don
// put stop above entry after hitting 70
// put stop at don 10 at all times 2 bars after entr.
// if 9 <20 ema then exit at 20 donchian highs.


My suggestion is that you add some Exploration code so that you can see how your PositionSize variable is being set. I'm not sure how you're ever getting an entry in the first place, since your SetPositionSize() function appears to only apply when you've found a scale-out condition.

As @mradtke said - your code is incorrect. You are scaling out entire position so position size is in fact ZERO when you later exit. Instead of doing 100% scale out and later exit zero position, you should just EXIT the position in first place.

I narrowed down the issue. I dont want to close the position fully. Now i fixed the code so that i scale out 1k at a time. I want to scale out 50% of the position.
how do i determine my position size and scale out only 50%?

if( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();
     bo.PreProcess();    //  Do pre-processing
     
     for (i = 0; i < BarCount; i++)    //  Loop through all bars
    {
        for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i))
        {    //  Loop through all signals at this bar 
                 if( sig.IsScale() ) 
         { 
          // handle entry signal 
         
          sig.PosSize=1000;  // need to figure out how to make it only 50% of the position invested.
         
         }   
        }    //  End of for loop over signals at this bar
        bo.ProcessTradeSignals(i);    //  Process trades at bar (always required)
    }    //  End of for loop over bars
    bo.PostProcess();    //  Do post-processing (always required)

1 Like

It is explained (with examples) in the manual:
http://www.amibroker.com/guide/h_pyramid.html

1 Like