This repository has been archived on 2025-01-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
mexc-trade/test.py
dongho 82216daaf8
All checks were successful
SonarQube Scan / SonarQube Trigger (push) Successful in 1m15s
migrationt to websocket
2024-12-22 00:57:07 +09:00

65 lines
2.1 KiB
Python

from urllib.parse import quote_plus
from dotenv import load_dotenv
from pymongo import MongoClient
from mexc_sdk import Spot
import yaml
import os
from datetime import timedelta
load_dotenv()
with open("coin.yaml", "r") as file:
data = yaml.safe_load(file)
interest_coins = data["interest"]
MONGO_URI = f'mongodb://{quote_plus(os.getenv("DB_USER"))}:{quote_plus(os.getenv("DB_PWD"))}@{os.getenv("DB_HOST")}:{os.getenv("DB_PORT")}'
client = MongoClient(MONGO_URI)
db = client[os.getenv("DB_NAME")] # Replace with your database name
if __name__ == "__main__":
target = interest_coins[0] + "USDT" + "_kline"
collection = db[target.lower()]
latest_doc = collection.find_one(sort=[("T", -1)])
latest_end_time = latest_doc["T"]
time_threshold = latest_end_time - (
60 * 100
) # 100 minutes before the latest endTime
pipeline = [
# Filter based on endTime
{"$match": {"T": {"$gte": time_threshold}}},
# Group by start time and end time
{
"$group": {
"_id": {"startTime": "$t", "endTime": "$T"},
"open": {"$first": "$o"},
"high": {"$max": "$h"},
"low": {"$min": "$l"},
"close": {"$last": "$c"},
"volume": {"$sum": "$v"},
"weighted_price": {"$sum": {"$multiply": ["$c", "$v"]}},
"count": {"$sum": 1},
}
},
# Sort by start time
{"$sort": {"_id.startTime": 1}},
# Reshape for output
{
"$project": {
"_id": 0,
"time": {"$multiply": ["$_id.startTime", 1000]},
"open": 1,
"high": 1,
"low": 1,
"close": 1,
"volume": 1,
"vwap": {"$divide": ["$weighted_price", "$volume"]},
"trades": "$count",
"endTime": "$_id.endTime",
}
},
]
candles = list(collection.aggregate(pipeline))
print(candles)