Author: shyam

Building a Cross-Over Trading System

Investors often use moving averages to time their entry and exit into the markets. One of the most often used signals are the “Golden Cross” (for entry) and the “Death Cross” (for exit.)

The Golden Cross represents a major shift from the bears to the bulls. It triggers when the 50-day average breaks above the 200-day average. Conversely, the Death Cross restores bear power when the 50-day falls back beneath the 200-day.

The problem with most technical signals is that it requires you to inspect a chart of the stock you are interested in. However, with the StockViz API, you can automate just about every step of the process from checking for the cross-over to placing the trade.

Technical API

The StockViz Technical API for Equities (doc) gives you more that 50 technical stats to play with. Here’s how you access it in python:

unirest.get("https://stockviz.p.mashape.com/technicaldata/technicalsEquity",
     params={
          "symbol": ticker
     },
     headers={
          "X-Mashape-Authorization": mashape_auth,
          "Accept": "application/json"
     }
);

The result is a time-series in ascending order of dates. By comparing the last two elements, you can check if a cross-over occurred or not.

Accounts API

The StockViz Account API (doc) allows you to directly place trades through StockViz. These are “dummy” trades that update your portfolio. This allows you to track the performance of your investment strategy over a period of time.

Placing a trade is just another call to the API:

unirest.get("https://stockviz.p.mashape.com/account/OrderEquity", 
	params={
		"symbol": symbol,
		"qty": qty,
		"bs": buyOrSell,
		"t": "regular",
		"px": price,
		"asof": strTime
	},
	headers={
		"X-Mashape-Authorization": mashape_auth,
		"Accept": "application/json"
	}
);

Now all you have to do is run the script at the beginning of the day and you have your basic automated cross-over trading system. You can read the whole code here. The script runs the cross-over system for the NIFTYBEES ETF [stockquote]NIFTYBEES[/stockquote]

Note: You have to link your Mashape and StockViz accounts for the Accounts API to work (doc.)

Previously

A News Reading and Sentiment Analysis App

We previously discussed how you can pull news items for specific topics/stocks using the StockViz News API. But all that the script did was show you a link to the news item. But who has the patience to click on a link, go to the website only to get annoyed by the ads? Wouldn’t it be nice if you could just read the news then and there?

Goose

Python Goose is an open-source article extractor written in python. It takes any news article and extracts the main body of the article and also all meta data. By adding a few lines of code, you can convert the previous script into a news reader of sorts that displays just the article, minus the cruft.

4 lines of code:

import the library: from goose import Goose
initialize goose: g = Goose()
extract the article: article = g.extract(url=r[‘SOURCE’])
clean the article: cleaned = article.cleaned_text.encode(‘ascii’, ‘ignore’)

You can see the whole script here.

Sentiment

One of the advantages of Mashape is that there are number of other APIs that you can access from within your account. Most of these have a “freemium” plan, just like our API. One of these is the Free Natural Language Processing Service.

The NLP Service takes a URL and returns the sentiment (positive, negative, neutral) along with a sentiment score.

2 lines of code:

make the call: response2 = unirest.get(…)
read the sentiment: sentiment = response2.body[“sentiment-text”]

You can see the whole script here.

Now you can tailor the what, when and how of the news you want to track. You could bundle all this up with a fancy user interface. Or pipe this through a messaging API (like SendHub?) Chief ingredient: your imagination!

Previously

More Information ≠ Better Information

We live in an era of information overload – the 18 hour business channel, endlessly refreshing twitter streams and news feeds. Our brains just haven’t evolved fast enough to keep pace with the bombardment of information. Add the typical uncertainties of investing into the mix and our brains just can’t cope. However, that doesn’t prevent us from seeking incremental information. We tend to believe that if we just had a “bit more” information, we may arrive at “better” decisions. But this is not necessarily true.

Consider the experiment outlined in “On the Pursuit and Misuse of Useless Information,” by Bastardi and Shafir (pdf).

Group I

Question:

You are considering registering for a course in your major that has very interesting subject matter and will not be offered again before you graduate. While the course is reputed to be taught by an excellent professor, you have just discovered that he will be on leave, and that a less popular professor will be teaching the course.
Do you:

  1. Decide to register for the course? [82%]
  2. Decide not to register for the course? [18%]

(the percentage of participants who chose each option appears in brackets)

So a large majority (82%) of respondents effectively doesn’t care about the teacher, and cares only about the course.

Group II

Another set of students were asked the uncertain version of the question:

You are considering registering for a course in your major that has very interesting subject matter and will not be offered again before you graduate. While the course is reputed to be taught by an excellent professor, you have just discovered that he may be on leave. It will not be known until tomorrow if the regular professor will teach the course or if a less popular professor will.

Do you:

  1. Decide to register for the course? [42%]
  2. Decide not to register for the course? [2%]
  3. Wait until tomorrow? [56%]

Here, 56% of the respondents believe that if they just had that incremental piece of information, they’ll make a better decision.

Group II Followup

Here’s the follow up question to the same set of (Group II) students who chose (3) in the question above:

It is the next day, and you find out that the less popular professor will be teaching the course.

Do you:

  1. Decide to register for the course? [29%]
  2. Decide not to register for the course? [27%]

Therefore, the new distribution of overall preferences under the “uncertain version”:

  1. Decide to register for the course [42% + 29% = 71%]
  2. Decide not to register for the course [2% + 27% = 29%]

Whereas before, under the “simple version,” 82% chose to take the course, in the “uncertain version,” after the introduction of a piece of information that we know to be useless, only 71% chose to take the course.

So not only do we lust after incremental information that is useless, but once we get them, we assign too much value to them.

h/t TurnkeyAnalyst

Related

API Samples on GitHub

We will be posting sample code on how to use the StockViz API on our repository at GitHub. All samples use Python and the readme walks you through how you need to be setup on windows to use the samples.

Basic News Reader

As a first cut, we put together a very basic news reader that uses the StockViz API to download company and topic specific news items. It prints out the title and the source url for now. You can jump directly to the python code here.

Next: Your Own News Reading and Sentiment Analysis App

Related

Linking Your Mashape and StockViz Accounts

The StockViz API on Mashape allows programmers to not only build models, but also to place orders. These APIs are listed under the “Accounts” section. If you don’t have a trading account with us, these orders are “dummy” executed and your StockViz portfolio is kept in sync with your model trading activity. If you have a trading account with us, then we will call you first to confirm the order and then place the order for you. For this to work, you need to link your Mashape and StockViz accounts.

Linking Mashape and StockViz

Your Mashape user name can be found on the top-right corner of the page:

mashape username

In this case, the username is “drona”

Now, login to your StockViz account, and navigate to your portfolio.

mashape-stockviz

Under the “Accounts” tab, just enter your Mashape username (in our example, “drona” without the quotes) and hit “submit.”

That’s it. Now your Mashape and StockViz accounts are linked.