Algorithmic Trading Essentials  - 3 Useful Tips When Storing Trade Signals (Part 2)

Handling & Storing Trading Signals Are Hard

The calculation of simple trading indicators is made easy with the use of any one of the Technical Analysis libraries available, however, the efficient handling and storage of trading signals can be one of the most complex aspects of a live trading system.

 Photo by  Jeremy Thomas  on  Unsplash

Calculating Basic Indicators? No Problem

While it’s often necessary to create custom indicators and trading signals, there is still significant benefit to using a standard library such as Ta-Lib for the basics. This saves a lot of time rather than having to reimplement a set of common indicators in your language of choice. It also has the added bonus of increased processing speed as opposed to calculation done in native Python, for example.

When it comes to moving averages and other simple time-series indicators, the process is fairly self explanatory — at every time step you calculate the next numerical value which is then used as the most up-to-date signal to trade against.

(Code Snippet to read data CSV files and process into trading indicators) https://gist.github.com/yoshyoshi/73f130026c25a7dcdb9d6909b1990277

The signals themselves will be stateless in that respect — you aren’t concerned with previous signals that have been made, only the combination of indicators present at that moment. However, you may still wish to store some of the information from the indicators, if only for external analysis at a later point.

Different Story For Advanced Pattern Recognition

Meanwhile, more advanced pattern recognition cannot be handled in such a simple manner. If, for example, your strategy relies on finding divergence between indicators, its possible to get a significant performance boost by storing some past data-points from which to construct the signal at each new step, rather than having to reprocess the full set of data in the look-back period every time.

This is the trade-off between storage/ RAM efficiency and processing efficiency, with the latter also requiring greater software complexity to achieve.

How You Should Store Signals Depends On How Fast You Need It To Be

For optimal processing efficiency, you would not only store all the previously calculated signals from past time-stamps, but also the relevant information to calculate the next step in as fewer steps as possible.

While this would be completely unnecessary for any system with a polling rate above a seconds, it is exactly the kind of consideration you would have for a higher frequency strategy.

Meanwhile, a portfolio re-balancing system, or even most day-trading strategies, have all the time in the world (relatively). You could easily recalculate all the relevant signals at each time-step, which would cut down on the need for the handling of historical indicator sets.

Depending on the trading period of the system, it may also be worth using a hybrid approach to indicator and signal storage. Rather than permanently saving the data, you could calculate the full set of indicators at start-up and periodically dump and refresh the data to keep only whats going to be used in RAM.

The precise design trade-offs should considered on an individual basis, as holding more data in RAM may not be an option when running the software from lower power cloud computing instances nor, at the other end of the spectrum, would you be able to spare the seconds to recalculate everything for a market making bot.

3 Useful Tips When Storing Trade Signals

As mentioned in the part 1 of this series, there are range of different storage solutions that can be used for trading data. However, there are several best practices which apply across all:

  1. Keep indicators in a numeric or boolean format where possible for storage. For example, splitting a more complex signal set into boolean components. This particular problem caused me several issues in projects I’ve had to work on in the past.
  2. Only store what is complex or time-consuming to recalculate. If a set of signals can be calculated in time in a stateless manner, its probably easier to do so than add the design complexity of storing extra information.
  3. Plan out the flow of data through your system before you start programming anything. What market data is going to be pulled for each time-step? What will then be calculated from this and what is necessary to store? A well thought-out design will reduce complexity and hassle down the line.

Past this, common sense applies. Its probably best to store the indicators and signals in the same time-series format as, and along side, the underlying symbols they’re derived from. More complex signals, or indicators derived from multiple symbols, may even warrant their own calculation and storage process.

You could even go as far as to create a separate indicator feed script which calculates and stores everything separately from the trading bot software itself. The database could then be read by each bot as just another data feed. This not only has the benefit of keeping the system more modular, but also allowing you to create a highly optimized calculation function without the complexity of direct integration into a live system.

Whatever flavour of system you end up using, make sure to plan out the data storage and access first and foremost, before starting the rest of the design and implementation process.

By Matthew Tweed

/

Algorithmic Trading Essentials  -  Collecting & Storing The Market Data (Part 1)

The lifeblood of any algorithmic trading system is, of course, its data — so that’s what we’ll cover in the first two posts of the mini-series.

 Photo by  Farzad Nazifi  on  Unsplash

Always Always Collect Any Live Data

For the retail trader, most platforms and brokers are broadly the same, you’ll be provided with a simple wrapper for a relatively simple REST or Websocket API. It’s usually worth modifying the provided wrapper to suit your purposes, and potentially create your own custom wrapper — however, that can be done later once you have a better understanding of the structure and requirements of your trading system.

Depending on the nature of the trading strategy, there are various types of data you may need to access and work with — OHLCV data (candlesticks), bid/ asks, and fundamental or exotic data. OHLCV is usually the easiest to get historical data for, which will be important later for back-testing of strategies. While there are some sources for tick data and historic bid/ask or orderbook snapshots, they generally come at high costs.

With this last point in mind, it’s always good to collect any live data which will be difficult or expensive to access at a later date. This can be done by setting up simple polling scripts to periodically pull and save any data that might be relevant for back-testing in the future, such as bid/ask spread. This data can provide helpful insight into the market structure, which you wouldn’t be able to track otherwise.

Alpaca Python Wrapper Lets You Start Off Quickly

The Alpaca Python Wrapper provides a simple API wrapper to begin working with to create the initial proof of concept scripts. It serves well for both downloading bulk historical data and pulling live data for quick calculations, so will need little modification to get going.

It’s also to be noted that the Alpaca Wrapper returns market data in the form of pandas Dataframes, which has slightly different syntax compared to a standard Python array or dictionary — although this is covered thoroughly in the documentation so shouldn’t be an issue.

Keeping A Local Cache Of Data

While data may be relatively quick and easy to access on the fly, via the market API, for live trading, even small delays become a serious slow down when running batches of backtesting across large time periods or multiple trading symbols. As such, it’s best to keep a local cache of data to work with. This also allows you to create consistent data samples to design and verify your algorithms against.

There are many different storage solutions available, and in most cases it will come down to what you’re most familiar with. But, we’ll explore some of the options anyway.

No Traditional RDB For Financial Data Please

Financial data is time-series, meaning that each attribute is indexed by its associated time-stamp. Depending on the volume of data-points, traditional relational databases can quickly become impractical, as in many cases it is best to treat each data column as a list rather than the database as a collection of separate records.

On top of this, a database manager can add a lot of unnecessary overhead and complexity for a simple project that will have limited scaling requirements. Sure, if you’re planning to make a backend data storage solution which will be constantly queried by dozens of trading bots for large sets of data, you’ll probably want a full specialised time-series database.

However, in most cases you’ll be able to get away with simply storing the data in CSV files — at least initially.

Cutting Down Dev Time By Using CSVs

(Code Snippet to download and store OHLCV data into a CSV) https://gist.github.com/yoshyoshi/5a35a23ac263747eabc70906fd037ff3

The use of CSVs, or another simple format, significantly cuts down on usage of a key resource — development time. Unless you know that you will absolutely need a higher speed storage solution in the future, it’s better to keep the project as simple as possible. You’re unlikely be using enough data to make local storage speed much of an issue.

Even an SQL database can easily handle the storage and querying of hundreds of thousands of lines of data. To put that in perspective, 500k lines is equivalent to the 1 minute bars for a symbol between June 2013 and June 2018 (depending on trading hours). A well optimized system which only pulls and processes the necessary data will have no problem in overheads, meaning that any storage solution should be fine. Whether than be an SQL database, NoSQL or a collection of CSV files in a folder.

Additionally, it isn’t infeasible to store the full working dataset in RAM while in use. The 500k lines of OHLCV data used just over 700MB of RAM when serialized into lists (Tested in Python with data from the Alpaca client mentioned earlier).

When it comes to the building blocks of a piece of software, its best to keep everything as simple and efficient as possible, while keeping the components suitably modular so they may be adjusted in future if the design specification of the project changes.

By Matthew Tweed

/

So You Want to Trade Crypto - Hedging with Cryptocurrency and correlation structure (Part 6)

As a new asset class with historically low correlation to traditional financial products, many see Cryptocurrencies as a useful hedging tool against global downturns. However, the structure of Crypto volatility and correlation relative to market capitalization may prove somewhat detrimental to this use-case.

 Photo by  Tyler Milligan  on  Unsplash

A story of Volatility

 (Raw data from  coinmarketcap.com . These charts show the mean of the 60 day annualized volatility from 1st Jan 2017 to time of writing.)

(Raw data from coinmarketcap.com. These charts show the mean of the 60 day annualized volatility from 1st Jan 2017 to time of writing.)

As within equity markets, we see a small decrease in volatility as the market cap of coins increase (albeit with a relatively low correlation). This can be likened to blue-chip stocks vs mid-caps, with the former providing greater stability due to their established dominance in their respective sectors.

Although market cap is a slightly misleading metric when applied to Cryptocurrencies, it at least implies a higher value to a coin - thus requiring more money to shift its direction dramatically. That being said, volatility has been higher across the board over the last couple of years as Crypto shifted from the accumulation phase post 2013 into the major bull run. Finally pushing to record high as we moved into the final phase of the bull run and subsequent bear market as we entered 2018.

This structure of volatility allows Crypto portfolios and indexes to be constructed similarly to those of equities: high-cap only selection for reduced risk and volatility; mid-caps for higher risk and reward; or a more diversified index to try to capture a middle ground.

The Trend of Correlation

 (Raw data from  coinmarketcap.com . These charts show the mean of the 60 day Pearson’s Correlation Coefficient against Bitcoin USD from 1st Jan 2017 to time of writing.)

(Raw data from coinmarketcap.com. These charts show the mean of the 60 day Pearson’s Correlation Coefficient against Bitcoin USD from 1st Jan 2017 to time of writing.)

Here we see nearly zero correlation between the market capitalization of a coin and its average correlation to Bitcoin (the historical leader of the Cryptocurrency space).

While this disproves the theory of high cap Cryptos holding a closer correlation to Bitcoin, it highlights the extremely high levels of correlation present throughout the market. This, as mentioned in previous posts, is likely due to the highly speculative and sentiment driven nature of the market, along with its relative immaturity compared to more traditional traded assets.

Interestingly, there isn’t much difference between the mean of correlation and the mean of absolute (positive only) correlation, meaning that we rarely see any negative correlation between ALT/USD pairs and BTC/USD.

Cryptocurrency as an Asset Class for hedging

Crypto holds the useful property of historically low correlation to other asset classes, such as equity and commodities, suggesting it to be a good hedge against external global factors. However, there are two main issues to this plan: Cryptocurrency has never weathered a global financial crisis; and the internal correlation within the Crypto space.

Since Bitcoin, and the rest of the Cryptocurrency market, has been experiencing its own market cycles due to its rapid growth over the past few years, any fluctuations due to correlation with equity markets has been almost unnoticeable - leading many to speculate that Cryptocurrency would continue this trend and make a good hedging tool against global downturns.

This observation happens to come on the back of a decade of huge growth in both US and global equity markets. Investors have been increasingly complacent in their gains over the past few years, and are happy to take greater and greater risks, betting money on more speculative assets such as Cryptocurrencies. However, such high yield assets are always the first to tumble at the onset of a recession, as investors scramble claw back their risk as their other positions drop.

Always "Different This Time"

Many will claim that its somehow “different this time” - it always is until the inevitable pullback. This was true of the dot-com bubble and I wouldn’t be surprised if the same fate will hold true for Cryptocurrency during a global dip. Not to say that Cryptocurrencies won’t be successful long term - the internet didn’t exactly disappear after 2000. But it should be approached with the same caution as any other high risk investment.

As alluded to in the first half of the article, the levels of volatility and correlation in Cryptocurrency make it difficult to create a well diversified portfolio - no matter what you pick you’re still at the mercy of Bitcoin and can incur the same volatility spikes and drawdowns.

While it may be possible to hedge a portfolio by shorting Bitcoin itself and creating synthetic ALT/BTC pairs, this won’t be able to eliminate the sensitivity of low-mid cap coins to shifts in market sentiment, so would have to be more actively managed.

All-in-all, Cryptocurrencies provide an interesting new opportunity for traders and investors alike - with high risk but much higher reward possibilities. They will not be a miracle financial product, nor a get rich quick scheme - but they can provide something truly new and different for those who have the time to understand and appreciate them.

By Matthew Tweed

/

I am a College Student and I Built My Own Robo Advisor

I’m Rao, and I’m an intern at Alpaca working on building an open-source robo advisor. I don’t have much experience in the space, and I had to find a lot of answers. While there’s a wealth of material available on the web, very little is organized. This post is the combination of various threads and forums that I read through looking for answers.

 “Yes, you an do it too!”

“Yes, you an do it too!”

What is a Robo Advisor anyway?!

Robo advisors are automated advising services that require little to no user interaction. They specialize in maintaining portfolios based on the investors chosen risk level. Btw, they were launched at the start of the financial crisis in 2008.

The actual logic for a robo advisor is straightforward.

  • Allocation” — Given a risk level, portions of capital are allocated to different positions.
  • Distance” — Over a regular time interval, the adviser scans to see if there’s a significant change in the portfolio balance, and
  • Rebalancing” — If necessary, rebalances.

Allocation:

The process of allocation is where different weights are assigned to asset classes based on the developer’s algorithm. These asset classes cover the various aspects of the market, and keep a diversified portfolio. Wealthfront sees these asset classes as:

  • US Stocks
  • Foreign Stocks
  • US Bonds
  • Foreign Bonds
  • Inflation Protection

Each of these assets is represented with ETFs, who capture a broad look at the asset class.

Distance:

Rebalancing a portfolio comes with a certain amount of overhead and cost. To avoid this, the robo advisor computes a “distance” vector. To trigger a rebalance, this distance will have to be greater than a given threshold. Most robo advisors usually have this threshold set to 5 percent.

The distance in this case, is the individual change in weight of each position. This is done by first calculating the current allocation. This is the value of a position against the total value of the portfolio. If any one position has a weight that’s 5 percent greater (current_weight/target_weight > 1.05), then the robo advisor will trigger a rebalance

Rebalancing:

Rebalancing is the act of buying and selling to return the portfolio to its target state. It’s important that all selling is done first. Once all cash available is gathered, necessary purchases are made.

To calculate how much to sell, use the target allocation of cash (weight * portfolio value), and see the number of shares that result from that cash. The difference between the number of shares currently held and the target level of shares is the amount to buy/sell.

Implementing a Robo Advisor with the Vanguard Core Series:

Now that we’ve established the various parts of what makes a robo advisor, we’ll start executing the steps to build one. We’ll build our robo advisor using Quantopian’s IDE.

When implementing our advisor, I find it easiest to do in steps. Quantopian’s IDE can only run backtests on a full algorithm. What that means, is there’s no “halfway” development. So, each step is a self-contained algorithm by itself.

Note: The implementation of Vanguard’s Core Series is using the information found here. The portfolio includes a 2% investment in a money market fund, but that isn’t available on Quantopian, so for the purposes of this tutorial, we’ll ignore it.

Risk-Based Allocation:

The first step, is to determine our universe, and assign weights using risk level. Our universe will be the Vanguard Core series:

  • VTI — Domestic Equity
  • VXUS — International Equity
  • BND — Domestic Fixed Income (Bonds)
  • BNDX — International Fixed Income (Bonds)

The risk level is actually the ratio of fixed income to equity, with equity being more volatile. So, a risk level 0 would have all capital allocated to fixed income (BND, BNDX), while a risk level of 5 would be 50/50. All 11 possible risk levels are catalogued in a dictionary, with the keys being the risk level, and values being tuples containing weight allocation:

risk_level = 5
risk_based_allocation = {0: (0,0,0.686,0.294),
                         1: (0.059,0.039,0.617,0.265),
                         2: (0.118,0.078,0.549,0.235),
                         3: (0.176,0.118,0.480,0.206),
                         4: (0.235,0.157,0.412,0.176),
                         5: (0.294,0.196,0.343,0.147), 
                         6: (0.353,0.235,0.274,0.118),
                         7: (0.412,0.274,0.206,0.088),
                         8: (0.470,0.314,0.137,0.059),
                         9: (0.529,0.353,0.069,0.029),
                         10: (0.588,0.392,0,0)}

The next step will be to implement our allocation in Quantopian. For this step, the only methods we’ll need are initialize and handle_data. The rest are superfluous.

The initialize method is the main. When a backtest is started, it automatically calls initialize. So, initialize will be the function that contains the dictionary for risk levels, as well as choosing the risk levels. The handle_data method is called automatically after initialize. It’s here that we’ll actually purchase the positions outlined in initialize.

Both initialize and handle_data have the variables context and data. Context allows the user to store global variables that get passed from method to method. Data helps the algorithm fetch different sorts of data. (Note: This is a change from Quantopian 1 where data was the object where global variables were stored).

Let’s get started. In initialize, copy and paste the following code:

context.stocks = symbols(‘VTI’, ‘VXUS’, ‘BND’, ‘BNDX’)
context.bought = False

risk_level = 5
risk_based_allocation = {0: (0,0,0.686,0.294),
                         1: (0.059,0.039,0.617,0.265),
                         2: (0.118,0.078,0.549,0.235),
                         3: (0.176,0.118,0.480,0.206),
                         4: (0.235,0.157,0.412,0.176),
                         5: (0.294,0.196,0.343,0.147),
                         6: (0.353,0.235,0.274,0.118),
                         7: (0.412,0.274,0.206,0.088),
                         8: (0.470,0.314,0.137,0.059),
                         9: (0.529,0.353,0.069,0.029),
                         10: (0.588,0.392,0,0)}
    #Saves the weights to easily access during rebalance
context.target_allocation = dict(zip(context.stocks,
                            risk_based_allocation[risk_level]))

    #To make initial purchase
context.bought = False

The variable context.stocks is a list of stock objects. The symbols function turns the strings into objects. The objects have attributes like current price, closing price, etc. Using that list, as well as the dictionary for allocation, we’ll create a second dictionary context.target_allocation. The keys are each ticker (VTI, VXUS, etc.) and the values are the ticker’s weights. This dictionary will be useful for both allocation and rebalancing.

Copy and paste the following code to create handle_data:

if not context.bought:
        for stock in context.stocks:
            amount = (context.target_allocation[stock] *
                      context.portfolio.cash) / data.current(stock,’price’)
            if (amount != 0):
                order(stock, int(amount))
                #log purchase
            log.info(“buying “ + str(int(amount)) + “ shares of “ +
                     str(stock))

         #now won’t purchase again and again
         context.bought = True

The variable context.bought refers to the value originally set to False in initialize. Since handle_data is called every time a market event occurs. So, using context.bought ensures the shares are only bought once.

To buy shares for each stock, the list of stock objects is iterated through. For each stock, the total number of shares are calculated by allocating the proper amount of capital (weight * capital), and then dividing that capital by the current price. Since we can only buy whole shares, leftover capital is added back to the cash on hand.

Lastly, for a smoother backtesting experience, all transactions are logged. The log is in the lower right hand corner. Build the current algorithm. Set the start date to June 2013 or later as the ETF BNDX was first tradeable on June 7, 2013.

Calculating Distance:

Before rebalancing a portfolio, it’s best to first calculate if the current portfolio is worth rebalancing. In more sophisticated robo advisors, this step would have a target portfolio calculated with an algorithm. For this example, our target portfolio is going to be the initial allocation defined by the risk level in initialize.

As the value of each position fluctuates, their portfolio weight may change too. The distance vector will be the change in weight of each individual position. Calculating the distance takes place in three steps:

  1. Calculating the current weights of each position
  2. Calculating the target weights (already done)
  3. Comparing the two weights

We want to check the balance daily, so we’ll go ahead and add the following function , before_trading_starts, to our code:

def before_trading_starts(context, data):
    #total value
    value = context.portfolio.portfolio_value +
                                         context.portfolio.cash
    #calculating current weights for each position
    for stock in context.stocks:
        if (context.target_allocation[stock] == 0):
            continue
        current_holdings = data.current(stock,’close’) *
                      context.portfolio.positions[stock].amount
        weight = current_holdings/value
        growth = float(weight) /
                        float(context.target_allocation[stock])
        if (growth >= 1.025 or growth <= 0.975):
            log.info(“Need to rebalance portfolio”)
            break

We first calculate total value. Since that value doesn’t change, it can be down outside the loop. Then, each position’s individual weight is calculated, and compared to the target weight (we’re using division since we’re interested in the relative growth of the position, not the absolute). If that growth exceeds the threshold (currently 2.5%) or is under the threshold (de-valued), then a rebalance is triggered. We don’t have an actual rebalance function written, so for now, we’ll simply log that a rebalance is necessary. It’s important to note the break added. Once the algorithm realizes that it’s time to rebalance, there’s no need to continue checking the rest of the stock. It creates a better best-case scenario.

However, we’re not actually done. We have to call before_trading_start, but calling it in a loop is inefficient. That’s why we’ll use the schedule_function command in initialize. Add this line of code to the end of the function block:

    schedule_function(
    func=before_trading_starts,
    date_rule=date_rules.every_day(),
    time_rule=time_rules.market_open(hours=1))

This schedules a distance calculation as the market opens every day. By computing the distance in the morning, we have the time and space necessary to execute rebalancing actions.

Rebalancing

The act of rebalancing a portfolio occurs in two steps. First, all assets that need to be sold are sold, and then all assets that need to bought are bought. Selling first makes sure that we don’t run out of cash.

The following is the code for a rebalance function:

def rebalance(context, data):
    for stock in context.stocks:
        current_weight = (data.current(stock, ‘close’) *
                          context.portfolio.positions[stock].amount) /
                          context.portfolio.portfolio_value
        target_weight = context.target_allocation[stock]
        distance = current_weight — target_weight
        if (distance > 0):
            amount = -1 * (distance * context.portfolio.portfolio_value) /
                     data.current(stock,’close’)
            if (int(amount) == 0):
                continue
            log.info(“Selling “ + str(int(amount)) + “ shares of “ +
                      str(stock))
            order(stock, int(amount))
    for stock in context.stocks:
        current_weight = (data.current(stock, ‘close’) *
                          context.portfolio.positions[stock].amount) /
                          context.portfolio.portfolio_value
        target_weight = context.target_allocation[stock]
        distance = current_weight — target_weight
        if (distance < 0):
            amount = -1 * (distance * context.portfolio.portfolio_value) /
                     data.current(stock,’close’)
            if (int(amount) == 0):
                continue
            log.info(“Buying “ + str(int(amount)) + “ shares of “ +
                      str(stock))
            order(stock, int(amount))

The selling and buying mechanics are the same. The only difference is when selling stock, you use a negative value when calling the order function. In both cases, we take the absolute difference in weight (target — current), and use that value to find the number of shares to buy or sell.

Multiple Universes

Vanguard provides several more universes beyond the core-series. Currently, we’re able to manipulate the risk level, and observe the outcome. Let’s add the functionality of also being able to select a universe.

The first and straightforward method, is to implement multiple dictionaries. Here’s how something like that would look. The CRSP series was added to our algorithm. The user now chooses both the universe and the risk level.

    core_series = symbols(‘VTI’, ‘VXUS’, ‘BND’, ‘BNDX’)
    crsp_series = symbols(‘VUG’, ‘VTV’, ‘VB’, ‘VEA’, ‘VWO’, ‘BSV’, ‘BIV’, ‘BLV’, ‘VMBS’, ‘BNDX’)
    #universe risk based allocation
    core_series_weights = {0: (0,0,0.686,0.294),
                           1: (0.059,0.039,0.617,0.265),
                           2: (0.118,0.078,0.549,0.235),
                           3: (0.176,0.118,0.480,0.206),
                           4: (0.235,0.157,0.412,0.176),
                           5: (0.294,0.196,0.343,0.147),
                           6: (0.353,0.235,0.274,0.118),
                           7: (0.412,0.274,0.206,0.088),
                           8: (0.470,0.314,0.137,0.059),
                           9: (0.529,0.353,0.069,0.029),
                           10: (0.588,0.392,0,0)}
    crsp_series_weights = {0: (0,0,0,0,0,0.273,0.14,0.123,0.15,0.294),
1: (0.024,0.027,0.008,0.03,0.009,0.245,0.126,0.111,0.135,0.265),
2: (0.048,0.054,0.016,0.061,0.017,0.218,0.112,0.099,0.12,0.235),
3: (0.072,0.082,0.022,0.091,0.027,0.191,0.098,0.086,0.105,0.206),
4: (0.096,0.109,0.03,0.122,0.035,0.164,0.084,0.074,0.09,0.176),
5: (0.120,0.136,0.038,0.152,0.044,0.126,0.07,0.062,0.075,0.147),
6: (0.143,0.163,0.047,0.182,0.053,0.109,0.056,0.049,0.06,0.118),
7: (0.167,0.190,0.055,0.213,0.061,0.082,0.042,0.037,0.045,0.088),
8: (0.191,0.217,0.062,0.243,0.071,0.055,0.028,0.024,0.030,0.059),
9: (0.215,0.245,0.069,0.274,0.079,0.027,0.014,0.013,0.015,0.029),
10: (0.239,0.272,0.077,0.304,0.088,0,0,0,0,0)}

    #set universe and risk level
    context.stocks = crsp_series
    risk_based_allocation = crsp_series_weights
    risk_level = 1

The user can use the three variables, context.stocks, risk_based_allocation, and risk_level, to set both the universe and the risk level.

What’s Next

Developing in Quantopian is a great experience — they provide lots of useful tools. But, it’s also limiting, being forced to only work in one file, etc. To build further on this robo advisor, I plan to migrate my work so far to the backend, and use Quantopian’s open source python library Zipline to run backtests. The next installment will cover all this and more!

You can find the algorithm here. Try it yourself!

by Rao Vinnakota

/

So You Want to Trade Crypto - Exploiting Cryptocurrency Correlation (Part 5)

There is correlation within any sector or asset class, however there are particularly interesting patterns in Cryptocurrency due to the new and speculative nature of the market, along with its historical pairs structure.

Weakness and Strength

 (Raw USD pairs from  coinmarketcap.com , raw BTC pairs from  poloniex.com )

(Raw USD pairs from coinmarketcap.com, raw BTC pairs from poloniex.com)

Historically, the Cryptocurrency space has been dominated and led by Bitcoin, with Bitcoin’s 80% — 90% of total market cap only starting to be challenged in the last couple of years, as covered in “Market Cap Distribution and Rise of Altcoins”. This huge shift in capital distribution caused a bloom in many Altcoins during the major bull run of 2017.

However, despite this redistribution of power in the market, a correlation between different altcoins and Bitcoin stayed strong throughout 2017, suggesting that the market is still centering around Bitcoin both as an indicator of general sentiment and health and, as a safe haven asset.

During the bulk of the bull run, correlation of USD pairs stayed high, with the notable exception of periods prior to Bitcoin pullbacks, such as the dips from $3k and $5k. This seems to form a bit of a leading indicator (albeit a very noisy one), as a divergence in altcoin movement appears to precede a local top and a pullback.

Correlation and investor sentiment

The BTC pairs also tell an interesting tale, moving into negative correlation as the Bitcoin trend weakened before bouncing back once a bottom had been reached.

This view of combined market correlation can also give clues to the sentiment of investors. During bull markets, projects have a high-value premium based on the expectation of future success, meaning that while correlation stays generally positive, the price movement of projects will shift around based on their own news and merits — lowering overall correlation.

However, if we look to 2018 and the bearish trend, we see a very different pattern as fear enters the market. Correlation tends towards 1.0 in a bear market, as sell-offs are sharp across the board due to the cycle of panic. Individual projects are no longer values on their own merits, instead being sold off at whatever price they’ll fetch as the market falls in unison.

This fear can be seen clearly as Bitcoin’s first sell-off from $19.6k to $6k takes effect, followed by a very slight regaining of hope after the bounce (drop in correlation) before tending back towards 1.0 as we moved for a retest of $6k.

ALT/BTC pairs and hedging

Historically, most Cryptocurrency trading was done with Bitcoin as the base pair, and even now we still see $100Ms daily through Bitcoin pairs. Back in the days where both regulation and market volume was limited, this made a lot of sense. A Crypto-Crypto exchange didn’t need to deal with the hassle of accepting and storing fiat currencies, nor the regulatory issues of handling money.

This had the effect of tying the USD value of altcoins closer to the shifts of Bitcoin, which is still a factor today (although to a lesser extent). This, along with the psychology of fear during a bear market, has lead to the levels of correlation we see in the USD pairs during large pullbacks.

In theory, this makes ALT/BTC pairs extremely useful for trading: during a bull market you’re betting that your chosen coin does better than Bitcoin on its technical merits; during a bear market you expect the ratio to stay relatively level as your coin maintains a correlation near to 1.0 against BTC/USD.

However, in reality we also start to see correlations between the ALT/BTC pairs and Bitcoin also rising during major downturns, suggesting that Bitcoin is used as a safe haven asset to hedge against pullbacks, as people rotate out of higher risk and reward altcoins.

This causes issues in the assumption of a hedged market exposure from trading Bitcoin pairs during a pullback. This does, however, provide yet more signals as to market sentiment. Since you will often see sharp dips in BTC/USD being mirror across ALT/BTC pairs, a deviation from this trend is a helpful indicator of market strength and bullish sentiment.

Trading the correlation

An understanding how different Cryptocurrencies react within the market allows you to optimize portfolios and reduce beta to Bitcoin during downturns, while maintaining upside during the bulk of a bull run.

Well established coins with higher market caps tend to keep high correlations to Bitcoin throughout the market cycle along with lower volatility (at least in terms of crazy Crypto volatility). Meanwhile, low-mid cap coins tend to be used as high risk high reward speculation tools during bull markets, but drop sharply as market sentiment shifts.

The optimal portfolio would likely re-balance periodically between a mix of mid and high cap coins, weighted by a market sentiment metric, while also hedging some exposure by shorting Bitcoin to create synthetic ALT/BTC pairs.

While it is impossible to hedge away all risk in such a new and underdeveloped market, such a portfolio may help to ease the nerves of the more risk averse investor, while maintaining exposure to Cryptocurrency as a whole.

by Matthew Tweed

/

Algo Trading News Headlines 6/7/2018

How a job invented by Goldman Sachs took over the banking industry

(news.efinancialcareers.com)

It’s not just GS though. In the past few years, strats have caught on elsewhere too. Deutsche Bank now has them. So does Morgan Stanley. So does J.P. Morgan. So does Credit Suisse. And wherever there are strats, there is strat hiring. Morgan Stanley, for example, currently has around 50 strat vacancies. Insiders say Deutsche Bank’s strats team, set up by ex-GS strat Sam Wisnia, has gone from zero to over 100 in four years

 Photo by  chuttersnap &nbsp;on  Unsplash

Photo by chuttersnap on Unsplash

Hedging with Machine Learning

(themarketmogul.com)

Before McDonald’s could introduce Chicken McNuggets, they had to hedge against the cost of chicken. If chicken prices rose dramatically, they would no longer be able to offer the product. After some time, a financial consultant determined that there were two costs associated with raising chickens. That is grain and water.

9 Most Commonly Asked Questions About MarketStore And Answers To Them

(blog.alpaca.markets)

In social media and offline, we’ve been answering questions and responding to comments, but today we wanted to take the opportunity to put all the queries and responses together in one post and share it with the entire community so everyone can get a look at the responses on a single post.

/

9 Most Commonly Asked Questions About MarketStore And Answers To Them

 Photo by  William Stitt  on  Unsplash

Each of these articles seeks to explain the technology we build along with our Alpaca algo trading brokerage. These articles led active discussions on Reddit and Medium, and it became clear to us that there is a lot of interest and a pretty large need in the community for a timeseries database dedicated for financial market data. The database world and software engineering in general have changed so much over the last decade as we’ve seen an explosion in open source programming and databases. We are seeing some people now actively using open source to and contributing some code in the GitHub repository.

In social media and offline, we’ve been answering questions and responding to comments, but today we wanted to take the opportunity to put all the queries and responses together in one post and share it with the entire community so everyone can get a look at the responses on a single post.

Q: Does MarketStore store data in memory?

A: No. MarketStore is designed to run in a reasonable size of host without huge hardware investment. If you have lots of cash, software technology is irrelevant, but what software engineering can bring is that you can do a lot better job with cheaper hardware. MarketStore’s primary use case is to be able to store and distribute years of data at second level granularity for more than tens of thousands of series (US equities and crypto coins across exchanges can easily become this size). The data size can be a few terabytes, and it is not still very common to have this big size RAM in a commodity hardware. MarketStore instead stores everything in disk, but the on-disk format is nearly identical to the layout in the memory, and thanks to SSD evolution, MarketStore can load the data at the speed competitive to in-memory storage.

Q: How does it make sense to compare with PostgreSQL and includes DataFrame loading?

A: Even if you can store the data, offloading it from application processes, it is not useful if you cannot use it. MarketStore is mainly used in the context of AI machine learning and backtesting, and the application typically loads it into some tabular structure such as Pandas DataFrame. That is why MarketStore’s network protocol is byte sequence in MessagePack so the inefficient JSON deserialization can be avoided. The client can load the delivered byte data into memory as C array, which is what is used behind DataFrame.

Q: How is it better compared to InfluxDB?

A: We have not compared the performance with InfluxDB, but InfluxDB and other general-purpose timeseries databases use-case is as system metrics or activity log analysis. Those require more flexible data structure and don’t necessarily need specific functions such as timezone-aware aggregate. The flexibility comes with necessary overhead as tradeoffs as always, and MarketStore should be much faster and cost effective if the use case is the financial market data.

Q: Why are you comparing with PostgreSQL when Timescale should be faster?

A: You can send us the benchmark results if you have them, but in our internal experiments, Timescale is even slower than PostgreSQL compared to MarketStore. The loading time at the database server level for Timescale is 2–3x slower than PostgreSQL, since Timescale makes use of table partitioning (aka table constraints exclusion) that needs to open lots of files from disk. It will give advantage to filter a small slice of the data out of large amount of data, but it will not work better if you scan most of it. MarketStore stores the data in an optimal way on disk and reads sequentially direct to memory compared to those relational databases, so it is way faster.

Q: MarketStore can be used only for historical data but not for real-time data right?

A: There is a new feature coming soon to MarketStore that will allow streaming and realtime push on every new data write. MarketStore was originally designed to help our algo trading platform that builds trading algorithms using deep learning, and run them in the real market, and had JSON websocket streaming. The feature has been for the time being so that Marketstore can find a way to fit in larger use cases. But thankfully it is now back in as a plugin. We have been testing this with thousands of updates every few seconds and so far it is working perfectly.

Q: Why do I need this for machine learning? I can load the data from disk without a problem

A: If your training process doesn’t use much data (e.g. just daily bars from one stock), then yes probably you don’t need MarketStore for performance reasons. What we needed to do on Alpaca trading platform requires a server that is large enough to store an amount of intraday data across the entire market (can be up to terabyte range), and load the necessary series data back and forth. If you are familiar with typical machine learning training process, you can tell how the training iteration can load random data from the pool. That said, MarketStore is not just for performance, but also for the convenience to prove the uniformed way to access historical and real-time timeseries data the same way without worrying about how to manage local files etc. And the built-in data ingestor can load the data without even writing any code.

Q: Where is the installer?

A: Sorry, at the moment, we are not providing the one-click installer! But instead, we package the server process into a docker container image, so if you have docker, you can just start it in a second.

Q: Why is it open sourced?

A: Because there is a problem to be solved! MarketStore was implemented proprietary for our internal use and has been used in our production, but we have also seen the common problems affecting many people in the space. Our mission at Alpaca is to help individual investors with technology, and improve the algo trading environment, regardless of whether we give that information away to users or offer it in a premium package. This kind of product has only been accessible by financial institutions with large capital resources. But now we are making it available to anyone who is eager to try out! That’s awesome, isn’t it!?

Q: I found a bug

A: Please report it in the GitHub issue!

/

Algo Trading News Headlines 6/6/2018

Opinion: AI will change stock-market trading, but it can’t wipe out the human touch

(www.marketwatch.com)

But just as Tesla’s TSLA, +4.83% Elon Musk recently claimed that the use of robots in auto assembly had gone too far and that humans needed to be brought back, the same is likely true for AI on Wall Street and its disappearing trading jobs. It is easy to be enamored by the power and speed of the new prediction machines. However, there are risks that real innovation and competitive advantage will be lost by relying solely on them.

 Photo by  Lukas &nbsp;on  Unsplash

Photo by Lukas on Unsplash

So You Want to Trade Crypto — Market Cap Distribution and Rise of Altcoins (Part 4)

(blog.alpaca.markets)

If someone creates a new coin with a total supply of 100B and manages to get it listed on a small exchange and trades it a few times with their friends for $1 per coin, it technically has a market cap of $100B. But in reality, is has no true value and no trading volume to sustain any kind of selling pressure.

Systematic trading entrepreneur to launch AI fund

(hfm.global)

Entrepreneur and systematic trader Matthew McElligott is launching an artificial intelligence-focused quant equity hedge fund. London-based Ylang Capital will use AI and machine learning trading techniques to trade global equities and is aiming to launch with around $50m, according to sources.

AI Trader: Deep Learning Artificial Intelligence Crypto Trading?

(bitcoinexchangeguide.com)

AI Trader executes trades without emotion, without prejudice, without fatigue and can trade relentlessly for days and months. It’s lightning quick responses to changing market conditions cannot be replicated by human traders. Notwithstanding the significant risks of trading in a highly volatile market, AI Trader was developed to enhance your profit potential.

Intelligent Machines and FX Trading

(www.finextra.com)

A major problem with trading strategies with AI is that they can produce models that are worse than random. The traditional technical analysis is an unprofitable method of trading because strategies based on chart patterns and indicators draw their returns from a distribution with zero mean before any transaction costs.

/

Algo Trading News Headlines 6/5/2018

Commissions Slump as Mifid II, Algo Trading Take Bites Out of Brokers

(www.waterstechnology.com)

The commissions that brokers take from trade executions dropped sharply in the first quarter after the enactment of new European rules on research unbundling, increasing downward pressure on an area that has already felt the effects of moves away from high-touch strategies.

 Photo by  Markus Spiske &nbsp;on  Unsplas

Photo by Markus Spiske on Unsplas

Welch: A super cycle has arrived

(www.agrinews-pubs.com)

“The reemergence of the algo boys and the HFT (high frequency trading) funds has sparked the largest rally with commodities in eight months. After declining for a record five years in a row, it seems the odds are high that hard assets in general will post a year-over-year gain. For agricultural producers that is great news because selling into a rising market is far more profitable than selling into a declining market.”

Ex-Goldman Sachs Programmer Seeks to Overturn Code Theft Conviction

(www.bankerandtradesman.com)

Former Goldman Sachs Group Inc. programmer Sergey Aleynikov on Monday filed a new challenge to his criminal conviction of stealing computer code from the investment bank when he left for another job. Aleynikov, whose case partly inspired Michael Lewis’ bestselling book “Flash Boys” on high-frequency trading in the U.S. equity market, previously had his conviction overturned by the judge who oversaw his 2015 trial in Manhattan Supreme Court.

Making sense of quants

(www.asiaasset.com)

Finally, is quant the magic panacea and answer to the kind of cross-asset correlation that we saw during the global financial crisis? I doubt it. A black swan event, by definition, comes from where you don’t expect. If quant has helped iron out cross-asset correlation, then different forces will probably come into play to bring on the next crisis — or it wouldn’t be a crisis. And as more and more quant funds start playing on particular risk factors, their behaviour is bound to influence the market, which always reacts on itself. Quants may start to influence the very terms of the equations they seek to rely on. That is one way that crises tend to develop.

New Report: Algorithmic Trading Market — drivers, challenges, key trends, standardization, deployment models, Sales Market Comparison and strategies

(businessinvestor24.com)

The report represents a thorough review of the entire growth trajectory of the worldwide Algorithmic Trading market over the span of the forecast period. Different sections have been taken into consideration to give the readers an all-round perspective of the entire market scenario.

/

So You Want to Trade Crypto — Market Cap Distribution and Rise of Altcoins (Part 4)

Bitcoin dominating >90% of the total value of the market to <40%

From the start of 2016 to the end of 2017, we’ve gone from Bitcoin dominating >90% of the total value of the market to <40%.

This flow of capital has lead to a boom in alternative Cryptocurrencies, which offer newer technologies and wider use-cases.

Market Cap Misconceptions

The first point to address is the issue with market capitalization as a metric when applied to Cryptocurrency. For a stock, the market cap is calculated as:

     Price per share * shares outstanding

Which makes sense, as each share represents a stake in the assets and profits of the company. This same calculation is applied to Cryptocurrencies:

     Price per token * tokens available

This starts to cause issues due to the ease that a new token can be made and added to one of the dozens of small exchanges.

If someone creates a new coin with a total supply of 100B and manages to get it listed on a small exchange and trades it a few times with their friends for $1 per coin, it technically has a market cap of $100B. But in reality, is has no true value and no trading volume to sustain any kind of selling pressure.

Maintaining an artificially inflated market cap

Adding to this, there are many coins that do have significant daily trading volumes while maintaining an artificially inflated market cap as the majority the of the supply is locked up by developers and isn’t tradeable. This raises serious questions about how the investors and traders price in total supply of a token and whether the theoretical value of a project lines up with reality.

Many people also misunderstand what market capitalization means in terms of capital flow.

A market cap of $100B does not mean that $100B has been invested into the token, as shown earlier. Nor does a token’s market cap changing from $100B to $150B or $50B mean that $50B of capital has changed hands.

Not enough money in the system to redeem every token

The profit from a Cryptocurrency investment should be treated as “paper gains” until cashed out or hedged — there is simply not enough money in the system to redeem every token to anywhere near the value of its market cap.

Despite this, for a Crypto with sufficient trading volume and age, market cap can be useful for rough comparison, but make sure to always take it with a grain of salt.

Shift in Market Cap Distribution

 (Market Cap Values from  coinmarketcap.com )

(Market Cap Values from coinmarketcap.com)

As we can see, the last few years have not been kind to Bitcoin’s historical dominance of the Crypto market, with many new projects taking off in the first half of 2017.

For many years, Bitcoin has held onto its “first mover advantage”. However, political issues surrounding the development of Bitcoin caused a slow down in advancement — creating a void for a multitude of altcoins to fill.

Result of “ICO Mania”

Over the past year this new crop of development has accelerated, with smart contract platforms taking many of the top spots. 2017 also saw the rise of “ICO Mania”, with dozens of new tokens and projects gaining investment from speculators looking for yet higher yields on their equity.

In the long term, Bitcoin will likely continue its decline in market share, as its older technology simply cannot compete with new offerings. As long as the political issues surrounding development continue, this will not change. Bitcoin made for an excellent proof of concept, but if it can’t adapt, it risks becoming the Myspace of the Crypto world.

Trading the Altcoin Boom

With altcoins making consistent gains in market share and the relative stagnation of Bitcoin development, Bitcoin is likely to drop from the top spot over the next couple of years (if not sooner) in favour of a newer generation Cryptocurrency.

This shift will likely see a huge change in the attitude and composition of the market as a whole, as everyone tries to pile into the new top coin and related technologies, so that they can ride the hype train.

As always, it is best to keep a level head and stick to your trading and investment strategies. A firm understanding of the underlying technology and use-case of a wide range of Cryptocurrencies will serve well in positioning yourself to take advantage of this shift.

There are a wide variety of projects which all have their use-cases

While smart contract focused Blockchains are some of the leaders at the moment, in the long term their value and success will be measured by the applications and businesses that run on top of them. Meanwhile, we shouldn’t forget the other uses of Blockchain, such as ledgers for supply chains, auditing or even Internet of Things devices. There are a wide variety of projects which all have their use-cases.

Cryptocurrency investments should be managed like a stock portfolio. You wouldn’t place your entire value into a single stock, similarly you shouldn’t be overly dear about a single coin. A well balanced holding of different projects across different areas can help hedge against black swan events in the market while profiting from the broad growth of Crypto as an asset class.

by Matthew Twee

/