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 [2025/03/02 22:10] 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/
  
-import pandas as pd 
- 
-#df = kbt_init('XXBTZEUR',1440) 
-df = pd.read_csv('XXBTZEUR_1440.csv') 
-#df = df[int(-3*365):] 
- 
-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 = kbt_compute(df) 
-    return df['R_net'].iloc[-1] 
- 
-SLOW = np.arange(5, 500, 5) 
-FAST = np.arange(5, 500, 5) 
-SLOW, FAST = np.meshgrid(SLOW, FAST) 
-Z = np.zeros_like(SLOW) 
-# https://blog.finxter.com/applying-functions-to-python-meshgrid-5-effective-techniques/ 
-for i in range(SLOW.shape[0]): 
-    for j in range(SLOW.shape[1]): 
-        Z[i, j] = fct (df, SLOW[i, j], FAST[i, j]) 
-        print(f"{SLOW[i,j]}, {FAST[i,j]} -> {Z[i,j]}                 ",end="\r") 
- 
-import matplotlib.pyplot as plt 
-from matplotlib import cm 
-from mpl_toolkits.mplot3d import axes3d 
- 
-fig = plt.figure() 
-ax = fig.add_subplot(projection='3d') 
- 
- 
-               
-# Plot a basic wireframe. 
-ax.plot_surface(SLOW, FAST, Z, cmap=cm.coolwarm) 
- 
-ax.view_init(elev=20, azim=115, roll=0) 
- 
-ax.set_xlabel('SLOW') 
-ax.set_ylabel('FAST') 
-ax.set_zlabel('Z') 
-ax.set_title('Single Point in 3D') 
- 
-# https://numpy.org/doc/stable/reference/generated/numpy.argmax.html#numpy.argmax 
-ind = np.unravel_index(np.argmax(Z, axis=None), Z.shape) 
-print(ind, SLOW[ind], FAST[ind], Z[ind]) 
- 
-plt.show() 
-</code> 
- 
-<code python> 
 import numpy as np import numpy as np
 import pandas as pd import pandas as pd
 import talib as ta import talib as ta
  
-df = pd.read_csv('https://raw.githubusercontent.com/brulint/backtesting/main/btceur-2h.csv'+# 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['slow'] = ta.EMA(df.close, timeperiod=slow)
Line 68: Line 22:
     df['position'] = df.slow < df.fast     df['position'] = df.slow < df.fast
     df['r_hodl'] = np.log( df['close'] / df['close'].shift() )     df['r_hodl'] = np.log( df['close'] / df['close'].shift() )
-    df['r_strat'] = df['position'].shift() df['r_hodl']+    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_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_strat'] - df['r_fee']
     df['R_net'] = df['r_net'].cumsum()     df['R_net'] = df['r_net'].cumsum()
-    print(df['R_net'].iloc[-1]) +    return np.exp(df['R_net'].iloc[-1])-1
-    return df['R_net'].iloc[-1]+
  
 +def fct2 (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 = kbt_compute(df)
 +    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")
Line 97: Line 58:
 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')
Line 109: Line 70:
    
 plt.show() plt.show()
 +
 </code> </code>
compare_strategies_w_graph_3d.1740953422.txt.gz · Last modified: 2025/03/02 22:10 by bruno