{"id":6,"date":"2006-03-06T09:15:22","date_gmt":"2006-03-06T14:15:22","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/2006\/03\/06\/preventing-exit-during-first-n-bars\/"},"modified":"2014-12-04T08:24:08","modified_gmt":"2014-12-04T13:24:08","slug":"preventing-exit-during-first-n-bars","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2006\/03\/06\/preventing-exit-during-first-n-bars\/","title":{"rendered":"Preventing exit during first N bars"},"content":{"rendered":"

Here is sample technique that allows to prevent exiting position during first N bars since entry. The implementation uses loops that checks for signals in Buy array and if it finds one it starts counting bars in trade. During first N bars all sell signals are then removed (set to zero) only once counter reaches user-defined limit sell signals are accepted.

To change the N parameter please modify MinHoldBars variable (in this sample formula it is set to 17). <\/p>

Note also that code works only for regular trading systems that do not use any stops.
If you use stops or use rotational trading then the only solution would be using custom backtester. On the positive side: next version of AmiBroker (4.78) will have native implementation of MinHoldBars so you will not need to code it for yourself.<\/p><\/span>\/\/ Sample system
\/\/ Buy when MACD is greater than zero AND RSI is greater than 30
\/\/ Sell if either MACD is less than zero
\/\/ OR RSI crosses below 70

<\/span>Buy <\/span>= <\/span>MACD<\/span>() > <\/span>0 <\/span>AND <\/span>RSI<\/span>() > <\/span>30<\/span>;
<\/span>Sell <\/span>= <\/span>MACD<\/span>() < <\/span>0 <\/span>OR <\/span>Cross<\/span>( <\/span>70<\/span>, <\/span>RSI<\/span>() );

<\/span>\/\/ now we would like to ensure that position is NOT
\/\/ exited during first MinHoldBars 

<\/span>MinHoldBars <\/span>= <\/span>17<\/span>; <\/span>\/\/ say don't want to exit for first 17 bars since entry

\/\/ first filter out buy signals occuring outside testing range
<\/span>Buy <\/span>= <\/span>Buy <\/span>AND <\/span>Status<\/span>(<\/span>"barinrange"<\/span>);

<\/span>BarsInTrade <\/span>= <\/span>0<\/span>;
for( <\/span>i <\/span>= <\/span>0<\/span>; <\/span>i <\/span>< <\/span>BarCount<\/span>; <\/span>i<\/span>++ )
{
  <\/span>\/\/ if in-trade, then increase bar counter
  <\/span>if( <\/span>BarsInTrade <\/span>> <\/span>0 <\/span>) <\/span>BarsInTrade <\/span>++;
  else
  if( <\/span>Buy<\/span>[ <\/span>i <\/span>] ) <\/span>BarsInTrade <\/span>= <\/span>1<\/span>; <\/span>\/\/ on buy signal start counting bars-in-trade

  \/\/ if we are in trade - remove sells occurring too soon
  <\/span>if( <\/span>BarsInTrade <\/span>< <\/span>MinHoldBars <\/span>) <\/span>Sell<\/span>[ <\/span>i <\/span>] = <\/span>0<\/span>;
  else
  if( <\/span>Sell<\/span>[ <\/span>i <\/span>] ) <\/span>BarsInTrade <\/span>= <\/span>0<\/span>; <\/span>\/\/ on sell reset barsintrade flag
<\/span><\/code>","protected":false},"excerpt":{"rendered":"

Here is sample technique that allows to prevent exiting position during first N bars since entry. The implementation uses loops that checks for signals in Buy array and if it finds one it starts counting bars in trade. During first N bars all sell signals are then removed (set to zero) only once counter reaches […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[53,14,15],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/6"}],"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=6"}],"version-history":[{"count":1,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/6\/revisions"}],"predecessor-version":[{"id":832,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/6\/revisions\/832"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}