{"id":285,"date":"2014-09-20T04:53:26","date_gmt":"2014-09-20T09:53:26","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=285"},"modified":"2014-12-04T08:24:39","modified_gmt":"2014-12-04T13:24:39","slug":"broad-market-timing-in-system-formulas","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/09\/20\/broad-market-timing-in-system-formulas\/","title":{"rendered":"Broad market timing in system formulas"},"content":{"rendered":"

Some trading systems may benefit from attempt to time the broad market. A market-wide valuation, such as moving average, sentiment or some other mechanism may be used to tell if we should be in the market or not. <\/p>

Flexibility of AFL language allows to create rules or indicators, which are based on more than just one symbol. This enables us to introduce additional filters based on wide-market index performance.<\/p>

For the purpose of reading quotes of another symbol one can use Foreign<\/strong> or SetForeign<\/strong> functions.<\/p>

The following formula shows how to generate entry signals in individual stocks when S&P500 index is above its 200-period moving average and exit signals when S&P500 is equal or below 200-period average (^GSPC is a ticker for Yahoo Finance data for S&P500)<\/p><\/span>\/\/
\/\/ read S&P 500 values from ^GSPC ticker
\/\/
<\/span>sp500 <\/span>= <\/span>Foreign<\/span>( <\/span>"^GSPC"<\/span>, <\/span>"C" <\/span>);
<\/span>\/\/
\/\/ market-wide filter should be in \"state" form
\/\/ (so it is True all the time when market is up)
\/\/
<\/span>marketup <\/span>= <\/span>sp500 <\/span>> <\/span>MA<\/span>( <\/span>sp500<\/span>, <\/span>200 <\/span>);
<\/span>marketdown <\/span>= <\/span>NOT marketup<\/span>;
<\/span>\/\/
\/\/ sample trading rules (MACD crossovers)
\/\/
<\/span>BuySignal <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() );
<\/span>SellSignal <\/span>= <\/span>Cross<\/span>( <\/span>Signal<\/span>(), <\/span>MACD<\/span>() );
<\/span>\/\/
\/\/ combine per-symbo signals with broad-market timing
\/\/
<\/span>Buy <\/span>= <\/span>BuySignal <\/span>AND <\/span>marketup<\/span>; <\/span>\/\/ enter trade only when buy signal AND market is in up trend
<\/span>Sell <\/span>= <\/span>SellSignal <\/span>OR <\/span>marketdown<\/span>; <\/span>\/\/ exit position if sell signal OR market turns dow<\/code>

A more complex broad-market timing that requires not only closing price of market index can be implemented using SetForeign<\/strong> function. SetForeign<\/strong> replaces all OHLCV data series with that of the “other” security and allows to calculate all kind of indicators that would normally use current security. Broad market timing does not need to be just “all-in” or “all-out” switch. For example one can switch the trading method depending on whenever broad market is trending or sideways.<\/p><\/span>\/\/
\/\/ Switch to S&P symbol to calculate broad-market timing
\/\/
<\/span>SetForeign<\/span>( <\/span>"^GSPC" <\/span>);
<\/span>\/\/
\/\/ now we can calculate any indicator based on SP500
\/\/
<\/span>MarketIsTrending <\/span>= <\/span>ADX<\/span>( <\/span>40 <\/span>) > <\/span>20<\/span>; <\/span>\/\/ ADX (40 days) from SP500
\/\/
\/\/ now go back to original data (current symbol)
\/\/
<\/span>RestorePriceArrays<\/span>();
<\/span>\/\/
\/\/ you can have different rules that are switched
\/\/ depending on what broad market is doing
\/\/
<\/span>TrendingBuy <\/span>= <\/span>Cross<\/span>( <\/span>C<\/span>, <\/span>MA<\/span>( <\/span>C<\/span>, <\/span>30 <\/span>) );
<\/span>TrendingSell <\/span>= <\/span>Cross<\/span>( <\/span>MA<\/span>( <\/span>C<\/span>, <\/span>30 <\/span>), <\/span>C <\/span>);
<\/span>\/\/
<\/span>SidewaysBuy <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() );
<\/span>SidewaysSell <\/span>= <\/span>Cross<\/span>( <\/span>Signal<\/span>(), <\/span>MACD<\/span>() );
<\/span>\/\/
\/\/ switch methods using broad-market timing
\/\/
<\/span>Buy <\/span>= <\/span>IIf<\/span>( <\/span>MarketIsTrending<\/span>, <\/span>TrendingBuy<\/span>, <\/span>SidewaysBuy <\/span>);
<\/span>Sell <\/span>= <\/span>IIf<\/span>( <\/span>MarketIsTrending<\/span>, <\/span>TrendingSell<\/span>, <\/span>SidewaysSell <\/span>)<\/code>

In this simple example we assume that market timing signals change very infrequently so they change much less often than Trending\/Sideways Buy\/Sell signals are generated. If that is not the case the switching logic would need to be more complex to decide what to do when we are in the “trending” trade and market switches to sideways mode. In such situation, the code above uses SidewaysSell signal to sell the position, which may or may not be what you are after.<\/p>

Another example is changing position sizing depending on broad market conditions. We can choose to be fully invested when broead market is up and only 30% invested in down market. <\/p><\/span>\/\/
\/\/ Switch to S&P symbol to calculate broad-market timing
\/\/
<\/span>SetForeign<\/span>( <\/span>"^GSPC" <\/span>);
<\/span>\/\/
\/\/ now we can calculate any indicator based on
\/\/ SP500
\/\/
<\/span>MarketIsUp <\/span>= <\/span>C <\/span>> <\/span>MA<\/span>( <\/span>C<\/span>, <\/span>200 <\/span>); <\/span>\/\/ here C represents closing price of SP500
\/\/
\/\/ now go back to origiginal data (current symbol)
\/\/
<\/span>RestorePriceArrays<\/span>();
<\/span>\/\/
\/\/ normal rules (in this example they do not chang)
\/\/
<\/span>Buy <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() );
<\/span>Sell <\/span>= <\/span>Cross<\/span>( <\/span>Signal<\/span>(), <\/span>MACD<\/span>() );
<\/span>\/\/
\/\/ no more than 10 positions open at a time
\/\/
<\/span>SetOption<\/span>(<\/span>"MaxOpenPositions"<\/span>, <\/span>10 <\/span>);
<\/span>\/\/
\/\/ change position sizing depending on broad market conditions
\/\/ in this example we will allocate:
\/\/ 10% per position
\/\/ if broad market is up (so we can be allocated upto 100% of funds)
\/\/ 3% per position
\/\/ if broad market is down (so we can be allocated upto 30% of funds)
\/\/
<\/span>SetPositionSize<\/span>( <\/span>IIf<\/span>( <\/span>MarketIsUp<\/span>, <\/span>10<\/span>, <\/span>3 <\/span>), <\/span>spsPercentOfEquity <\/span>)<\/code>","protected":false},"excerpt":{"rendered":"

Some trading systems may benefit from attempt to time the broad market. A market-wide valuation, such as moving average, sentiment or some other mechanism may be used to tell if we should be in the market or not. Flexibility of AFL language allows to create rules or indicators, which are based on more than just […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","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\/285"}],"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=285"}],"version-history":[{"count":1,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/285\/revisions"}],"predecessor-version":[{"id":789,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/285\/revisions\/789"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}