User Tools

Site Tools


compare_strategies_w_graph_3d

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
compare_strategies_w_graph_3d [2024/09/13 09:34] brunocompare_strategies_w_graph_3d [2025/03/14 11:20] (current) bruno
Line 7: Line 7:
 # https://www.geeksforgeeks.org/plot-single-3d-point-on-top-of-plot_surface-in-python-matplotlib/ # https://www.geeksforgeeks.org/plot-single-3d-point-on-top-of-plot_surface-in-python-matplotlib/
  
-#df = kbt_init('XXBTZEUR',1440) +import numpy as np 
-df = pd.read_csv('XXBTZEUR_1440.csv') +import pandas as pd 
-#df = df[int(-3*365):]+import talib as ta
  
 +# Donwload data
 +# https://support.kraken.com/hc/en-us/articles/360047124832
 +df = pd.read_csv('XBTEUR_1440.csv', names=['time', 'open', 'high', 'low', 'close', 'volume', 'count']) 
 +df = df[int(-10.5*365):]
 +df['close'] = df.close.replace(to_replace=0, method='ffill')
 + 
 def fct (df, slow, fast): def fct (df, slow, fast):
 +    df['slow'] = ta.EMA(df.close, timeperiod=slow)
 +    df['fast'] = ta.EMA(df.close, timeperiod=fast)
 +    df['position'] = df.slow < df.fast
 +    df['r_hodl'] = np.log( df['close'] / df['close'].shift() )
 +    df['r_strat'] = np.where(df['position'].shift(), df['r_hodl'], 0)
 +    df['r_fee'] = np.where(df['position'] != df['position'].shift(), 0.0025, 0)
 +    df['r_net'] = df['r_strat'] - df['r_fee']
 +    df['R_net'] = df['r_net'].cumsum()
 +    return np.exp(df['R_net'].iloc[-1])-1
 +
 +def fct2 (df, slow, fast):
     df['slow'] = ta.EMA(df.close, timeperiod=slow)     df['slow'] = ta.EMA(df.close, timeperiod=slow)
     df['fast'] = ta.EMA(df.close, timeperiod=fast)     df['fast'] = ta.EMA(df.close, timeperiod=fast)
Line 17: Line 34:
     df = kbt_compute(df)     df = kbt_compute(df)
     return df['R_net'].iloc[-1]     return df['R_net'].iloc[-1]
 +     
 SLOW = np.arange(5, 500, 5) SLOW = np.arange(5, 500, 5)
 FAST = np.arange(5, 500, 5) FAST = np.arange(5, 500, 5)
 SLOW, FAST = np.meshgrid(SLOW, FAST) SLOW, FAST = np.meshgrid(SLOW, FAST)
-Z = np.zeros_like(SLOW)+Z = np.zeros_like(SLOW).astype(float)
 # https://blog.finxter.com/applying-functions-to-python-meshgrid-5-effective-techniques/ # https://blog.finxter.com/applying-functions-to-python-meshgrid-5-effective-techniques/
 for i in range(SLOW.shape[0]): for i in range(SLOW.shape[0]):
     for j in range(SLOW.shape[1]):     for j in range(SLOW.shape[1]):
 +#        print(fct (df,SLOW[i,j], FAST[i,j]))
         Z[i, j] = fct (df, SLOW[i, j], FAST[i, j])         Z[i, j] = fct (df, SLOW[i, j], FAST[i, j])
         print(f"{SLOW[i,j]}, {FAST[i,j]} -> {Z[i,j]}                 ",end="\r")         print(f"{SLOW[i,j]}, {FAST[i,j]} -> {Z[i,j]}                 ",end="\r")
 + 
 import matplotlib.pyplot as plt import matplotlib.pyplot as plt
 from matplotlib import cm from matplotlib import cm
 from mpl_toolkits.mplot3d import axes3d from mpl_toolkits.mplot3d import axes3d
 + 
 fig = plt.figure() fig = plt.figure()
 ax = fig.add_subplot(projection='3d') ax = fig.add_subplot(projection='3d')
- +  
- +  
-              + 
 # Plot a basic wireframe. # Plot a basic wireframe.
 ax.plot_surface(SLOW, FAST, Z, cmap=cm.coolwarm) ax.plot_surface(SLOW, FAST, Z, cmap=cm.coolwarm)
- +  
-ax.view_init(elev=20, azim=115, roll=0) +ax.view_init(elev=35, azim=115, roll=0) 
 + 
 ax.set_xlabel('SLOW') ax.set_xlabel('SLOW')
 ax.set_ylabel('FAST') ax.set_ylabel('FAST')
 ax.set_zlabel('Z') ax.set_zlabel('Z')
 ax.set_title('Single Point in 3D') ax.set_title('Single Point in 3D')
 + 
 # https://numpy.org/doc/stable/reference/generated/numpy.argmax.html#numpy.argmax # https://numpy.org/doc/stable/reference/generated/numpy.argmax.html#numpy.argmax
 ind = np.unravel_index(np.argmax(Z, axis=None), Z.shape) ind = np.unravel_index(np.argmax(Z, axis=None), Z.shape)
 print(ind, SLOW[ind], FAST[ind], Z[ind]) print(ind, SLOW[ind], FAST[ind], Z[ind])
 + 
 plt.show() plt.show()
 +
 </code> </code>
compare_strategies_w_graph_3d.1726220046.txt.gz · Last modified: 2024/09/13 09:34 by bruno