User Tools

Site Tools


compare_strategies_w_graph_3d

This is an old revision of the document!


# coding: utf-8
# use Kissbacktest:
# %load https://github.com/brulint/kissbacktest/raw/main/kissbacktest.py
 
# https://matplotlib.org/stable/gallery/mplot3d/surface3d.html
# 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()
compare_strategies_w_graph_3d.1731062652.txt.gz · Last modified: 2024/11/08 10:44 by bruno