carkod / binbot

An API, React app dashboard, Telegram bot to monitor and trigger bots for cryptocurrency trading
5 stars 14 forks source link

Improve raw_lines aggregation #565

Closed carkod closed 2 months ago

carkod commented 4 months ago

Currently only support "15m" candlesticks. Previous attempt didn't work some numbers like high and low were correct, but open and close times were not grouping correctly and close and open numbers were always wrong compared to Binance charts, using the following aggregation query:

    {"$match": { "symbol": symbol }},
                {"$group": {
                    "_id": {
                        "_id": "$_id",
                        "symbol": "$symbol",
                        "close_time": {
                            "$dateTrunc": {
                                "date": "$close_time",
                                "unit": unit,
                                "binSize": bin_size,
                                "timezone": timezone.key,
                            },
                        },
                        "open_time": {
                            "$dateTrunc": {
                                "date": "$open_time",
                                "unit": unit,
                                "binSize": bin_size,
                                "timezone": timezone.key,
                            },
                        },
                        "end_time": "$end_time",
                    },
                    "high": { "$max": "$high" },
                    "low": { "$min": "$low" },
                    "open": { "$first": "$open" },
                    "close": { "$last": "$close" },
                    "volume": { "$sum": "$volume" },
                }},
                {"$unwind": "$_id"},
                {
                    "$addFields": {
                        "symbol": "$_id.symbol",
                        "close_time": "$_id.close_time",
                        "open_time": "$_id.open_time",
                        "interval": interval,
                        "end_time": "$_id.end_time",
                    }
                },
                {"$set": {"_id" : "$_id._id"}},
                {"$limit": limit},
                {"$skip": offset},
                {"$sort": {"close_time": DESCENDING}},
            ])
carkod commented 2 months ago

Completed by https://github.com/carkod/binbot/pull/579