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.
59 lines
1.6 KiB
59 lines
1.6 KiB
import matplotlib.pyplot as plt
|
|
import pandas as pd # type: ignore
|
|
import logging
|
|
|
|
from auto_trading.broker.backtest import Backtest
|
|
from auto_trading.indicators.dumb import Dumb
|
|
from auto_trading.indicators.slopy import Slopy
|
|
from auto_trading.indicators.sma2 import SMA
|
|
from auto_trading.interfaces import Indicator
|
|
from auto_trading.strat.buyupselldown import BuyUpSellDown
|
|
from auto_trading.strat.hold import Hold
|
|
from auto_trading.ptf.in_memory import InMemoryPortfolio
|
|
from auto_trading.bot import Bot
|
|
|
|
|
|
pd.options.plotting.backend = "plotly"
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
bt = Backtest("./data/NYSE_small.csv")
|
|
ptf = InMemoryPortfolio(
|
|
base_balance=100, change_rate_getter=lambda: bt.current_change
|
|
)
|
|
|
|
strategy = BuyUpSellDown({"slopy": Slopy(), "sma": SMA(windowSize=5)})
|
|
|
|
bot = Bot(ptf, strategy, bt)
|
|
|
|
bot.run()
|
|
|
|
###### Visualisation ######
|
|
|
|
data = bt.data.unstack()
|
|
|
|
# select high prices for each action
|
|
closeData = data["close"]
|
|
|
|
sma = closeData.rolling(window=5).mean()
|
|
ema = closeData.ewm(alpha=0.6).mean()
|
|
|
|
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()
|
|
|
|
for order in ptf.history:
|
|
print(order)
|
|
|
|
print(bot.ptf.total_balance(bot.broker.current_change))
|
|
|
|
plt.show()
|
|
|
|
ch_history: pd.DataFrame = bot.broker.change_rate_history # type: ignore
|
|
st_history = pd.DataFrame(
|
|
[s.stocks for s in bot.ptf.states_history],
|
|
index=ch_history.index,
|
|
)
|
|
(st_history * ch_history).fillna(0).plot.area().show()
|
|
|