StatArb / StatBot / Strategy / main_strategy.py
main_strategy.py
Raw
from unicodedata import decimal
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

from func_get_symbols import get_tradeable_symbols
from func_prices_json import store_price_history
from func_cointegration import get_cointegrated_pairs
from func_plot_trends import plot_trends
import pandas as pd
import json

from config_strategy_api import session

from schedule import every, repeat, run_pending
import time

# @repeat(every(10).seconds)
class StatStrategy:
    
    # sym_1 = ''
    
    def __init__(self):
        self.sym_1 = ""
        self.sym_1_price_decimals = 0
        self.sym_1_qty_decimals = 0
        self.sym_2 = ""
        self.sym_2_price_decimals = 0
        self.sym_2_qty_decimals = 0
        self.hedge_ratio = 0
    
    def perform_strategy(self):
    # """STRATEGY CODE"""
    #if __name__ == "__main__":

        # STEP 1 - Get list of symbols
        print("Getting symbols...")
        sym_response = get_tradeable_symbols()

        # STEP 2 - Construct and save price history
        print("Constructing and saving price data to JSON...")
        if len(sym_response) > 0:
            store_price_history(sym_response)

        # STEP 3 - Find Cointegrated pairs
        print("Calculating co-integration...")
        with open("1_price_list.json") as json_file:
            price_data = json.load(json_file)
            if len(price_data) > 0:
                coint_pairs = get_cointegrated_pairs(price_data)

        # Extract the top ranked cointegrated pairs and calculate decimal points of their price and qty 
        self.sym_1 = coint_pairs.iloc[0,0]
        self.sym_2 = coint_pairs.iloc[0,1]

        sym_1_rec = session.public_trading_records(symbol=self.sym_1,limit=500)
        sym_1_price = sym_1_rec["result"][0]["price"]
        sym_1_qty = sym_1_rec["result"][0]["qty"]
        sym_2_rec = session.public_trading_records(symbol=self.sym_2,limit=500)
        sym_2_price = sym_2_rec["result"][0]["price"]
        sym_2_qty = sym_2_rec["result"][0]["qty"]

        self.hedge_ratio = coint_pairs.iloc[0,5]
        # print(sym_1, sym_2)
        # print(sym_1_price, type(sym_1_price))
        # print(sym_1_qty, type(sym_1_qty))
        self.sym_1_price_decimals = str(sym_1_price)[::-1].find('.')
        self.sym_1_qty_decimals = max(str(sym_1_qty)[::-1].find('.'),0)
        self.sym_2_price_decimals = str(sym_2_price)[::-1].find('.')
        self.sym_2_qty_decimals = max(str(sym_2_qty)[::-1].find('.'),0)

        print("\nsym1 price & qty: ",sym_1_price, sym_1_qty)

        print("\nsym2 price & qty: ",sym_2_price, sym_2_qty)

        # STEP 4 - Plot trends and save for backtesting
        print("Plotting trends...")
        symbol_1 = self.sym_1
        symbol_2 = self.sym_2
        with open("1_price_list.json") as json_file:
            price_data = json.load(json_file)
            if len(price_data) > 0:
                plot_trends(symbol_1, symbol_2, price_data)

        # return (sym_1, sym_1_price_decimals, sym_1_qty_decimals, sym_2, sym_2_price_decimals, sym_2_qty_decimals)
# def perform_strategy():
#     """STRATEGY CODE"""
#     #if __name__ == "__main__":

#     # STEP 1 - Get list of symbols
#     print("Getting symbols...")
#     sym_response = get_tradeable_symbols()

#     # STEP 2 - Construct and save price history
#     print("Constructing and saving price data to JSON...")
#     if len(sym_response) > 0:
#         store_price_history(sym_response)

#     # STEP 3 - Find Cointegrated pairs
#     print("Calculating co-integration...")
#     with open("1_price_list.json") as json_file:
#         price_data = json.load(json_file)
#         if len(price_data) > 0:
#             coint_pairs = get_cointegrated_pairs(price_data)

#     # Extract the top ranked cointegrated pairs and calculate decimal points of their price and qty 
#     sym_1 = coint_pairs.iloc[0,0]
#     sym_2 = coint_pairs.iloc[0,1]

#     sym_1_rec = session.public_trading_records(symbol=sym_1,limit=500)
#     sym_1_price = sym_1_rec["result"][0]["price"]
#     sym_1_qty = sym_1_rec["result"][0]["qty"]
#     sym_2_rec = session.public_trading_records(symbol=sym_2,limit=500)
#     sym_2_price = sym_2_rec["result"][0]["price"]
#     sym_2_qty = sym_2_rec["result"][0]["qty"]
#     # print(sym_1, sym_2)
#     # print(sym_1_price, type(sym_1_price))
#     # print(sym_1_qty, type(sym_1_qty))
#     sym_1_price_decimals = str(sym_1_price)[::-1].find('.')
#     sym_1_qty_decimals = max(str(sym_1_qty)[::-1].find('.'),0)
#     sym_2_price_decimals = str(sym_2_price)[::-1].find('.')
#     sym_2_qty_decimals = max(str(sym_2_qty)[::-1].find('.'),0)

#     print("\nsym1 price & qty: ",sym_1_price, sym_1_qty)

#     print("\nsym2 price & qty: ",sym_2_price, sym_2_qty)

#     # STEP 4 - Plot trends and save for backtesting
#     print("Plotting trends...")
#     symbol_1 = sym_1
#     symbol_2 = sym_2
#     with open("1_price_list.json") as json_file:
#         price_data = json.load(json_file)
#         if len(price_data) > 0:
#             plot_trends(symbol_1, symbol_2, price_data)

#     return (sym_1, sym_1_price_decimals, sym_1_qty_decimals, sym_2, sym_2_price_decimals, sym_2_qty_decimals)