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.
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.
//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!
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
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".