You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.7 KiB

8 months ago
import matplotlib.pyplot as plt
8 months ago
import pandas as pd # type: ignore
8 months ago
import logging
8 months ago
11 months ago
from auto_trading.broker.backtest import Backtest
from auto_trading.strat.buyupselldown import BuyUpSellDown
7 months ago
from auto_trading.indicators.ema import EMA
from auto_trading.indicators.sma import SMA
from auto_trading.interfaces import Strategy
from auto_trading.orders import Long, Short
from auto_trading.strat.yoyo import Yoyo
8 months ago
from auto_trading.strat.hold import Hold
11 months ago
from auto_trading.ptf.in_memory import InMemoryPortfolio
from auto_trading.bot import Bot
7 months ago
from auto_trading.strat.prop import Prop
9 months ago
8 months ago
pd.options.plotting.backend = "plotly"
logging.basicConfig(level=logging.INFO)
8 months ago
9 months ago
if __name__ == "__main__":
8 months ago
bt = Backtest("./data/NYSE_small.csv")
8 months ago
ptf = InMemoryPortfolio(
base_balance=100, change_rate_getter=lambda: bt.current_change
11 months ago
)
strategy = BuyUpSellDown()
10 months ago
8 months ago
bot = Bot(ptf, strategy, bt)
9 months ago
8 months ago
bot.run()
9 months ago
###### Visualisation du Dataset ######
ANALYSE_DATASET = False
if ANALYSE_DATASET:
data = bt.data.unstack()
8 months ago
# select high prices for each action
closeData = data["close"]
8 months ago
sma = closeData.rolling(window=5).mean()
ema = closeData.ewm(alpha=0.6).mean()
8 months ago
plt.plot(closeData, label="real", marker="x", linewidth=0)
plt.plot(sma, color="red", label="SMA")
plt.plot(ema, color="green", label="EMA")
plt.legend()
8 months ago
plt.show()
print(bot.ptf.balance)
8 months ago
print(bot.ptf.total_balance(bot.broker.current_change))
7 months ago
# plot the history
8 months ago
ch_history: pd.DataFrame = bot.broker.change_rate_history # type: ignore
ch_history["USD"] = 1
7 months ago
st_history = bot.ptf.states_history
st_history.index = ch_history.index
8 months ago
(st_history * ch_history).fillna(0).plot.area().show()