[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: Scaling out of positions



PureBytes Links

Trading Reference Links

Allan,

had a similar code working on a different and simpler platform,
VisualChart. Backtested multiple scale out without troubles in
different timeframes. Moved to AB because of higher flexibility, first
class programming interface and some expert recommendation. I'm
delighted with AB, great UKB and support, but found out that up to the
current version, since AB is only capable to pass one exit or scaleout
signal per bar to the backtester, you will get 'other' results if for
instance two scale out signals are triggered over the same bar. Coding
a custom backtester you get up to 1 scaleout and exit signals in the
same bar.
Please see backtester posts on UKB and check manually your code's
backtester results if multiple signals happen on the same bar. 
This is restrictive if you want to truly backtest multiple scaleout
without taking care that the signals get distributed over different
bars. In real life, depending on the scaleout targets it is likely
that multiple scaleout happen on the same bar, even on 5 min bars.
Have posted an improvement suggestion and hope in newer AB releases
this limitation is removed or a feasible workaround is implemented.
Took time to find out, hope you can save this time.
Regards,
FMartin

--- In amibroker@xxxxxxxxxxxxxxx, "matrix10014" <allansn@xxx> wrote:
>
> Hi all,
> 
> Would appreciate a little help with the code posted below.I am 
> attempting to find out if scaling out of positions has merits over 
> full liquidation.I am capable of optimising the first 2 profit 
> targets,as well as the trailing stop,but I am unable to figure out 
> how to optimize the percent of the position to scale out of at the 
> first profit target.I know it entails optimising 
> the "SetPositionSize".Any thoughts or help much appreciated
> 
> Allan
> 
> Buy = Cross( MA( C, 10 ), MA( C, 50 ) ); 
> 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 = Optimize( "FirstPT",20,15,400,5 ); 
> ; // profit 
> SecondProfitTarget = Optimize( "SecondPT",50,50,100,5 ); 
> ; // in percent 
> TrailingStop = Optimize( "TS",15,10,40,5 ); 
>  // also in percent 
> 
> priceatbuy=0; 
> highsincebuy = 0; 
> 
> exit = 0; 
> 
> for( i = 0; i < BarCount; i++ ) 
> { 
>    if( priceatbuy == 0 AND Buy[ i ] ) 
>     { 
>        priceatbuy = BuyPrice[ 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; 
>        } 
> 
>       if( exit == 1 AND 
>           High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * 
> priceatbuy ) 
>        { 
>          // second profit target hit - exit 
>          exit = 2; 
>          SellPrice[ i ] = Max( Open[ i ], ( 1 + SecondProfitTarget * 
> 0.01 ) * priceatbuy ); 
>        } 
> 
>       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
>



------------------------------------

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/