{"id":675,"date":"2014-11-04T05:15:28","date_gmt":"2014-11-04T10:15:28","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=675"},"modified":"2014-12-04T08:29:26","modified_gmt":"2014-12-04T13:29:26","slug":"indicators-based-on-user-values","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/11\/04\/indicators-based-on-user-values\/","title":{"rendered":"Indicators based on user values rather than standard OHLC prices"},"content":{"rendered":"

Sometimes we may want to calculate indicators based not only on standard OHLC prices but on some other user-definable values. Some functions like RSI or CSI have additional versions (RSIa, CCIa respectively) that accept custom input array. In this case it is very easy to calculate the indicator based on user defined value. For example RSI from average of High and Low prices could be written as follows:<\/p>customArray <\/span>= ( <\/span>High <\/span>+ <\/span>Low <\/span>) \/ <\/span>2<\/span>;

<\/span>Plot<\/span>( <\/span>RSIa<\/span>( <\/span>customArray<\/span>, <\/span>14 <\/span>), <\/span>"RSI from (H+L)\/2"<\/span>, <\/span>colorRed <\/span>)<\/code>

But many of the built-in indicators available in AFL as functions refer indirectly to standard OHLC arrays and their parameters do not offer array argument as one of inputs. <\/p>

Fortunatelly there is an easy way to provide custom array as input for any other built-in functions. For this purpose, it is enough to override OHLC arrays (or just Close if the indicator only uses Close as input) within the code before calling given function and assign our custom array. As a simple example, let us consider calculating MACD indicator out of average of High and Low prices as input.<\/p>procedure SaveRestorePrices<\/span>( <\/span>DoSave <\/span>)
{
  global <\/span>SaveO<\/span>, <\/span>SaveH<\/span>, <\/span>SaveL<\/span>, <\/span>SaveC<\/span>, <\/span>SaveV<\/span>;

  if( <\/span>DoSave <\/span>)
  {
     <\/span>SaveO <\/span>= <\/span>Open<\/span>;
     <\/span>SaveH <\/span>= <\/span>High<\/span>;
     <\/span>SaveL <\/span>= <\/span>Low<\/span>;
     <\/span>SaveC <\/span>= <\/span>Close<\/span>;
     <\/span>SaveV <\/span>= <\/span>Volume<\/span>;
  }
  else
  {
    <\/span>Open <\/span>= <\/span>SaveO<\/span>;
    <\/span>High <\/span>= <\/span>SaveH<\/span>;
    <\/span>Low <\/span>= <\/span>SaveL<\/span>;
    <\/span>Close <\/span>= <\/span>SaveC<\/span>;
    <\/span>Volume <\/span>= <\/span>SaveV<\/span>;
  }
}

<\/span>\/\/ save OHLCV arrays
<\/span>SaveRestorePrices<\/span>( <\/span>True <\/span>);

<\/span>\/\/ calculate our array
<\/span>customArray <\/span>= ( <\/span>High <\/span>+ <\/span>Low <\/span>) \/ <\/span>2<\/span>;

<\/span>\/\/ override built-in array(s)
<\/span>Close <\/span>= <\/span>customArray<\/span>;

<\/span>\/\/ calculate our function, MACD and Signal in this case
<\/span>Plot<\/span>( <\/span>MACD<\/span>( <\/span>12<\/span>, <\/span>26 <\/span>), <\/span>"MACD"<\/span>, <\/span>colorRed <\/span>);
<\/span>Plot<\/span>( <\/span>Signal<\/span>( <\/span>12<\/span>, <\/span>26<\/span>, <\/span>9 <\/span>), <\/span>"Signal"<\/span>, <\/span>colorBlue <\/span>);

<\/span>\/\/ restore OHLCV arrays
<\/span>SaveRestorePrices<\/span>( <\/span>False <\/span>)<\/code>

The code first calculates the custom array (we use just use average of High<\/strong> and Low<\/strong> prices in this example, but of course the calculations may be more complex), then assigns the result of these calculations to Close<\/strong> overriding the regular values stored in close array. Then – when we call MACD() function which uses Close<\/strong> as input – it will be based on the modified values.<\/p>

The above operations do not affect the underlying database at all – the prices are overridden only for the purpose of calculation of this particular formula and other charts \/ indicators are not affected at all.<\/p>","protected":false},"excerpt":{"rendered":"

Sometimes we may want to calculate indicators based not only on standard OHLC prices but on some other user-definable values. Some functions like RSI or CSI have additional versions (RSIa, CCIa respectively) that accept custom input array. In this case it is very easy to calculate the indicator based on user defined value. For example […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[53,55],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/675"}],"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=675"}],"version-history":[{"count":3,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/675\/revisions"}],"predecessor-version":[{"id":682,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/675\/revisions\/682"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}