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()
import numpy as np import pandas as pd import talib as ta df = pd.read_csv('https://raw.githubusercontent.com/brulint/backtesting/main/btceur-2h.csv') 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'] = df['position'].shift() * df['r_hodl'] 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() print(df['R_net'].iloc[-1]) 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.1740953422.txt.gz · Last modified: 2025/03/02 22:10 by bruno