log.warning(f"Timeout {timeout} seconds when requesting kline data.")
return1
exceptExceptionase:
log.warning(f"Exception when requesting kline data.")
return1
# Add to the database (will overwrite existing klines if any)
ifresandres.keys():
# res is dict for symbol, which is a list of record lists of 12 fields
# ==============================
# TODO: We need to check these fields for validity (presence, non-null)
# TODO: We can load maximum 999 latest klines, so if more 1600, then some other method
# TODO: Print somewhere diagnostics about how many lines are in history buffer of db, and if nans are found
results.update(res)
try:
added_count=App.analyzer.store_klines(res)
exceptExceptionase:
log.error(f"Error storing kline result in the database. Exception: {e}")
return1
else:
log.error("Received empty or wrong result from klines request.")
return1
return0
asyncdefrequest_klines(symbol,freq,limit):
"""
Request klines data from the service for one symbol. Maximum the specified number of klines will be returned.
:return: Dict with the symbol as a key and a list of klines as a value. One kline is also a list.
"""
klines_per_request=400
now_ts=now_timestamp()
startTime,endTime=get_interval(freq)
klines=[]
try:
iflimit<=klines_per_request:# Server will return these number of klines in one request
# INFO:
# - startTime: include all intervals (ids) with same or greater id: if within interval then excluding this interval; if is equal to open time then include this interval
# - endTime: include all intervals (ids) with same or smaller id: if equal to left border then return this interval, if within interval then return this interval
# - It will return also incomplete current interval (in particular, we could collect approximate klines for higher frequencies by requesting incomplete intervals)
# {"code": 1103, "msg": "An unknown parameter was sent"}
log.error(f"BinanceRequestException while requesting klines: {bre}")
return{}
exceptBinanceAPIExceptionasbae:
# {"code": 1002, "msg": "Invalid API call"}
log.error(f"BinanceAPIException while requesting klines: {bae}")
return{}
exceptExceptionase:
log.error(f"Exception while requesting klines: {e}")
return{}
#
# Post-process
#
# Find latest *full* (completed) interval in the result list.
# The problem is that the result also contains the current (still running) interval which we want to exclude
klines_full=[klforklinklinesifkl[0]<startTime]
last_full_kline=klines_full[-1]
last_full_kline_ts=last_full_kline[0]
iflast_full_kline_ts!=startTime-60_000:
log.error(f"UNEXPECTED RESULT: Last full kline timestamp {last_full_kline_ts} is not equal to previous full interval start {startTime-60_000}. Maybe some results are missing and there are gaps.")
# Return all received klines with the symbol as a key
return{symbol:klines_full}
#
# Server and account info
#
asyncdefdata_provider_health_check():
"""
Request information about the data provider server state.
"""
symbol=App.config["symbol"]
# Get server state (ping) and trade status (e.g., trade can be suspended on some symbol)