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
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

data property writable ¤

data: DataFeed

Get plotting main datafeed

Returns:

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
 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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 self._datas_stored is None:
        self._datas_stored = self.datas.copy()

    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_stored.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_stored.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_stored.l.index.get_loc(since)
    elif isinstance(since, pd.Timestamp):  # Get index of Timestamp
        since = self._data_stored.l.index.get_loc(since)

    if name is None:
        name = self._data_stored.name

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

    # Jump
    jump_start_dt = None
    jump_stop_dt = None
    for i, data in enumerate(self._datas_stored):
        if i == 0:
            self.datas[i] = data.__class__(
                data=data.l[since : since + range],
                name=data.name,
                timeframe=data.timeframe,
            )
            jump_start_dt = self.data.index[0]
            jump_stop_dt = self.data.index[-1]
        else:
            self.datas[i] = data.__class__(
                data=data.loc[
                    (data.index >= jump_start_dt) & (data.index <= jump_stop_dt)
                ],
                name=data.name,
                timeframe=data.timeframe,
            )

        if hasattr(data, DATAFRAME_PLOTTERS_NAME):
            object.__setattr__(
                self.datas[i],
                DATAFRAME_PLOTTERS_NAME,
                getattr(data, DATAFRAME_PLOTTERS_NAME),
            )

    # Reload data
    self.load()

jump_reset ¤

jump_reset() -> bool

Reset jump datafeeds back to bot datafeeds

Source code in lettrade/plot/bot.py
162
163
164
165
166
167
168
def jump_reset(self) -> bool:
    """Reset jump datafeeds back to bot datafeeds"""
    if not self._datas_stored or self.data is self._data_stored:
        return False

    self.datas = self._datas_stored.copy()
    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"""