Skip to content

bot ¤

BotPlotter ¤

BotPlotter(bot: LetTradeBot)

Bases: Plotter

Class help to plot bot result

Source code in lettrade/plot/bot.py
34
35
36
37
38
39
40
41
42
def __init__(self, bot: "LetTradeBot") -> None:
    self.bot = bot
    self.feeder = bot.feeder
    self.exchange = bot.exchange
    self.account = bot.account
    self.strategy = bot.strategy

    self.datas = self.feeder.datas
    self.data = self.feeder.datas[0]

datas instance-attribute ¤

datas: list[DataFeed] = datas

All plotting datafeeds

jump ¤

jump(
    since: int | str | Timestamp | None = None,
    order_id: str | None = None,
    position_id: str | None = None,
    range: int = 300,
    name: str | None = None,
)

Jump to place on datefeed

Parameters:

  • since (int | str | Timestamp | None, default: None ) –

    Jump to index/datetime. Defaults to None.

  • order_id (str | None, default: None ) –

    Jump to order id. Defaults to None.

  • position_id (str | None, default: None ) –

    Jump to position id. Defaults to None.

  • range (int, default: 300 ) –

    number of candle plot. Defaults to 300.

  • name (str | None, default: None ) –

    description. Defaults to None.

Raises:

Source code in lettrade/plot/bot.py
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def jump(
    self,
    since: int | str | pd.Timestamp | None = None,
    order_id: str | None = None,
    position_id: str | None = None,
    range: int = 300,
    name: str | None = None,
):
    """Jump to place on datefeed

    Args:
        since (int | str | pd.Timestamp | None, optional): Jump to index/datetime. Defaults to None.
        order_id (str | None, optional): Jump to order id. Defaults to None.
        position_id (str | None, optional): Jump to position id. Defaults to None.
        range (int, optional): number of candle plot. Defaults to 300.
        name (str | None, optional): _description_. Defaults to None.

    Raises:
        RuntimeError: _description_
    """
    if since is None:
        if order_id is not None:  # Jump to order id
            if not isinstance(order_id, str):
                order_id = str(order_id)

            if order_id in self.exchange.orders:
                order = self.exchange.orders[order_id]
            elif order_id in self.exchange.history_orders:
                order = self.exchange.history_orders[order_id]
            else:
                raise RuntimeError(f"Order id {order_id} not found")

            loc = self.data.l.index.get_loc(order.placed_at)
            since = loc - int(range / 2)

        elif position_id is not None:  # Jump to position id
            if not isinstance(position_id, str):
                position_id = str(position_id)

            if position_id in self.exchange.positions:
                position = self.exchange.positions[position_id]
            elif position_id in self.exchange.history_positions:
                position = self.exchange.history_positions[position_id]
            else:
                raise RuntimeError(f"Position id {position_id} not found")

            loc = self.data.l.index.get_loc(position.entry_at)
            since = loc - int(range / 2)
        else:  # Reset
            self.jump_reset()
            return

    elif isinstance(since, str):  # Parse string to pd.Timestamp, then since=index
        since = pd.to_datetime(since, utc=True)
        since = self.data.l.index.get_loc(since)
    elif isinstance(since, pd.Timestamp):  # Get index of Timestamp
        since = self.data.l.index.get_loc(since)

    # Since min at pointer_start
    if since < self.data.l.pointer_start:
        since = self.data.l.pointer_start
    # Since max at pointer_stop
    if since > self.data.l.pointer_stop - range:
        since = self.data.l.pointer_stop - range

    self._jump_since = self.data.l.index[since]
    self._jump_to = self.data.l.index[since + range]

    # Reload data
    self.load()

jump_reset ¤

jump_reset() -> bool

Reset jump datafeeds back to bot datafeeds

Source code in lettrade/plot/bot.py
115
116
117
118
119
def jump_reset(self) -> bool:
    """Reset jump datafeeds back to bot datafeeds"""
    self._jump_since = None
    self._jump_to = None
    return True

load abstractmethod ¤

load()

Load plot config from Strategy.plot() and setup candlestick/equity

Source code in lettrade/plot/plot.py
 9
10
11
@abstractmethod
def load(self):
    """Load plot config from `Strategy.plot()` and setup candlestick/equity"""

plot abstractmethod ¤

plot(**kwargs)

Plot equity, orders, and positions then show

Source code in lettrade/plot/plot.py
13
14
15
@abstractmethod
def plot(self, **kwargs):
    """Plot `equity`, `orders`, and `positions` then show"""

stop abstractmethod ¤

stop()

stop plotter

Source code in lettrade/plot/plot.py
17
18
19
@abstractmethod
def stop(self):
    """stop plotter"""