Quantitative trading on cryptocurrency market Q2

This is the second chapter of a learning process that started last September.

Second Quarter

The second step is defined for the next 3 months, where the main goal is to define a specific strategy of quantitative trading and work on it with real money on crypto currency market.

Following the V2MOM model:

  • Vision: Have a strategy running in crypto currency market.
  • Values: have fun, learn a lot, build a team with Dani, do practices and more practices.
  • Method: learn about quantitative trading principles and patterns, understand the basis of how blockchain markets work.
  • Obstacles: Time.
  • Measures: have an account with money and making short/long decisions. Have a lot of data related to the backtesting of different versions of a specific strategy. Accumulate a list of learned lessons and experiences for blockchain, quantitative trading and python.

Death line = March 2018

Results (xx April 2018)

  • Time to be accountable

Hello world in quantitative trading

Since I started on this field the focus has been to be able to build a set of

I found this training course with the slides posted, where Michael Halls-Moore explains the basis of the backtesting with Python and Pandas.

Things to remind

  • Taxonomy of trading strategies: forecasting, mean reversion, momentum and high frequency trading.
  • Review of the backtesting pitfalls: the same ones that mentioned by Ernie Chang.
  • Things to do after you perform the moving average crossover strategy:
    • Multi-symbol portfolio.
    • Risk management (the most important topic).
    • True event-driven backtesting, realistic handling of transaction costs, fees…

Moving Average Crossover Strategy

this is defined as the “Hello World” of quantitative trading. It’s included a specific article,

Exponentially weighted moving average (EWMA)

EWMA is a type of infinite impulse response filter that applies weighting factors which decrease exponentially. The weighting for each older datum decreases exponentially, never reaching zero.

Code written in Python 2

The proposed code is in python 2, but here you can find a guy that adapted it to python3. It uses Quandl library for obtaining financial data easily.

 

 

Python, Good morning

Good morning is the name of a library that enables you to download data from stocks related to fundamentals.

I have uploaded the GIT file on my eclipse and I have shown some basic data in a data frame. This was something initially easy.

My goal is to draw data in a monthly basis for these fundamentals and be able to compare with other datasets. By the moment I’m not able to show data by at least in a monthly basis.

The code of my basic test:

import good_morning as gm

kr = gm.KeyRatiosDownloader()
frames = kr.download(‘T’)

count = 0
frame_size = len(frames)
while (count < frame_size):
         print( ‘The count is:’, count)
print(frames[count])

count = count + 1

print(“Good bye!”)

Quantitative trading strategy: Pandas and matplotlib

Start from basis is important to me to understand how to handle basic data and start having real contact with the data and the code.

I found this tutorial very useful for these purposes. The use of Pandas for reading data from yahoo, google… and matplotlib to build an easy chart is key to take the first steps. The best of the tutorial are the comments about each step.

Problems I have found

Yahoo has closed the API that enabled Pandas to retrieve data. My colleague has found a workaround to continue using the API. The solution consists on:

  1. Add with pip the library: fix_yahoo_finance.
  2. Add these 2 lines to the code:

import fix_yahoo_finance as yf

yf.pdr_override() # <== that’s all it takes 🙂

This video is a must see to me as Jev Kuznetsov explains it from scratch.

The first code I performed was this one:

from pandas_datareader import data
import pandas as pd
import matplotlib.pyplot as plt

# Define the instruments to download. We would like to see Apple, Microsoft and the S&P500 index.
tickers = [‘T’, ‘VZ’, ‘SPY’]

# Define which online source one should use
data_source = ‘google’

# We would like all available data from 01/01/2000 until 12/31/2016.
start_date = ‘2015-01-01’
end_date = ‘2017-10-10’

# User pandas_reader.data.DataReader to load the desired data. As simple as that.
panel_data = data.DataReader(tickers, data_source, start_date, end_date)

# Getting just the adjusted closing prices. This will return a Pandas DataFrame
# The index in this DataFrame is the major index of the panel_data.
close = panel_data.ix[‘Close’]

# Getting all weekdays between 01/01/2000 and 12/31/2016
all_weekdays = pd.date_range(start=start_date, end=end_date, freq=’B’)

# How do we align the existing prices in adj_close with our new set of dates?
# All we need to do is reindex close using all_weekdays as the new index
close = close.reindex(all_weekdays)
#print(close.head(-100))
print(close.describe())
close = close.fillna(method=’ffill’)
# Get the MSFT time series. This now returns a Pandas Series object indexed by date.
vz = close.ix[:, ‘VZ’]
spy = close.ix[:, ‘SPY’]
# Calculate the 20 and 100 days moving averages of the closing prices
short_rolling_vz = vz.rolling(window=20).mean()
long_rolling_vz = vz.rolling(window=100).mean()

# Plot everything by leveraging the very powerful matplotlib package
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(vz.index, vz, label=’VZ’)
#ax.plot(spy.index, spy, label=’SPY’)
ax.plot(short_rolling_vz.index, short_rolling_vz, label=’20 days rolling’)
ax.plot(long_rolling_vz.index, long_rolling_vz, label=’100 days rolling’)
ax.set_xlabel(‘Date’)
ax.set_ylabel(‘Closing price ($)’)
ax.legend()
plt.show()

Pydev with Eclipse

I find EMACS too much complex for me. I want to concentrate on the analysis of data with Pyhton, so I have looked for and alternative IDE for Python: Pydev.

The steps I have followed are:

  1. Download and install Eclipse (in reality I had it on my computer).
  2. Then, download, install and test Phyton.
  3. Once done, download and install Jython.
  4. Download and install IronPython.
  5. Install PyDev.
  6. Create a new PyDev project.
  7. I have created a hello.py file with print(“hello world”), then I have debugged it and it worked.
  8. Create a package to gather all the information.
  9. The set-up of the environment is very well explained. You first have to configure the interpreter, then you can develop all you need. In my case I’m starting importing some finance data from Yahoo Finance.
  10. Using Windows>Properties>Python Interpreter , install library “numpy” using “pip” as installer. This step is crucial to me as Python interpreter just work on PyDev in my computer.

EMACS

I’m sure EMACS runs faster and it’s more efficient than Eclipse, but the point is that now I can concentrate on the development of the scripts I want to implement. 🙂

Zipline, for trading simuation

Zipline is an open-source algorithmic trading simulator written in Python.

I have installed Zipline library with pip, in my case, as I’m using PyDev I went to Window>Preferences>Python Interpreter>Install/Uninstall with pip

Once I have tried to use it, I was not able to find the quick way to build the basic code I wanted, so I finally has uninstalled it as with numpy and pandas I have enough.

So I was not able to understand the power of this library, by the moment I do not need it.

EMACS, how to install it for Python purposes

What IDE should I start with?

I have asked about some different IDEs for Python and this was the short list: EMACS, jupyter, syder and anaconda.

Then, I asked my friend: what is the more convenient for a basic learner as me? EMACS.

So this is the one I have installed. The basis of how to use EMACS can be read here. The guidelines recommends the installation of Elpy so this is the next step I have done.

Elpy – Python Development

Emacs is distributed with a python-mode (python.el), but if we want to have a more sophisticated IDE you can install Elpy (Emacs Lisp Python Environment) package.

Before to install Elpy, you have to install these 2 packages:

  • Flake8: flymake-python-pyflakes.
  • Jedi.

The document I used to install it is this one.

The issue I’m finding is that the these 2 packages are legacy and they seem to not work properly. I’m sure that in reality the issue is that I’m not able to enable them in the right way or to enable python properly. I’m stuck here by the moment.

Python extensions

Some basic notes about the python files extensions:

  • .py: This is normally the input source code that you’ve written (the basis).
  • .pyc: This is the compiled bytecode. If you import a module, python will build a *.pyc file that contains the bytecode to make importing it again later easier (and faster).
  • .pyo: This is a *.pyc file that was created while optimizations (-O) was on.
  • .pyd: This is basically a windows dll file. http://docs.python.org/faq/windows.html#is-a-pyd-file-the-same-as-a-dll

EMACS basic commands I have learned today.

  • M-x list-packages: list the available packages you have in EMACS
  • M-x customize-group: enable you to customize a package (in my case: “package”. I have added Melpa packages to the list so these packages can be installed.
  • M-x package-install: to perform the installation of a package.

This video shows you how to install a package: .Emacs #3 – Installing Packages and Extensions. The series of videos are useful for new users as me.

Intro to Python for Data Science

DataCamp offers a set of courses oriented on programming and da ta scientist. I found some basic courses about python that have helped me to understand how the language works and practice with a good balance of theory and exercise.

Intro to Python for Data Science , is the free course that enables you to understand how basic things work.

I have also done a couple of chapters related to data import and data visualization that helped me to understand how to work on it.

 

Quantitative trading on cryptocurrency market Q1

Closing a chapter

This year, after so many years without PMP certification, I decided to take it, so I have invested 4 months to study, practice and obtain it. After the achieving the PMP certification I though about the next project I would like to focus, so I was thinking about it.

What do I want to do?

After thinking about so many things, I have defined 3 basis of learning that combined should let me learn, these are:

  • Block chain,
  • Quantitative trading,
  • Rescue programing skills.

The overall learning path should be around 2 years.

First Quarter

The first step is defined for the next 3 months, where the main goal is to perform the “hello world” of quantitative trading.

Following the V2MOM model:

  • Vision: perform a strategy based on moving average cross over: including backtesting analysis and comparision with the standard ratio (Sharpe Ratio).
  • Values: have fun, learn a lot, build a team with Dani, do practices and more practices.
  • Method: learn python, learn about quantitative trading principles and patterns, understand the basis of how blockchain markets work.
  • Obstacles: Time.
  • Measures: have an environment where I test a strategy with real time data from an external source. Have a list of learned lessons and experiences for blockchain, quantitative trading and python.

Death line = December 2017

Results (22 December 2017)

  • We created a set of different scripts in Python for testing specific
  • We have a script with a specific strategy that has been tested in different periods. It’s not a good strategy by the way 🙂
  • We have reviewed basic concepts as basic indicators, basic strategies, how to combine indicators, the importance of the habits…
  • We have done an analysis of the different market for crypto-currencies and this enable us to make a list of things to learn for next quarter.
  • Learns about how to manage a portfolio, manage risks and understand the level of exposure.
  • Learn about Python, how to install it, add libraries, code, visualize data, understand how powerfull is the language.
  • We not just did the “hello world” of quantitative trading, we went far beyond.

Now defining the V2MOM for Q1 !!