Advertisement
z1rix

Untitled

Feb 9th, 2023 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.25 KB | Software | 0 0
  1. import cProfile
  2. import pstats
  3. from SQL_new import SQLite as Sql_new
  4. from test_py import tp_sl_loop_py
  5. from test_cy import tp_sl_loop
  6. import pandas as pd
  7. import numpy as np
  8. import traceback
  9. import timeit
  10.  
  11. lenght_array = 100
  12. columns = ['Open_Time', 'Open_Price', 'High_Price', 'Low_Price', 'Close_Price', 'Volume', 'Color']
  13. pair="asset name"
  14. directory="Directory to the DataBase"
  15.  
  16. def find_min_max(df):
  17.  
  18.     df['Percent_High'] = df['High_Price'] / df['Open_Price']
  19.     df['Percent_Low'] = df['Low_Price'] / df['Open_Price']
  20.  
  21.     df_high_max = (df['Percent_High'].max() - 1) * 100
  22.     df_low_max = abs((1 - df['Percent_Low'].min())) * 100
  23.  
  24.     return df_high_max, df_low_max
  25.  
  26.  
  27. def call_py():
  28.     np.set_printoptions(suppress=True)
  29.  
  30.     db = Sql_new(directory)
  31.     data = pd.DataFrame(data=db.get_data(), columns=columns)
  32.     data = data[['Open_Price', 'High_Price', 'Low_Price', 'Close_Price']]
  33.  
  34.     data = data.replace(to_replace='NEUTRAL', value='RED')
  35.     data = data.replace(to_replace='RED', value='r')
  36.     data = data.replace(to_replace='GREEN', value='g')
  37.  
  38.     high_max, low_max = find_min_max(data)
  39.     data = data[['Open_Price', 'High_Price', 'Low_Price', 'Close_Price']]
  40.  
  41.     # print(f'High MAX: {high_max}, Low MAX: {low_max}')
  42.  
  43.     tp_array = np.linspace(0, (high_max + 2 * (high_max / lenght_array)), lenght_array)
  44.     sl_array = np.linspace(0, (low_max + 2 * (low_max / lenght_array)), lenght_array)
  45.  
  46.     tp_sl_mash = np.array(np.meshgrid(tp_array, sl_array)).T.reshape(-1, 2)
  47.  
  48.     X, Y, Z, tp_sl_list = tp_sl_loop_py(data.to_numpy(), tp_sl_mash, pair)
  49.  
  50.     tp_sl_list = np.array(tp_sl_list)
  51.     tp_sl_list = tp_sl_list[tp_sl_list[:, 2].argsort()]
  52.     tp_sl_list = tp_sl_list[::-1]
  53.  
  54.     # print(f'Top 10: \n{tp_sl_list[0:11]}\n')
  55.     # print(f'Max: {tp_sl_list[-0]}')
  56.  
  57.     max_index = Z.index(max(Z))
  58.  
  59.     from_tp, to_tp = (X[(max_index - lenght_array)] / 100), (X[(max_index + lenght_array)] / 100)
  60.     from_sl, to_sl = (Y[(max_index - 1)] / 100), (Y[(max_index + 1)] / 100)
  61.  
  62.     tp_array_2 = np.linspace(start=from_tp, stop=to_tp, num=lenght_array)
  63.     sl_array_2 = np.linspace(start=from_sl, stop=to_sl, num=lenght_array)
  64.  
  65.     tp_sl_mash_2 = np.array(np.meshgrid(tp_array_2, sl_array_2)).T.reshape(-1, 2)
  66.  
  67.     X_2, Y_2, Z_2, tp_sl_list_2 = tp_sl_loop_py(data.to_numpy(), tp_sl_mash_2, pair)
  68.  
  69.     tp_sl_list_2 = np.array(tp_sl_list_2)
  70.     tp_sl_list_2 = tp_sl_list_2[tp_sl_list_2[:, 2].argsort()]
  71.     tp_sl_list_2 = tp_sl_list_2[::-1]
  72.     tp_finale, sl_finale, balance = tp_sl_list_2[0]
  73.  
  74.     print(f'Top 10: \n{tp_sl_list_2[0:11]}\n')
  75.     print(f'Max: {tp_sl_list_2[-0]}')
  76.     print(f'TP_Finale: {tp_finale}; SL_Finale: {sl_finale}')
  77.  
  78.  
  79. def call_cy():
  80.     np.set_printoptions(suppress=True)
  81.  
  82.     db = Sql_new(directory)
  83.     data = pd.DataFrame(data=db.get_data(), columns=columns)
  84.     data = data[['Open_Price', 'High_Price', 'Low_Price', 'Close_Price']]
  85.  
  86.     data = data.replace(to_replace='NEUTRAL', value='RED')
  87.     data = data.replace(to_replace='RED', value='r')
  88.     data = data.replace(to_replace='GREEN', value='g')
  89.  
  90.     high_max, low_max = find_min_max(data)
  91.     data = data[['Open_Price', 'High_Price', 'Low_Price', 'Close_Price']]
  92.  
  93.     tp_array = np.linspace(0, (high_max + 2 * (high_max / lenght_array)), lenght_array)
  94.     sl_array = np.linspace(0, (low_max + 2 * (low_max / lenght_array)), lenght_array)
  95.  
  96.     tp_sl_mash = np.array(np.meshgrid(tp_array, sl_array)).T.reshape(-1, 2)
  97.  
  98.     X, Y, Z, tp_sl_list = tp_sl_loop(data.to_numpy(), tp_sl_mash, pair)
  99.  
  100.     X, Y, Z = X.tolist(), Y.tolist(), Z.tolist()
  101.  
  102.     tp_sl_list = np.array(tp_sl_list)
  103.     tp_sl_list = tp_sl_list[tp_sl_list[:, 2].argsort()]
  104.     tp_sl_list = tp_sl_list[::-1]
  105.  
  106.     # print(f'Top 10: \n{tp_sl_list[0:11]}\n')
  107.     # print(f'Max: {tp_sl_list[-0]}')
  108.  
  109.     max_index = Z.index(max(Z))
  110.  
  111.     from_tp, to_tp = (X[(max_index - lenght_array)] / 100), (X[(max_index + lenght_array)] / 100)
  112.     from_sl, to_sl = (Y[(max_index - 1)] / 100), (Y[(max_index + 1)] / 100)
  113.  
  114.     tp_array_2 = np.linspace(start=from_tp, stop=to_tp, num=lenght_array)
  115.     sl_array_2 = np.linspace(start=from_sl, stop=to_sl, num=lenght_array)
  116.  
  117.     tp_sl_mash_2 = np.array(np.meshgrid(tp_array_2, sl_array_2)).T.reshape(-1, 2)
  118.  
  119.     X_2, Y_2, Z_2, tp_sl_list_2 = tp_sl_loop(data.to_numpy(), tp_sl_mash_2, pair)
  120.  
  121.     tp_sl_list_2 = np.array(tp_sl_list_2)
  122.     tp_sl_list_2 = tp_sl_list_2[tp_sl_list_2[:, 2].argsort()]
  123.     tp_sl_list_2 = tp_sl_list_2[::-1]
  124.  
  125.     tp_finale, sl_finale, balance = tp_sl_list_2[0]
  126.  
  127.     print(f'Top 10: \n{tp_sl_list_2[0:11]}\n')
  128.     print(f'Max: {tp_sl_list_2[-0]}')
  129.     print(f'TP_Finale: {tp_finale}; SL_Finale: {sl_finale}')
  130.  
  131.  
  132. def main():
  133.  
  134.     cy = timeit.timeit(f'call_cy()', "from __main__ import call_cy", number=100)
  135.     py = timeit.timeit(f'call_py()', "from __main__ import call_py", number=1)
  136.     cy /= 100
  137.  
  138.     print('\n\n')
  139.     print(f"Cython: {'{:.5f}'.format(round(cy, 10))}s")
  140.     print(f"Python: {'{:.5f}'.format(round(py, 10))}s")
  141.  
  142.     print(f'Cython is {round(py/cy, 2)}x faster')
  143.  
  144.  
  145. with cProfile.Profile() as pr:
  146.     try:
  147.         main()
  148.  
  149.     except (KeyboardInterrupt, OverflowError):
  150.         traceback.print_exc()
  151.  
  152. stats = pstats.Stats(pr)
  153. stats.sort_stats(pstats.SortKey.TIME)
  154. stats.dump_stats(filename='STATS.prof')
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement