03 December 2019

Calculation of Average True Range (ATR)

Average True Range (ATR) is a Technical Indicator that measures Market Volatiity. This indicator was developed by Welles Wilder

From Statistics , we know that Range in Trading refers to the (High - Low) for the timeperiod/candlestick in concern. Wilder has started a Concept caled True Range which is defined as the Greater of the following:
  • Current High minus the current Low
  • Absolute Value of Current High minus the previous Close
  • Absolute Value of Current Low minus the previous Close
For more details on ATR, check out Investopedia

Welles Wilder used the Absolute Values as he was interested in measuring the distance between two points, not the direction.

Take a look at the Google Sheet for Calculation of ATR. I have used GOOGLEFINANCE function to retreive the OHLCV of NIFTY for the first quarter of 2019. The GOOGLEFINANCE function is actually what makes Google Sheets such a compelling use. In due course I intend to publish a post to retreive OHLCV data for 1 minute candlestick from Google Sheets , on that can be used with Pandas DataFrame. In my opinion, once we create such an API, it is a better alternative to Quandl or Alpha Vantage or Quantra Blueshift or Twelve Data API for Backtesting. It also gives us the opportunity to do Live Testing of a Trading System with Live Data , but without risking actual Money. But more on that later on .... Here is the Google Sheet for now :

https://docs.google.com/spreadsheets/d/13pKpTZuDgPlb7jqH2Pyp8ypo7G97NKgV1YIgJ4j4toI/edit#gid=1308165568

The calculation in the above google sheet is based on the standards as per StockCharts

Now lets copy the Values in the Excel We can copy the OHLCV data from the above google sheet and calculate the rest of the data. If you do it n a Excel spreadsheet, you will see that the ATR obtained from the excel is the same as the ATR obtained from Google Sheet. Now let us convert the excel to csv and use this csv file as the Input for our Python program.

15 October 2019

Create a Virtual Environment With Anaconda

Background:

Sooner than latter we will have to setup a Virtual Environment in Python. And this blog post is an endeavour to provide a method to create a virtual environment using Conda Package Manager for the Anaconda installation. Pls. note that I am running Anaconda on Windows 10 64 bit.


Why Create a Virtual Environment ?

Sometimes we may be running different projects on our computer with each project using its own version of different packages. As an example, I have a project that uses quantopian Zipline which needs Python 3.5 but the present version of anaconda download is using Python 3.7. So trying to run this project in Python 3.7 will not work. Another project was developed on Python 3.6 and Selenium 3.4.3 which supports Firefox browser automation and this will require its own environment. If I try to run the code of the first project in the environment of the second project and vice-versa it will not run.
Essentially , the virtual environment keep the dependencies in a separate Sandbox where we can run them without any conflicts. To give a rough analogy, they are analogous to a Docker Container in DevOps.


Using Conda Manager:

Venv is the default from Python 3.3 onwards. And Anaconda is a python distribution, with installation and package management tools. Since I am using Anaconda, I prefer to stick to the official Anaconda Package Manager called Conda. This translates to lesser chances of any breakage in the anaconda system. Tip: If you are also using Anaconda, stick to conda wherever and whenever  possible for installation. As an example, if I wanted to install the Technical Indicator Library, Ta-Lib , I can either do a pip install or I can search the anaconda cloud for Ta-Lib . It is always better to go with the latter as it can be installed with Conda.

Now lets Create the Conda Virtual Environment with:
conda create -n testenv
where testenv is the name of the new environment.

In the event that we want to setup the environment for running quantopian's zipline which requires Python 3.5, then we can type:
conda create --name testenv python=3.5 -y
The additional ‘-y’ flag essentially informs the command line to say ‘yes’ to all of the prompts that follow. While this is optional, it does save me a little it of a hassle. The -n can also be expanded and replaced with --name