Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cProfile
- import pstats
- from SQL_new import SQLite as Sql_new
- from test_py import tp_sl_loop_py
- from test_cy import tp_sl_loop
- import pandas as pd
- import numpy as np
- import traceback
- import timeit
- lenght_array = 100
- columns = ['Open_Time', 'Open_Price', 'High_Price', 'Low_Price', 'Close_Price', 'Volume', 'Color']
- pair="asset name"
- directory="Directory to the DataBase"
- def find_min_max(df):
- df['Percent_High'] = df['High_Price'] / df['Open_Price']
- df['Percent_Low'] = df['Low_Price'] / df['Open_Price']
- df_high_max = (df['Percent_High'].max() - 1) * 100
- df_low_max = abs((1 - df['Percent_Low'].min())) * 100
- return df_high_max, df_low_max
- def call_py():
- np.set_printoptions(suppress=True)
- db = Sql_new(directory)
- data = pd.DataFrame(data=db.get_data(), columns=columns)
- data = data[['Open_Price', 'High_Price', 'Low_Price', 'Close_Price']]
- data = data.replace(to_replace='NEUTRAL', value='RED')
- data = data.replace(to_replace='RED', value='r')
- data = data.replace(to_replace='GREEN', value='g')
- high_max, low_max = find_min_max(data)
- data = data[['Open_Price', 'High_Price', 'Low_Price', 'Close_Price']]
- # print(f'High MAX: {high_max}, Low MAX: {low_max}')
- tp_array = np.linspace(0, (high_max + 2 * (high_max / lenght_array)), lenght_array)
- sl_array = np.linspace(0, (low_max + 2 * (low_max / lenght_array)), lenght_array)
- tp_sl_mash = np.array(np.meshgrid(tp_array, sl_array)).T.reshape(-1, 2)
- X, Y, Z, tp_sl_list = tp_sl_loop_py(data.to_numpy(), tp_sl_mash, pair)
- tp_sl_list = np.array(tp_sl_list)
- tp_sl_list = tp_sl_list[tp_sl_list[:, 2].argsort()]
- tp_sl_list = tp_sl_list[::-1]
- # print(f'Top 10: \n{tp_sl_list[0:11]}\n')
- # print(f'Max: {tp_sl_list[-0]}')
- max_index = Z.index(max(Z))
- from_tp, to_tp = (X[(max_index - lenght_array)] / 100), (X[(max_index + lenght_array)] / 100)
- from_sl, to_sl = (Y[(max_index - 1)] / 100), (Y[(max_index + 1)] / 100)
- tp_array_2 = np.linspace(start=from_tp, stop=to_tp, num=lenght_array)
- sl_array_2 = np.linspace(start=from_sl, stop=to_sl, num=lenght_array)
- tp_sl_mash_2 = np.array(np.meshgrid(tp_array_2, sl_array_2)).T.reshape(-1, 2)
- X_2, Y_2, Z_2, tp_sl_list_2 = tp_sl_loop_py(data.to_numpy(), tp_sl_mash_2, pair)
- tp_sl_list_2 = np.array(tp_sl_list_2)
- tp_sl_list_2 = tp_sl_list_2[tp_sl_list_2[:, 2].argsort()]
- tp_sl_list_2 = tp_sl_list_2[::-1]
- tp_finale, sl_finale, balance = tp_sl_list_2[0]
- print(f'Top 10: \n{tp_sl_list_2[0:11]}\n')
- print(f'Max: {tp_sl_list_2[-0]}')
- print(f'TP_Finale: {tp_finale}; SL_Finale: {sl_finale}')
- def call_cy():
- np.set_printoptions(suppress=True)
- db = Sql_new(directory)
- data = pd.DataFrame(data=db.get_data(), columns=columns)
- data = data[['Open_Price', 'High_Price', 'Low_Price', 'Close_Price']]
- data = data.replace(to_replace='NEUTRAL', value='RED')
- data = data.replace(to_replace='RED', value='r')
- data = data.replace(to_replace='GREEN', value='g')
- high_max, low_max = find_min_max(data)
- data = data[['Open_Price', 'High_Price', 'Low_Price', 'Close_Price']]
- tp_array = np.linspace(0, (high_max + 2 * (high_max / lenght_array)), lenght_array)
- sl_array = np.linspace(0, (low_max + 2 * (low_max / lenght_array)), lenght_array)
- tp_sl_mash = np.array(np.meshgrid(tp_array, sl_array)).T.reshape(-1, 2)
- X, Y, Z, tp_sl_list = tp_sl_loop(data.to_numpy(), tp_sl_mash, pair)
- X, Y, Z = X.tolist(), Y.tolist(), Z.tolist()
- tp_sl_list = np.array(tp_sl_list)
- tp_sl_list = tp_sl_list[tp_sl_list[:, 2].argsort()]
- tp_sl_list = tp_sl_list[::-1]
- # print(f'Top 10: \n{tp_sl_list[0:11]}\n')
- # print(f'Max: {tp_sl_list[-0]}')
- max_index = Z.index(max(Z))
- from_tp, to_tp = (X[(max_index - lenght_array)] / 100), (X[(max_index + lenght_array)] / 100)
- from_sl, to_sl = (Y[(max_index - 1)] / 100), (Y[(max_index + 1)] / 100)
- tp_array_2 = np.linspace(start=from_tp, stop=to_tp, num=lenght_array)
- sl_array_2 = np.linspace(start=from_sl, stop=to_sl, num=lenght_array)
- tp_sl_mash_2 = np.array(np.meshgrid(tp_array_2, sl_array_2)).T.reshape(-1, 2)
- X_2, Y_2, Z_2, tp_sl_list_2 = tp_sl_loop(data.to_numpy(), tp_sl_mash_2, pair)
- tp_sl_list_2 = np.array(tp_sl_list_2)
- tp_sl_list_2 = tp_sl_list_2[tp_sl_list_2[:, 2].argsort()]
- tp_sl_list_2 = tp_sl_list_2[::-1]
- tp_finale, sl_finale, balance = tp_sl_list_2[0]
- print(f'Top 10: \n{tp_sl_list_2[0:11]}\n')
- print(f'Max: {tp_sl_list_2[-0]}')
- print(f'TP_Finale: {tp_finale}; SL_Finale: {sl_finale}')
- def main():
- cy = timeit.timeit(f'call_cy()', "from __main__ import call_cy", number=100)
- py = timeit.timeit(f'call_py()', "from __main__ import call_py", number=1)
- cy /= 100
- print('\n\n')
- print(f"Cython: {'{:.5f}'.format(round(cy, 10))}s")
- print(f"Python: {'{:.5f}'.format(round(py, 10))}s")
- print(f'Cython is {round(py/cy, 2)}x faster')
- with cProfile.Profile() as pr:
- try:
- main()
- except (KeyboardInterrupt, OverflowError):
- traceback.print_exc()
- stats = pstats.Stats(pr)
- stats.sort_stats(pstats.SortKey.TIME)
- stats.dump_stats(filename='STATS.prof')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement