fix_prices_in_between_time_frames#

API documentation for tradingstrategy.utils.wrangle.fix_prices_in_between_time_frames Python function.

fix_prices_in_between_time_frames(dfgb, fix_inbetween_threshold=(- 0.99, 5.0), pair_id_column='pair_id')[source]#

Fix MEV bots breaking open/price.

TODO: This needs to be later fixed at the data collection level, MEV transactions masked out.

Example daily candle with a broken open price:

COMP-WETH on Uniswap v2 on Ethereum

2023-12-10 23:00:00     2023-12-10 23:00:00     56.253001       56.253001       55.948422       55.948422       348.915474      2373.049643     0.0     3.0     0.000000        348.915474      1015    18758972        18759006        NaN
2023-12-11 00:00:00     2023-12-11 00:00:00     0.363204        55.181369       0.354456        55.181369       180421.431179   2357.352148     1.0     6.0     90162.684736    90258.746443    1015    18759541        18759541        NaN
2023-12-11 02:00:00     2023-12-11 02:00:00     50.059992       50.612159       50.059992       50.612159       2859.011552     2241.581295     1.0     1.0     628.775186      2230.236366     1015    18759912        18760129        NaN

Underlying MEV TX causing the issue, ChatGPT formatted:

+--------------------------------------+-------------------------------------+-------------------+-------------+-------------------+-------------------------------------+-------------------+-------------+-------------+
|               Action                 |          Swap Amount (Token 1)      |   Value (Token 1)  |  Token 1    |    For (Token 2)   |          Swap Amount (Token 2)      |   Value (Token 2)  |  Token 2    |  Platform   |
+--------------------------------------+-------------------------------------+-------------------+-------------+-------------------+-------------------------------------+-------------------+-------------+-------------+
| Aggregated Swap of 3 Tokens           |                                     |                   |             |                   |                                     |                   |             |             |
| Swap                                 | 20,096.81048332570788104            | $956,407.21       | COMP        | ETH               | 38.247439949510380362              | $101,436.63       | ETH         | Uniswap V2  |
| Swap                                 | 65.583941528193262325               | $3,121.14         | COMP        | ETH               | 0.010044352430924858               | $26.64            | ETH         | Uniswap V2  |
| Swap                                 | 0.010044352430924858                | $26.64            | ETH         | DAI               | 23.587216376218824815              | $23.58            | DAI         | Uniswap V2  |
| Swap                                 | 65.780693352777842112               | $3,130.50         | COMP        | ETH               | 0.010014399362431825               | $26.56            | ETH         | Uniswap V2  |
| Swap                                 | 0.010014399362431825                | $26.56            | ETH         | DAI               | 23.516703895990702452              | $23.51            | DAI         | Uniswap V2  |
| Swap                                 | 65.978035432836175638               | $3,139.89         | COMP        | ETH               | 0.009984535616403163               | $26.48            | ETH         | Uniswap V2  |
| Swap                                 | 0.009984535616403163                | $26.48            | ETH         | DAI               | 23.446402724807108015              | $23.44            | DAI         | Uniswap V2  |
| Swap                                 | 66.175969539134684165               | $3,149.31         | COMP        | ETH               | 0.009954760926472085               | $26.40            | ETH         | Uniswap V2  |
| Swap                                 | 0.009954760926472085                | $26.40            | ETH         | DAI               | 23.376312226345838338              | $23.37            | DAI         | Uniswap V2  |
| Swap                                 | 5.013744524035853058                | $238.60           | COMP        | ETH               | 0.000751785040201655               | $1.99             | ETH         | Uniswap V2  |
| Swap                                 | 0.000751785040201655                | $1.99             | ETH         | DAI               | 1.765375649239336674               | $1.76             | DAI         | Uniswap V2  |
| Swap                                 | 38.247439949510380362               | $101,436.63       | ETH         | COMP              | 20,353.762314725950969279          | $968,635.55       | COMP        | Uniswap V2  |
| Swap                                 | 0.149285130679667947                | $395.92           | ETH         | COMP              | 4,200                              | $199,878.00       | COMP        | Sushiswap   |
+--------------------------------------+-------------------------------------+-------------------+-------------+-------------------+-------------------------------------+-------------------+-------------+-------------+
Parameters:
  • dfgb (pandas.core.frame.DataFrame | pandas.core.groupby.generic.DataFrameGroupBy) – Assume grouped by pair_id and MultiLevel index (pair_id, timestamp).

  • fix_inbetween_threshold (tuple | None) –