Browse Source

feat: retrive the change rate

pull/14/head
QuentinN42 1 year ago
parent
commit
383b75c434
Signed by: number42 GPG Key ID: 2CD7D563712B3A50
  1. 0
      auto_trading/bot.py
  2. 4
      auto_trading/interfaces.py
  3. 14
      auto_trading/ptf/in_memory.py
  4. 14
      main.py

0
auto_trading/main.py → auto_trading/bot.py

4
auto_trading/interfaces.py

@ -50,6 +50,10 @@ class DataBroker(ABC):
def properties(self) -> CandlesProperties:
"""Return the properties of the candles for this broker."""
@abstractproperty
def current_change(self) -> DataFrame:
"""Return the current change for each money."""
def __iter__(self) -> "DataBroker":
"""Initialise the iterator."""
return self

14
auto_trading/ptf/in_memory.py

@ -4,6 +4,7 @@ from pandas import DataFrame # type: ignore
from ..interfaces import PTF, PTFState
from ..orders import Long, Short
from ..bot import Bot
class InMemoryPortfolio(PTF):
@ -12,12 +13,17 @@ class InMemoryPortfolio(PTF):
_blanance: float
_stocks: Dict[str, float]
def __init__(self, bot_class: Bot):
"""Init the class with a pointer to the bot to retrieve the change rate."""
self.bot = bot_class
@property
def state(self):
return PTFState(
0,
self._stocks.copy(),
)
return PTFState(0, self._stocks.copy())
@property
def change_rate(self):
return self.bot.broker.change_rate
def execute_short(self, order: Short) -> None:
"""Sell actions."""

14
main.py

@ -5,9 +5,10 @@ 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.main import Bot
from auto_trading.bot import Bot
from tqdm import tqdm # type: ignore
import pandas as pd # type: ignore
pd.options.plotting.backend = "plotly"
@ -15,11 +16,13 @@ if __name__ == "__main__":
csv = "data/gold.csv"
bt = Backtest(csv, start=10, index_col=0)
start = {name: 0 for name in bt.data.columns}
start["USD"] = 10_000
pred = MeanAggregator([RandomPredictor(), SelectorPredictor({"USD": -0.1}), RandomPredictor()])
pred = MeanAggregator(
[RandomPredictor(), SelectorPredictor({"USD": -0.1}), RandomPredictor()]
)
bot = Bot(
ptf=InMemoryPortfolio(start.copy()), strategy=AllIn(), broker=bt, predictor=pred
@ -30,7 +33,10 @@ if __name__ == "__main__":
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()}
converted_investments = {
name: amount * bot.current_conversion_rate[name]
for name, amount in current_investments.items()
}
data.loc[date] = converted_investments
bot.print_results()

Loading…
Cancel
Save