Browse Source

added USD to plot

pull/24/head
ErwanFagnou 7 months ago
parent
commit
e9b401e8dc
  1. 12
      auto_trading/interfaces.py
  2. 20
      main.py

12
auto_trading/interfaces.py

@ -6,6 +6,8 @@ import logging
from abc import ABC, abstractmethod, abstractproperty
from dataclasses import dataclass, field
from datetime import datetime
import pandas as pd
from pandas import DataFrame, Timedelta, Series # type: ignore
from typing import Dict, List, Optional, Any, Callable
@ -25,6 +27,9 @@ class PTFState:
for stock_name, amount in self.stocks.items()
)
def to_dict(self):
return {'USD': self.balance, **self.stocks}
@dataclass
class Order:
@ -143,7 +148,7 @@ class PTF(ABC):
executors: Dict[Any, Callable[[Any, Any], None]] = {}
orders_history: List[Order]
states_history: List[PTFState]
states_history: pd.DataFrame
def __init__(self, skip_errors: bool = True, save_errors: bool = True):
"""Init the class.
@ -154,7 +159,7 @@ class PTF(ABC):
"""
self.logger = logging.getLogger(self.__class__.__name__)
self.orders_history = []
self.states_history = []
self.states_history = pd.DataFrame()
self.skip_errors = skip_errors
self.save_errors = save_errors
@ -189,7 +194,8 @@ class PTF(ABC):
order.successfull = False
if self.save_errors or order.successfull:
self.orders_history.append(order)
self.states_history.append(self.state)
# self.states_history.append(self.state.to_dict(), ignore_index=True)
self.states_history = self.states_history.append(self.state.to_dict(), ignore_index=True)
def _execute(self, order: Order) -> None:
"""Execute one order.

20
main.py

@ -2,10 +2,15 @@ import pandas as pd # type: ignore
import logging
from auto_trading.broker.backtest import Backtest
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
from auto_trading.strat.hold import Hold
from auto_trading.ptf.in_memory import InMemoryPortfolio
from auto_trading.bot import Bot
from auto_trading.strat.prop import Prop
pd.options.plotting.backend = "plotly"
logging.basicConfig(level=logging.DEBUG)
@ -16,7 +21,9 @@ if __name__ == "__main__":
ptf = InMemoryPortfolio(
base_balance=100, change_rate_getter=lambda: bt.current_change
)
strategy = Hold("GOOGL")
# strategy = Hold("GOOGL")
# strategy = Prop({"EMA": EMA(10)})
strategy = Yoyo('GOOGL')
bot = Bot(ptf, strategy, bt)
@ -27,9 +34,10 @@ if __name__ == "__main__":
print(bot.ptf.total_balance(bot.broker.current_change))
# plot the history
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,
)
ch_history['USD'] = 1
st_history = bot.ptf.states_history
st_history.index = ch_history.index
(st_history * ch_history).fillna(0).plot.area().show()

Loading…
Cancel
Save