image_output_1.png
from botsFactoryLib import processData, genExposure, genExposureDown, genPreds, genShiftedSMA, loadModel

import os

import pandas as pd

import plotly.graph_objects as go

import talib

import vectorbt as vbt

import os
import json
import pytz
import talib
import pickle
import numpy as np
import pandas as pd
import datetime as dt
import vectorbt as vbt
import matplotlib.pyplot as plt
import plotly.graph_objects as go
from histDataHandler import loadSuchData
from botsFactoryLib import processData, genExposure, genExposureDown, genPreds, genShiftedSMA, loadModel
current_directory = os.getcwd()
parent_directory = os.path.dirname(current_directory)
os.chdir(parent_directory)
vbt.settings.set_theme('dark')
vbt.settings['plotting']['layout']['width'] = 700
vbt.settings['plotting']['layout']['height'] = 350
subplots = ['trades', 'trade_pnl', 'cum_returns', 'underwater', 'net_exposure']
data = pd.read_csv('backtesting_ohlcv_data.csv', index_col=0, parse_dates=True)
data

shiftWindow = 48
modelName = f'articleModelSMA{shiftWindow}'
model, modelParamsDict, targetScaler, scalers = loadModel(modelName, data)
preds = pd.concat([genPreds(data, model, modelParamsDict, targetScaler, scalers), talib.SMA(data['Close'], timeperiod=shiftWindow).rename('Current_SMA')], axis=1).dropna()
exposure = genExposure(pd.concat([preds, data['Close'].shift(1).rename('Shifted_Close')], axis=1), 2.5, 10, 'Prediction')
preds_SMA = pd.concat([genShiftedSMA(data, shiftWindow, shiftWindow + 1).rename('Prediction'), talib.SMA(data['Close'], timeperiod=shiftWindow).rename('Current_SMA')], axis=1).dropna()
exposure_SMA = genExposure(pd.concat([preds_SMA, data['Close'].shift(1).rename('Shifted_Close')], axis=1), 1.25, 10, 'Prediction')

pf_model = vbt.Portfolio.from_orders(data['Open'][exposure.index], exposure, size_type='targetpercent', freq=modelParamsDict['frequency'])
pf_shiftedSMA = vbt.Portfolio.from_orders(data['Open'][exposure_SMA.index], exposure_SMA, size_type='targetpercent', freq=modelParamsDict['frequency'])

pf_model.stats()

figSpot_model = go.Figure(pf_model.plot(subplots=subplots))
figSpot_model.show()