http:\/\/www.amibroker.com\/gifs\/bt_regular.gif<\/a><\/p>The procedure below changes this behaviour and allows to use redundant signals (they are not removed). <\/p>
This is done by changing Buy array values from “true” to sigScaleIn (this prevents redundant signals from being removed because scale-in marks are kept untouched) and modifying standard procedure to treat scale-in signals as normal buys (no scaling).<\/p>
Note that there are many ways to achieve the same effect. The technique presented here was choosen because it is easy-to-use (does not require changes in your core trading system code – all it needs is to plug-in the custom backtest part). Longer implementation would be required if you do not want to (ab)use scaling signals. <\/p>
One thing worth mentioning is the fact that since scaling-in signals do not store position score this example formula does not support ranking of signals according to user-defined scores.<\/p><\/span>\/\/ YOUR TRADING SYSTEM HERE
<\/span>Buy<\/span>= <\/span>H <\/span>== <\/span>HHV<\/span>( <\/span>H<\/span>, <\/span>10 <\/span>); <\/span>\/\/ REPLACE THIS WITH YOUR OWN BUY RULE
<\/span>Sell <\/span>= <\/span>L <\/span>== <\/span>LLV<\/span>( <\/span>L<\/span>, <\/span>10 <\/span>); <\/span>\/\/ REPLACE THIS WITH YOUR OWN SELL RULE
<\/span>PositionSize <\/span>= -<\/span>20<\/span>;
<\/span>SetOption<\/span>(<\/span>"MaxOpenPositions"<\/span>, <\/span>5 <\/span>);
<\/span>\/\/ END OF TRADING SYSTEM HERE
\/\/ COMMON CODE PART
\/\/ TO BE COPY-PASTED if you want keep redundant signals
\/\/ This is long-only version.
\/\/ It is easy to extend to handle short trades as well
<\/span>Buy <\/span>= <\/span>IIf<\/span>( <\/span>Buy<\/span>, <\/span>sigScaleIn<\/span>, <\/span>False <\/span>); <\/span>\/\/ replace regular buy signals by scale in
\/\/ so they do not get filtered
<\/span>SetOption<\/span>(<\/span>"UseCustomBacktestProc"<\/span>, <\/span>True <\/span>);
if( <\/span>Status<\/span>(<\/span>"action"<\/span>) == <\/span>actionPortfolio <\/span>)
{
<\/span>bo <\/span>= <\/span>GetBacktesterObject<\/span>();
<\/span>bo<\/span>.<\/span>PreProcess<\/span>(); <\/span>\/\/ Initialize backtester
<\/span>for(<\/span>bar<\/span>=<\/span>0<\/span>; <\/span>bar<\/span><<\/span>BarCount<\/span>; <\/span>bar<\/span>++)
{
for ( <\/span>sig<\/span>=<\/span>bo<\/span>.<\/span>GetFirstSignal<\/span>(<\/span>bar<\/span>); <\/span>sig<\/span>; <\/span>sig<\/span>=<\/span>bo<\/span>.<\/span>GetNextSignal<\/span>(<\/span>bar<\/span>) )
{
<\/span>\/\/ first handle exit signals
<\/span>if (<\/span>sig<\/span>.<\/span>IsExit<\/span>() AND <\/span>sig<\/span>.<\/span>Price <\/span>!= -<\/span>1 <\/span>)
{
<\/span>\/\/ Exit Signal
<\/span>bo<\/span>.<\/span>ExitTrade<\/span>(<\/span>bar<\/span>,<\/span>sig<\/span>.<\/span>symbol<\/span>,<\/span>sig<\/span>.<\/span>Price<\/span>);
}
}
<\/span>\/\/ update stats after closing trades
<\/span>bo<\/span>.<\/span>UpdateStats<\/span>(<\/span>bar<\/span>, <\/span>1 <\/span>);
<\/span>bContinue <\/span>= <\/span>True<\/span>;
for ( <\/span>sig<\/span>=<\/span>bo<\/span>.<\/span>GetFirstSignal<\/span>(<\/span>bar<\/span>); <\/span>sig <\/span>AND <\/span>bContinue<\/span>;
<\/span>sig<\/span>=<\/span>bo<\/span>.<\/span>GetNextSignal<\/span>(<\/span>bar<\/span>))
{
<\/span>\/\/ enter new trade when scale-in signal is found
\/\/ and we don't have already open position for given symbol
<\/span>if (<\/span>sig<\/span>.<\/span>IsScale<\/span>() AND <\/span>sig<\/span>.<\/span>Price <\/span>!= -<\/span>1 <\/span>AND
<\/span>IsNull<\/span>( <\/span>bo<\/span>.<\/span>FindOpenPos<\/span>( <\/span>sig<\/span>.<\/span>Symbol <\/span>) ) )
{
<\/span>\/\/ Entry Signal
<\/span>if( <\/span>bo<\/span>.<\/span>EnterTrade<\/span>(<\/span>bar<\/span>, <\/span>sig<\/span>.<\/span>symbol<\/span>, <\/span>sig<\/span>.<\/span>IsLong<\/span>(),
<\/span>sig<\/span>.<\/span>Price<\/span>,<\/span>sig<\/span>.<\/span>PosSize<\/span>) == <\/span>0 <\/span>)
{
<\/span>\/\/ if certain trade can not be entered due to insufficient funds
\/\/ or too small value (less than \"MinPositionValue\") or
\/\/ to few shares (less than \"MinShares"
\/\/ then do NOT process any further signals
<\/span>bContinue <\/span>= <\/span>False<\/span>;
}
}
}
<\/span>bo<\/span>.<\/span>UpdateStats<\/span>(<\/span>bar<\/span>,<\/span>1<\/span>); <\/span>\/\/ MAE\/MFE is updated when timeinbar is set to 1.
<\/span>bo<\/span>.<\/span>UpdateStats<\/span>(<\/span>bar<\/span>,<\/span>2<\/span>);
}
<\/span>bo<\/span>.<\/span>PostProcess<\/span>(); <\/span>\/\/ Finalize backtester
<\/span>} <\/code>","protected":false},"excerpt":{"rendered":"NOTE: THIS ARTICLE IS NOW OUTDATED AS AMIBROKER SUPPORTS NEW BACKTEST MODE THAT HANDLES THIS NATIVELY http:\/\/www.amibroker.com\/f?setbacktestmodeThe sample code below shows how to use custom portfolio backtester procedure to change the way backtester works. Normally buy is matched against sell and redundant buy signals between initial buy and matching sell are removed as shown in […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[53,14,15],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/28"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=28"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":1558,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/28\/revisions\/1558"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}