Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/prem07a/sma_backtesting
This Python Code can do SMA back testing on a particular stock and also give optimzed SMA statergy.
https://github.com/prem07a/sma_backtesting
backtesting-trading-strategies python simple-moving-average stock-market
Last synced: 23 days ago
JSON representation
This Python Code can do SMA back testing on a particular stock and also give optimzed SMA statergy.
- Host: GitHub
- URL: https://github.com/prem07a/sma_backtesting
- Owner: Prem07a
- License: mit
- Created: 2023-02-18T13:06:38.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-04T16:54:06.000Z (about 1 year ago)
- Last Synced: 2023-11-05T05:25:17.685Z (about 1 year ago)
- Topics: backtesting-trading-strategies, python, simple-moving-average, stock-market
- Language: Python
- Homepage:
- Size: 323 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SMA_Backtesting
This is a Python back tester for a simple moving average (SMA) crossover trading strategy. The backtested calculates the performance metrics of the strategy and visualizes the results.## REQUIREMENTS:
To use this backtest, you need:
1. A folder named data in the same location as your Python code file.2. A CSV file named stock.csv is in the data folder. The file should contain at least two columns: timestamp and close. The timestamp column should contain dates in the format yyyy-mm-dd and the close column should contain the closing prices of the stock.
## INSTALLATION:
To use this back tester, simply download the SMABacktester.py file and save it in the same location as your Python code file. Then, import the SMABacktester class in your code.
or
else clone the repo:
```
git clone https://github.com/Prem07a/SMA_Backtesting
```
Make a new folder inside in the same location as that of SMA_Backtesting and name it as dataAdd the stock.csv file to that folder
### Note:
You can add any stock data just put it in the data folder and name it stock.csv
## USAGEImport the SMABACKTESTER:
```
from SMABacktester import SMABacktester
```
To use the back tester, create an instance of the SMABacktester class and pass the following parameters:* symbol: the stock symbol to be backtested
* SMA_S: the short-term moving average window size
* SMA_L: the long-term moving average window size
* start: the start date of the backtesting period (format: 'yyyy-mm-dd')
* end: the end date of the backtesting period (format: 'yyyy-mm-dd')## Available Method:
The SMABacktester class has the following methods:
get_data(): retrieves the stock price data from the stock.csv file and calculates the logarithmic returns
prepare_data(): calculates the short-term and long-term moving averages
set_parameters(SMA_S=None, SMA_L=None): updates the short-term and/or long-term moving average window sizes
test_strategy(): backtests the strategy and calculates the performance metrics
plot_results(): visualize the stock price, cumulative returns, and cumulative strategy returns
optimize_parameters(SMA_S_range, SMA_L_range): find the optimal short-term and long-term moving average window sizes by exhaustively testing all combinations
## EXAMPLE USAGE:```
backtester = SMABacktester(symbol='SBI', SMA_S=50, SMA_L=200, start='Any', end='Any') *Note- Select date as per the data
backtester.test_strategy()
backtester.plot_results()
```* Note: This is only for Educational Purpose.