Roundtrips
Round-trip trade computation from fill records.
Aggregates fill events into complete round-trip trades with P&L, duration, high watermark, and maximum drawdown metrics.
compute_watermarks_and_drawdown(conn, run_id, symbol, direction, avg_entry, quantity, entry_ts, exit_ts)
Compute high watermark, low watermark, maximum drawdown, and bar count for a round-trip trade.
Iterates through bars during the trade period to track: - High watermark: the best unrealized P&L reached - Low watermark: the worst unrealized P&L reached - Max drawdown: the largest decline from any peak to any subsequent trough - Duration bars: number of bars from entry to exit (inclusive)
Note: Within each bar, we assume the high occurred before the low (worst-case assumption for drawdown calculation), since intra-bar sequence is unknown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
Connection
|
SQLite database connection. |
required |
run_id
|
str
|
Unique identifier of the backtest run. |
required |
symbol
|
str
|
Instrument symbol for the trade. |
required |
direction
|
str
|
Trade direction, either "LONG" or "SHORT". |
required |
avg_entry
|
float
|
Average entry price. |
required |
quantity
|
float
|
Total position quantity. |
required |
entry_ts
|
int
|
Entry timestamp in nanoseconds. |
required |
exit_ts
|
int
|
Exit timestamp in nanoseconds. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float, float, int]
|
Tuple of (high_watermark, low_watermark, max_drawdown, duration_bars) in currency units. |
Source code in src/onesecondtrader/dashboard/roundtrips.py
get_roundtrips(run_id)
Compute round-trip trades from fill records for a run.
Aggregates fills by symbol into complete round-trips, calculating entry/exit prices, P&L, duration, high watermark, and maximum drawdown for each.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_id
|
str
|
Unique identifier of the backtest run. |
required |
Returns:
| Type | Description |
|---|---|
list[dict]
|
List of round-trip dictionaries with symbol, direction, duration, max_position, |
list[dict]
|
high_watermark, max_drawdown, pnl_before_commission, pnl_after_commission, |
list[dict]
|
entry_ts, and exit_ts. |
Source code in src/onesecondtrader/dashboard/roundtrips.py
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |