Skip to content

python tool to help in trading stocks and options

License

Notifications You must be signed in to change notification settings

ethanlu/pystonk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pystonk

Python tool that leverages publicly available stock data to do some simple calculations that I often perform when trading stocks and options.

Table of Contents

Prerequisite

This tool uses the following services:

  • Schwab Market Data API : tool's calculations for trades are based on data provided by Schwab
  • Slack Bot : tool runs as a bot in a Slack workspace to allow easier sharing of its information and discussion with others

Schwab Market Data API

We will be leveraging Schwab's Market Data product to access stock and option information:

  • Create an account on their Developer Portal
  • Register an app to get a key and secret. These will need to be exported as PYSTONK_SCHWAB_KEY and PYSTONK_SCHWAB_SECRET
    • The API Product we want to use is Market Data Production
    • The callback URL will not be used in this tool, so just enter any valid URL

Configure Slack Bot

PyStonk runs as a Slack application in a workspace. Once the app is created:

  • In the Basic Information page
  • In the OAuth & Permissions page
    • In the OAuth Tokens section, create a bot oauth token and it will need to be exported as PYSTONK_SLACK_TOKEN:
    • In the Scopes section, the Bot Token Scopes will need the following scopes defined:
      • app_mention:read
      • chat:write
      • commands
  • In the Event Subscriptions page
    • Enable events and set the request endpoint the server that will host the app (see deployments section)
    • In the Subscribe bot to events section, add app_mention as an event with app_mention:read scope
  • In Slash Commands page
    • Create a /pystonk command (or whatever slack command you choose) and set the endpoint url to the server that will host the app (see deployments section)

Environment Variables

The tool will need these required environment variables defined in an .env file

PYSTONK_SCHWAB_KEY={key from schwab app}
PYSTONK_SCHWAB_SECRET={secret from schwab app}
PYSTONK_SLACK_SECRET={signing secret from slack app}
PYSTONK_SLACK_TOKEN={bot oauth token from slack app}

Following are optional environment variables that can be set depending on deployment and needs:

PYSTONK_LOG_LEVEL={amount of logs generated by app (for debugging purposes). defaults to ERROR}
PYSTONK_AWS_ACCOUNT={aws account id when deployed as aws lambda}
PYSTONK_AWS_REGION={aws region when deployed as aws lambda}
NGROK_AUTHTOKEN={token needed to run ngrok when deployed locally}

Deployments

Once Schwab API access and Slack application are all configured, this tool can be deployed to run locally on a machine or as an AWS Lambda service

Local Run

This tool can run locally as a Docker image, but will rely on Ngrok to act as an API gateway into the container:

  • Create an account to get an auth token. This will need to be exported as NGROK_AUTHTOKEN
  • Create an .env file in docker directory of this project and add the required and optional environment variables (see Environment Variables)
  • Build image and run
    # go into docker folder
    cd docker
    
    # assumes .env file exists
    docker-compose build
    docker-compse up -d
  • Check Ngrok dashboard to get the ingress endpoint needed to configure the Slack app
    • For both Events Subscription and Slash Commands configurations, the endpoint will need to be suffixed with /slack/events
    example: https://some-id.ngrok-free.app/slack/events
    

AWS Lambda

This tool can also be deployed as an AWS Lambda service by leveraging AWS CDK.

  • Create an .env file in aws-cdk directory of this project and add the required and optional environment variables (see Environment Variables)
  • Install AWS CDK CLI:
cd aws-cdk

# install the tool within a virtual environment to avoid polluting global environmentm
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

# cdk commands are now installed and assumes .env file exists
cdk synth

# assumes aws profile is configured
cdk --profile default deploy

Customizations

  • The tool will respond to commands using an emoji selected randomly from a list for successful and failed commands. These can be customized as a space-delimited string of emoji labels separated into:
    • PYSTONK_SLACK_SUCCESS_EMOJIS
    • PYSTONK_SLACK_FAIL_EMOJIS
# example
PYSTONK_SLACK_SUCCESS_EMOJIS=":+1: :clap: :pray:"
PYSTONK_SLACK_FAIL_EMOJIS=":middle-finger: :poop:"

TODO

  • More slack commands for helping with trades
  • Kivy desktop app

About

python tool to help in trading stocks and options

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages