Skip to content

Horizon trade aggregations response is incorrect when requesting data in descending order #114

@tamirms

Description

@tamirms

Running the following trade aggregations request for ascending data:

 curl "https://horizon.stellar.org/trade_aggregations/?counter_asset_type=native&base_asset_code=USDC&base_asset_issuer=GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN&base_asset_type=credit_alphanum4&resolution=3600000&start_time=1728514800000&end_time=1728532800000&order=asc"

Produces the following response:

{
  "_links": {
    "self": {
      "href": "https://horizon.stellar.org/trade_aggregations/?counter_asset_type=native\u0026base_asset_code=USDC\u0026base_asset_issuer=GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN\u0026base_asset_type=credit_alphanum4\u0026resolution=3600000\u0026start_time=1728514800000\u0026end_time=1728532800000\u0026order=asc"
    },
    "next": {
      "href": "https://horizon.stellar.org/trade_aggregations/?base_asset_code=USDC\u0026base_asset_issuer=GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN\u0026base_asset_type=credit_alphanum4\u0026counter_asset_type=native\u0026end_time=1728532800000\u0026order=asc\u0026resolution=3600000\u0026start_time=1728532800000"
    },
    "prev": {
      "href": ""
    }
  },
  "_embedded": {
    "records": [
      {
        "timestamp": "1728514800000",
        "trade_count": "755",
        "base_volume": "8955.3120511",
        "counter_volume": "99768.5870573",
        "avg": "11.1407159",
        "high": "11.1719361",
        "high_r": {
          "n": "100000",
          "d": "8951"
        },
        "low": "11.1190406",
        "low_r": {
          "n": "660878300",
          "d": "59436630"
        },
        "open": "11.1719361",
        "open_r": {
          "n": "100000",
          "d": "8951"
        },
        "close": "11.1466510",
        "close_r": {
          "n": "11146651",
          "d": "1000000"
        }
      },
      {
        "timestamp": "1728518400000",
        "trade_count": "678",
        "base_volume": "18467.6782360",
        "counter_volume": "206991.4529633",
        "avg": "11.2083095",
        "high": "11.2359551",
        "high_r": {
          "n": "96000",
          "d": "8544"
        },
        "low": "11.1395913",
        "low_r": {
          "n": "10000000",
          "d": "897699"
        },
        "open": "11.1401499",
        "open_r": {
          "n": "111401499",
          "d": "10000000"
        },
        "close": "11.2270868",
        "close_r": {
          "n": "28067717",
          "d": "2500000"
        }
      },
      {
        "timestamp": "1728522000000",
        "trade_count": "1048",
        "base_volume": "16267.9553017",
        "counter_volume": "181440.7137693",
        "avg": "11.1532587",
        "high": "11.2272071",
        "high_r": {
          "n": "112272071",
          "d": "10000000"
        },
        "low": "11.1320270",
        "low_r": {
          "n": "10000000",
          "d": "898309"
        },
        "open": "11.2272071",
        "open_r": {
          "n": "112272071",
          "d": "10000000"
        },
        "close": "11.1493160",
        "close_r": {
          "n": "2787329",
          "d": "250000"
        }
      },
      {
        "timestamp": "1728525600000",
        "trade_count": "1313",
        "base_volume": "16924.6146600",
        "counter_volume": "188274.8314250",
        "avg": "11.1243201",
        "high": "12.0000000",
        "high_r": {
          "n": "12",
          "d": "1"
        },
        "low": "11.0973873",
        "low_r": {
          "n": "10000000",
          "d": "901113"
        },
        "open": "11.1340349",
        "open_r": {
          "n": "10000000",
          "d": "898147"
        },
        "close": "11.1204819",
        "close_r": {
          "n": "923",
          "d": "83"
        }
      },
      {
        "timestamp": "1728529200000",
        "trade_count": "333",
        "base_volume": "3962.4794772",
        "counter_volume": "43978.2311842",
        "avg": "11.0986647",
        "high": "11.1204819",
        "high_r": {
          "n": "923",
          "d": "83"
        },
        "low": "11.0925004",
        "low_r": {
          "n": "1000000",
          "d": "90151"
        },
        "open": "11.1044608",
        "open_r": {
          "n": "10000000",
          "d": "900539"
        },
        "close": "11.1122223",
        "close_r": {
          "n": "1000000",
          "d": "89991"
        }
      }
    ]
  }
}

But when changing the order to desc:

 curl "https://horizon.stellar.org/trade_aggregations/?counter_asset_type=native&base_asset_code=USDC&base_asset_issuer=GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN&base_asset_type=credit_alphanum4&resolution=3600000&start_time=1728514800000&end_time=1728532800000&order=desc"

I expected to receive the same response but in reverse order. However, the response appears to be incorrect. Specifically the open and close values do not match the response when the order is asc:

{
  "_links": {
    "self": {
      "href": "https://horizon.stellar.org/trade_aggregations/?counter_asset_type=native\u0026base_asset_code=USDC\u0026base_asset_issuer=GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN\u0026base_asset_type=credit_alphanum4\u0026resolution=3600000\u0026start_time=1728514800000\u0026end_time=1728532800000\u0026order=desc"
    },
    "next": {
      "href": "https://horizon.stellar.org/trade_aggregations/?base_asset_code=USDC\u0026base_asset_issuer=GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN\u0026base_asset_type=credit_alphanum4\u0026counter_asset_type=native\u0026end_time=1728514800000\u0026order=desc\u0026resolution=3600000\u0026start_time=1728514800000"
    },
    "prev": {
      "href": ""
    }
  },
  "_embedded": {
    "records": [
      {
        "timestamp": "1728529200000",
        "trade_count": "333",
        "base_volume": "3962.4794772",
        "counter_volume": "43978.2311842",
        "avg": "11.0986647",
        "high": "11.1204819",
        "high_r": {
          "n": "923",
          "d": "83"
        },
        "low": "11.0925004",
        "low_r": {
          "n": "1000000",
          "d": "90151"
        },
        "open": "11.1050404",
        "open_r": {
          "n": "2500000",
          "d": "225123"
        },
        "close": "11.1122223",
        "close_r": {
          "n": "1000000",
          "d": "89991"
        }
      },
      {
        "timestamp": "1728525600000",
        "trade_count": "1313",
        "base_volume": "16924.6146600",
        "counter_volume": "188274.8314250",
        "avg": "11.1243201",
        "high": "12.0000000",
        "high_r": {
          "n": "12",
          "d": "1"
        },
        "low": "11.0973873",
        "low_r": {
          "n": "10000000",
          "d": "901113"
        },
        "open": "11.1493160",
        "open_r": {
          "n": "2787329",
          "d": "250000"
        },
        "close": "11.1393803",
        "close_r": {
          "n": "111393803",
          "d": "10000000"
        }
      },
      {
        "timestamp": "1728522000000",
        "trade_count": "1048",
        "base_volume": "16267.9553017",
        "counter_volume": "181440.7137693",
        "avg": "11.1532587",
        "high": "11.2272071",
        "high_r": {
          "n": "112272071",
          "d": "10000000"
        },
        "low": "11.1320270",
        "low_r": {
          "n": "10000000",
          "d": "898309"
        },
        "open": "11.1754693",
        "open_r": {
          "n": "111754693",
          "d": "10000000"
        },
        "close": "11.1493160",
        "close_r": {
          "n": "2787329",
          "d": "250000"
        }
      },
      {
        "timestamp": "1728518400000",
        "trade_count": "678",
        "base_volume": "18467.6782360",
        "counter_volume": "206991.4529633",
        "avg": "11.2083095",
        "high": "11.2359551",
        "high_r": {
          "n": "96000",
          "d": "8544"
        },
        "low": "11.1395913",
        "low_r": {
          "n": "10000000",
          "d": "897699"
        },
        "open": "11.2048193",
        "open_r": {
          "n": "930",
          "d": "83"
        },
        "close": "11.2270868",
        "close_r": {
          "n": "28067717",
          "d": "2500000"
        }
      },
      {
        "timestamp": "1728514800000",
        "trade_count": "755",
        "base_volume": "8955.3120511",
        "counter_volume": "99768.5870573",
        "avg": "11.1407159",
        "high": "11.1719361",
        "high_r": {
          "n": "100000",
          "d": "8951"
        },
        "low": "11.1190406",
        "low_r": {
          "n": "660878300",
          "d": "59436630"
        },
        "open": "11.1607143",
        "open_r": {
          "n": "625",
          "d": "56"
        },
        "close": "11.1594687",
        "close_r": {
          "n": "111594687",
          "d": "10000000"
        }
      }
    ]
  }
}

This bug does not occur when using a resolution of 1 minute. But the bug does occur when using higher resolutions (e.g. 5 mins, 15 mins, 1 hour, etc)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    To Do

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions