GithubHelp home page GithubHelp logo

prem07a / sma_backtesting Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 331 KB

This Python Code can do SMA back testing on a particular stock and also give optimzed SMA statergy.

License: MIT License

Python 100.00%
backtesting-trading-strategies python simple-moving-average stock-market

sma_backtesting's Introduction

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 data

Add 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

USAGE

Import 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.

sma_backtesting's People

Contributors

prem07a avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

aditya-dhodade

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.