Showing posts with label Indian Equity. Show all posts
Showing posts with label Indian Equity. Show all posts

Saturday, October 21, 2017

Systematic Trading | Using Autoencoder for Momentum Trading

In a previous post, we discussed the basic nature of various technical indicators and noted some observations. One of the ideas was: at a basic level, most indicators captures the concept of momentum vs mean-reversion. Most do so in the price returns space, but some in a non-linear transformation of the returns space, like signed returns or time since new high/ low. We presented the idea of a PCA approach to extract the momentum signals embedded in these indicators. From there to a trading model, the steps will be to collate this momentum signal (1st PCA component or higher if required) along with other input variables (like returns volatility and/ or other fundamental indicators) to train a separate regression/ classification model (like a random forest or a deep NN).

One of the issues with using simple PCA is that it is linear and hence may not be appropriate to summarize different measures captured across all these indicators. Here we discuss the next logical improvement - a nonlinear dimensional reduction approach using autoencoder.

As discussed here, the new Keras R interface has now made it very easy to develop deep learning models in R using the TensorFlow framework. Here we use this interface to train an autoencoder to fit the same set of technical indicators on NSE Nifty 50 Index as before. The steps involved are relatively straight-forward. First we generate and standardize the inputs (technical indicators levels). Then we build the computation graph.

To do so, first we define the encoding layers (2 hidden layers, the latent coded unit size is 3, to match the first 3 components of the PCA we use for comparison), and two different decoding layers. The two different decoding layers are to  enable us to train the auto-encoder as well as compute only decoding independently.


Next we combine these layers to create the computational graph. One for the encoder only, another for the decoder, and a third one for the end-to-end autoencoder, that we will actually train.

The rest of it is standard. We define a loss function to map the input to the output, measuring mean squared losses, and train the model. The training is done on data till 2013, and test set is since 2014 till present. Once the training is done, we can use the encoder and decoder separately to generate a dimensionality reduction of the input space and vice-versa.

The output of the dimensionality reduction is compared with the PCA. As it appears from the correlations, the PCAs are almost one-to-one mapped to the three latent dimensions in the hidden layer generating the encoding. So the encoded layers are orthogonal in our case, although this need not be true always.

V1
V2
V3
PC1
1
-0.3
0.2
PC2
0.1
-0.2
0.8
PC3
-0.2
-0.9
0.5

The scatter plot below captures the same, but also highlights the some non-linearity, especially the first component of PCA vs the first latent dimension from the autoencoder.


From here the next step is obvious, replace the PCA factors inputs in the momentum trading model in the first paragraph with these latent dimensions from the autoencoder and re-evaluate. This will capture a richer set of inputs that can handle non-linearity and hopefully performs better than linear PCA. Here are some results what other reported (opens PDF). Here are some more (opens PDF) on the using autoencoder for cross-sectional momentum trading. The entire code is available here.

Friday, September 22, 2017

Macro | A Paradigm Shift For India's Retail Investors?

The Indian economy is at an interesting point. We had two large scale policy moves in recent time - the much controversial Demonetization in November last year, and the implementation of (a somewhat rundown version) of Goods and Services Tax regime this year. Early this month, we had the first GDP print following these two major steps. The headline prints came in lower than consensus - 5.7 percentage for Q2 vs. 6.5 (and 6.1 last quarter). This was followed by equally weak Industrial Production release. A stronger than expected headline CPI prints did not help, as this squeezes the room for any rate cuts from the RBI.

A closer look at the GDP data (see component break-down in the chart below) shows some serious weakness. The private consumption part (C) has weakened significantly following the demonetization (the vertical red dashed line). The investment component (I) has been weak for a while (although staged a comeback in the last quarter). Exports growth was not helped by a strong rupee. In last few quarters, government expenditure helped the headline a lot. But the sustainability of this is questionable. We will have the fiscal deficit data out later this month. But the street does not expect anything great.

The story of the IIP paints a similar picture (see chart below, overall IIP, manufacturing, base materials, consumer durable, consumer non-durable, capital goods, electricity, intermediate goods and mining respectively). While demonetization appears to have caused a negative shock, in general most of them peaked out before that, around early 2016 to be fair. The capital goods, which staged a minor comeback since bottoming out in 2014, again resumed the downward trend, along with most (except consumer durable, and to some extend mining).


This is all in a relatively benign global macro scenario. In spite of the Fed taper 2.0 announcement, we have little jitters in the markets. Rates, both global and local, are relatively low and volatility remains subdued. Oil prices remain range-bound. A rally in oil along with a weakening INR following Fed and expected ECB taper later this year can worsen the scope of fiscal stimulus. Most in the business sectors does not expect private investments to turn around before end of this year at the earliest. The investment exuberance back in 2004-06 left many corporates laden with unmanageable debt burden and bank balance sheets with NPA.

In this background of weakening macro story, the Indian equity markets is in a tear. The flagship NSE Nifty Index posted a YTD 21%+ gain, among the best globally and compared to it's own history. The trailing 12-month PE ratio is looking worryingly high. High valuation remains a big concern among investors in this, and most other traditional metrics (a bit better in terms of price to book).

However, comparing the PE ratio to its historical average is not very good way to capture everything that goes on to determine fair price. In the most basic approach, the price of equity is a function of market risk free rates (say the local sovereign bond) and equity risk premium. Following the approach in this paper from AQR, I modeled the BSE SENSEX P/E based on the risk factors - the bond yields as well as the equity and bond volatilities (as in the original paper) along with current account balance as a percentage of GDP (reflecting the fiscal risk of the economy) and spread of bond yields to US Treasury (captures the flow risks). The last two are more relevant for an emerging market economy like India. The time-series shows a marked shift in relationship between pre- and post-crisis era. I fitted the model only on (monthly) data from 2010 onward to capture the recent dynamics. As it turns out, the bond vol has little contribution to market risk premia for India. The bond yield and equity vol shows significant but low correlation, whereas the CA deficit and spread to treasury captures a significant portion of the variance. The chart below shows the fit on this model (adjusted R-squared ~0.72).
According to this model, the PE ratio is only slightly on the over-valuation side - not a cause of great alarm. According to this model, the market was highly over-valued around late 2011, and early 2015. We saw corrections in both cases. Also the under-valued period, early this year, was followed by upward corrections as well. This model does not forecast a large correction anytime soon unless we rally up a lot quickly from here (obvious caveat: these are in-sample results).

But what is most interesting, and perhaps most significant is the recent flows that we have seen in Indian equity markets. Traditionally, the equity markets in India has been shunned by a large portion of retail investors. The experience of scams in 1990s and the melt-downs, once during dot-com busts and another in 2008, did not helped. The foreign portfolio investors dwarfed the domestic flows in cash equities for a long time (although it is a different story in F&O). But since 2014, something changed. The extra-ordinary flows in to the equities markets, led by domestic mutual funds (presumably on the back on retail savings channeled to equities) completely outpaced the foreign flows.
Is this a mass optimism following the 2014 election outcome and equity rally? Or are we witnessing a major shift in the savings behaviour of retail investors in India. The retail money has missed the initial come-back equity rally following the 2008 crash, and a part of the early 2014 rally as well, where the foreign investors made out handsomely. But much of the late rally in Indian equities has gone to the retail pockets. Is this dumb money chasing recent gains? We do not know for sure, but as we argued above, we are some distance away from any valuation melt-down in Indian equities. And the flow signifies the loss tolerance of the retails - who are sitting on some comfortable profits - has quite a bit room before panic. And finally, the weakening property markets and demonetization may have incentivized a permanent change in retail behaviour.

We do not know for sure. But what is the implication if it is indeed a fundamental shift in savings behaviour? As argued above, the macro in India is down, but with policies properly executed, the turn-around can be sharp. If oil remains range-bound and the Fed and ECB do not stray afar from the implied forward curves, we will have little in terms of global shock to upset the local economy. On the other hand, the efforts to put banking sector NPA in shape, along with the full kick-back of the GST regime should significantly improve the badly needed private investments. Add to this mixture this retail savings paradigm shift, and we are looking at the very beginning of a multi-year rally in Indian equity markets.

Thursday, January 7, 2016

Macro: The Chinese New Year

The gyrating market this week so far has more than done its bit to jolt people out of their holiday stupor. The Chinese equity markets and the law makers kept everyone, well, engaged. Late this afternoon we have seen some respite after the Chinese authorities repelled the stock circuit breaker rules. Equities rallied from day's low and bonds sold off. All fine and good. The question is, is it time to fade the market full of confused and panicked investors? Or should you panic yourself instead.
 
Whether the Chinese episode is something to worry about depends on the opinion about how in control the Chinese policy makers are, and what is their line of thinking. Chinese Yuan devaluation is not necessarily such a risk-off thing in itself. Arguably the Chinese authority looks at CNY against a trade-weighted baskets and not only dollars. And also in terms real effective exchange rates, Yuan is far from cheap, and a bit of regression to the mean (at whatever the authorities think it should be) should not cause so much pandemonium. Then again, it is not clear how much control the authorities have in executing these changes. Last year, and this year so far, most of the Yuan "devaluation" has been rather abrupt. This may be the communication policy of PBoC. But apparently that did not go well with the market. The re-balancing problem in China is a real thing. And it is just that, a re-balancing problem. If it can be controlled, with controlled devaluation and a smooth transition from investment based to consumption based GDP, and most importantly manage the debt from blowing off in between, this will be an adjustment.
 
If it is not controlled, it will be a crisis. Some are already calling it so. I tend to think it is not. Referring to someone who knows more about China than perhaps anyone else, there are encouraging signs in this rebalancing effort. The Chinese foreign exchange reserve is a hot issue in near terms. But more importantly, it is how they maintain the balance in the economy (low enough unemployment and no mass-bankruptcies) before they adjust to the new GDP paradigm is the most important question. And FX devaluation is a pretty smart and cheap way to achieve that. At a milder cost of a negative pressure on global inflation. Unless that creates a panic and becomes a self-fulfilling crisis.
 
Market seems to be focused on the second point of FX devaluation, fed by researches on where the level official reserve is and how much outflow it has seen in recent time. Remember how the Chinese market sold off massively in the June and rest of the world hardly noticed? And when that reversed in August when the PBoC revised CNY fixings. The question is how justified this fixation on FX is. The political economy in China is pretty much different than the developed nation or what we have seen in case of the Asian tigers or LatAms under sudden stop, The authorities, in principle, has much wider control on the economy. And if they do succumb to the sudden stop problem and that blows in to a full scale crisis with a collapse of asset prices, it will be felt far more geopolitically, than economically (apart from a certain global deflation, again). I would tend to assume so far what we have seen from the policy-makers are more likely to be mistakes and experiments than a sign of loss of control.
 
In the meanwhile, coming back to the original question, should to panic or fade? Statistically speaking, the odds are something like below (click to enlarge).

 
The charts shows the conditional upside and downside in representative equities after a given amount of weekly sell-off (the opportunity is the difference between upside and downside). It captures the next week's percentage move (vertical axis) given a percentage sell-off this week (x-axis). As we can see, with extreme moves come extreme opportunities. Equities so far sold off around 4% to 5% this week across markets. As you can see the time to jump in to wanton bullishness is still a couple of percentage points away, statistically speaking.
 
Note: these data sets excludes the wild days of 2008, but including them does not change the picture much.

Sunday, August 16, 2015

Systematic Strategies: HIgh Probability Trading Under Uncertainities

Stop-loss and take profit orders are powerful tools to manage an underlying trading view. And there are some misconceptions about them. Many people believe take-profit and stop-loss limits define the risk-reward ratio of a strategy. Far from it.

If a strategy has 100 on the upside as take-profit and 50 on the downside as stop-loss it is preposterous to estimate the risk-reward as 2:1. The most important input missing here are the probabilities of hitting the take-profit and stop-loss limits. For example if the probability of hitting the take profit in the trading horizon is 20% and hitting the stop-loss is 50% (i.e. the underlying can be within the range with a probability of 100% - 20% - 50% = 30%), then expected upside is 20%*100 = 20 and downside is 25. And the real risk-reward is 20:25 i.e. 4:5, far from the 2:1 before.

We can use this powerful tools and this concept of probabilities to devise profitable strategies under uncertainties. For example, suppose the underlying view is long an asset. Let's assume the asset follows a (geometric) Brownian motion (in real world) as below

dS/S = drift*dt + vol*dW

Where dt is differential of time, and dW is the standard Brownian Motion. So according to our view, here the drift is a positive value (the underlying view is long). One way to position for this is to go long and unwind at the end of the trading time horizon (let's say 1 day). The probability of our position being in profit can simply be calculated from a Black-Scholes like digital call option price.

However, another way is to put a take-profit order. This order gets executed whenever the underlying breaches the target from below. The probability of this execution is the probability of the underlying Brownian motion breaching the barrier any time up to our trading horizon. There are standard approximate solutions to this problem. For example, see here (opens PDF). As an example I have plotted the ratio of this probability to the original Digital probability (i.e. being in the money at the end of trading horizon, irrespective of what happens in between) for a range of volatility and Sharpe ratios (ratio of the drift term above to volatility) in the chart below.
As you can see, the probabilities of hitting our profit target intraday is much higher than being in the money end of the day. Additionally, higher the uncertainties about the underlying trend (lower the Sharpe) this ratio works better in out favor. This shows clearly that if your view is not with high conviction, it is better to use a take-profit target than a buy-and-hold approach.

This gives rise to an interesting way to implement a short-dated view using options. The strategy is to buy an option (say a ATM call option to implement a long view on the underlying) and put a close take-profit target, and a wide stop-loss. The worst-case loss is the option premium. And we will hit out target with 1.2x to 2x more frequently, depending on the strength of the trend. By design it may appear we have a skewed risk to reward ratio here. But given the concept of probabilities (than just the width of the stops and targets) and the convexity of a long option position this is much less risky than it appears. None-the-less it is a skewed strategy, with high probabilities of hitting our profit targets regularly, and occasional large losses.
The chart above shows the expected profit ranges for such a strategy on the NIFTY index (the flagship index of National Stock Exchange, India). The Spot level assumed to be 8500. The profit-target is 1x the daily vol move and the stops are 2x of that. (Note this is approximated, i.e. I ignored the order of stop-loss and target hitting, which is valid for large enough stops). As you can see if you are somewhat certain about the direction of the underlying trend, this is quite profitable (the break-even here is daily trend is 40% or more of the daily vol) under high vol. The profit distribution as below (click to enlarge)
This ignores two realities -1) the convexity of the option position, which goes in our favor and 2) the jumps in stock moves (price moves are rarely Brownian), which goes against us (the probabilities should get affected symmetrically, but the size of loss makes it asymmetric).

Those interested in the underlying codes (in R) can find it here.

Tuesday, June 30, 2015

Back-Testing Systematic Strategies Part 1: A Flow Chart Based Approach

Systematic trading brings on a certain discipline and the advantage in terms of risk management that comes along with it. And with today's cheap computing power, it is available to anyone. A proper back-testing is almost always at the core of a systematic trading strategy. A wisely designed back-test is the litmus test for an investment hypothesis. At the same time, it helps in sizing the trades and risk management given a particular ability and appetite to take on risks.

However, for the individual traders and investors who has developed market insights over the years, but not very familiar or eager or able to get their hands dirty with coding, running a back-test with dependable results can be a huge headache. Here I try to show how we can approach this problem in a structured manner.

The very essence of back-testing is defining some rules of trading (buy or sell signals) and risk management (stop loss or take profit etc.) based on a market price (of the instrument we are trading) and probably some more indicators (which can be as simple as a technical indicator, or a complex function defined by the user). The R package quantstrat handles this problem in a very well defined manner and split the functionalities in different blocks or modules so that designing a back test, running the strategy and analyzing the data all becomes simple and modular. We take a simple example of running a intraday momentum strategy on the National Stock Exchange flagship index NIFTY50 to highlight this point.

The core strategy we want to back-test is simple. Every trading day we wait for a certain amount of time (or price bars) after the market opens to see the initial momentum. If the open is positive (negative) beyond certain threshold, we then go long (short). Before the end of the trading day we square-off the position. Optionally we can also add a stop loss and take profit target. 

Thinking backwards, the back-test will be driven by our buy and sell trading rules and square-off rule. In turn they will be generated based on:

a) if the opening price criterion meets the condition and 
b) if we are at the beginning of the day, and finally 
c) if we are approaching end of day to place square off. 

This in turn means, the signals that need to go in is: 

i) a price move indicator for positive move
ii) a price move indicator for negative move
iii) a start-of-day indicator and 
iv) an end-of-day indicator

We would need i) and iii) both to be true (logical AND) for a buy signal, and ii) and iii) for a sell signal. The indicator iv) is enough to drive square-off. Schematically, the entire scheme looks below


And if we can design this schematic logically, in terms of a simple, easy to understand flow chart as above, it becomes rather straight forward to develop the underlying codes to run the strategies. As an example I ran the strategy for 2-minute bar of intraday data since 19th of May to 26th of June, and here are the results with different combinations (with 100 units for buying or selling for each trade).



For the underlying strategy without any stop-loss and take profit targets, the trade positions and evolution of profit and loss looks as below (click to enlarge).


And once we run the back-test, we can further delve in to the details and analytics, like the chart below which plot the Maximum Adverse Excursion plot for the base strategy
In short, a very simple strategy, quickly designed in to a simple flow charts. The back test shows it really works, and also throws lights on how to size the trades given your investible capital and risk tolerance.

We will further follow up on how to translate any back-testing in to a similar flow chart, which enables quick and reliable back-testing.

(Those who are interested in the underlying code, it is available here.)

Tuesday, March 24, 2015

Nifty: Small Caps vs Large Caps

Focusing back to Indian Equity markets!

The interplay between small cap and large cap has been very interesting back home, compared to global benchmarks. For last couple of years, S&P 500 and Russell 2000 more or less matched each others performance. In late 2013/ Early 2014 the small cap index outperformed, which is now reversed by a relative under-performance. Compare this to India. Ever since the financial crisis, the market recovery has been led by the large caps. Small caps (or mid caps) consistently under-performed, except since last June. The election saw a large out-performance by the small caps, but otherwise it has been pretty much dull. Small caps premiums has been in fact negative.

We take a look at the financials to see if there is any clue there. Below the aggregated balance sheet for Nifty 50 stocks, vs. Nifty Mid cap 50*



And here are the corresponding PnL figures*


* All data from Bloomberg as they report. I think more or less the trend is captured here.

As we can see, one large issue with the small cap balance sheet is in general total indebtedness. Since 2007, both the large caps and small caps companies increased their sales 2.3x, with an increase in balance sheet in the 3.44x/3.42x range. However while for Nifty companies it has been funded 70% by non-current liabilities, for Nifty Mid cap 50, the figure is at 80%. Both not great, but mid cap definitely worse. On top, the figures for small caps were worse to begin with. So the current levels look far from comforting from investors' point of view.

On the other hand, as far as the standard valuation parameters are concerned, on both revenue and balance sheet related metrics (like price-to-sales or P/E pr P/B) large caps are slightly overvalued (relative to historical spreads).

So overall it is not a straight forward call. Small caps are undervalued, but not by much as they were before mid 2014. At the same time, the overall sector balance sheet looks vulnerable to any interest rates shock. The question is if that valuation compensates for the leverage risks. Given the current outlook, probably this will tilt in the favor of small caps and mid caps over all. But not without a constant watch. I will definitely avoid any adventure in this space.

Tuesday, December 30, 2014

2015: Points to Ponder

As you gear up for the year end, here a list of things and points for the next year. To mull over, without any iota of attempts to forecast!

1. Oil: from peak-oil to freak oil. And how the story unfold will be driving a lot in 2015. IMF Direct (the blog from IMF) had a very interesting piece on this recently. They estimate unexpected lower demand can account for only 20% to 35% of the price drop. And they find little evidence of financialization. In this context what is surprising is the speed of adjustment. For 2015 most analysts maintain gloomy forecasts for oil. Perhaps rightly so. But a lot of that comes from forecast of continued lower demand from China and Europe. Given the lower contribution of demand in the price change (as above), and the still volatile geopolitics of a large part on the supply side, the question remains what if there is a strong come back of oil price in 2015? It will mostly reverse what we have seen in 2014. The hysteresis loss will be for new investments in oil sector with renewed long term risk assessment; and in Europe, especially if the ECB had not gone through with the QE by then.

2. Russia: very much related to above. Will they get out of it? yes if the oil price bounces back. What if it does not. That is the hard part to speculate. On the face of it Russia does not look particularly bad on economic parameters. Yes, the inflation is running a bit high, and the GDP has slowed down. But they have been there before. The missing links are current account weakness, ruble appreciation reversal, and the possibility of capital flight. Krugman explains the first two of them here. The last part is the hardest to explain and quantify. See here, for example. And in my opinion this is the most crucial make-or-break factor. Russia will survive in the short run if the oligarchs have a lot to lose otherwise, and if Putin survives.

3. Wage growth: That will shape the Fed policy to a large extent. We have already seen some encouraging trends. 2014 has been a great year for job growth in the US. 2015 might as well be a good (perhaps not great) year for wage growth. If that is supported by lower oil price, it is good. If that coincides with a sudden rise in oil price, that can spook the market and push up break-evens and rates.

4. Housing: One of the weakest part of the so far good enough recovery of the US, is the contribution of housing to the investment component and hence the economy. The flow of funds from the Fed has consistently shown continued deleveraging in mortgages while consumer credit picked up. The higher mortgage rates and increasing prices did not help it either. Historically the contribution of housing to GDP is near record low. And that to me seems like a lot of upside in 2015.

5. Europe: If we have a Grexit start of the year (or even a panic towards that), that will greatly ease Draghi's case for an all-out QE. European equities missed out a lot compared to elsewhere, and can benefit from both improved earnings and re-rating. As I mentioned before, I think people are unusually bearish on Europe now (just like they were unusually bullish a while back). If you think the US equities are done with most of the run, and Abenomics not really working for Japan, and missed out the Chinese rally and now scared of the EM, you do not have much choice. On the rates side, a lot of the curve flattening has been driven by global influence and a re-pricing of the long end. I do not think the rates market is nowhere near as confident of a QE as most analysts are. The European swap markets now looks hardly any different from Japan. And with much much better upside.

6. Abenomics: And speaking of Japan, which I frankly do not understand much, all I say I do not see Abenomics working. The problem with that is if Abenomics does not work, the challenge for the subsequent governments will be progressively humongous. What are the odds that we will stop to see the yen rallying in a global panic? And what are the odds we will actually see yen selling off in a panic? I will keep rolling my yen shorts. In good times or bad.

7. China: Perhaps most discussed. One good thing about China for traders and investors is that China, with its mighty central bank and strong command control hardly produces any large surprise for the markets. (Of course the antithesis is that when the surprise does come it will be huge and bad, but somehow I do not buy in to that yet). With the rally belying the economy, the central bank and policies will be in the driving seat.

8. The bull run in India: I am a believer. Well for one, the benefits of the large oil re-pricing on India is still totally lost in the panic about EM. In fact India has been a net importer of non-agri commodities. So recent secular weakness is a huge bonanza if they sustain. In terms of valuation it may not be cheap, but much scope remains for earning improvements. 

9. Return of volatility: A sustained period of low vol can be policy driven (when the central bankers become sellers of vols), or it can be just a phase of a complex system. Because low vols just happen some times. FX has already seen some uptick in vols. And yes, commodities of course. May be time for the rest.

10. What else: move away from rotation to diversification? a policy-driven liquidity crisis? year of the frontier markets? crisis in Europe? middle-east mayhem? HY melt-down? comeback from the UK? Wide open. As always.

Best wishes and a happy new year

Sunday, December 21, 2014

NIFTY: Day traders Vs. Investors (+ A Christmas Present !!)

Here are some interesting charts comparing how S&P CNX Nifty has performed over last many years - split between day-session performance vs. overnight. The pattern is very interesting. In 2007-2008, the day traders dominated, both in profits and in losses. Be it the run up to the pre-crisis top in early 2008, or the crash. It was again the day traders who profited most in the comeback in 2009. This continued till the peak in 2010. 

However, after that, something changed. 2010 was the last great year for the day traders. Since 2011, the overnight returns dominated returns during the day session, far and steady. That was the case during the mild bearish runs in 2011, the sideways market in 2012. And the trend continues strongly in to the current bull period. 

The overnight returns now dominates day session so much that if this continues, going long overnight and shorting the markets during the day is now a super profitable strategy !!



What is driving this? Well to start with: the vols are down, and NIFTY (like most emerging markets) is perhaps influenced by the Feds and the BoJ much more than it used to be back in 2007. I would suspect most emerging markets will show very similar patterns. And this is VERY different than, say , S&P 500, where overnight and day-session has their fare share of misery and joy.

Will this continue? Well, the flow of funds that world-wide QEs initiated is still churning around, and will perhaps take a long time before the dust settles down. But it is an altogether different scenario if we enter a high vol regime in 2015, irrespective of market direction.

The tail piece: for folks looking for public source of intraday data on stocks - here is a quick and dirty R scripts. Feel free to use and modify as you please. Quantmod of course does a wonderful job for daily data. This routines are similar and extend to intraday.


. Merry Christmas and happy holidays everyone!

Friday, July 11, 2014

NIFTY: Technicals - Choose Your Divination!

Now with the budget out of the line, we will be trading relatively event-less in near term more or less. So focus is less macro and more micro, stock-picking and timing the markets etc. In case you rely on technical indicators, here is a quick summary of what works and what does not among the weapons in the technical traders' arsenal for NIFTY. All data from Bloomberg.

The first chart shows the total performance of different strategies based on technical indicators, against simple buy and hold. The whiskers show the maximum and minimum annual returns, while the thicker bar s show the average annual returns in a trending market and in a range-bound markets (it is white if trending return < range bound return and black otherwise). The data spans 2004 to YTD 2014. The trending years are identified as 2004 to 2007 and then 2010 and 2014.


The second chart shows the relative rankings of strategies in a given year (1 is the best, 23 the worst). Again the whiskers show the best and worst ranks over the years and the thick bars show the average annual ranks in trending as well as range bound markets (again, it is white if the average trending rank is lower than, i.e. better than, average range bound rank)



So based on this if you believe we are in a trending market, NOTHING beats the simple strategy of buy and holds. And if you think we are in a range bound market, the best performing strategy is a variation of moving average (Triangular moving average - a three-point double-smoothed variation of the moving average method, with majority of weights in the middle point).

In general, you are better off following Ichimoku or different variations of moving average methods in a trending markets (if buy and hold is too simple for your taste!). And in range bound market also, the moving averages perform relatively better than other complicated indicators. But even then, they do not beat the simple buy and hold strategy by a large margin.

Of course, as we all know, past performance is not indicative of future returns.

Wednesday, July 2, 2014

Macro View Series: Cross Country Market Cap To GDP

Out of sheer lack of actions in the market (which I hope will change with the NFP and ECB tomorrow, the ADP came in great today), we take a look at cross country relative equity valuation. That is basically a vague sounding smartspeak for checking out the market capitalization of listed companies (as a % of GDP). Market cap to GDP is a quick and dirty way to compare fundamental valuations across countries, assuming fundamentals matters in your trade horizon (so we are talking long term here). Of course, this ratio will be influenced by, among others, share of unorganized sectors (inversely proportional) and proportions of productive companies listed (directly proportional), and claim on other countries' GDP (like Switzerland - a home of many multinationals, directly proportional)

We look at two aspects. First, the market cap to GDP vs real GDP growth rate - this kind of gives how the market prices in the expected growth in earning vs price. 


Also we look at the ratios with comparison to investment share of GDP. Note the countries are presented using internal country code (ISO 3166) here.


From the above we see a certain patterns. Most economies lie with reasonably narrow band no the 2nd chart. Look at the outliers - like on richer side Switzerland and Singapore. Both are financial hubs and home of many multinationals. So we naturally expect the market cap to GDP ratio to be higher. However, the South African and Malaysian markets are suspect of overvaluation. On the cheaper side you have Venezuela, Argentina and China. Venezuela and Argentina have their own pressing problems. And China is, well, China. So hop over them, and you see the suspects for cheap valuation: Kazakhstan and Czech Republic

Now the fun is to look in to more details of the specific economy and convince yourself. Happy hunting!

And here for the tail piece: the market cap to GDP ratio for India and US over the years (approximated from BSE 500 and S&P 500 market cap respectively)




Tuesday, June 10, 2014

NIFTY: Inside Stories

Reported insider selling has been quite active lately for Indian listed equities. So we take a quick look at it



Looking at the graphs, here are some stylized facts

1) the inside selling activity has been most wide spread recently. Although the total value of gross and net insider selling has come off the peak in April 14, the number of companies involved is still much higher than last few years average

2) The aggregated inside selling does not appear to be a good predictor of the general direction of the market. It seems, the company insiders are usually happy to lock in a local maxima after a slump in price.

3) Give 1 and 2 above, it may not be time yet to worry about the end of the bull market

Thursday, May 15, 2014

NIFTY: Are FIIs Really Overweight India?

I have my doubts

Here is an interesting article from the good folks from FTAlphaville

What is striking is that although the general feeling is that the FIIs have been "euphoric" about India and its' resurgence under Mr Modi as the PM, as I see, the data fails to show the same. Here are couple of charts to drive home the point.



So irrespective of what analysts at foreign banks says, I think a large part of the rally in the Indian equity markets so far this year has been driven by domestic buyers or may be even retail money. And a lots of potential FIIs flows sitting on the sidelines. Through the last phase of the election campaigns and actual elections, my perception is that FIIs have been cautious and decided to follow a wait and watch policy. And it would not take a dramatic positive results for NDA to kick start the next leg of the bull run. A simple confirmation of average exit polls prediction will do.

Monday, April 14, 2014

NIFTY: Election 2014 Positioning... Eliminate Tough Decision Making

The current move in NIFTY, expected to end in a crescendo after the elections, is probably one that will be the defining move this year. If you have already missed the rally so far, or fail to capture the large expected moves after the results are out, your portfolio performance is probably doomed for this year.

The question is do you really give a damn. You are in the game for the long term, right? it does not matter if you miss an election move or two.

So... a first strategy for election 2014 is, well, DO NOTHING. It is so often overlooked in the heat of things that doing nothing can turn out to be a pretty neat strategy. If it rallies after the results you will capture it anyways. If it sells off, you were buying value right? So unless we have a radical outcome, it should be an opportunity to buy.

Okay, now let's say you DON'T plan to do nothing. Here is a way to think about your move. 

Like poker, in markets too, apart from the goal of making money, another important objective is to avoid tough decision. Because tough decisions are always emotional, and that is exactly when you are most likely to make mistakes. And avoiding tough decisions in future is achieved simply by making choices now that makes your decision easy later on.

So let's apply this rule to see how you should be positioned. First thing first. I have no clue which way the market will move from here. Nor does anyone. Let's assume for argument's sake, the market has an equal chance of a large rally or a correction from here. If you go short now, and it does make a correction, congratulations! You made it. Now what if it does not? You make a loss on your shorts, AND you miss the rally. That's okay, no big deal. But what next? can you enter it now? You thought the market was already on the higher side and then it rallies quite a bit more. All you are going to do is to spend the next 6 months on the sideline waiting for a dip. A large one at that. You missed the entire 2014!

Now the other side of the bet. Suppose you went long. The market rallies. Well done. Now you take a re-look at the valuation and decide further action. And what if it corrects. No big deal. It just offers a more compelling valuation then. Way better outcomes no matter if you are right or wrong

And that's the kind of bets to make. Because with markets, the probability that your views are right is not much different than pure chance

NIFTY: Great Expectations?

Here is a fantastic background for India 2014, more so for the uninitiated! (Do click)

And now the questions is how much juice left in this rally and which sectors are really overheated. Below a snapshot of the relative performance of different sectors vis-a-vis the benchmark (NIFTY) index. As you can see the rally that started late last august once quite contained. But the pre-election rally (a possibility I noted before) has been, well, fantastic. With all the usual suspects racing away - only Energy, Pharma, FMCG and IT still lagging.



The question is what now. Obviously the pattern of the rally since March shows a lot have been on expectation of a radical shift in policy after the elections are done with. PSU banks, Real Estate, Financials, commodities and energy sectors especially seem to have performed on this expectation. How realistic these expectations are - a few wise words from JP Morgan (via FTAlphaville)

The belief in certain quarters is that as long as the next government were to go all out at de-bottlenecking projects, sentiment would surge and this would spark an investment revival in the economy. However, this appears to be an overly-simplistic read on the situation for at least three reasons.
First, the vast majority of projects are currently stuck because of issues that are under the purview of state governments, over which the central government has little jurisdiction...

They also warn of the circular link between the bank bad loans and stalled infra projects. Do go read the full text. 

One thing is sure, we do have rallied a lot on expectation. Or rather hope. That is not saying we can't rally further. But given the uncertainties of election outcome, the tail risk of hung parliament results may not be a tail risk. That can rattled the market which seem to have priced in too many rosy assumptions

Thursday, April 10, 2014

Trivia: Dance of Democracy

The election time is here again in the largest democracy in the world. Here is a link from an old post back in 2009 (the last general election). A mathematical awakening for all those lost in debates over Mr Modi's ghosts from the past, Mr Gandhi's incompetence, Mr Kejriwal's lack of direction and irrelevance of others. 

Actually the only system that will work for you is a dictatorship in which YOU are the dictator!!

Jokes apart, democracy is built on institutions. I honestly don't mind lack of leadership, as long institutions  are strong. If you think a country is a super example of a complex system, it is far better for it to walk randomly and find direction through evolution and time, than hurtling at Mach 2 in a direction under a great leader only to realise later as a nation it was wrong direction to begin with. 

Have a great time voting. By the way the implied volatility levels where the options on Nifty are trading, my back of the envelop calculation shows if u want to cover the time decay from now till the election results are out, the volatility levels should be around 50%. That will be higher than the 2008 crash!