Thursday, January 28, 2016

Quantopian Introduction, part 2

This is part 2 in our tutorial demonstrating how to use the online interactive investment trading tool known as Quantopian, using Python. These are the most basic code inputs that are either REQUIRED or they are OPTIONAL; the optional ones are encouraged if you have information to query before running the actual trading algorithm.

#this function is run once at the beginning of the algorithm REQUIRED
def initialize(context)
context.stock = sid(24)
     pass


#this function is run once per day before any calls to handle_data OPTIONAL
def before_trading_start(context, data)
     pass

^**This is typically where we will run calculations necessary for pre-computations that will be used for the rest of the day.


#this function is run once per bar, REQUIRED
def handle_data(context, data);
     pass

^**Ok, so this bit of code right here is retrieves data either by the minute or by the day, depending on the variable you set. Speaking of variables, the command 'handle_data' requires two inputs: context, and data.

Now let's go over securities referencing, SID, or "Security Identifier". The SID function takes care of the issues that might arise when backtesting an algorithm over an extended period of time, including if the stock ticker symbol changes. That's right, the SID function will keep the correct stock that you're using.

An example would be our previous function that we used above:
context.stock = sid(24)
..... sid(24) means that the function is calling upon the Apple stock symbol (AAPL). the (24) is the ID number given to the AAPL stock, so rather than having AAPL as the stock symbol, Quantopian identifies it by the (24) integers.

The context.stock, is a function used to store the stock data.
Next, we can 'print' out the function using the following:
     #this function is run once per bar, REQUIRED
def handle_data(context, data);
     print context.stock

Notice how this is mostly the same list of functions we used previously? The only difference is that we removed the 'pass' function and replaced it with the 'print context.stock'

A really cool feature of Quantopian with Python in this instance is that we don't have to use the ID number (SID) if we don't want to. We can simply use the following bit of code to locate the stock by it's ticker symbol:

symbol('AAPL')
**NOTE: the ticker symbol is not robust to ticker changes; in other words, if the ticker symbol changes the function will not change with it, rendering it useless. This is not a dynamic function so it is best to use the SID function instead!


Now we are going to go over how to order securities!
The portfolio value in some contexts, including this one, is calculated as the sum value of all open positions plus the cash balance.
Let's say we want half our portfolio value to be long Apple stock

     #this function is run once per bar, REQUIRED
def handle_data(context, data);
     order_target_percent(context.stock, 0.5)
This takes two parameters, the first being the stock that we want to order and the second being the target percent. The 0.5 dictates that it will take half of our available funds (0.5) and order Apple stock. So if we have $100,000 available and no open positions, this function will go through and order $50,000 of Apple stock.

Now, let's say we want to hedge our investment, we can go short in SPY, a stock that mirrors the performance of the S&P 500 index, for the other half of our portfolio.

 #this function is run once at the beginning of the algorithm REQUIRED
def initialize(context)
context.stock = sid(24)
context.market = sid(8554)
The (8554) is a reference to the SPY security.
Notice that we added the 'context.market = sid(8554)' to our previous function.

#this function is run once per bar, REQUIRED
def handle_data(context, data);
     order_target_percent(context.stock, 0.5)
     order_target_percent(context.market, -0.5)

Notice how we added 'order_target_percent(context.market, -0.5)' to our previous function? This algorithm is saying 'I want to short the SPY using half of my portfolio (-0.5). The short position is denoted by the negative ("-") symbol before the (0.5) number.

I hope you enjoyed this brief tutorial! Part 3 will be added shortly!

Let me know if you have any questions or comments!

No comments:

Post a Comment