/Simple-MA-crosover-strategy

This project aims to provide a straightforward implementation of a popular trading strategy known as the moving average crossover strategy.

Primary LanguagePython

Simple Moving Average Strategy Project

This project involves building a simple trading strategy using Python and a PostgreSQL database. The strategy used in this project is the simple moving average crossover strategy.

Setup

PostgreSQL Setup

To set up PostgreSQL, follow these steps:

  1. Download and install PostgreSQL on your system.
  2. Open pgAdmin (a tool to manage PostgreSQL databases).
  3. Create a new database by right-clicking on "Databases" in the left panel and selecting "New Database".
  4. Give your database a name and click "Save".
  5. Right-click on your new database and select "Query Tool".
  6. In the query tool, enter the following command to create a new table:
CREATE TABLE stock_data (
    id SERIAL PRIMARY KEY,
    instrument VARCHAR(10),
    date DATE,
    open DECIMAL,
    high DECIMAL,
    low DECIMAL,
    close DECIMAL,
    volume INTEGER
);

Python Setup

To set up Python, follow these steps:

  1. Install Python 3.8 or higher on your system.
  2. Install the required packages by running pip install -r requirements.txt.

Usage

Inserting Data

To insert data into the database, run the following command:


python insert_data.py

This will load the data from the Google Sheets link and insert it into the stock_data table in the PostgreSQL database.

Running the Trading Strategy

To run the trading strategy, run the following command:


python trading_strategy.py

This will load the data from the stock_data table, calculate the 50-day and 200-day moving averages, and create a new column for the position (1 for long, 0 for short) based on the crossover of the moving averages.

Running the Unit Tests

To run the unit tests, run the following command:


python -m unittest discover

This will run all of the unit tests in the tests directory.

Strategy Performance

The performance of the simple moving average crossover strategy can be analyzed by examining the results of the trades.csv file that is generated by the trading_strategy.py script. This file contains a list of trades made by the strategy, including the entry date, entry price, exit date, exit price, and profit/loss.

By analyzing the trades, we can determine the overall profitability of the strategy. It is important to note that past performance is not indicative of future results, and that trading strategies should be thoroughly tested and backtested before being used in a live trading environment.

License

This project is licensed under the MIT License - see the LICENSE file for details.