HarryLudemann / Trader-Python

Python trader project to find the best design and structure for back testing, running and creating algorithms in a fast development pythonic way

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Trader

Python Tests

Module to easily write, run and back test algorithms against current list of data sources below.

Install Setup:

pip install trader-python

Functions:

Name File Function
backtest trader Given algorithm object backtest's getting information and passing to objects on_data, returns nothing
run trader Given algorithm object run's getting information and passing to objects on_data, returns nothing

Data sources

  • Yfinance: 1m, 5m, 15m, 30m, 60m, 1d, 1w, 1m (Back testing doesn't use start/end date when using a minute interval (1m, 5m, 15m) auto set to get last 7 days of info)
  • Alpha Vantage (requires API_KEY var): 1m, 5m, 15m, 30m, 60m, 1d, 1w, 1m (Back testing doesn't use start and end dates)

Creating Algorithm Classes:

Algorithm(StockAlgorithm):

Init Variables:
Used to set fields
**Name**  =                     # Name of algorithm     
**Symbol** =                    # Stock Ticker     
**StartDate** =                 # Start Date for algorithm (Some data sources don't use)     
**EndDate** =                   # End Date for algorithm (Some data sources don't use)     
**Cash** =                      # Cash allowed for algorithm to use     
**Data_Source** =               # data Source for stock information (Check data sources)     
**API_KEY** =                   #if data source requires api key
**Adjusted** =                  # Wether to use Adjusted data (Some data sources don't use)  - Default False     
**Interval** =                  # Interval for data eg 1m, 5m, 1d, 1m     
**Back_Test** =                 # Wether to the strategy is to back test or to run  - Default False     
Example:
from trader import StockAlgorithm, run
from datetime import datetime

class Algorithm(StockAlgorithm):
    """ Example Algorithm to Run """

    def init(self):
        """ method to set variables """
        self.Active =True
        self.Name = "Example Algo"
        self.Symbol = "TSLA"
        self.StartDate = datetime.now().strftime("%Y-%m-%d") # current time
        self.Cash = 100000
        self.Data_Source = 'yfinance'
        self.Adjusted = False
        self.Interval = "1m"

    def on_data(self, data):
        """ Method passed df of single line, method called on new data """
        print(data)
        
    def stats(self):
        """ Method Called when finished running or back testing, passed nothing """
        print(self.Name, 'Finished with', self.Cash)


test_algo = Algorithm()
test_algo.init()

run([test_algo])

Algorithm(ForexAlgorithm):

Init Variables:
Used to set fields
Active =                        # Signal wether to run/backtest
Name =                          # Name of algorithm
From_Currency =                 # From Currency Ticker
To_Currency =                   # To Currency Ticker
StartDate =                     # Start Date for algorithm (Some datasources don't use)
EndDate =                       # End Date for algorithm (Some datasources don't use)
Cash =                          # Cash allowed for algorithm to use
data_Source =                   # data Source for stock infomation (Check data sources)
**API_KEY** =                   #if data source requires api key
Interval =                      # Interval for data eg 1m, 5m, 1d, 1m
Example:
from trader import ForexAlgorithm, backtest
from datetime import datetime

class Algorithm(ForexAlgorithm):
    """ Example Algorithm to Run """

    def init(self):
        """ method to set variables """
        self.Active =True
        self.Backtest = True
        self.Name = "Example Algo"
        self.From_Currency = "USD"
        self.To_Currency = "NZD"
        self.StartDate = datetime.now().strftime("%Y-%m-%d") # current time
        self.Cash = 100000
        self.Data_Source = 'yfinance'
        self.Adjusted = False
        self.Interval = "1m"

    def on_data(self, data):
        """ Method passed df of single line, method called on new data """
        print(data)
        
    def stats(self):
        """ Method Called when finished running or back testing, passed nothing """
        print(self.Name, 'Finished with', self.Cash)


test_algo = Algorithm()
test_algo.init()

backtest([test_algo])

Development:

Adding data sources

example function name: get_{API}_{Stock/Forex/Crypto}
returns df:

                      open   high    low  close
Datetime
2021-08-06 21:28:00  1.426  1.426  1.426  1.426
2021-08-06 21:27:00  1.426  1.426  1.426  1.426
2021-08-06 21:26:00  1.426  1.426  1.426  1.426
2021-08-06 21:25:00  1.426  1.426  1.426  1.426
2021-08-06 21:24:00  1.426  1.426  1.426  1.426
2021-08-06 21:23:00  1.426  1.426  1.426  1.426
2021-08-06 21:22:00  1.426  1.426  1.426  1.426
2021-08-06 21:21:00  1.426  1.426  1.426  1.426
2021-08-06 21:20:00  1.426  1.426  1.426  1.426
2021-08-06 21:19:00  1.426  1.426  1.426  1.426

Tests:

Name File Function
test_stock_algorithm Test/test_algorithm.py Tests stock algorithm class instantiates with appropriate fields
test_forex_algorithm Test/test_algorithm.py Tests forex algorithm class instantiates with appropriate fields

About

Python trader project to find the best design and structure for back testing, running and creating algorithms in a fast development pythonic way


Languages

Language:Python 100.0%