Why don't my any comments using PrintF() not to show on interpretation window

Hi

I am studying how to scale in/scale out position from the guide below.
https://www.amibroker.com/guide/h_pyramid.html

I am focused on Example 4: partial exit (scaling out) on profit target stops

However It 's doesn't a matter at that point, The most thing is that I would like to use printf() function in for loop for my studying

As the figure shown in below, once clicked BackTest Botton , there is nothing to show on interpretation window.
x

1.What is my something wrong???

2.What else way to show debug statement in for loop.

Thank you for all the recommendation.
Pongthorn.Sa

printf() does not output new results when Analysis is selected tab as it is the case in your picture. printf() is not analysis function. printf() does output to Interpetation window if chart tab is selected or does output within Guru chart commentary window (of Analysis menu of menu bar).

In Analysis use _Trace(), TraceF() functions instead of printf() function, for example.
You also have to remove your 'if statement' -> Status("action") == actionCommentary if using _Trace in place of your printf line because otherwise _Trace/TraceF will not output in analysis run, too. If you want to keep that if statement then place _Trace* line(s) outside of that if statement.

2 Likes

Hi @fxshrat

Thank you for your guide.

I change from printf() to Trace , The function is able to show output statement as expected but I am doubtful in Log Window below, I set range for backtest during 1/12/2915 to 31/121/5.
However, output shows 31/10/2017 - 25/9/2018 (current) instead.
Why not show in my range??? I just want to see in particular period as needed.
x
x2

//Example of code that exits 50% on first profit target, 50% on next profit target and everything at trailing stop:
SetOption ("InitialEquity", 100000);
//SetOption ("MaxOpenPositions", PosQty);
SetOption ("MinShares", 500);
RoundLotSize=500;


Buy = Cross( EMA( C, 10 ), eMA( C, 25 ) ); 
Sell = 0; 

// the system will exit 
// 50% of position if FIRST PROFIT TARGET stop is hit 
// 50% of position is SECOND PROFIT TARGET stop is hit 
// 100% of position if TRAILING STOP is hit 

FirstProfitTarget = 30; // profit 
SecondProfitTarget = 60; // in percent 
TrailingStop = 10; // also in percent 

priceatbuy=0; 
highsincebuy = 0; 

exit = 0; 

dt=DateTime();
dtformat = "\n%d-%m-%Y\n";
//DateTimeFormat(dtformat,dt[i])

for( i = 0; i < BarCount; i++ ) 
{ 
   if( priceatbuy == 0 AND Buy[ i ] ) 
    { 
       priceatbuy = BuyPrice[ i ]; 

       _TRACe("Buy Price=" +priceatbuy+" at "+DateTimeFormat(dtformat,dt[i]));

    } 

   if( priceatbuy > 0 ) 
    { 
       highsincebuy = Max( High[ i ], highsincebuy ); 
       

       _TRACE("HighSinceBuy at "+highsincebuy+" at "+DateTimeFormat(dtformat,dt[i]));


      if( exit == 0 AND 
          High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy ) 
       { 
         // first profit target hit - scale-out 
        
         exit = 1; 
         Buy[ i ] = sigScaleOut; 
         
         pecentX=( 1 + FirstProfitTarget * 0.01 );
         _TRACE("Sell#1 from scale out due to Hi "+High[ i ]+ " >=" +pecentX +" of BuyPrice="+ priceatbuy);
         _TRACE("ScaleOut="+Buy[ i ]+ " at "+DateTimeFormat(dtformat,dt[i]));
         
         
       } 

      if( exit == 1 AND 
          High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy ) 
       { 
         // second profit target hit - exit 
         exit = 2; 
         
         pecentX2=( 1 + SecondProfitTarget * 0.01 );
         _TRACE("Sell#2 All due to Hi "+High[ i ]+ " >=" +pecentX2 +" of BuyPrice="+ priceatbuy);
         
         SellPrice[ i ] = Max( Open[ i ], ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy ); 
         _TRACE("Sell price ="+SellPrice[ i ]+ " at "+DateTimeFormat(dtformat,dt[i]));
       } 

      //if( Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy ) 
      // { 
         // trailing stop hit - exit 
      //   exit = 3;    
      //   SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01 ) * highsincebuy ); 
      // } 

      if( exit >= 2 ) 
       { 
         Buy[ i ] = 0; 
         Sell[ i ] = exit + 1; // mark appropriate exit code 
         exit = 0; 
         priceatbuy = 0; // reset price 
         highsincebuy = 0; 
       } 
    } 
} 

SetPositionSize( 50, spsPercentOfEquity ); 
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position

You have Barcount loop iterating from start of array to end of array so loop does not care for your analysis range!

Also internal _trace does have lines limit setting which by default is set to 200!
505

So of course the iterations of year 2015 range will disappear there if array size is larger than that (>Trace line limit).

If you would use external trace application such as DebugView then 2015 would show up there (since it has no line limit setting by Amibroker).


Now, what to do as far as analysis range is concerned....

Add Status( "BarInRange" ) check via if statement.
Then only analysis range will be considered within BarCount loop's _Trace output.

But again, remember of internal trace line limit!
If year 2015 has more true occurrences than lines limit then it will still not show all there (if you want to show all of analysis range). I am just repeating so that you will not wonder once again.

//Example of code that exits 50% on first profit target, 50% on next profit target and everything at trailing stop:

SetOption ("InitialEquity", 100000);
//SetOption ("MaxOpenPositions", PosQty);
SetOption ("MinShares", 500);

RoundLotSize=500;

Buy = Cross( EMA( C, 10 ), eMA( C, 25 ) );
Sell = 0; 

// the system will exit 
// 50% of position if FIRST PROFIT TARGET stop is hit 
// 50% of position is SECOND PROFIT TARGET stop is hit 
// 100% of position if TRAILING STOP is hit 

FirstProfitTarget = 30; // profit 
SecondProfitTarget = 60; // in percent 
TrailingStop = 10; // also in percent 
priceatbuy=0; 
highsincebuy = 0; 
exit = 0; 
dt=DateTime();
dtformat = "\n%d-%m-%Y\n";

//DateTimeFormat(dtformat,dt[i])

debugON = True;
TraceIsTrue = debugON AND Status( "BarinRange" );

for ( i = 0; i < BarCount; i++ )
{
    if ( priceatbuy == 0 AND Buy[ i ] )
    {
        priceatbuy = BuyPrice[ i ];

        if ( TraceIsTrue[i]  )
            _TRACE( "Buy Price=" + priceatbuy + " at " + DateTimeFormat( dtformat, dt[i] ) );
    }

    if ( priceatbuy > 0 )
    {
        highsincebuy = Max( High[ i ], highsincebuy );
        if ( TraceIsTrue[i]  )
			_TRACE( "HighSinceBuy at " + highsincebuy + " at " + DateTimeFormat( dtformat, dt[i] ) );

        if ( exit == 0 AND  High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy )
        {
            // first profit target hit - scale-out
            exit = 1;
            Buy[ i ] = sigScaleOut;
            pecentX = ( 1 + FirstProfitTarget * 0.01 );

            if ( TraceIsTrue[i]  ) {
				_TRACE( "Sell#1 from scale out due to Hi " + High[ i ] + " >=" + pecentX + " of BuyPrice=" + priceatbuy );
				_TRACE( "ScaleOut=" + Buy[ i ] + " at " + DateTimeFormat( dtformat, dt[i] ) );
            }
        }

        if ( exit == 1 AND   High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy )
        {
            // second profit target hit - exit
            exit = 2;
            pecentX2 = ( 1 + SecondProfitTarget * 0.01 );
            SellPrice[ i ] = Max( Open[ i ], ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy );

            if ( TraceIsTrue[i] ) {
				_TRACE( "Sell#2 All due to Hi " + High[ i ] + " >=" + pecentX2 + " of BuyPrice=" + priceatbuy );
				_TRACE( "Sell price =" + SellPrice[ i ] + " at " + DateTimeFormat( dtformat, dt[i] ) );
            }
        }

        //if ( Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy )
        // {
        // trailing stop hit - exit
        //   exit = 3;
        //   SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01 ) * highsincebuy );
        // }

        if ( exit >= 2 )
        {
            Buy[ i ] = 0;
            Sell[ i ] = exit + 1; // mark appropriate exit code
            exit = 0;
            priceatbuy = 0; // reset price
            highsincebuy = 0;

        }
    }
}

SetPositionSize( 50, spsPercentOfEquity ); 
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position
3 Likes

Awesome!!!

Once I posted reply to you in the topinc , I come up with any idea about"firstbarinrange" and "lastbarinrange" or even "barinrang" status but I don't know about how to apply these.
Luckily, you help me fix this problem again by using "barinrang".

You ever help me in the past with a similar way as the link below.
https://forum.amibroker.com/t/how-to-explore-roc-by-start-date-to-to-date/7359

I understand completely because of you.

Thank you

Awesome!!!

Almost complete.

 if ( TraceIsTrue[i]  ){
       _TRACe("Buy Price=" +priceatbuy+" at "+DateTimeFormat(dtformat,dt[i]));
       }

The statment dissappear despite buy signal.

x4

Is it possible to loop just firstbarinrange to lastbarinrange

for( i = firstbarinrange ; i <lastbar; i++ )

I try
if ( TraceIsTrue[i] ) plus bir = Status( "barinrange" ); ,the first one as your suggess.

Refer: https://forum.amibroker.com/t/barindex-of-test-start-date-and-test-end-date/3286/6

I try to apply as above topic.

//Example of code that exits 50% on first profit target, 50% on next profit target and everything at trailing stop:
SetOption ("InitialEquity", 100000);
SetOption ("MaxOpenPositions", 1);
SetOption ("MinShares", 500);
RoundLotSize=500;



Buy = Cross( EMA( C, 10 ), eMA( C, 25 ) ); 
Sell = 0; 


FirstProfitTarget = 30; // 30% of position if FIRST PROFIT TARGET stop is hit 
SecondProfitTarget = 60; // 60% of position is SECOND PROFIT TARGET stop is hit 
 //100% of position if TRAILING STOP is hit 
TrailingStop = 10; // also in percent 

priceatbuy=0; 
highsincebuy = 0; 

exit = 0; 

dt=DateTime();
dtformat = "\n%d-%m-%Y\n";

debugOn=True;
TraceIsTrue=debugON AND Status("BarinRange");

bir = Status( "barinrange" );


for( i = 0; i < BarCount; i++ ) { 

   if(bir[i]){
   
   if( priceatbuy == 0 AND Buy[ i ] ) 
    { 
       priceatbuy = BuyPrice[ i ]; 

        //if ( TraceIsTrue[i]  )
       _TRACe("Buy Price=" +priceatbuy+" at "+DateTimeFormat(dtformat,dt[i]));
       
    } 

   if( priceatbuy > 0 ) 
    { 
       highsincebuy = Max( High[ i ], highsincebuy ); 
       
       //if ( TraceIsTrue[i]  )  {     
        // _TRACE("HighSinceBuy at "+highsincebuy+" at "+DateTimeFormat(dtformat,dt[i]));
       //}

      if( exit == 0 AND High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy ) 
       { 
          //first profit target hit - scale-out 
        
         exit = 1; 
         Buy[ i ] = sigScaleOut; 
         
         pecentX=( 1 + FirstProfitTarget * 0.01 );
         
          //if ( TraceIsTrue[i]  ) {
         _TRACE("Sell#1 from scale out due to Hi "+High[ i ]+ " >=" +pecentX +" of BuyPrice="+ priceatbuy);
         _TRACE("ScaleOut="+Buy[ i ]+ " at "+DateTimeFormat(dtformat,dt[i]));
         //}
         
       } 

      if( exit == 1 AND  High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy ) 
       { 
          //second profit target hit - exit 
         exit = 2; 
         
         pecentX2=( 1 + SecondProfitTarget * 0.01 );
         SellPrice[ i ] = Max( Open[ i ], ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy ); 
           
          //if ( TraceIsTrue[i] ) {
         _TRACE("Sell#2 due to Hi "+High[ i ]+ " >=" +pecentX2 +" of BuyPrice="+ priceatbuy);
         _TRACE("Sell price ="+SellPrice[ i ]+ " at "+DateTimeFormat(dtformat,dt[i]));
       
          //}
       } 

      //if( Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy ) 
      // { 
          //trailing stop hit - exit 
      //   exit = 3;    
      //   SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01 ) * highsincebuy ); 
      // } 

      if( exit >= 2 ) 
       { 
         Buy[ i ] = 0; 
         Sell[ i ] = exit + 1; // mark appropriate exit code 
         exit = 0; 
         priceatbuy = 0; // reset price 
         highsincebuy = 0; 
       } 
     } 
    }// process only bar in range
} //for

SetPositionSize( 50, spsPercentOfEquity ); 
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position



x6

Hi ,
I just download Dbgview.
How to integrate it to Amibroker?

x7

Thank you in advance.

You don't need to integrate. Simply _TRACE() output will appear in DebugView automagically.

1 Like

/*Sample Test 
Robins 1/2/16 -30/9/16
Test#1 take profit at FirstProfitTarget=30% and SecondProfitTarget=50%
Test#2 take profit at FirstProfitTarget=30% and SecondProfitTarget=60% but price was stopped put from TrailingStop=10
*/

//Example of code that exits 50% on first profit target, 50% on next profit target and everything at trailing stop:
MAxPosx=1;
SetOption ("InitialEquity", 200000);
SetOption ("MaxOpenPositions", MAxPosx);
SetOption ("MinShares", 100);
RoundLotSize=100;



Buy = Cross( EMA( C, 10 ), eMA( C, 200 ) ); 
Sell = 0; 


FirstProfitTarget = 30; // 30% of position if FIRST PROFIT TARGET stop is hit 
SecondProfitTarget = 50; // 60% of position is SECOND PROFIT TARGET stop is hit 
 //100% of position if TRAILING STOP is hit 
TrailingStop = 10; // also in percent 

priceatbuy=0; 
highsincebuy = 0; 

exit = 0; 

dt=DateTime();
dtformat = "\n%d-%m-%Y\n";


//Most important to debug
debugOn=True;
TraceIsTrue=debugON AND Status("BarinRange");


//Most important to DEbug
bir = Status( "barinrange" );


for( i = 0; i < BarCount; i++ ) { 

   if(bir[i])// process particullar bar in range
   {
   
   if( priceatbuy == 0 AND Buy[ i ] ) 
    { 
       priceatbuy = BuyPrice[ i ]; 

        //if ( TraceIsTrue[i]  )
       _TRACe("#Buy Price=" +priceatbuy+" @"+DateTimeToStr(dt[i]));
       
    } 

   if( priceatbuy > 0 ) 
    { 
       highsincebuy = Max( High[ i ], highsincebuy ); 
       


      if( exit == 0 AND High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy ) 
       { 
          //first profit target hit - scale-out 
        
         exit = 1; 
         Buy[ i ] = sigScaleOut; 
         
         pecentX=(( 1 + FirstProfitTarget * 0.01 )-1)*100;
         
          //if ( TraceIsTrue[i]  ) {
          
         _TRACE("#Sell#1 ScaleOut="+C[i] +" due to Hi "+High[ i ]+ " >=" +pecentX +" % of BuyPrice="+ priceatbuy+" @"+DateTimeToStr(dt[i]));
         //}
         
       } 

      if( exit == 1 AND  High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy ) 
       { 
          //second profit target hit - exit 
         exit = 2; 
         
         pecentX2=((1 + SecondProfitTarget * 0.01 )-1)*100;
         SellPrice[ i ] = Max( Open[ i ], ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy ); 
           
          //if ( TraceIsTrue[i] ) {
         _TRACE("Sell#2="+NumToStr( SellPrice[ i ],1.2)+" due to Hi "+High[ i ]+ " >=" +pecentX2 +" % of BuyPrice="+ priceatbuy+" @"+DateTimeToStr(dt[i]));

       
          //}
       } 

      if( Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy ) 
       { 
         //Trailing stop hit - exit 
         exit = 3;    
         percentrail= ((1 - TrailingStop * 0.01 )-1)*100;
         SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01 ) * highsincebuy ); 
          _TRACE("#Sell Trailing="+NumToStr( SellPrice[ i ],1.2)+" due to Low "+Low[ i ]+ " <=" +percentrail +" % of HighSinceBuy="+  highsincebuy+" @"+DateTimeToStr(dt[i]));
       } 

      if( exit >= 2 ) 
       { 
         Buy[ i ] = 0; 
         Sell[ i ] = exit + 1; // mark appropriate exit code 
         exit = 0; 
         priceatbuy = 0; // reset price 
         highsincebuy = 0; 
       } 
     } 
    }// process only bar in range
} //for


PositionScore=RSI(15);

SetPositionSize( 100/MAxPosx, spsPercentOfEquity ); 
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position



Finally, The most important thing to do as follows.

  1. activate debug mode
    debugOn=True;
    TraceIsTrue=debugON AND Status("BarinRange");

  2. setting barinrange to process particular own logic in your specific range in anaysis function.
    bir = Status( "barinrange" );

Many thank to @fxshrat

1 Like