def overall_most_profitable(cryptosymbol, window_length, min_or_max): create_directory( f'C:/Users/adam/Desktop/tradeBOT/{cryptosymbol}_data/simulation/{window_length}_days_window/{min_or_max}' ) directory = f'C:/Users/adam/Desktop/tradeBOT/{cryptosymbol}_data/simulation/{window_length}_days_window/{min_or_max}' most_profitable_filename = None # Declaration of variable highest_lose = None # Declaration of variable most_profitable_filename_list = [] most_profitable_list = [] highest_lose_list = [] for filename in os.listdir(directory): path = os.path.join(directory, filename) if os.path.isfile(path): print(os.path.join(directory, filename)) data = pd.read_parquet( f'C:/Users/adam/Desktop/tradeBOT/{cryptosymbol}_data/simulation/{window_length}_days_window/{min_or_max}/{filename}' ) profit = data['balance'].sum() most_profitable_filename = filename.replace('2o', '').replace('1o', '') print(most_profitable_filename) highest_lose = data.values.min(axis=0) most_profitable_filename_list.append(most_profitable_filename) most_profitable_list.append(profit) highest_lose_list.append(highest_lose.round(2)) return { 'most_profitable_filename': most_profitable_filename_list, 'most_profitable': most_profitable_list, 'highest_lose': highest_lose_list }
def simulation(main_file, file_with_dates, amount_of_crypto, after_how_many_days_sell, cryptosymbol): datas = pd.read_parquet(main_file) datas.reset_index(inplace=True) when_to_buy = pd.read_parquet(file_with_dates) bought_crypto_value = 0 list_of_when_to_buy = when_to_buy['Dates'].tolist() datas.set_index(['t'], inplace=True) print(datas) balance = [] initial_date = [] close_date = [] for date in datas.index: #print(data) if date in list_of_when_to_buy: #print(f'Data {data}') try: #print(bought_crypto_value) bought_crypto_value = datas.loc[date]['o'] * amount_of_crypto #print(bought_crypto_value) after_some_days = date + timedelta( days=after_how_many_days_sell) initial_date.append(date.strftime("%Y-%m-%d %H:%M:%S")) close_date.append(str(after_some_days)) print(date) #print(close_date) eventual_balance = (datas.loc[after_some_days]['o'] * amount_of_crypto) - bought_crypto_value balance.append(eventual_balance) #print(eventual_balance) except Exception as e: print(f'error found: {e}') print(balance) print(initial_date) print(close_date) create_directory( f'C:/Users/adam/Desktop/tradeBOT/{cryptosymbol}_data/simulation/7_days_window/min/dates' ) initial_datedf = pd.DataFrame(initial_date, columns=[ 'initial_date' ]).to_parquet( f'C:/Users/adam/Desktop/tradeBOT/{cryptosymbol}_data/simulation/7_days_window/min/dates/initial_dates{amount_of_crypto}o{after_how_many_days_sell}.pq' ) close_datedf = pd.DataFrame(close_date, columns=['close_date']).to_parquet( f'C:/Users/adam/Desktop/tradeBOT/{cryptosymbol}_data/simulation/7_days_window/min/dates/end_dates{amount_of_crypto}o{after_how_many_days_sell}.pq' ) eventualdf = pd.DataFrame(balance, columns=['balance']) create_directory( f'C:/Users/adam/Desktop/tradeBOT/{cryptosymbol}_data/simulation/7_days_window/min' ) eventualdf.to_parquet( f'C:/Users/adam/Desktop/tradeBOT/{cryptosymbol}_data/simulation/7_days_window/min/{amount_of_crypto}o{after_how_many_days_sell}' ) print(eventualdf)
def concatenate_into_full_data(balance_directory, initial_dates_directory, end_dates_directory, full_data_path): """Function concatenates dates and balances into one file ----------------------- full_data_path should be a path to store new concatenated files """ create_directory(full_data_path) for filename in os.listdir(balance_directory): file_directory = balance_directory + filename try: df = pd.read_parquet(f'{end_dates_directory}/end_dates{filename}.pq') fd = pd.read_parquet(f'{initial_dates_directory}/initial_dates{filename}.pq') fdd = pd.read_parquet(f'{balance_directory}/{filename}') except FileNotFoundError as e: break full_data = pd.concat([fdd['balance'], fd['initial_date'], df['close_date']], axis=1) full_data.dropna(inplace=True) full_data.to_parquet(f'{full_data_path}/full_data{filename}.pq')
def search_for_min_max(window_length: int, absolute_path: Path, cryptosymbol, min_or_max) -> list: window_length = window_length * 1440 # Translate window_length to minutes data = pd.read_parquet(absolute_path) prices = data["o"].tolist() min_max_list = [] counter = 0 while True: if counter > len(prices): break if counter + 1 > len(prices): break if counter <= window_length: min_max_list.append(False) else: if min_or_max == 'min': min_max_value = min(prices[counter - window_length:counter + 1]) else: min_max_value = max(prices[counter - window_length:counter + 1]) print(f'Searching in range {counter-window_length} to {counter}') if prices[counter] == min_max_value: min_max_list.append(True) else: min_max_list.append(False) counter += 1 if True in min_max_list: print('jest') #print(min_list) df = pd.DataFrame(min_max_list) df.columns = ['Value'] create_directory( f'C:/Users/adam/Desktop/tradeBOT/{cryptosymbol}_data/model_{window_length//1440}_days_{min_or_max}' ) df.to_parquet( f'C:/Users/adam/Desktop/tradeBOT/{cryptosymbol}_data/model_{window_length//1440}_days_{min_or_max}/{min_or_max}_{cryptosymbol}_tf.pq' ) print( pd.read_parquet( f'C:/Users/adam/Desktop/tradeBOT/{cryptosymbol}_data/model_{window_length//1440}_days_{min_or_max}/{min_or_max}_{cryptosymbol}_tf.pq' ))
import pandas as pd import numpy as np import alpaca_trade_api as tradeapi import json import websocket from config import * from download_price_data import create_directory create_directory(r'C:\Users\adam\Desktop\tradeBOT\AAPL_data\current_data') def on_message(ws, message): jsonObject = json.loads(message) #print(jsonObject) try: df = pd.DataFrame(jsonObject['data']) #print(df) df.to_parquet('C:/Users/adam/Desktop/tradeBOT/AAPL_data/current_data/temporary.pq') except Exception as e: print(e) df = pd.read_parquet('C:/Users/adam/Desktop/tradeBOT/AAPL_data/current_data/temporary.pq') price = df['p'][0] time = df['t'][0] df.to_parquet(f'C:/Users/adam/Desktop/tradeBOT/AAPL_data/current_data/{time}') print(df) print(price) print(time) return {'price': price, 'time': time} #x = pd.read_parquet(r'C:\Users\adam\Desktop\tradeBOT\AAPL_data\current_data\1621503523189') #print(x)