Giter Site home page Giter Site logo

pyx's Introduction

PYX (Python Exchange)

A program that simulates real-time stock trading using a basic mean reversion algorithm.

What is PYX?

PYX is a flexible program that simulates the trading of equity using different algorithms. The algorithm currently being used is a mean reversion strategy. PYX buys and sells shares of stock based on the price compared to its upper and lower bollinger bands.

What PYX does:

  1. Creates a database (SQLAlchemy) for storing each transaction and wallet
  2. Creates a wallet from which simulated funds will be added to and subtracted from
  3. Calculates the 50 day exponential moving average for specified stock
  4. Calculates the upper and lower bollinger bands based on the 50 day EMA
  5. Checks if the price of specified stock is >= or <= the upper and lower bollinger bands respectively
  6. If the share price is <= the lower bollinger band, the program will buy shares
  7. If the share price is => the upper bollinger band, the program will sell previously bought shares
  8. Each transaction is logged in a database transaction_database.db with the following information: id|stock|symbol|buy/sell|price|ema|shares|date

How does it work?

Mean Reversion:

Mean reversion is the theory suggesting that prices and returns eventually move back toward the mean or average. This mean that PYX utilizes is called a 50 day exponential moving average.

Dependencies:

PYX relies on two main dependencies, yahoo_finance and sqlalchemy. PYX also utilizes Python 3.5's built in datetime module.

To install these modules with PyPI, first install pip then in cmd or bash run:

pip install yahoo_finance

pip install sqlalchemy

Once the packages have unpacked and installed, the program is good to run.

Calculations:

Equation to calculate the 50 day EMA:

multiplier = 2 / (50 + 1)
EMA = (closePrice - prevEMA) * multiplier + prevEMA

Equation to calculate upper bollinger band:

upperBand = EMA * (1 + .02)

Equation to calculate the lower bollinger band:

lowerBand = EMA * (1 - .02)

Adjust the .02 to raise and lower the bollinger bands. The higher you raise the number to, the less likely it will be stock will be bought.

Example of Bollinger bands:

Bollinger bands

Database:

A database with two tables is created on the first initialization of the program, wallets and transactions. If there is no database transactions_database.db already, one will be created. A wallet containing a name and a balance will also be created and inserted into wallets the first time the program is run. To change the wallets balance simply edit balance='100000' in the main() function.

Everytime shares of a stock are bought and sold a row is added to the transactions database, code below.

new_transaction = Transaction(stock='Apple', symbol=apple.get_info()['symbol'], buy_or_sell='',
                              price=apple.get_price(),
                              ema=EMA, shares='100', time=datetime.datetime.now())
session.add(new_transaction)
session.commit()

To adjust the number of shares being bought, change the shares='100' to however many shares you want the program to buy.

Running PYX on a server

The best way to run PYX and test its capabilites is to run it from a server. By utilizing a time-based job scheduler like Cron you can run PYX every minute, hour, or even day. The more often you run it the lower the standard deviation for each bollinger band should be, this will produce more results.

DISCLAIMER

This program does not spend real money, nor does it buy real shares of stocks. It is simply a simulation.

pyx's People

Watchers

 avatar  avatar

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.