backtest_w_python
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
backtest_w_python [2025/03/23 07:30] – bruno | backtest_w_python [2025/03/27 17:02] (current) – [Sinon] bruno | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | === Deathcross === | + | ==== Deathcross |
<code python> | <code python> | ||
Line 107: | Line 107: | ||
</ | </ | ||
- | === Trend following RSI === | + | ==== Trend following RSI ==== |
<code python> | <code python> | ||
- | # coding: utf-8 | ||
- | # coding: utf-8 | ||
- | import numpy as np | ||
- | import pandas as pd | ||
- | import talib as ta | ||
- | |||
- | df = pd.read_csv(' | ||
- | #df = pd.read_csv(' | ||
- | |||
- | # strategy | ||
- | RSI = ta.RSI(df.close, | ||
- | signal_buy = RSI > 70 | ||
- | signal_sell = RSI < 30 | ||
- | |||
- | # position | ||
- | signal = signal_buy.astype(int) - signal_sell.astype(int) | ||
- | df[' | ||
- | |||
- | # returns | ||
- | df[' | ||
- | df[' | ||
- | df[' | ||
- | df[' | ||
- | |||
- | # cumulative | ||
- | df[[' | ||
- | |||
- | # graphs | ||
- | from bokeh.plotting import figure,show | ||
- | from bokeh.layouts import column,row | ||
- | from bokeh.models import DatetimeTickFormatter | ||
- | |||
- | df[' | ||
- | xformatter = DatetimeTickFormatter(hours=" | ||
- | |||
- | p1 = figure(height=325, | ||
- | p1.xaxis[0].formatter = xformatter | ||
- | p1.line(df[' | ||
- | |||
- | p11 = figure(height=125, | ||
- | p11.xaxis[0].formatter = xformatter | ||
- | p11.line(df.date, | ||
- | p11.line(df.date, | ||
- | p11.line(df.date, | ||
- | p11.line(df.date, | ||
- | |||
- | p2 = figure(height=325, | ||
- | p2.xaxis[0].formatter = xformatter | ||
- | p2.line(df[' | ||
- | p2.triangle(df[' | ||
- | p2.inverted_triangle(df[' | ||
- | |||
- | p3 = figure(height=125, | ||
- | p3.xaxis[0].formatter = xformatter | ||
- | p3.line(df.date, | ||
- | |||
- | p31 = figure(height=125, | ||
- | p31.xaxis[0].formatter = xformatter | ||
- | p31.line(df[' | ||
- | |||
- | p4 = figure(height=150, | ||
- | p4.xaxis[0].formatter = xformatter | ||
- | p4.line(df[' | ||
- | p4.line(df[' | ||
- | p4.line(df[' | ||
- | |||
- | p5 = figure(height=325, | ||
- | p5.xaxis[0].formatter = xformatter | ||
- | p5.line(df[' | ||
- | p5.line(df[' | ||
- | p5.line(df[' | ||
- | |||
- | layout = column(p1, p11, p3, p31, p2, p4, p5) | ||
- | show(layout) | ||
- | # coding: utf-8 | ||
- | |||
- | import numpy as np | ||
- | import pandas as pd | ||
- | import talib as ta | ||
- | |||
- | df = pd.read_csv(' | ||
- | #df = pd.read_csv(' | ||
- | |||
- | # strategy | ||
- | RSI = ta.RSI(df.close, | ||
- | signal_buy = RSI > 70 | ||
- | signal_sell = RSI < 30 | ||
- | |||
- | # position | ||
- | signal = signal_buy.astype(int) - signal_sell.astype(int) | ||
- | df[' | ||
- | |||
- | # returns | ||
- | df[' | ||
- | df[' | ||
- | df[' | ||
- | df[' | ||
- | |||
- | # cumulative | ||
- | df[[' | ||
- | |||
- | # graphs | ||
- | from bokeh.plotting import figure,show | ||
- | from bokeh.layouts import column,row | ||
- | from bokeh.models import DatetimeTickFormatter | ||
- | |||
- | df[' | ||
- | xformatter = DatetimeTickFormatter(hours=" | ||
- | |||
- | p1 = figure(height=325, | ||
- | p1.xaxis[0].formatter = xformatter | ||
- | p1.line(df[' | ||
- | |||
- | p11 = figure(height=125, | ||
- | p11.xaxis[0].formatter = xformatter | ||
- | p11.line(df.date, | ||
- | p11.line(df.date, | ||
- | p11.line(df.date, | ||
- | p11.line(df.date, | ||
- | |||
- | p2 = figure(height=325, | ||
- | p2.xaxis[0].formatter = xformatter | ||
- | p2.line(df[' | ||
- | p2.triangle(df[' | ||
- | p2.inverted_triangle(df[' | ||
- | |||
- | p3 = figure(height=125, | ||
- | p3.xaxis[0].formatter = xformatter | ||
- | p3.line(df.date, | ||
- | |||
- | p31 = figure(height=125, | ||
- | p31.xaxis[0].formatter = xformatter | ||
- | p31.line(df[' | ||
- | |||
- | p4 = figure(height=150, | ||
- | p4.xaxis[0].formatter = xformatter | ||
- | p4.line(df[' | ||
- | p4.line(df[' | ||
- | p4.line(df[' | ||
- | |||
- | p5 = figure(height=325, | ||
- | p5.xaxis[0].formatter = xformatter | ||
- | p5.line(df[' | ||
- | p5.line(df[' | ||
- | p5.line(df[' | ||
- | |||
- | layout = column(p1, p11, p3, p31, p2, p4, p5) | ||
- | show(layout) | ||
# coding: utf-8 | # coding: utf-8 | ||
Line 324: | Line 176: | ||
p5.line(df[' | p5.line(df[' | ||
p5.line(df[' | p5.line(df[' | ||
- | p6.line(df[' | + | p5.line(df[' |
layout = column(p1, p11, p3, p31, p2, p4, p5) | layout = column(p1, p11, p3, p31, p2, p4, p5) | ||
show(layout) | show(layout) | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Advanced trend following RSI ==== | ||
+ | <code python> | ||
# coding: utf-8 | # coding: utf-8 | ||
Line 339: | Line 197: | ||
# strategy | # strategy | ||
RSI = ta.RSI(df.close, | RSI = ta.RSI(df.close, | ||
- | signal_buy | + | signal_long_buy |
- | signal_sell | + | signal_long_sell |
+ | signal_short_sell = RSI < 20 | ||
+ | signal_short_buy = RSI > 50 | ||
# position | # position | ||
- | signal | + | signal_long |
- | position | + | signal_short |
+ | position_long = signal_long.where(signal_long | ||
+ | position_short = signal_short.where(signal_short != 0).ffill() > 0 | ||
+ | position = position_long.astype(int) - position_short.astype(int) | ||
# returns | # returns | ||
returns_hodl = np.log(df.close / df.close.shift()) | returns_hodl = np.log(df.close / df.close.shift()) | ||
- | returns_strat = returns_hodl * (position.shift() == 1) | + | returns_strat = returns_hodl * position.shift() |
returns_netto = returns_strat - 0.0025 * (position != position.shift()) | returns_netto = returns_strat - 0.0025 * (position != position.shift()) | ||
- | # graphs | + | # graphic |
from bokeh.plotting import figure,show | from bokeh.plotting import figure,show | ||
from bokeh.layouts import column,row | from bokeh.layouts import column,row | ||
from bokeh.models import DatetimeTickFormatter | from bokeh.models import DatetimeTickFormatter | ||
- | df[' | + | df[' |
xformatter = DatetimeTickFormatter(hours=" | xformatter = DatetimeTickFormatter(hours=" | ||
p1 = figure(height=325, | p1 = figure(height=325, | ||
p1.xaxis[0].formatter = xformatter | p1.xaxis[0].formatter = xformatter | ||
- | p1.line(df['date'], df['close']) | + | p1.line(df.date, df.close) |
p11 = figure(height=125, | p11 = figure(height=125, | ||
Line 370: | Line 233: | ||
p11.line(df.date, | p11.line(df.date, | ||
- | p2 = figure(height=325, width=800, x_range=p1.x_range) | + | p2 = figure(height=125, width=800, x_range=p1.x_range) |
p2.xaxis[0].formatter = xformatter | p2.xaxis[0].formatter = xformatter | ||
- | p2.line(df[' | + | p2.line(df.date, |
- | p2.triangle(df[' | + | |
- | p2.inverted_triangle(df[' | + | |
- | p3 = figure(height=125, width=800, x_range=p1.x_range) | + | p3 = figure(height=325, width=800, x_range=p1.x_range) |
p3.xaxis[0].formatter = xformatter | p3.xaxis[0].formatter = xformatter | ||
- | p3.line(df.date, | + | p3.line(df.date, |
- | + | position_long_in = df.close.where((position == 1) & (position.shift() != 1)) | |
- | p31 = figure(height=125, width=800, x_range=p1.x_range) | + | position_long_out |
- | p31.xaxis[0].formatter | + | position_short_in = df.close.where((position == -1) & (position.shift() != -1)) |
- | p31.line(df[' | + | position_short_out = df.close.where((position != -1) & (position.shift() == -1)) |
+ | p3.triangle(df.date, position_long_in, | ||
+ | p3.inverted_triangle(df.date, | ||
+ | p3.inverted_triangle(df.date, position_short_in, | ||
+ | p3.triangle(df.date, position_short_out, | ||
+ | p3.line(df.date, np.where(position == 1, df.close, np.nan), color='green' | ||
+ | p3.line(df.date, np.where(position | ||
p4 = figure(height=150, | p4 = figure(height=150, | ||
Line 395: | Line 262: | ||
p5.line(df[' | p5.line(df[' | ||
- | layout = column(p1, p11, p3, p31, p2, p4, p5) | + | layout = column(p1, p11, p2, p3, p4, p5) |
show(layout) | show(layout) | ||
</ | </ | ||
- | [[https:// | ||
- | === Sinon === | ||
- | |||
- | Un vieux code de 2022 (si pas plus). Chrypowatch n' | ||
- | |||
- | <code python> | ||
- | # coding: utf-8 | ||
- | import requests | ||
- | import numpy as np | ||
- | import pandas as pd | ||
- | |||
- | import talib | ||
- | |||
- | url = ' | ||
- | ohlc = requests.get(url).json()[' | ||
- | columns = [' | ||
- | df = pd.DataFrame(ohlc, | ||
- | df = df.iloc[-1000: | ||
- | |||
- | df[' | ||
- | # | ||
- | df[' | ||
- | # | ||
- | df[' | ||
- | # | ||
- | df[' | ||
- | # signal | ||
- | df[' | ||
- | df[' | ||
- | # | ||
- | # | ||
- | # | ||
- | # | ||
- | # | ||
- | |||
- | # | ||
- | # | ||
- | # | ||
- | |||
- | df[' | ||
- | df[' | ||
- | df[' | ||
- | # Rendements | ||
- | df[' | ||
- | df[' | ||
- | df[' | ||
- | df[' | ||
- | # tronquage datafame | ||
- | #df = df.iloc[-700: | ||
- | # Rendement cumulé | ||
- | df[' | ||
- | # Graphiques | ||
- | from bokeh.plotting import figure,show | ||
- | from bokeh.layouts import column,row | ||
- | p1 = figure(height=300, | ||
- | p1.line(df.time, | ||
- | # | ||
- | # | ||
- | p2 = figure(height=100, | ||
- | p2.line(df.time, | ||
- | #p3_0 = figure(height=100, | ||
- | # | ||
- | p3_1 = figure(height=100, | ||
- | p3_1.line(df.time, | ||
- | p3_2 = figure(height=100, | ||
- | p3_2.line(df.time, | ||
- | p3_3 = figure(height=100, | ||
- | p3_3.line(df.time, | ||
- | p3_3_2 = figure(height=100, | ||
- | p3_3_2.line(df.time, | ||
- | p3_4 = figure(height=100, | ||
- | p3_4.line(df.time, | ||
- | p4 = figure(height=150, | ||
- | p4.line(df.time, | ||
- | p4.line(df.time, | ||
- | p4.line(df.time, | ||
- | p5 = figure(height=300, | ||
- | p5.line(df.time, | ||
- | p5.line(df.time, | ||
- | p5.line(df.time, | ||
- | layout = column(p1, | ||
- | show(layout) | ||
- | </ |
backtest_w_python.1742715010.txt.gz · Last modified: 2025/03/23 07:30 by bruno