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:31] – 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> | ||
Line 183: | Line 183: | ||
</ | </ | ||
- | [[https:// | ||
- | |||
- | |||
- | |||
- | |||
- | === Sinon === | ||
- | |||
- | Un vieux code de 2022 (si pas plus). Chrypowatch n' | ||
+ | ==== Advanced trend following RSI ==== | ||
<code python> | <code python> | ||
# coding: utf-8 | # coding: utf-8 | ||
- | import requests | + | |
import numpy as np | import numpy as np | ||
import pandas as pd | import pandas as pd | ||
- | + | import talib as ta | |
- | import talib | + | |
- | + | ||
- | url = ' | + | |
- | ohlc = requests.get(url).json()[' | + | |
- | columns = [' | + | |
- | df = pd.DataFrame(ohlc, | + | |
- | df = df.iloc[-1000: | + | |
- | df[' | + | df = pd.read_csv('Téléchargements/ |
- | # | + | #df = pd.read_csv('https://raw.githubusercontent.com/ |
- | df['long'] = talib.SMA(df.close, | + | |
- | #df[' | + | |
- | df['short' | + | |
- | # | + | |
- | df[' | + | |
- | # signal | + | |
- | df[' | + | |
- | df[' | + | |
- | #df['sig_in' | + | |
- | # | + | |
- | # | + | |
- | # | + | |
- | # | + | |
- | #df[' | + | # strategy |
- | # | + | RSI = ta.RSI(df.close, timeperiod = 14) |
- | # | + | signal_long_buy |
+ | signal_long_sell | ||
+ | signal_short_sell = RSI < 20 | ||
+ | signal_short_buy = RSI > 50 | ||
- | df[' | + | # position |
- | df[' | + | signal_long |
- | df[' | + | signal_short = signal_short_sell.astype(int) - signal_short_buy.astype(int) |
- | # Rendements | + | position_long |
- | df[' | + | position_short |
- | df[' | + | position |
- | df[' | + | |
- | df[' | + | # returns |
- | # tronquage datafame | + | returns_hodl |
- | #df = df.iloc[-700: | + | returns_strat |
- | # Rendement cumulé | + | returns_netto |
- | df[' | + | |
- | # Graphiques | + | # 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 | ||
- | p1 = figure(height=300, | + | from bokeh.models import DatetimeTickFormatter |
- | p1.line(df.time, | + | |
- | #p1.line(df.time,df.long, | + | df[' |
- | #p1.line(df.time,df.short, | + | xformatter = DatetimeTickFormatter(hours=" |
- | p2 = figure(height=100, | + | |
- | p2.line(df.time,df.RSI) | + | p1 = figure(height=325, width=800) |
- | #p3_0 = figure(height=100, | + | p1.xaxis[0].formatter = xformatter |
- | #p3_0.line(df.time,df.trend) | + | p1.line(df.date, df.close) |
- | p3_1 = figure(height=100,width=800, | + | |
- | p3_1.line(df.time,df.sig_in, | + | p11 = figure(height=125, |
- | p3_2 = figure(height=100,width=800,x_range=p1.x_range) | + | p11.xaxis[0].formatter = xformatter |
- | p3_2.line(df.time,df.sig_out, | + | p11.line(df.date, RSI) |
- | p3_3 = figure(height=100,width=800,x_range=p1.x_range) | + | p11.line(df.date, 70, color=' |
- | p3_3.line(df.time,df.sig_0) | + | p11.line(df.date, 50, color=' |
- | p3_3_2 = figure(height=100,width=800,x_range=p1.x_range) | + | p11.line(df.date, 30, color=' |
- | p3_3_2.line(df.time,df.sig_1) | + | |
- | p3_4 = figure(height=100,width=800,x_range=p1.x_range) | + | p2 = figure(height=125, width=800, x_range=p1.x_range) |
- | p3_4.line(df.time,df.signal) | + | p2.xaxis[0].formatter = xformatter |
- | p4 = figure(height=150, | + | p2.line(df.date, position) |
- | p4.line(df.time,df.r_0, | + | |
- | p4.line(df.time, | + | p3 = figure(height=325, width=800, x_range=p1.x_range) |
- | p4.line(df.time, | + | p3.xaxis[0].formatter = xformatter |
- | p5 = figure(height=300, | + | p3.line(df.date, df.close, color=' |
- | p5.line(df.time,df.r_0.cumprod(), | + | position_long_in |
- | p5.line(df.time,df.r_strat.cumprod()) | + | position_long_out = df.close.where((position != 1) & (position.shift() == 1)) |
- | p5.line(df.time,df.R_net, | + | position_short_in = df.close.where((position |
- | layout = column(p1, | + | position_short_out |
+ | p3.triangle(df.date, position_long_in, color=' | ||
+ | p3.inverted_triangle(df.date, position_long_out, | ||
+ | p3.inverted_triangle(df.date, position_short_in, | ||
+ | p3.triangle(df.date, position_short_out, | ||
+ | p3.line(df.date, np.where(position | ||
+ | p3.line(df.date, np.where(position == -1, df.close, np.nan), color=' | ||
+ | |||
+ | p4 = figure(height=150, | ||
+ | p4.xaxis[0].formatter = xformatter | ||
+ | p4.line(df[' | ||
+ | p4.line(df['date'], returns_strat) | ||
+ | |||
+ | p5 = figure(height=325, width=800, x_range=p1.x_range) | ||
+ | p5.xaxis[0].formatter = xformatter | ||
+ | p5.line(df[' | ||
+ | p5.line(df[' | ||
+ | p5.line(df[' | ||
+ | |||
+ | layout = column(p1, p11, p2, p3, p4, p5) | ||
show(layout) | show(layout) | ||
+ | |||
</ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ |
backtest_w_python.1742715102.txt.gz · Last modified: 2025/03/23 07:31 by bruno