Friday, March 11, 2016

Quantopian Tutorials: Linear Regression

This, like all of the tutorials I am creating, comes from the Quantopian website. I am simply rehashing them for people and trying to make them more simplified with more detailed explanations of the "why" and "when".

Here we are going to discuss linear regression, followed by the actual application of regression in our analysis through Python code via Quantopian. The goal of this tutorial is not necessarily to rehash everything talked about by the folks at Quantopian already, but to help give you a better and more detailed explanation of everything. Everything in this tutorial is aimed at folks who are new to Python. So let's begin!

Link to the Quantopian notebook which is what this tutorial is based upon.

Let's look at some code:

# Import libraries
import numpy as np
from statsmodels import regression
import statsmodels.api as sm
import matplotlib.pyplot as plt
import math

Ok, so here we see the word "import" a lot, what is going on? It means we are importing something into our coding session, in this instance, import is used to import a set of commands and libraries into our IDLE. You can think of Python as a car frame where the axels, engine, seats, and actual body will connect to. The actual engine, seats, axels, and body are represented by what we are importing into it. Think of numpy as the engine, numpy is a library of functions and commands used to crunch numbers, we want this so of course to add it we tell Python to import it into our session and we will call it np rather than numpy because it's shorter and easier to input. The same goes for statsmodels, matplotlib. Math is short enough, we don't need to abbreviate it really so we just leave it as math. The from statsmodels import regression command is telling Python to import the regression function from the statsmodels database. The statsmodels.api is an api function used with Python to help utilize it's functions. It is a module that provides classes and functions for the estimation of many different statistical models. The same style of explanation can be used for the import of most everything.

Ok, we now know what each function does and why we have them (need them for calculations!), now we need to define them and plot the results.

def linreg(X,Y):
    # Running the linear regression
    X = sm.add_constant(X)
    model = regression.linear_model.OLS(Y, X).fit()
    a = model.params[0]
    b = model.params[1]
    X = X[:, 1]

Looking at this one at a time: X = sm.add_constant(X) , this means we are adding a constant to our graph along the X-axis, that constant is denoted within the parenthesis, so our constant is X. The sm.add part of our module comes from our previously imported statsmodels.api where we defined it as sm. Remember? So we are simply adding an X constant to our previously imported module. Next we have model = regression.linear_model.OLS(Y, X).fit() which is a simple ordinary least squares model. The OLS part stands for 'ordinary least squares', followed by both axis, constrained to fit() within our defined parameters which are denoted by a (alpha) and b (beta) below. The X = X(:, 1) means we are slicing the array and taking all the rows (:), but keeping the second column (1)

    # Return summary of the regression and plot results
    X2 = np.linspace(X.min(), X.max(), 100)
    Y_hat = X2 * b + a
    plt.scatter(X, Y, alpha=0.3) # Plot the raw data
    plt.plot(X2, Y_hat, 'r', alpha=0.9);  # Add the regression line, colored in red
    plt.xlabel('X Value')
    plt.ylabel('Y Value')
    return model.summary()

 X2 = np.linspace(X.min(), X.max(), 100), returns evenly spaced numbers over a specified interval. The X.min() is the starting point and the X.max() is the ending point, both constrained by our 100 limit. Y_hat (denoted  in statistics) represents the predicted equation for a line of best fit in linear regression. Within that best fit we have X2 * b + a. The plt.scatter plots the parameters you have input on a scatter plot.The rest of the code are customizations to the plot you can modify as you would like, described by the #.

start = '2014-01-01'
end = '2015-01-01'
asset = get_pricing('TSLA', fields='price', start_date=start, end_date=end)
benchmark = get_pricing('SPY', fields='price', start_date=start, end_date=end)

# We have to take the percent changes to get to returns
# Get rid of the first (0th) element because it is NAN
r_a = asset.pct_change()[1:]
r_b = benchmark.pct_change()[1:]

linreg(r_b.values, r_a.values)

Here, we have our start and end dates, which represent the time-line for which we want to obtain our stock data, defined by our asset function where we are going to get the price via the get_pricing function, which is defined by parameters such as the name of the stock, in this case it is 'TSLA' which is Tesla's stock, followed by our fields inputs where we will have 'price', start_date=start, end_date=end). Now we are setting this against a benchmark, as any security should be, and in this case we are setting it against the 'SPY' with the same fields of entry now input.

r_a = asset.pct_change()[1:] and r_b = benchmark.pct_change()[1:] both of these are the percentage change which is the daily returns because we are getting it at a daily frequency by default and the same thing goes for the benchmark. We want to consider the daily returns as our time-series.

The linreg(r_b.values, r_a.values)  is our command to begin our linear regression of our asset and benchmark daily returns.

Now, from our code you will a table that spits out, most of the statistics you needn't worry about, but you should pay attention to the F-statistic which is telling us how predictive our model actually is, you want this value to be less than 0.5 (X<0.5). If you F-statistic is any higher than 0.5 than it is pretty much useless.

The rest of the code and their descriptions is available through the link . If you have any questions or concerns let me know!

The last part to making our graph is the following code:
# Generate ys correlated with xs by adding normally-destributed errors
Y = X + 0.2*np.random.randn(100)
linreg(X,Y)

This "makes Y dependent on X plus some random noise".

And to help with interpreting your data, you can use the following code below to help identify your 95% confidence interval for the regression line:

import seaborn

start = '2014-01-01'
end = '2015-01-01'
asset = get_pricing('TSLA', fields='price', start_date=start, end_date=end)
benchmark = get_pricing('SPY', fields='price', start_date=start, end_date=end)

# We have to take the percent changes to get to returns
# Get rid of the first (0th) element because it is NAN
r_a = asset.pct_change()[1:]
r_b = benchmark.pct_change()[1:]

seaborn.regplot(r_b.values, r_a.values);

I like their clear explanation here:
The regression model relies on several assumptions:
  • The independent variable is not random.
  • The variance of the error term is constant across observations. This is important for evaluating the goodness of the fit.
  • The errors are not autocorrelated. The Durbin-Watson statistic detects this; if it is close to 2, there is no autocorrelation.
  • The errors are normally distributed. If this does not hold, we cannot use some of the statistics, such as the F-test.

Thursday, March 10, 2016

Quantopian Tutorial 6

So here we will talk about order management. By default on Quantopian there is no limit as to how much money you can borrow and invest in your algorithm, but in reality this isn't realistic. So here we will take a look at how to control the amount of money you invest.

def initialize(context)
     context.stock = sid(xtl) <---- now here, when you type in 'xtl' it will appear as '40768'.

def before_trading_start(context, data):
     pass

# Called every minute
def handle_data(context, data):
     open_orders = get_open_orders()
     if context.stock not in open _orders: 
          order_target_percent(context.stock, 1.00)
     record(cash = context.portfolio.cash)

What you see here in the 'order_target_percent(context.stock, 1.00) is the order we are placing for 100% (1.00) of the stock. Then we use the record function to plot the cash in my portfolio at the end of each day (even though we are in minute mode).

After you have written the code above, go ahead and hit the 'build' button.

When the build is finished, you will notice that the cash dips below zero, this is because we are investing borrowed money.

For this example, let's say we don't want to borrow any money, we just want to invest what is in our capital base.
Now, you might be wondering 'I already ordered 100% of my portfolio value so why is it ordering more?" The answer is because sometimes an order takes more than a bar to fill. Like with what is happening in our order. Remember, we are ordering more than a million dollars of XTL shares, and in every bar after that we are making similar-sized orders because our original order has not yet filled. Each of these order are going to stack up on each other to the point where, at the end, when all the orders are resolved, you will end up in a much bigger position in XTL than you had originally planned. To prevent this, we can use the function get_open_orders to see which securities we have placed orders for that have not yet been filled. The get_open_orders is going to give us a dictionary keyed by security ID of our open orders. So, if we hit the 'build' button again after inserting this function, we will notice that we are no longer borrowing a lot of money. The number we get is not zero (what we want) and this is due to slippage which we will discuss later.

So let's look at ticker XTL, using an algorithm that uses order-target percent to order 100% of our portfolio in XTL in a minute!

I hope you enjoyed this lesson about managing orders in Quantopian, as always, if you have any questions or comments please feel free to leave feedback!


Sunday, January 31, 2016

Quantopian Tutorial, part 5

This part of the tutorial will cover more advanced topics that build upon our previous knowledge of constructing Python algorithms for trading.

First, let's go over the 'set)universe function.

def initialize(context)
     set_universe(universe.DollarVolumeUniverse(99.0, 100.0))

def handle_data(context, data):
     symbol('GOOG')
     for stock in data:
          print len(data)

So from this point on, any stocks or securities that you inquiry about using functions will be automatically included into our stocks universe.

Now, let's go over the 'fetch_csv()' function. This function allows us to import an external dataset in the CSV format.

fetch_csv("location of your csv file"
     pre_func=preview,
     date_column='data,
     universe_func=(my_universe))

context.target_weight = 0.01

def handle_data(context, data)

the fetch_csv("location of your csv file" will import your csv file full of securities, ticker symbols, dates, etc, into our algorithm. date_column='data' will locate and import the date column of your securities. And finally it will use the my_universe function to define our universe of securities.

What our algorithm is going to do is take our csv file and convert all of our securities into SID objects sids = set(financials['sid']) and then pass it through to our my_universe function defined as def my_universe(context, fetcher_data): and then that is going to grab all financial securities in our financials fetcher data financials = fetcher_data[fetcher_data['GICS Sector'] === 'Financials'], so it will grab all of the unique symbols of those securities, and then finally we are going to get the universe size so that we know how much weight to put into each security context.target_weight = 1.0/context.count and then we will return the set we got previously sids = set(financials['sid'] and return it via return sids.

So, the function financials = fetcher_data[fetcher_data['GICS Sector'] === 'Financials'] is describing all the securities listed in the S&P 500, which we will then grab their unique SIDs via the sids = set(financials['sid'] function, and then it will return that list return sids, and then we will use that as our daily universe for the stock. The pre_func=preview is a function that will allow us to preview the outcome of algorithm. The df['data'] = '11/1/15' function will allow us to pull those dates directly from our csv file or we can view them manually in Excel. The point of using the function is to tell our algorithm how to associate the SID object to the date that it belongs to.

We are then ordering that security with the context.target_weight which we found by calculating the number of securities in our universe and dividing that by 1 context.target_weight = 1.0/context.count .

**NOTE: this is all in daily mode, if you want it in minutely mode use the  schedule_function(func=rebalance, day_rule=day_rules.every_day(), time_rule=time_rules.market_open()) def rebalance(context, data): **

**NOTE: the maximum number of securities that you can import from a csv file into your universe is going to be around 200! 82 is the typical number of stocks in a 1% range of the dollar volume universe. You can also define your universe with a mix of fundamental factors.
Here is what our code will look like:

import datetime
import pandas as pd
import numpy as np

def preview(df):    
    log.info(' \n %s ' % df.head()) 
    df['data'] = '11/1/15'
    return df

# Function for returning a set of SIDs from fetcher_data
def my_universe(context, fetcher_data):
   
    # Grab just the SIDs for the Financials sector w/in the SP500:
    financials = fetcher_data[fetcher_data['GICS Sector'] == 'Financials']
     sids = set(financials['sid'])
   symbols = [s.symbol for s in sids if s != 0]
    context.count = len(symbols)
    print "total universe size: {c}".format(c=context.count)
   
    # Compute target equal-weight for each stock in the SP500 Financials 
universe
    context.target_weight = 1.0/context.count
    return sids

def initialize(context):
   
    # Fetch the SP500 constituents -- sourced from Quandl
    # https://s3.amazonaws.com/quandl-static-content/Ticker+CSV
%27s/Indicies/SP500.csv
    # static snapshot of the SP500 constituents as of 10/2013, along with 
GICS sectors
    # I'm grabbing the data from a file on my dropbox folder which I 
modified by adding
    # a date column, alternatively you could add the date inside of a 
more complicated
    # pre_func and use the csv file as is.
   
    fetch_csv(
       "https://dl.dropboxusercontent.com/u/169032081/SP500.csv",
        pre_func=preview,
        date_column='date',
        universe_func=(my_universe))

    context.target_weight = 0.01
    
    schedule_function(func=rebalance,
                      date_rule=date_rules.every_day(),
                      time_rule=time_rules.market_open())
    
def rebalance(context, data):
    # Loop over every stock in the Financials sector and make sure we have an equal-weight
    # exposure using the order_target_percent() method.
    for stock in data:
        # Guard for missing stock data
        if 'price' in data[stock]:
            order_target_percent(stock,context.target_weight)
        else:
            log.warn("No price for {s}".format(s=stock))
    
def handle_data(context,data):
    pass

Saturday, January 30, 2016

Quantopian Introduction, part 4

Welcome to part 4 of my Quantopian tutorial! In this tutorial we will go over how to use fundamentals data!

This tutorial is going to cover how to select a dynamic portfolio from fundamentals data. This means that we will be selecting securities based upon a property like market cap rather than just hardcoding a list at the beginning of the algorithm. Dynamic portfolio selection leads to more robust algorithms as it reduces the chances of over-fitting your strategy to a particular backtest period. We can use the get_fundamentals function to access the skeleton of a fundamentals query featured below.

import numpy as np

# This function is run once at the beginning of the algorithm, REQUIRED
def initialize(context):
    # AAPL stock
    # context.stock = sid(24)
    # SPY stock
    # context.market = sid(8554)
   
    schedule_function(check_mean, date_rules.every_day(), time_rules.market_close(minutes=30))

# This function is run once per day before any calls to handle_data, OPTIONAL
def before_trading_start(context, data):
     get_fundamentals(
    #Fundamentals Reference Page: https://www.quantopian.com/help/fundamentals
    context.fundamental_df = get_fundamentals(
        query(
            fundamentals.income_statement.total_revenue
        )
        .filter(
            fundamentals.valuation.market_cap > 30000000000
        )
        .order_by(
            fundamentals.valuation.market_cap.desc()
        )
        .limit(10)
    )
   
    update_universe(context.fundamental_df.columns.values)






Ok, so let's explain what all this code is above us. First we have context.fundamental_df = fundamentals(query(, what this does is open up the fundamentals skeleton that will allow us to choose our criteria and values per category. An example of this would be the .fundamentals.valuation.market_cap > 3000000000 which tells the algorithm "hey look, I only want to include companies with a market cap of greater than 300 billion dollars!"



The query( section at the beginning of our algorithm on the second line above, tells the algorithm what data we actually want to access. As you can see, we are telling our algorithm to look at the income statement of the company to determine whether or not it is a 300 billion dollar company!


The .order_by( section will allow us to sort the results of our query, and in this situation we are going to sort them by market-cap in descending order.


And finally, our .limit(10) will limit the number of securities returned, in this case we are limiting the securities to 10.

Also, we are going to store our results (information) in a dataframe denoted by the df listed above and we are giving it the name context.fundamental. The results will be a pandas dataframe where the columns are securities and the rows are the fundamental values that we queried for.

update_universe(context.fundamental_df.columns.values) , this will update our securities universe by adding in our information that we queried above, notice how the context.fundamental_df is the name we gave to our query for market cap info? It is indeed the same!

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

       
def check_mean(context, data):  
   
    print get_datetime('US/Eastern')
   
    # Get the close price of each of the last 10 minutes for all stocks
    # in our universe.

    hist = history(10, '1m', 'close_price')
   
    # Print the 10-minute mean close price for each stock in our universe.
    for stock in data:
        print '%s - mean price: %.2f, total revenue: %.2f' % (stock.symbol, np.mean(hist[stock]), context.fundamental_df[stock][0])
    print '-------------------------------'
    print ''


The print '%s - mean price: %.2f, total revenue: %.2f' % (stock.symbol, np.mean(hist[stock]) function will go through and check the mean of the information we have gathered, and then it will print it for us so we can look through it. Additionally, it will print our total revenue through the use of the context.fundamental_df[stock][0] function.

And that's it! You have now mastered the ability to build your own algorithm! Next lesson, we will move into more complex and powerful functions that will allow us to check a greater amount of information using mathematical tools! 

Friday, January 29, 2016

Quantopian Introduction, part 3

This is the third part of our Quantopian tutorial! Let's dive in!

Data variable and trading universe will be the topic we will start with.

A trading universe is any stock that we might be interested in trading in a given bar in our algorithm. 
The trading universe is represented by the data variable, which is a dictionary key'd by securities.

#this function is run once per bar, REQUIRED
def handle_data(context, data);
     log.info(get_datetime())
     for stock in data;

          log.info('Close price for %s: .2f' % (stock.symbol, data[stock].close_price))
     log.info('/n')

So what we did here is logged all this so we can take a look at the minute/lead/close price of each our securities ('Close price for (stock.symbol, data[stock].close_price)), and then we added in some formatting so that it all prints out in a more readable form (%s: .2f' %). And then we added the print time so we can see which minute we are on ( log.info(get_datetime()) ) And then we printed a new line for spacing ( log.info('/n') ).

Hit the 'Build Algorithm' key and what you will find is the pricing data for AAPL and SPY in each minute.

The 'stock universe' includes any stocks that we currently hold position in, as well as any stocks that we have explicitly referenced in the context variable. Another way to get a stock in our universe is to use the 'update universe function'.

Now let's turn our attention to the 'history()' function! The history function is used to us historical data for each stock in our stock universe.

#this function is run once per bar, REQUIRED
def handle_data(context, data);
    
    print history(5, '1m', 'close_price')

What this is doing is grabbing the history for our SPY and APPL data for the last 5 minute bars, and then we put '1m' for our frequency, and then we put close_price as the field that we are interested in. This returns as a pandas data frame with each column being a different stock and each row being a different date-time.

If we wanted a days frequency we would simply put '1d' instead of '1m'. Additionally, we can also change our field, so that we can find the volume traded rather than the closing price. We do this by placing 'volume' where our 'closing_price' is. 

If we want to find the ten day mean in the closing price of our stocks, we can change the number of bars to '11' and keep the frequency at '1d' and then I am actually going to save this function which, we can do through the following function: hist = history(11, '1d', 'close_price')
Now we want to make a 'for' loop going through each stock in data:

#this function is run once per bar, REQUIRED
def handle_data(context, data);
    
     hist = history(11, '1d', 'close_price')
     for stock in data:
          print '10-day mean close price for stock  %s: %2f' % (stock.symbol, np.mean(hist[stock][:-1]))

So, what we did here was print a line of text '10-day mean close price for stock' that references our stock %s: %2f' % and then we retrieve the stock symbol, and then we import the numpy library denoted as np (**NOTE: when importing a library you must also type a line of a code at the very beginning of your algorithm (above the def initialize(context) function); so above that function we would have the following line of code: import numpy as np. )
followed by the use of the statistical .mean variable of the (hist <--- our historical data for our stock. And then we type [stock] for the column that corresponds to the specific stock in our loop.
The [:-1] portion of our code excludes the last row of our column.

And the result will give us our ten day mean for AAPL and SPY.

We can change the our days to minutes like this: '10-minute mean...'

Thank you for reading part 3 of our tutorial, and as always if you have any questions or comments please send me an e-mail or comment!

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!

Quantopian Introduction

Quantopian.com is pretty neat. You have access to a lot of data, can build your own algorithms, backtest them, and even have the support of the Quantopian community who will share their algorithms with you! Let's get into it here!

This introductory lesson is based upon the Quantopian tutorial, as we advance and move into more sophisticated techniques we will move away from their tutorial however I see fit. My goal is to make this simple and informative so that anyone can learn algorithmic trading without too much of a headache!

Here we go:

_________________________________________________________________________________

This is the default "Sample Mean Reversion" tutorial that I am regurgitating from Quantopian.com and in addition I will provide the reader with a deeper understanding of what's going on and why.

Background: Mean Reversion is a statistical method used in finance: Mean Reversion "is the assumption that a stocks price will tend to move to the average price over time"
"When the current market price is less than the average price, the stock is considered attractive for purchase, with the expectation that the price will rise. When the current market price is above the average price, the market price is expected to fall. In other words, deviations from the average price are expected to revert to the average"-https://en.wikipedia.org/wiki/Mean_reversion_(finance)

In other words, a stock that deviates either above or below it's market price will eventually revert back to it's historical mean. We can visualize this using continuous time-series analysis otherwise known as Orstein-Uhlenbeck process. We don't need to go into depth on the Orstein-Uhlenbeck process, but if you would like to know more see Wikipedia, and basic stochastic processes!

Ok, so now we know what a mean reversion is and why we are doing it. It helps us to understand if a stock is a buy or sell value based upon its current market price compared to its historical mean.

Now for the code: (mind you, this is in Python since we are using Quantopian.com).
**Any line that is preceded with a hashtag (#) and is underlined means that it is a 'comment'-comments are used within lines of code by the creator to define a process, or explain why he/she has done something. You do not need to include it in your code!

# Import the libraries we will use here
import numpy as np

**Ok, so import numpy as np is the coding package we are using in our Python module. This allows us to use certain commands as tools and helps us build the algorithm we intend to build!**

# The initialize function is the place to set your tradable universe and define any parameters. 
def initialize(context):
    # Use the top 1% of stocks defined by average daily trading volume.
    set_universe(universe.DollarVolumeUniverse(99, 100))

  # Set execution cost assumptions. For live trading with Interactive Brokers 
    # we will assume a $1.00 minimum per trade fee, with a per share cost of $0.0075. 
    set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1.00))
   
    # Set market impact assumptions. We limit the simulation to 
    # trade up to 2.5% of the traded volume for any one minute,
    # and  our price impact constant is 0.1. 
    set_slippage(slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.10))
**NOTE: slippage defines how your algorithm will impact the price of the security. As a participant in the market, you will have a small impact on prices depending on how you buy or sell.

    # Define the other variables
    context.long_leverage = 0.5
    context.short_leverage = -0.5
    context.lower_percentile = 20
    context.upper_percentile = 80
    context.returns_lookback = 5

  # Rebalance every Monday (or the first trading day if it's a holiday).
    # At 11AM ET, which is 1 hour and 30 minutes after market open.
    schedule_function(rebalance,
                      date_rules.week_start(days_offset=0),
                      time_rules.market_open(hours = 1, minutes = 30))

# The handle_data function is run every bar.  
def handle_data(context,data):  
    # Record and plot the leverage of our portfolio over time. 
    record(leverage = context.account.leverage)

    # We also want to monitor the number of long and short positions 
    # in our portfolio over time. This loop will check our positition sizes 
    # and add the count of longs and shorts to our plot.
    longs = shorts = 0
    for position in context.portfolio.positions.itervalues():
        if position.amount > 0:
            longs += 1
        if position.amount < 0:
            shorts += 1
    record(long_count=longs, short_count=shorts)

# This rebalancing is called according to our schedule_function settings.    
def rebalance(context,data):
    # Get the last N days of prices for every stock in our universe.
    prices = history(context.returns_lookback, '1d', 'price')
   
    # Calculate the past 5 days' returns for each security.
    returns = (prices.iloc[-1] - prices.iloc[0]) / prices.iloc[0]
   
    # Remove stocks with missing prices.
    # Remove any stocks we ordered last time that still have open orders.
    # Get the cutoff return percentiles for the long and short portfolios.
    returns = returns.dropna()
    open_orders = get_open_orders()
    if open_orders:
        eligible_secs = [sec for sec in data if sec not in open_orders]
        returns = returns[eligible_secs]

    # Lower percentile is the threshhold for the bottom 20%, upper percentile is for the top 20%.
    lower, upper = np.percentile(returns, [context.lower_percentile,
                                           context.upper_percentile])
   
    # Select the X% worst performing securities to go long.
    long_secs = returns[returns <= lower]
   
    # Select the Y% best performing securities to short.
    short_secs = returns[returns >= upper]
   
    # Set the allocations to even weights in each portfolio.
    long_weight = context.long_leverage / len(long_secs)
    short_weight = context.short_leverage / len(short_secs)
   
    for security in data:
       
        # Buy/rebalance securities in the long leg of our portfolio.
        if security in long_secs:
            order_target_percent(security, long_weight)
           
        # Sell/rebalance securities in the short leg of our portfolio.
        elif security in short_secs:
            order_target_percent(security, short_weight)
           
        # Close any positions that fell out of the list of securities to long or short.
        else:
            order_target(security, 0)
           
    log.info("This week's longs: "+", ".join([long_.symbol for long_ in long_secs.index]))
    log.info("This week's shorts: "  +", ".join([short_.symbol for short_ in short_secs.index]))



Ok, so run the code over at Quantopian.com or go ahead and run it through your own local Python IDLE and have a look at the results. 

If you're running it over at Quantopian.com and want to know the meaning of your results read here:

First, in the right hand side, you have your return; that is pretty self-explanatory, it is telling you how much you have made or lost given percentage of movement in the value of the stock.

Next we have alpha; alpha means 'a measure of performance on a risk adjusted basis' and additionally 'The abnormal rate of return on a security or portfolio in excess of what would be predicted by an equilibrium model like the capital asset pricing model (CAPM).' -http://www.investopedia.com/terms/a/alpha.asp

After alpha, we have beta which measures the volatility or risk.

After beta, we have 'Sharpe', which is the 'Sharpe ratio": it measures the risk-adjusted return. The Sharpe ratio is the average return earned in excess of the risk-free rate per unit of volatility or total risk.

Lastly, we have the drawdown: The peak-to-trough decline during a specific record period of an investment, fund or commodity. A drawdown is usually quoted as the percentage between the peak and the trough.

If you have any questions or concerns please send me an e-mail or post a comment!

Friday, January 8, 2016

Investing for Success: What to do in a down market

One of the scariest thing for a new, amateur investor or even those stock professionals who have been in the business for decades, is a sudden and sharp decline in stocks. Even worse for some folks and organizations who are heavily weighted in stocks, is a long-term decline in overall stock market.

Right now, the stock market is in decline and has been for the past week since it hit it's peak of 17,720.98 on the December 29th, 2015. Since then a lot has happened, Saudi Arabia and Iran are facing their highest tensions in years, the Chinese stock market fell 7% in the first 15 minutes of trading on Monday and continues to decline, and OPEC continues to pump out oil production at the highest rates ever, even in a congested market!

The DJIA right now (as of this writing) is at 16,578. So stocks haven't recovered yet and it could take a month, if not more, before they rebound and normalize again. This is a great lesson in psychology, not so much out of a fearful sort of psychology in people's minds, but of great awareness of their environment (stocks). I am neglected going into detail about the fed rate hike because it frankly is minimal that it has not had an impact on stocks.

First, let's look at why stocks are on the decline here in the U.S.

-As previously mentioned, China. Chinese exports are slowing dramatically as world growth is likewise slowing, having the second largest economy in the world, China makes a huge presence on the world stage, especially when you take into account their manufacturing and heavy industry sectors which are by far the largest in world, producing everything from concrete and copper to IPhones and DVD's.
    But.... it's important to note that their stock market (the Shanghai Stock Exchange), is NOT a critical component of their economy, nor is it anything more than an indication of how part of their economy is doing based upon highly speculative trading. This is also NOT how U.S. stock exchanges, specifically the Dow Jones Industrial Average operates. The reason for the differences between the U.S. stock exchange and the Chinese, is that our stock exchange is much more robust and developed, it includes the health and wealth of nearly every large company in the U.S. and every one that has gone public, and with the interconnectivity of our finances and how tied-in our retirement plans such as our 401k's are to our utility of life, everything that happens in the stock market is of some value to someone somewhere, whether or not they think it matters at all.

In China, their stock exchange is not fully developed, not well connected, and their people simply couldn't care less, mostly because it is foreign to them as in it bears no brunt or effect on their daily lives. There are not nearly as many companies listed as in the U.S. and many of them have little foreign investment, even then a lot of them see highly subsidized contracts and help from the Chinese government, and even then their stocks and financial health are not directly linked to Chinese economic performance.

In conclusion to this piece; the Chinese stock market (Shanghai Exchange) is not important, we need not worry about it unless you are directly invested through it. It does not represent the health of the Chinese economy the same way the Dow Jones Industrial Average represents the health of the U.S. economy. So the investors that are freaked out about the Chinese drop in stocks probably already know this, but stocks are about speculation and spatial awareness so they are doing their good-duty of playing it cautionary and pulling out of U.S. companies who have direct links to Chinese manufacturing and investment. It's a safe, but unnecessary strategy.

Were going to look into what we can do as investors here in the U.S. to combat the contagion of fear and also were going to see what we can do to capitalize on the most recent series of events.

------------


GENERAL RULES TO LIVE BY AS AN INVESTOR IN A DOWN MARKET

1. Sit on it, and wait for stocks to rebound and normalize. It might take a week or a year, but they will rebound unless there is something catastrophic, or a series of catastrophes in a specific market you are invested in. Otherwise, just wait it out and use this time as an opportunity to direct your funds typically used for investing in stocks, into debt repayment. Now is a great time to put extra cash down towards repaying your student loans, credit card debt, home equity loans, car payments, etc. The biggest mistake I see from investors is fear and panic, don't be like them it's just a dumb, bad strategy to have.

Let's take a look at some stocks and see what's going on, and what industries have been affected the most.

CHSCP is currently trading at $30.60 with no movement for the day, down from their previous high this week of 30.91. The past week has seen a lot of volatility much like Tesla Motors (TSLA) but to a smaller degree, overall this would be a fun stock to watch. TSLA has seen a steady decline from $233 five days ago to $213 now, with a lot of volatility mixed in. McDonalds (MCD) has been all over the place, their last peak was two days ago with $119, then dropped to $115 and now they're at $116, however if you had held this stock for anymore than a year you would have seen the stock shoot from $93 to it's current $116. That's a fantastic return if you're a long-term investor, and one year is hardly long-term. What about Google? They were $750 five days ago and now down to $717. Their 52 week low is $486, and the 52 week high is $779... ridiculous returns on such a pricey stock!

Pretty much everything is down today. Every sector, industry, everything is down. Bad day for stockholders overall, but even in such a case stocks will rebound and within the market there are some gainers.

2. Short stocks. Shorting is not for everyone and takes a greater understanding of the markets and stock fundamentals then normal stock trading. If you're going to short, you had better be sure that your stock is indeed failing, or have some sort of insurance or hedge against it as most folks will do. In order to short, you must have extra cash laying around that you can put down in case things go bad, because you will need it for the broker depending on the outcome of the short.

3. Continue investing in specific companies through intensive research. This is the riskiest move to make in a down market and only for those investors who are willing to put in the time and research involved in making such a move. You can find gainers in the markets and jump in with the trend and hope the momentum will continue (risky) or go ahead and see if your research leads you to a company that is about to break from the herd and realize some gains.

Take today for example, the best performing stock was that of NGL Energy Partners (NGL) whose stock soared over 45%! But why? If you're thinking it had something to do with the commodities industry or the energy sector you would be wrong. Instead, they sold one of their assets for $350 million, in a move that apparently shook investors the right way. Meanwhile, the rest of the energy sector was mostly meh, not going anywhere and those that did went down. There were a few exceptions like Kinder Morgan (KMI) and others but most every sector overall was down significantly.

------------

Friday, November 13, 2015

Fed funds rate with Taylor Rule implied

Let's take a look at the fed funds rate with implied Taylor Rule


Calculated as follows:

Taken from the St.Louis Fed website.

This is part of a post from here that you can read about. I am wanting to clarify what he has posted here that I hope will help someone with a very basic understanding of economics.

Take a look at this chart:

We have a lot going on here. Let's look at each one individually:
1) Fed funds (black line): The fed funds line you see here is the interest rate at which banks trade with one another, over night, using funds provided by the federal reserve--which acts as a primary lender. Generally speaking, the funds held at the federal reserve are only for the most credit-worthy depository institutions, and not just any bank. The fed funds rate influences both monetary and financial policy, which affects employment, growth, and inflation.

Currently, the federal reserve has been hinting at a rate hike, taking rates from near zero where they currently are to whatever number they want. It is most likely that they will increase the rate in small intervals so as not to shake the economy anymore than necessary. With an increase in the fed funds rate, generally speaking, comes at collapse, or at least modest decline in stocks; both price and volume, but is seen as a boon for bonds. Also, if you're an avid old-school saver, you will receive a higher interest rate on your bank deposits.

2) We have the Taylor Rule (red line): First, the Taylor Rule is a prediction here; predicting that when inflation is at target and the output is at potential (the output gap is zero), the FOMC will set the real federal funds rate at 2%, (the historical average), in this example 2.5%. The Taylor Rule is a guideline whose purpose is to describe the interest rate decisions of the FOMC.

The Taylor Rule was developed by Stanford economist John Taylor, which he describes it as being used as a "benchmark for monetary policy".

Here is the formula: r = p + .5y + .5(p – 2) + 2 (the “Taylor rule”)

where
r = the federal funds rate
p = the rate of inflation
y = the percent deviation of real GDP from a target

So on this it is important to explain that the feds target for real GDP is potential output; the amount the economy can sustainably produce when capital and labor are fully employed.

Looking at variably y in the Taylor rule, we can interpret it as being the excess of actual GDP over output, thus giving us the "output gap".

Using the Taylor Rule will lead us to predict that the FOMC will increase the fed funds rate (tightening of monetary policy) by one half of a percentage point (0.5%). It also predicts that when inflation is at target and output is at potential, the FOMC will set the real fed funds rate at 2%.



 This figure shows how monetary policy was too weak relative to using the Taylor rule for periods 2003-2005 and 2011. How do we know? By comparing the fed funds rate to the Taylor Rule, we see that the fed funds rate fell below the Taylor rule during those periods. Is that a good or bad thing? Well, mostly it doesn't matter, also this chart is from 2011, but I thought it both useful and interesting to use as we can see how the Brookings institute predicted the fed funds rate four years ago in comparison the the actual fed funds rate and the zero bound defined by the solid line.


3) Next, referring back to our second figure, we have the Taylor rule Laubach-Williams natural rate, the natural rate is assumed to change over time due to various unobservable influences.

4) The Wu-Xia shadow fed funds rate is not bounded below by zero percent, unlike many short-term models. In other words, it models what the fed funds rate would do negative interest rates were possible, using a host of historical macroeconomic data.

So now you're probably wondering what the heck all these models are doing on the same graph!?! It's simply giving us a comparison of what each model looks like given the same inputs. I like the Wu-Xia shadow myself, where it shows fed funds rates actually falling negative because it can be argued that it gives us a more accurate picture of whats going on in terms of monetary policy. That said, it, like many other models in economics can be argued against as well. It is currently not the model of choice for most economists, especially the feds, but it is considered a powerful representation of current macro policy; i.e. it's not as revered as the Taylor rule.

And finally, putting everything together, what does this tell us? This tells us that it is probably not the time for the feds to raise interest rates because the Laubach-Williams natural rate is 2.6% below the corresponding 2.5% equilibrium value derived on our Taylor rule (red) line.

Well I hope that was informative, if you have any questions please do not hesitate to ask!

Tuesday, November 10, 2015

The Experts vs. The Rest of Us

So finance is a science, right? Or is it an art? Either way, some individuals are clearly better at it than others. At least, in terms of money managers, investors, and analysts. Right? Well, I think it depends.

***My personal opinion**
From my experience in dealing with financial experts, and sort of being a "financial expert" myself ... that is without the incredible amount of wealth that normally seems to accumulate with most experts... I don't think there is much of a difference in the outcomes of an investors performance given their technical training (or lack there of). This extends beyond simple investing and into market forecasts and the prediction of large-scale economic events (such as a recession).

Check out these papers: here and here. To summarize both articles, they come to the conclusion that the experts are only "slightly" better at economic forecasting and at executing successful trades and investment decision making. There are actually more articles and papers on the subject that reinforce that statement that you can find through a Google search.

On an entirely theoretical scale, the reason why this seems to be the case is because markets are infamously tough to beat, and those that beat it, mostly beat it not because of any sort of magic, intensification of resources, or even because they're good at math and statistics; no. They beat the market because of two things: intuition, and guts.

Intuition may be attributable to some degree of luck, but you cannot make the argument that intuition itself is entirely luck, no. Most intuition comes from some level of knowledge of how markets work. Plain and simple. Maybe you have heard of Wave Theory, or maybe you haven't. A lot of folks I know have never heard of it yet, they're aware of it to some degree and that knowledge helps drive their investor sentiment. It helps guide them in what to buy, when, and for how long. These sorts of people notice trends before most other investors, and they also act much quicker than a similar person with an equal amount of knowledge, you might call it courage, fortitude, drive, whatever. The point is they are not afraid of execution.

That is most of investing.

Having spent most of my life working with math, especially the last seven years of it to very complex mathematical formulas and models, I can say with some degree of certainty that most successful investment strategies are more-so the product of intuition than they're good math. I can tell you what the price of a security should be, and how it should act under various situations and economic conditions, but I cannot tell exactly what will happen, even with perfect knowledge one slight change in a single variable will distort the entire investment environment.

That said, knowing the math can help. A great example of someone who is good with both math and at calling big economic events is Robert Schiller. But for everyday investment decision-making and money management, you don't need a highly polished educational background or to understand stochastic processes; no. You can simply observe trends, talk to your neighbors, and watch the news.

Again, these are my personal opinions based on my experience and knowledge. Feel free to prove me wrong with any number of papers and empirical facts, I would love that because I have yet to find any such paper.

Tuesday, August 25, 2015

Rate hike?!

Are rate hikes coming soon?

The folks at the federal reserve, including the head, Janet Yellen, would have you think so.

First, if a rate hike comes it will likely come in either September or December as those are the only two months that the federal reserve has scheduled press conferences. They have a meeting in October however there is no press conference scheduled for that time and quite frankly if you're going to be hiking rates you pretty much have to have a press conference.

After the recent turmoil in the markets, September could be too soon as the markets will not have had enough time to recover. December is also unlikely because liquidity tends to dry up at that time of year. According to some analysts, October could actually be the time to raise rates after-all.

Raising rates before December is increasingly likely, amidst the downturn in the recent bull-market. Why? Because of credibility. If the fed is going to maintain any credibility then they will have to stick by their previous statements and hike rates before years' end, and as mentioned earlier, December is not likely.

Global equity markets have lost $5 trillion in value over the last two weeks!-On expectations of slowing economic growth.

*remember, Yellen has indicated that any rate hike decision would be made without regard for scheduled press conferences!

Turning to the recent sell-off in US stocks which is to be blamed on the slowing of China's economy, the devaluation of the yen (which was China's attempt at reversing the recent slow-growth) and the lack of faith in the Chinese government to adequately control the housing bubble that has been growing for the last decade.

Here is Citi Banks' economist William Lee's comment: "If he (Fed Vice Chair Stanley Fischer) shows signs of worrying that the transitory downward pressures (commodity and energy prices and the appreciating dollar) are feeding through and becoming entrenched in wages and domestic prices—THAT would be a big event," the economist writes. "His concern would suggest reduced confidence in reaching the Fed inflation target in the medium term."

Thursday, June 18, 2015

AMD

RiddlerThis here!

I want to take some time out of your day to discuss the financial situation of AMD and whether or not this is a stock we should be buying or selling

Some background: AMD (Advanced Micro Devices) is an American worldwide semi-conductor company located in Sunnyvale, California. Their main products consist of semi-conductors, servers, motherboard chipsets, graphics cards, and other technological devices.

  • AMD is the second largest global supplier of microprocessors based on the x86  architecture and one of the largest suppliers of graphics cards worldwide.
  • AMD is the only major rival of Intel in the central processor (CPU) market.
  • AMD is one of two major competitors in the graphics processor unit (GPU) market. The other being Nvidia.
I won't go as in-depth as many analysts go, mostly because I feel it is unnecessary and I'd like to focus on the most important aspects of the company to determine whether or not they are at a simple buy or sell status. To do this, I read and scan a vast number of articles over a short-time frame and compare the information from each article. After digesting the information and summarizing the most important factual data, I then look into its historical performance and compare those numbers. Lastly, I run my own models and back-test them extensively. 


Currently: AMD has been underperforming in comparison to the market, and is considered a :penny stock" due to the <$5.00/per share price.

  • As of Wednesday, June 17th, the share price for AMD was $2.36.
  • At closing on Thursday, June 18th, the share price for AMD was $2.52.
    -Resulting in a $0.16 increase per share.
    -It's important to note that the increase in share price from Wednesday to Thursday was the result of an announcement AMD made at the Electronic Entertainment Expo (E3), where they announced their latest graphics cards; the Radeon R9 and the R7 300.
So what's the big deal surrounding the announcement of these two new graphics cards? Why did this announcement cause excitement surrounding price of their stock? 

Well, first: these are the first graphics cards produced by AMD that will utilize AMD's high-bandwidth memory (HBA).

FBR&Co's Christopher Rolland believes a target price of $3.50. Why? Because analysts believe that the price of both the Radeon R9 and the R7 300 are priced well. With the R7 marketed towards lower-end gamers and starting at a roughly $150 price range, while the R9 is geared towards 4K quality displays and starting around the $300 price range. 

Many analysts and investors are happy with the direction AMD is currently going and are optimistic in regards to its stock price. 

The company is currently is rated as UNDERPERFORMING, meaning no sell, and I'd argue a cautionary buy. 

*As of this writing the after-hours trading price has dropped to $2.47.

Using R, I did a simple chartSeries plot:



I'll be following AMD and posting more detailed analysis in the future, as well as focusing on several penny stocks. I will also be sharing several strategies and various other information as time allows.

Thanks!

And let me know if you have any questions, tips, concerns, or requests!

RT


Monday, January 5, 2015

January 5th, 2015

The DJIA took a hit today; dropping 331.34 points (-1.86%) ending at 17,501.65. The S&P 500 declined 37.62 points (-1.83%) ending at 2,020.58. The FTSE declined 130.64 points (-2.00%) ending at 6,417.16. Crude also declined $2.70 (-5.12%) and dipped around $49.99/barrel. Currently it's still hovering around the $50/barrel range.

First, crude oil is a dirty a business so why exactly is West Texas Intermediate crude dropping so much? Excess supply. We have huge growth in oil production coming from the United States right now as fracking continues to see expansion mostly in places like North Dakota, Texas, Colorado, and other states. The fracking business can be brutal and is highly competitive especially in places like the Bakken and Eagle Ford; already companies like Baker Hughes are downsizing both in terms of their employees and the number of rigs in operation.

US oil and gas rigs jan 5 2015 The low price of oil caused by excess supply is negatively affecting oil producers around the world, including here in the U.S.

Also, OPEC. OPEC has continued to push out a steady supply of oil in the face of the current supply gut in hopes that they might drive many of the smaller producers out of the business. This is pretty much an act of desperation on their part as the increase in U.S. production has negatively affected them, so we might call this a "tit for tat" move on their part. Other may just call it a price war.

Another reason? Slowing demand-- mostly stemming from China. The International Energy Agency cut its forecast by 230,000 barrels a day to 900,000 and OPEC revised their forecasts of demand for their oil from 29.4 million to 28.9 million barrels a day.
Demand for oil has been increasing steadily over the long-term.

And then there is the dollar. The dollar has been strong lately. Here is what MW has to say on it:
""Commodity prices are inversely correlated to the dollar. The oft-cited rationale is that a stronger currency makes dollar-priced commodities more expensive to buyers using other currencies.
The ICE dollar index DXY, +0.00% a measure of the currency against a basket of six major rivals, is up more than 12% since the beginning of the year and by around 1.9% since the beginning of December.
Binky Chadha, chief global strategist at Deutsche Bank, argues that the strong dollar is the primary factor in oil’s decline. After all, oil supplies have been building for a long time. It’s hard to believe that investors just “suddenly woke up” to the oil glut at midyear, he said.
Oil’s plunge started not long after the dollar rally began to accelerate, Chada notes, observing that it usually takes a rallying dollar a year and a half to cover the ground it’s gained in the last five months, Chadha told reporters earlier this month, which might make finding a bottom in oil “a function of where the dollar stalls.”

Wednesday, August 6, 2014

August 6th, 2014. More Junk

Junk is in the news!

That's right! I am quite fixated on junk these days and have more to bring!

Coming from the folks at Bloomberg, junk bonds have seen a 1.5% decline over the past month, which includes $9.9 billion pulled from mutual funds that invest in junk. According to the article, the most plausible reason why investors are pulling out of the junk-bond market is due to their rising costs.

Accordint to BoA, since 2008, every-time junk-bonds have lost value they have rebounded right back and delivered an annualized return of 17.3% for the period.

From Bloomberg: "The 6.2 percent yield on junk bonds is 2.7 percentage points below their decade-long average, yet 3.2 percentage points more than investment-grade securities, about the most since October, according to Bank of America Merrill Lynch index data."

Just an fyi: the junk bond market is currently valued at about $1.6 trillion versus the estimated $12 trillion treasury market.

Saturday, July 26, 2014

QE and junk bonds.

macromarketmusings

Here the professor breaks down whether or not QE3 was successful in stimulating the economy. His conclusion is that yes, QE3 helped keep the economy afloat during a time of crisis when it would have otherwise collapsed by the weight of toxic mortgages.

His data goes on to show that during periods of treasury purchases under QE, long-term yields rose as opposed to have fallen. This actually increased the cost of the governments purchases.


He concludes that QE programs pushed up the economic outlook and that in turn decreased the risk premium on other assets. Because of the decreased risk on these 'other' assets, investors became more attracted to higher-yielding assets a.k.a. junk bonds. He also believes that the government failed in part because they were not able to restore full employment to the economy, rather they continued to allow risk premiums to stay elevated and interest rates on safe assets to stay depressed.

Friday, July 25, 2014

Junk bond outflows!

So this is interesting!

The outflow of junk bonds from the market has increased this week from last week.

From the WSJ: "Prices on bonds issued by lower-rated U.S. companies tumbled to a three-month low this week, according to a Bank of America Merrill Lynch index. Investors yanked $2.38 billion from mutual funds and exchange-traded funds dedicated to junk bonds in the week ended Wednesday, the largest weekly withdrawal since June last year, said fund tracker Lipper. That came on the heels of $1.68 billion that poured out the week before."The reason why? Geopolitical risk and possible borrower defaults. Military clashes in the Ukraine, Gaza, and Iraq right now are causing concern for high-yield bond holders who have seen their yield decrease more than 1% from last summer.




The spread between junk bonds and Treasuries is 3.65 now.

Monday, July 21, 2014

Sneaky Hedge Funds

Well, hedge funds are at again. And again. Ok, so they probably won't stop with their unethical-ness anytime soon. I hate to say it but most of the criticism they face is well deserved.

In this latest report, coming from our friends over at Naked Capitalism, hedge funds are now using basket options more than ever to save on tax expenses.

Here are some important abstracts taken from the article:


The Senate Permanent Subcommittee on Investigations released a report today that found that hedge funds have been using basket options to save billion in taxes. And when we say “billions,” the report indicates it’s more like tens of billions, since the paper estimates that the tax reduction achieved at one hedge fund, Renaissance Technologies, operated by the famed James Simons, was $6.8 billion.
Basket options were sold by Wall Street firms, in particular Barclays and Deutsche Bank, as a way to convert what would otherwise have been labor income into capital gains income. The bone of contention is that the IRS wrote a memo in 2010 telling players involved to cut it out, and they didn’t.

It's not a new strategy or a new idea by any means. Wall Street and banks in particular have been doing this for years. What makes it remarkable is how they continue to use such strategies in the face of harsh criticism coming from the public and some lawmakers. Of course, Wall Street is not obligated to act upon any kind of human emotion or morals, rather their main goal is to make money and become as profitable as possible so as to increase the return on their investors holdings. Of course, their clients come into play as only after looking out for numero uno!

The blame doesn't fall entirely on the banks' shoulders, but equally so on the government for a lack of oversight and regulation. This bell should be rung more.

Just because it's legal doesn't mean it's ethical.

This is an example of hyper or supercapitalism.

More on junk bonds.

Just saw this fun article over at Marketwatch.



Junk bonds are on the retreat!

Mutual funds and exchange traded funds that invest in high-yield bonds saw a total of $1.68 billion in redemptions in the week ended Wednesday.

And oldie but a goodie still, look at the over-valuation of junk-bonds up through April.


What's more interesting is The Barclays High Yield Bond index, which is tracked by the SPDR Barclays High Yield Bond ETF JNK -0.12% , has seen yields rise from a record low of 4.83% on June 20th to 5.17% on Wednesday

And...

The premium that junk bonds pay over comparable Treasury bonds rose to 3.52 percentage points from 3.24 percentage points during the same time period, according to Barclays.

So as short-term traders sell, the long-term traders are buying. The 10yr yield is approaching 2% while JUNK hovers around 5%! It's likely that as interest rates rise junk bonds won't be impacted as much as interest-rate sensitive securities.

If junk goes, stocks go.

The Fed, Janet Yellen, and junk.

Let's start off with junk bonds.

First, what is a junk bond? "A colloquial term for a high-yield or non-investment grade bond. Junk bonds are fixed-income instruments that carry a rating of 'BB' or lower by Standard & Poor's, or 'Ba' or below by Moody's. Junk bonds are so called because of their higher default risk in relation to investment-grade bonds."-http://www.investopedia.com/terms/j/junkbond.asp
In addition; junk bonds are securities and are monitored by the SEC (Securities and Exchange Commission). Junk bonds are easily traded through large funds like hedge funds, and mutual funds. They are also relatively liquid (easily converted into cash). Investors who trade junk bonds can convert to cash typically within three days.

In laymens terms; a junk bond is a bond that offers a higher yield (interest) than traditional bonds. However, the higher yield also comes with an attached higher risk, that risk being the possibility of a default of the bond by the issuer (typically a large corporation). Credit rating agencies asses these companies and distinguish the credit-worthiness of companies who issue such bonds. Because the bonds have a higher degree of risk associated with them, investors demand higher yields than in safer bonds.

As I mentioned above junk bonds are traded by a lot of different folks including hedge funds, mutual funds, and Exchange Traded Funds (ETF's). What is an exchange Traded Fund? "
A security that tracks an index, a commodity or a basket of assets like an index fund, but trades like a stock on an exchange. ETFs experience price changes throughout the day as they are bought and sold."-http://www.investopedia.com/terms/e/etf.asp
Or more simple terms as Investopedia describes it: "
Because it trades like a stock, an ETF does not have its net asset value (NAV) calculated every day like a mutual fund does.

By owning an ETF, you get the diversification of an index fund as well as the ability to sell short, buy on margin and purchase as little as one share. Another advantage is that the expense ratios for most ETFs are lower than those of the average mutual fund. When buying and selling ETFs, you have to pay the same commission to your broker that you'd pay on any regular order.
One of the most widely known ETFs is called the Spider (SPDR), which tracks the S&P 500 index and trades under the symbol SPY."
In my own words: you get a little slice of variety. Rather than buying a single stock or bond, you can purchase an ETF that consists of dozens of different stocks, bonds, whatever.

Now what is a junk loan? Well, it's like a bond but it's not really a bond. It's classified as an asset rather than a security. Also important to note that junk loans cannot be quickly converted into cash so they are not very liquid. Instead, there is a long legal process behind the trading of junk loans that take as much as two to three weeks.

Junk loans are pegged to floating rate bench-marks, where as junk-bonds are not. Instead, junk bonds are fixed. Floating rates mean that the yield of the loan (or security, asset, etc) is not just 5% or 6% or any fixed, permanent number. Instead, the interest rate (yield) floats with the underlying interest rate set by the fed. If the fed raises interest rates and you are a loaner than you have just gained that higher bit of interest. If the fed lowers rates, the yield on that loan also decreases. Depending on your situation this could be good or bad. The bench-mark is a target number they (the loaners) set. It's like the fed when they set an inflationary target of 2% or 3.5%. They usually aren't aiming specifically for that number but rather a number close to it. Say, something like 1.85% or 3.0%. There are also inverse floaters where the yield of a specific security or asset moves inversely to the actual rate set by the fed. 

The junk loan market currently sits at around $750 billion in the U.S. alone whereas the total size for junk bonds here is about $1.3 trillion. That said, there has been a rush by investors to get their hands on junk loans due to their higher yields in comparison to many securities, bonds, and other traditionally safer loans. 

One of the main areas of concern between junk loans and junk bonds is how they're traded. With a junk bond you can trade it much like a stock because of it's relative liquidity. But with a junk loan, where a trade can take two to three weeks due to the legal process and paper-work behind it, they're not so easy--or fun! If you own an ETF as mentioned before then you own a share of many different stocks, bonds, or whatever. The share itself is not the actual stock or bond, it is a share, so a percentage of that stock, or bond that was issued by the company. So you may trade shares as quickly as you can trade stock but the actual junk loan is not traded at the same time the share is. Why? Because of the time it takes to process that loan and get it signed by attorneys, managers, and whomever else. 

As Bloombergs Lisa Abramowicz explained recently it's a problem that is shared by mutual funds, and exchange traded funds. In her own words "you've got shares of mutual funds and exchange traded funds that trade like stocks and trade like securities and they are backed by assets that are much less liquid..." so what do fund managers do? "They hold more cash, they hold more bonds rather than loans to allow you to have something to sell more easily and they also get credit lines. They actually have facilities with banks where they can borrow if they need to, to meet redemption's while they wait to get their money back. The thing is, all those things cause a drag on their performance. So the investor is getting hurt by that drag."

Pretty interesting! 
Junk bonds have become popular once again, as they were in the early 90's which led to a series of banking catastrophes. This is what leads us on to the new fed chairwoman, Janet Yellen who has yet to prove she has the gusto or fortitude to lead us out of the turmoil were in and clamp down on both banks and investors alike. She's continued QE though she announced an end-date for it which probably shouldn't have been mentioned specifically. All we needed was a general time-frame not the immediate date for the pullout as banks will now prepare themselves in such a way that will prove to disappoint those of us who have to deal with them. Sort of like when the Obama administration just recently announced the exact time when we would pull out our remaining forces from Afghanistan (to be taken lightly, we will still have hundreds of operatives over there). 

So what is the fuss with Janet Yellen?

First, it's the low interest rates. She's adamant about keeping them hovering around 0%. This act encourages risky behavior by investors and banks alike as investors seek the highest possible return and banks being banks are always more than eager to find ways to provide investors with those possible yields. Part of the risky behavior that investors are now participating in is with junk loans and junk bonds. Yes, that same junk I mentioned above. That's what's fueling the bubble in junk right now! I say bubble because as soon as the fed raises interest rates, investors will pull out of such risky investments and pour their hard-earned cash into more traditional and safer investments. But that's also not the only reason junk is believed to be in bubbles at the moment. The rise in interest would also affect the amount the debt these companies have taken on, so their liabilities will increase slightly to significantly depending on the amount of the rise in interest rates by the fed.

This goes on further if we were to examine the leverage of the top companies and funds in the U.S. Just going back on junk loans and bonds again, we remember that while bonds are fairly liquid, loans are not. So that would mean $750 billion invested in junk loans could quickly turn into something like a run as investors rush to pull out and cash in on what they have left as a result of rising interest rates and rising speculation about the future of junk loans. Worst case scenario: the pull out of investors in the junk loan market could also see small sell-offs in other categories of financial products like asset-backed-securities (ABS) as investors look to cover any small losses in the junk-loan market, fearing a quick-sell within their own investments by other twitchy investors.