Browse Source

feat: main class

pull/14/head
QuentinN42 8 months ago
parent
commit
809bd0f5ea
Signed by: number42 GPG Key ID: 2CD7D563712B3A50
  1. 6
      auto_trading/orders.py
  2. 43
      main.py
  3. 16
      tests/orders/test_orders.py

6
auto_trading/orders.py

@ -17,6 +17,9 @@ class Long(Order):
"""The amount in $"""
return self.amount * self.price
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.stock}: {self.amount_usd}$)"
@dataclass
class Short(Order):
@ -30,3 +33,6 @@ class Short(Order):
def amount_usd(self) -> float:
"""The amount in $"""
return self.amount * self.price
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.stock}: {self.amount_usd}$)"

43
main.py

@ -1,44 +1,19 @@
from auto_trading.broker.backtest import Backtest
from auto_trading.strat.all_in import AllIn
from auto_trading.strat.hold import Hold
from auto_trading.ptf.in_memory import InMemoryPortfolio
from auto_trading.predictor.mean_agg import MeanAggregator
from auto_trading.predictor.normalized import NormalizedPredictor
from auto_trading.predictor.selector import SelectorPredictor
from auto_trading.predictor.random_predictor import RandomPredictor
from auto_trading.bot import Bot
from tqdm import tqdm # type: ignore
import pandas as pd # type: ignore
pd.options.plotting.backend = "plotly"
if __name__ == "__main__":
csv = "data/gold.csv"
bt = Backtest(csv, start=10)
start = {name: 0 for name in bt.data.columns}
start["USD"] = 10_000
pred = MeanAggregator(
[RandomPredictor(), SelectorPredictor({"USD": -0.1}), RandomPredictor()]
)
bot = Bot(
ptf=InMemoryPortfolio(start.copy()), strategy=AllIn(), broker=bt, predictor=pred
bt = Backtest("./data/NYSE_smallest.csv")
ptf = InMemoryPortfolio(
base_balance=100, change_rate_getter=lambda: bt.current_change
)
strategy = Hold("GOOGL")
data = pd.DataFrame(index=bt.data.index, columns=bt.data.columns)
bot = Bot(ptf, strategy, bt)
for date in tqdm(data.index):
bot.run_once()
current_investments = bot.ptf.content()
converted_investments = {
name: amount * bot.current_conversion_rate[name]
for name, amount in current_investments.items()
}
data.loc[date] = converted_investments
bot.print_results()
bot.run()
# data.plot().show() # faster plot
data.plot.area().show()
for order in ptf.history:
print(order)

16
tests/orders/test_orders.py

@ -18,3 +18,19 @@ from auto_trading.orders import Short, Long
def test_order_usd(order: Union[Long, Short], usd: float):
"""Test the USD value of an order."""
assert order.amount_usd == usd
@pytest.mark.parametrize(
"order, string",
[
(Long("BTC", 1, 1), "Long(BTC: 1$)"),
(Short("BTC", 1, 1), "Short(BTC: 1$)"),
(Long("BTC", 1, 10), "Long(BTC: 10$)"),
(Short("BTC", 1, 10), "Short(BTC: 10$)"),
(Long("BTC", 10, 1), "Long(BTC: 10$)"),
(Short("BTC", 10, 1), "Short(BTC: 10$)"),
],
)
def test_repr(order: Union[Long, Short], string: str):
"""Test repr of an order."""
assert repr(order) == string

Loading…
Cancel
Save