I have tested golden cross strategy in S&P500 (SPY) and NIFTY_50 using python and backtrader. Also added buy and hold strategy to get the idea of calling strategies from console.
- What is moving average?
- What is Golden Cross strategy?
- Libraries
- Description of each file
- Datasets
- How to use?
A moving averageis a tool used by technical analysts to track the price movements of a security. It plots average prices over a defined period of time, with the moving average typically overlaid onto a candlestick or bar chart. The bars or candlesticks show the price data for each time period.
The golden cross occurs when a short-term moving average (here I have taken 50 days) crosses over a major long-term moving average (here I haven taken 200 days) to the upside and is interpreted by analysts and traders as signaling a definitive upward turn in a market.
- backtrader - It is an open-source framework that allows for strategy testing on historical data.
- pandas - the most powerful and flexible open source data analysis / manipulation tool.
- math - It provides us access to some common math functions and constants in Python.
- os - The OS module in Python provides functions for interacting with the operating system.
- sys - This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available.
- argparse - This tutorial is intended to be a gentle introduction to argparse, the recommended command-line parsing module in the Python standard library.
- moving_average.py - Just an example of calculating moving average.
- main.py - This is the main method of the project where strategy is being executed with the help of backtrader's cerebro method.
- golden_cross.py - Here I have created the golden cross strategy using 50 days as the short term moving average and 200 days as the long term moving average.
- buy_hold.py - A simple strategy, just buy a specific amount of stocks and hold it for a long period.
I have only used S&P500 and NIFTY50 dataset which I ahve added bellow. Other stocks historical data also can be downloaded from yahoo finance.
Finally one can run the project by typing the name of the strategy wants to use:
- Golden Cross
$ python main.py golden_cross
2.Buy and Hold
$ python main.py buy_hold