Installation¶
In [ ]:
Copied!
!pip install hyperopt
!pip install hyperopt
Sample Strategy¶
In [1]:
Copied!
# import talib.abstract as ta
from lettrade import DataFeed, Strategy, indicator as i
from lettrade.exchange.backtest import ForexBackTestAccount, let_backtest
from lettrade.indicator.vendor.qtpylib import inject_indicators
inject_indicators()
class SmaCross(Strategy):
ema1_window = 9
ema2_window = 21
def indicators(self, df: DataFeed):
# df["ema1"] = ta.EMA(df, timeperiod=self.ema1_window)
# df["ema2"] = ta.EMA(df, timeperiod=self.ema2_window)
df["ema1"] = df.close.ema(window=self.ema1_window)
df["ema2"] = df.close.ema(window=self.ema2_window)
df["signal_ema_crossover"] = i.crossover(df.ema1, df.ema2)
df["signal_ema_crossunder"] = i.crossunder(df.ema1, df.ema2)
def next(self, df: DataFeed):
if len(self.orders) > 0 or len(self.positions) > 0:
return
if df.l.signal_ema_crossover[-1]:
price = df.l.close[-1]
self.buy(size=0.1, sl=price - 0.001, tp=price + 0.001)
elif df.l.signal_ema_crossunder[-1]:
price = df.l.close[-1]
self.sell(size=0.1, sl=price + 0.001, tp=price - 0.001)
lt = let_backtest(
strategy=SmaCross,
datas="example/data/data/EURUSD_5m-0_10000.csv",
account=ForexBackTestAccount,
# plotter=None,
)
# import talib.abstract as ta
from lettrade import DataFeed, Strategy, indicator as i
from lettrade.exchange.backtest import ForexBackTestAccount, let_backtest
from lettrade.indicator.vendor.qtpylib import inject_indicators
inject_indicators()
class SmaCross(Strategy):
ema1_window = 9
ema2_window = 21
def indicators(self, df: DataFeed):
# df["ema1"] = ta.EMA(df, timeperiod=self.ema1_window)
# df["ema2"] = ta.EMA(df, timeperiod=self.ema2_window)
df["ema1"] = df.close.ema(window=self.ema1_window)
df["ema2"] = df.close.ema(window=self.ema2_window)
df["signal_ema_crossover"] = i.crossover(df.ema1, df.ema2)
df["signal_ema_crossunder"] = i.crossunder(df.ema1, df.ema2)
def next(self, df: DataFeed):
if len(self.orders) > 0 or len(self.positions) > 0:
return
if df.l.signal_ema_crossover[-1]:
price = df.l.close[-1]
self.buy(size=0.1, sl=price - 0.001, tp=price + 0.001)
elif df.l.signal_ema_crossunder[-1]:
price = df.l.close[-1]
self.sell(size=0.1, sl=price + 0.001, tp=price - 0.001)
lt = let_backtest(
strategy=SmaCross,
datas="example/data/data/EURUSD_5m-0_10000.csv",
account=ForexBackTestAccount,
# plotter=None,
)
Optimize¶
In [2]:
Copied!
# define a search space
from hyperopt import fmin, tpe, space_eval, Trials
from hyperopt import hp
lettrade_model = lt.optimize_model()
def train_model(params):
# Model
result = lettrade_model(params)
# Score
return -result["equity"]
# Hyperopt
search_space = {
"ema1_window": hp.uniformint("ema1_window", 5, 25, q=1),
"ema2_window": hp.uniformint("ema2_window", 5, 50, q=1),
}
trials = Trials()
best_params = fmin(
train_model,
search_space,
algo=tpe.suggest,
max_evals=1_000,
trials=trials,
)
# define a search space
from hyperopt import fmin, tpe, space_eval, Trials
from hyperopt import hp
lettrade_model = lt.optimize_model()
def train_model(params):
# Model
result = lettrade_model(params)
# Score
return -result["equity"]
# Hyperopt
search_space = {
"ema1_window": hp.uniformint("ema1_window", 5, 25, q=1),
"ema2_window": hp.uniformint("ema2_window", 5, 50, q=1),
}
trials = Trials()
best_params = fmin(
train_model,
search_space,
algo=tpe.suggest,
max_evals=1_000,
trials=trials,
)
100%|██████████| 1000/1000 [01:17<00:00, 12.97trial/s, best loss: -10187.82]
In [3]:
Copied!
hyperparams = space_eval(search_space, best_params)
print(best_params)
print(hyperparams)
hyperparams = space_eval(search_space, best_params)
print(best_params)
print(hyperparams)
{'ema1_window': 17.0, 'ema2_window': 8.0} {'ema1_window': 17, 'ema2_window': 8}
Plot¶
In [4]:
Copied!
lt.plotter.heatmap(x="ema1_window", y="ema2_window", z="equity")
lt.plotter.heatmap(x="ema1_window", y="ema2_window", z="equity")
In [5]:
Copied!
lt.plotter.contour(x="ema1_window", y="ema2_window", z="equity")
lt.plotter.contour(x="ema1_window", y="ema2_window", z="equity")
Init Plotly environment¶
In [6]:
Copied!
import plotly.io as pio
pio.renderers.default = "notebook"
pio.templates.default = "plotly_dark"
import plotly.io as pio
pio.renderers.default = "notebook"
pio.templates.default = "plotly_dark"
In [7]:
Copied!
import pandas as pd
import numpy as np
def unpack(x):
if x:
return x[0]
return np.nan
# We'll first turn each trial into a series and then stack those series together as a dataframe.
df = pd.DataFrame([pd.Series(t["misc"]["vals"]).apply(unpack) for t in trials])
# Then we'll add other relevant bits of information to the correct rows and perform a couple of
# mappings for convenience
df["loss"] = [t["result"]["loss"] for t in trials]
df["trial_number"] = df.index
df["win"] = -df["loss"]
df
import pandas as pd
import numpy as np
def unpack(x):
if x:
return x[0]
return np.nan
# We'll first turn each trial into a series and then stack those series together as a dataframe.
df = pd.DataFrame([pd.Series(t["misc"]["vals"]).apply(unpack) for t in trials])
# Then we'll add other relevant bits of information to the correct rows and perform a couple of
# mappings for convenience
df["loss"] = [t["result"]["loss"] for t in trials]
df["trial_number"] = df.index
df["win"] = -df["loss"]
df
Out[7]:
ema1_window | ema2_window | loss | trial_number | win | |
---|---|---|---|---|---|
0 | 19.0 | 10.0 | -10000.68 | 0 | 10000.68 |
1 | 10.0 | 7.0 | -10062.72 | 1 | 10062.72 |
2 | 18.0 | 40.0 | -9899.16 | 2 | 9899.16 |
3 | 9.0 | 8.0 | -10052.78 | 3 | 10052.78 |
4 | 12.0 | 27.0 | -9954.98 | 4 | 9954.98 |
... | ... | ... | ... | ... | ... |
995 | 15.0 | 15.0 | -10000.00 | 995 | 10000.00 |
996 | 16.0 | 5.0 | -10029.46 | 996 | 10029.46 |
997 | 18.0 | 13.0 | -10020.16 | 997 | 10020.16 |
998 | 20.0 | 9.0 | -9970.36 | 998 | 9970.36 |
999 | 13.0 | 8.0 | -10140.46 | 999 | 10140.46 |
1000 rows × 5 columns
Type 1¶
In [8]:
Copied!
from plotly import express as px
fig = px.scatter(df, x="trial_number", y="win")
fig.show()
from plotly import express as px
fig = px.scatter(df, x="trial_number", y="win")
fig.show()
Type 2¶
In [9]:
Copied!
import plotly.express as px
fig = px.density_contour(
df,
x="ema1_window",
y="ema2_window",
z="win",
histfunc="max",
)
fig.update_traces(contours_coloring="fill", contours_showlabels=True)
fig.show()
import plotly.express as px
fig = px.density_contour(
df,
x="ema1_window",
y="ema2_window",
z="win",
histfunc="max",
)
fig.update_traces(contours_coloring="fill", contours_showlabels=True)
fig.show()
Type 3¶
In [10]:
Copied!
import plotly.express as px
fig = px.density_heatmap(
df,
x="ema1_window",
y="ema2_window",
z="win",
nbinsx=20,
nbinsy=40,
histfunc="max",
color_continuous_scale="Viridis",
)
fig.show()
import plotly.express as px
fig = px.density_heatmap(
df,
x="ema1_window",
y="ema2_window",
z="win",
nbinsx=20,
nbinsy=40,
histfunc="max",
color_continuous_scale="Viridis",
)
fig.show()