{"id":573,"date":"2014-10-17T16:27:06","date_gmt":"2014-10-17T21:27:06","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=573"},"modified":"2017-07-01T10:13:17","modified_gmt":"2017-07-01T09:13:17","slug":"using-price-levels-with-applystop-function","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/10\/17\/using-price-levels-with-applystop-function\/","title":{"rendered":"Using price levels with ApplyStop function"},"content":{"rendered":"

ApplyStop<\/strong> function by default requires us to provide stop amount (expressed in either dollar or percentage distance from entry price). Therefore, if we want to place stop at certain price level, then we need to calculate the corresponding stop amount in our code.<\/p>

This example shows how to place stops at previous bar Low<\/strong> (for long trades) and previous bar High<\/strong> (for short trades).<\/p>

Stop amount parameter is simply the distance between entry price and desired trigger price (exit point). For long trade it is entry price minus stop level, while for short trade it is trigger (exit) price minus entry price. Additionally we may check if calculated distance is at least 1-tick large. We can distinguish between long and short entry by checking if one of entry signals is present (if a Buy<\/strong> signal is active then it is long entry, otherwise short). We only need to take care about the fact that if we are using trade delays we need to get delayed Buy<\/strong> signal as shown in the code below:<\/p>TradeDelay <\/span>= <\/span>1<\/span>; <\/span>\/\/ set it to 0 for no delays

<\/span>SetTradeDelays<\/span>( <\/span>TradeDelay<\/span>, <\/span>TradeDelay<\/span>, <\/span>TradeDelay<\/span>, <\/span>TradeDelay <\/span>);
<\/span>TickSize <\/span>= <\/span>0.01<\/span>;

<\/span>\/\/ sample entry rules
<\/span>Buy <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() );
<\/span>Short <\/span>= <\/span>Cross<\/span>( <\/span>Signal<\/span>(), <\/span>MACD<\/span>() );
<\/span>Sell <\/span>= <\/span>Cover <\/span>= <\/span>0<\/span>; <\/span>\/\/ no other exit conditions, just stops

<\/span>BuyPrice <\/span>= <\/span>SellPrice <\/span>= <\/span>ShortPrice <\/span>= <\/span>CoverPrice <\/span>= <\/span>Close<\/span>;

<\/span>\/\/ define stop level
<\/span>stopLevelLong <\/span>= <\/span>Ref<\/span>( <\/span>L<\/span>, -<\/span>1 <\/span>); <\/span>\/\/ use previous bar low
<\/span>stopLevelShort <\/span>= <\/span>Ref<\/span>( <\/span>H<\/span>, -<\/span>1 <\/span>); <\/span>\/\/ use previous bar high

\/\/ calculate stop amount
<\/span>stopAmountLong <\/span>= <\/span>BuyPrice <\/span>- <\/span>stopLevelLong<\/span>;
<\/span>stopAmountShort <\/span>= <\/span>stopLevelShort <\/span>- <\/span>ShortPrice<\/span>;

<\/span>\/\/ make sure stop-amount is at least one tick
<\/span>stopAmountLong <\/span>= <\/span>Max<\/span>( <\/span>TickSize<\/span>, <\/span>BuyPrice <\/span>- <\/span>stopLevelLong <\/span>);
<\/span>stopAmountShort <\/span>= <\/span>Max<\/span>( <\/span>TickSize<\/span>, <\/span>stopLevelShort <\/span>- <\/span>ShortPrice <\/span>);

<\/span>\/\/ assign stop amount conditionally by checking if there is a Buy signal on given bar
<\/span>IsLong <\/span>= <\/span>Ref<\/span>( <\/span>Buy<\/span>, -<\/span>TradeDelay <\/span>);
<\/span>stopAmount <\/span>= <\/span>IIf<\/span>( <\/span>IsLong<\/span>, <\/span>stopAmountLong<\/span>, <\/span>stopAmountShort <\/span>);
<\/span>ApplyStop<\/span>( <\/span>stopTypeLoss<\/span>, <\/span>stopModePoint<\/span>, <\/span>stopAmount<\/span>, <\/span>True <\/span>)<\/code>","protected":false},"excerpt":{"rendered":"

ApplyStop function by default requires us to provide stop amount (expressed in either dollar or percentage distance from entry price). Therefore, if we want to place stop at certain price level, then we need to calculate the corresponding stop amount in our code.This example shows how to place stops at previous bar Low (for long […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[40],"tags":[53,14,15],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/573"}],"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=573"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/573\/revisions"}],"predecessor-version":[{"id":1439,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/573\/revisions\/1439"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}