IB量化博客


1 2 3 4 5 2 15


Quant

K-Means Clustering For Pair Selection In Python - Overview


In this series, we will cover what K-Means clustering is, how it can be used for solving the age-old problem of pair selection for Statistical Arbitrage, and the advantage of using K-Means for pair selection compared to using a brute force method. We will also create a Statistical Arbitrage strategy using K-Means for pair selection and implement the elbow technique to determine the value of K.

Let’s get started!
 

Part I – Life Without K-Means

To gain an understanding of why we may want to use K-Means to solve the problem of pair selection we will attempt to implement a Statistical Arbitrage as if there was no K-Means. That is, we will attempt to develop a brute force solution to our pair selection problem and then apply that solution within our Statistical Arbitrage strategy.

Let’s take a moment to think about why K-Means could be used for trading. What’s the benefit of using K-Means to form subgroups of possible pairs? Couldn’t we just come up with the pairs ourselves?

This is a great question and one undoubtedly you may have wondered about. To better understand the strength of using a technique like K-Means for Statistical Arbitrage, we’ll do a walk-through of trading a Statistical Arbitrage strategy if there was no K-Means. I’ll be your ghost of trading past so to speak.

First, let’s identify the key components of any Statistical Arbitrage trading strategy.

  1. We must identify assets that have a tradable relationship
  2. We must calculate the Z-Score of the spread of these assets, as well as the hedge ratio for position sizing
  3. We generate buy and sell decisions when the Z-Score exceeds some upper or lower bound

To begin, we need some pairs to trade. But we can’t trade Statistical Arbitrage without knowing whether or not the pairs we select are cointegrated. Cointegration simply means that the statistical properties between our two assets are stable. Even if the two assets move randomly, we can count on the relationship between them to be constant, or at least most of the time.

Traditionally, when solving the problem of pair selection, in a world with no K-Means, we must find pairs by brute force or trial and error. This was usually done by grouping stocks together that were merely in the same sector or industry. The idea was that if these stocks were of companies in similar industries, thus having similarities in their operations, their stocks should move similarly as well. But, as we shall see, this is not necessarily the case.

The first step is to think of some pairs of stocks that should yield a trading relationship. We’ll use stocks in the S&P 500 but this process could be applied to any stocks within any index. Hmm, how about Walmart and Target. They both are retailers and direct competitors. Surely they should be cointegrated and thus would allow us to trade them in a Statistical Arbitrage Strategy.

Let’s begin by importing the necessary libraries as well as the data that we will need. We will use 2014-2016 as our analysis period.

#importing necessary libraries

#data analysis/manipulation

import numpy as np
import pandas as pd

#importing pandas datareader to get our data
import pandas_datareader as pdr

#importing the Augmented Dickey Fuller Test to check for cointegration
from statsmodels.tsa.api import adfuller

Now that we have our libraries, let’s get our data.

#setting start and end dates
start='2014-01-01'
end='2916-01-01'
#importing Walmart and Target using pandas datareader
wmt=pdr.get_data_yahoo('WMT',start,end)
tgt=pdr.get_data_yahoo('TGT',start,end)

Before testing our two stocks for cointegration, let’s take a look at their performance over the period. We’ll create a plot of Walmart and Target.

#Creating a figure to plot on
plt.figure(figsize=(10,8))

#Creating WMT and TGT plots
plt.plot(wmt["Close"],label='Walmart')

plt.plot(tgt[‘Close'],label='Target')
plt.title('Walmart and Target Over 2014-2016')

plt.legend(loc=0)
plt.show()

Plot

In the above plot, we can see a slight correlation at the beginning of 2014. But this doesn’t really give us a clear idea of the relationship between Walmart and Target. To get a definitive idea of the relationship between the two stocks, we’ll create a correlation heat-map.

To begin creating our correlation heatmap, must first place Walmart* and Target* prices in the same dataframe. Let’s create a new dataframe for our stocks.

#initializing newDF as a pandas dataframe
newDF=pd.DataFrame()
#adding WMT closing prices as a column to the newDF
newDF['WMT']=wmt['Close']
#adding TGT closing prices as a column to the newDF
newDF['TGT']=tgt['Close']

Now that we have created a new dataframe to hold our Walmart* and Target* stock prices, let’s take a look at it.

newDF.head()

Chart

We can see that we have the prices of both our stocks in one place.

In the next post, we will create a correlation heat map of stocks and run some ADF tests

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

*Disclaimer: All investments and trading in the stock market involve risk. Any decisions to place trades in the financial markets, including trading in stock or options or other financial instruments is a personal decision that should only be made after thorough research, including a personal risk and financial assessment and the engagement of professional assistance to the extent you believe necessary. The trading strategies or related information mentioned in this article is for informational purposes only.

If you want to learn more about K-Means Clustering for Pair Selection in Python, or to download the code, visit QuantInsti website and the educational offerings at their Executive Programme in Algorithmic Trading (EPAT™).

This article is from QuantInsti and is being posted with QuantInsti’s permission. The views expressed in this article are solely those of the author and/or QuantInsti and IB is not endorsing or recommending any investment or trading discussed in the article. This material is for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IB to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.


17880




Quant

SGX - Update on India: Accessing the World's Fastest Growing Large Economy via Offshore Futures in 2018


Join us for a free webinar with Tariq Dennison, QuantOfAsia, on May 23, 2018 4:30pm HK
 

Register


By some measures, India has surpassed China as the world’s fastest growing large economy, but it is still one of the most difficult stock and currency markets for foreign investors to access. This webinar discusses how to use offshore SGX-listed futures to trade India following this year’s updates and explains how the new SGX India Futures work.

Presented by Tariq Dennison, GFM Asset Management, QuantOfAsia,

Sponsored by Singapore Exchange

SGX-GFM


Information posted on IBKR Quant that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Quant are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.


17922




Quant

Quantra by QuantInsti presents Automated Trading using Interactive Brokers TWS and IBridgePy, an open-source Python software*


By Milind Paradkar

For those Python enthusiasts interested in using open-source software IBridgePy*, Quantra by QuantInsti® presents a free course "Automated Trading with IBridgePy using Interactive Brokers Platform".

The focus of this course would be on the applicability of IBridgePy, which is an open-sourced software used to connect to Interactive Brokers C++ API for execution of Python codes. It is free and available on Quantra, and it includes detailed lessons on using the IBridgePy API.

The topics covered are on:

  • How to install and configure Python environment and IBridgePy
  • Learn the syntax and functions for conducting various operations like fetching and handling data
  • Learn various steps involved to correctly fetch historical and real-time data from Interactive Brokers servers into your setup
  • Learn about different types of orders and how to place them using IBridgePy*
  • Learn to code, test and implement a trading strategy on Interactive Brokers platform using IBridgePy*

Quantra-Python

 

Visit Quantra by QuantInsti® to enroll for this free course*.

 

*Disclaimer: This software does not directly use IB's Python API, but instead uses the third party module IBridgePy. IBridgePy is an open-source Python software and is in no way affiliated, endorsed, or approved by Interactive Brokers or any of its affiliates. It comes with absolutely no warranty and should not be used in actual trading unless the user can read and understand the source.


Milind Paradkar holds an MBA in Finance from the University of Mumbai and a Bachelor’s degree in Physics from St. Xavier’s College, Mumbai. At QuantInsti®, Milind is involved in creating technical content on Algorithmic & Quantitative trading. Prior to QuantInsti®, Milind had worked at Deutsche Bank as a Senior Analyst where he was involved in the cash flow modeling of structured finance deals covering Asset-backed Securities (ABS) and Collateralized Debt Obligations (CDOs).


This article is from QuantInsti and is being posted with QuantInsti’s permission. The views expressed in this article are solely those of the author and/or QuantInsti and IB is not endorsing or recommending any investment or trading discussed in the article. This material is for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IB to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.


17968




Quant

Actuaries Versus Quants


By David J. Merkel, CFA, FSA, Aleph Blog

I’m an Actuary and a Quant.  I’m not an Actuary, and I’m not a Quant.

I suppose I could do the other two permutations.  I won’t, but I will explain.

I passed all of the exams from the Society of Actuaries to become a Fellow in the Society of Actuaries.  I maintained that credential for around 18 years by paying my dues.  Oddly, once I stopped paying my dues, and thus ceased to be a Fellow, I got more requests to be on committees, and give talks to the Society of Actuaries.  Things that I recommended to the Society 20 years ago are finally getting a serious hearing now.  My main suggestion that actuaries should have to pass a writing test is at least floating among some thinkers at the Society.

Why a writing test?  The ability to express ideas verbally to those who may not have a strong math background is important, and correlates with other social skills that aid in leadership.  The Society of Actuaries at one time (not sure when) did have an English test, and in that era, actuaries were not merely math nerds, and many of them were leaders in insurance and pensions.

So, I am no longer a Fellow in the Society of Actuaries.  I am still an actuary.  The way I reason and use math to solve investing and other problems stems from the skills that I learned when practicing as an actuary.  As actuaries went, I was a generalist, and would enjoy tackling unusual and multi-disciplinary problems.  The problems I liked best were the toughest ones — the ones where there is no easy answer, some degree of qualitative reasoning must be employed, and new techniques created.

Unlike most actuaries, I know the investment math relatively well, which makes me a Quant [quantitative analyst].  But I am not a Quant, or maybe, I’m a skeptical Quant.  Why?  I don’t believe that Modern Portfolio Theory is right.  In general, higher returns are achieved by taking moderate risk, not low or high risk.  More risk does not mean more return after some point.

Also, markets are more complex than the Quants will generally accept.  Disturbances are not normally distributed.  Variances of stock returns are infinite, but normality is used in order to get tractable results.  Markets sometimes fail to trade in a continuous manner.  There are jumps/falls in prices far greater than a normal distribution would expect.  And that gets borne out by the greater volatility of markets close to open, than open to close.

So I am a skeptical Quant at best, but if someone asks me whether I am an Actuary or a Quant, I will say I am an Actuary.  Why?

The main thing is differences in method.  Actuaries believe in table stability; Quants believe in bicycle stability.  Actuaries look at the cash flows, and make minimal assumptions about markets continuing to operate.  Quants usually assume that market continue to operate.  Actuaries look long-term, and do stress-testing.  Quants look short-term, and do hedging.

This is one reason why few pure insurance companies failed during the crisis, while many banks did.  If assets and liabilities are matched, it is hard to have a run on the company, aside from credit events.

Actuaries think long-term, while Quants think short-term.  In the short-run, listening to the Quants will yield greater profits.  In the long-run, listening to the actuaries will yield greater profits.

There’s one more issue.  Actuaries have an ethics code.  Quants don’t.  (Quants that are CFAs have an ethics code, but most aren’t CFAs.)  Actuaries are supposed to act in the best interests of clients.

In my opinion, Wall Street would be far better of replacing their Quants in risk control positions with Actuaries.  Actuaries have a public policy interest, and would not merely bend to the needs of the company.  They would cut back risk positions far more than the Quants do.

I know this only makes sense to Wall Street if they are willing to adopt a stable model.  Actuaries would help to stabilize things, and a focus on long-term stability would aid Wall Street.

But if forced to choose: I am an actuary, I am not a quant!

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

About David Merkel

Learn more about Aleph Blog here:  http://alephblog.com/

David J. Merkel, CFA, is a Registered Investment Adviser through his firm, Aleph Investments, LLC.  He shares most of his views through his blog Alephblog.com.  He writes on equity and bond portfolio management, macroeconomics, derivatives, quantitative strategies, insurance issues, corporate governance, and more. His specialty is looking at the interlinkages in the markets in order to understand individual markets better. Formerly he worked as Director of Research for Finacorp Securities. He was a leading commentator for the investment website RealMoney.com. David was a senior investment analyst at Hovde Capital, responsible for analysis and valuation of investment opportunities for the FIP funds, particularly of companies in the insurance industry. He also manages the internal profit sharing and charitable endowment monies of the firm. Prior to joining Hovde in 2003, Merkel managed corporate bonds for Dwight Asset Management. In 1998, he joined the Mount Washington Investment Group as the Mortgage Bond and Asset Liability manager after working with Provident Mutual, AIG and Pacific Standard Life. His background as a life actuary has given David a different perspective on investing. Merkel holds bachelor’s and master’s degrees from Johns Hopkins University.

This article is from David J. Merkel, CFA, and is being posted with Mr. Merkel’s permission. The views expressed in this article are solely those of the author and/or Mr. Merkel’s and IB is not endorsing or recommending any investment or trading discussed in the article. This material is for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IB to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.


17925




Quant

The Continuous Evolution of Bitcoin and Blockchain


By Dan Gramza

When most people think of cryptocurrencies, they think of bitcoin.  Although it is one of the most popular cryptocurrencies, there are over 1,600 cryptocurrencies. The applications of bitcoin and the other cryptocurrencies are constantly evolving. 

Bitcoin is being recognized as legal tender by some governments.  For example, Japan recognized bitcoin as legal tender in April 2017.  Bitcoin is illegal as a payment tool in some countries, such as Iceland.  Indonesia, India and Bangladesh.   

Cryptocurrencies continue to develop as a lifeline for undeveloped countries plagued with hyperinflation and failed government monetary policy. There are number of countries or organizations that are using cryptocurrencies as a tool to try to solve economic issues.  Examples of countries that are currently using a cryptocurrency or are contemplating using cryptocurrencies are Ecuador, Argentina, Palestine, Marshall Islands, Dubai, Zimbabwe, Senegal and Venezuela.

These special use cryptocurrencies are typically identified as that country’s currency. On the surface, the use of cryptocurrencies in this fashion may seem to alleviate some of the challenges that country may be facing on the local and global market. Some countries are considering cryptocurrency based crude oil transactions to eliminate potential control by the US government of US dollars used in crude oil transactions.  However, new challenges can arise. Just because a country has created a cryptocurrency it does not mean the global marketplace will accept it.  For example: Venezuelan President Nicolás Maduro announced the creation of a virtual currency in an effort to ease the country's economic crisis and the Trump administration prohibits US purchases of the Venezuelan Petro Cryptocurrency.

Another evolutionary trend is the backing of a cryptocurrencies with hard assets, such as diamonds and gold, as a way to create a feeling that there is something supporting the cryptocurrency other than the hope that someone else will be willing to pay a higher price for the cryptocurrency. The Israel Diamond Exchange is launching diamond-backed cryptocurrency. 

A potentially hazardous evolution is when the price of a bitcoin is used as a basis to determine the price of another cryptocurrency.  An example of this is the use of bitcoin prices in the valuation of Tether which is supposed to be back by US dollars.  The movement of Tether could have the potential to artificially inflate bitcoin’s price and any other cryptocurrency that is basing its price on the bitcoin’s price.

Bitcoin is a worldwide payment system which verifies bitcoin transactions by recording a transaction in a record system (distributed ledger) called a blockchain. Blockchain is a network of peer-to-peer connected computers. The bitcoin network of peer to peer computers use software to connect them to each other over the internet to the other computers running the same software.  This software creates a network of computers that can communicates with each other, relaying information about and recording new transactions to the bitcoin blockchain.  Once a new a block of valid transactions is confirmed it is distributed to all of the computers on the network.  Bitcoin ownership is followed in the Bitcoin Blockchain through Bitcoin addresses.  

Blockchain architecture is being used to explore non-cryptocurrency applications.   Here are a few examples of blockchain applications that are currently being used or being considered: fighting art forgers, tracking ownership and royalty payments in the music industry, DeBeers tracking diamonds production cycle, land and marine shipping industries tracking shipments, restaurant loyalty programs, Coca Cola certifying suppliers, clearing of stock trades on the Australian Stock Exchange, Dubi recording local real estate contracts, Hong Kong tracking trade financing and  Kodak looking to protect  registered images or photographs.

Blockchain is also going through an evolution.  Hashgraph, like blockchain, is a distributed ledger, with the expectation that hashgraph could be a faster and cheaper alternative to the blockchain.

Hashgraph, and the similar IOTA and ByteBall, rely on Directed Acyclic Graphs (DAG) to follow transactional information flow. Hashgraph’s DAG records information in a timed series. This means that the record of each transaction is dependent upon the order of all the previous transactions in the series.

This also means this new network can efficiently operate with massive amounts of data such as stock trades. It is the users of the DAG that confirm transactions for one another instead of relying on blockchains outside “miners.” Bitcoin blockchain requires transactions to be assembled in 1 megabyte blocks which can throttle back its capability of handling large amounts of data quickly. Hashgraph’s ledger does not bundle transactions which increases its efficiency and speed. Since every hashgraph user participates in confirming transactions, transactions are processed as they come in, meaning large amounts of data can be processed in a second compared to the Bitcoin blockchain which is much slower.

With the learning curve and technology enhancements for cryptocurrencies and distributed ledgers increasing at an exponential rate with no sign of slowing down means that it is important for investors and traders to stay up-to-date by monitoring the evolution of these markets.

 

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

Dan at Gramza is President of Gramza Capital Management Inc. and DMG Advisors, LLC. He provides daily market updates from around the globe on subjects ranging from the Nasdaq and currencies to crude oil and grains dangramza.com.

 

Click here to learn more about Gramza Capital Management, Inc.

 

This article is from Dan Gramza and is being posted with Dan Gramza’s permission. The views expressed in this article are solely those of the author and/or Dan Gramza and IB is not endorsing or recommending any investment or trading discussed in the video. This material is for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IB to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

 

These financial products are not suitable for all investors and customers should read the relevant risk warnings before investing. You should be aware that your losses may exceed the value of your original investment.
TRADING IN BITCOIN FUTURES IS ESPECIALLY RISKY AND IS ONLY FOR CLIENTS WITH A HIGH RISK TOLERANCE AND THE FINANCIAL ABILITY TO SUSTAIN LOSSES.
More information about the risk of trading Bitcoin products can be found on the IB website.


17912




1 2 3 4 5 2 15

披露

感谢您给予我们反馈。如您有任何关于IB量化博客的问题或意见,请联系ibkrquant@ibkr.com

IBKR量化博客上提供的材料(包括文章与点评)仅供参考。此等材料不代表盈透证券(IB)推荐您或您的客户采用任何在IBKR量化博客上发布内容的独立顾问、对冲基金或其他实体之服务或在此类个人/实体处投资,也不代表盈透证券推荐您在任何顾问或对冲基金处投资。在IBKR量化博客上发布内容的顾问、对冲基金及其他分析师独立于IB,IB不就此等顾问、对冲基金及其他个人/实体的历史及未来业绩、或其提供的信息的准确性做任何称述与担保。盈透证券不会开展“适合性审查”来确保任意顾问、对冲基金或其他个人/实体的交易适合您。

IB量化博客上提及的证券或其他金融产品并不适合所有投资者。发布的材料并未考虑您特定的投资目标、财务状况或需求,也不构成任意证券、金融产品或策略之推荐。在进行任意投资或交易前,您应考虑该投资或交易是否适合您特定的情况,且在必要时寻求专业建议。过去的业绩不保证将来的业绩。

任何第三方提供的信息均来自我们认为可靠及准确的来源;然而,IB不担保其准确性且不对任何错误或遗漏承担责任。

盈透或其关联公司的雇员发布的任何信息基于一般认为的真实可信的信息。然而,IB及其关联公司无法保证信息的完整性、准确性或适当性。IB不对任何金融产品过去或将来的表现作出任何声明或担保。在IB量化博客上发布内容不代表IB认为特定的金融产品或交易策略适合您。