我尝试使用 API 和 ccxt 包在 phemex 上进行交易。我像这样连接到 phemex:
phemex = ccxt.phemex({
'apiKey': API_KEY,
'secret': API_SECRET,
'adjustForTimeDifference': False,
'enableRateLimit': False,
'verbose': False})
现在我可以像这样 fethc_ohlcv :
candles = phemex.fetch_ohlcv('BTCUSD', timeframe = '5m', limit = 100)
我可以像这样得到当前时间:
print("we run the code at: ", pd.Timestamp.now())
但现在我从 fetch_ohlcv 得到这个数据:
Index Datum Open High Low Close Volume
0 2022-02-07 08:45:00 42608.0 42634.0 42608.0 42623.0 2480360.0
1 2022-02-07 08:50:00 42623.0 42680.5 42623.0 42680.5 1232756.0
2 2022-02-07 08:55:00 42680.5 42685.0 42622.0 42622.5 3700511.0
3 2022-02-07 09:00:00 42622.5 42642.5 42600.5 42627.0 6300997.0
4 2022-02-07 09:05:00 42627.5 42642.5 42613.0 42613.5 1144301.0
.. ... ... ... ... ... ...
95 2022-02-07 16:40:00 43754.5 43754.5 43526.5 43535.0 13005074.0
96 2022-02-07 16:45:00 43535.0 43675.0 43535.0 43670.5 6104987.0
97 2022-02-07 16:50:00 43671.0 43690.5 43635.0 43675.0 5581606.0
98 2022-02-07 16:55:00 43675.0 43831.0 43675.0 43813.5 3895638.0
99 2022-02-07 17:00:00 43816.0 43819.0 43730.0 43762.5 4510734.0
所以最新的时间戳是下午 5 点(我位于德国)但是我运行这段代码的时间戳是:
we run the code at: 2022-02-07 18:10:00.473305
所以现在是下午 6.10。我认为有 1 小时的差异,因为 ccxt 显示的 UTC 时间戳没问题。但是为什么获取数据会有 10 分钟的延迟呢?
PS:我只每整整 5 分钟运行一次代码,我不会太频繁地获取数据。
我希望有人可以帮助我解决这个问题。
谢谢!
最好的问候,丹尼尔
编辑完整代码:
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
import talib, requests, ccxt, schedule
import pandas as pd
import pandas_ta as ta
from pprint import pprint
API_KEY = ""
API_SECRET = ""
########## ----- Eingabe:
coin = "BTC"
leverage = 5
limit = 100
faktor = 10000000
symbol = 'BTCUSD'
zeitraum = '5m'
phemex = ccxt.phemex({
'apiKey': API_KEY,
'secret': API_SECRET,
"adjustForTimeDifference": False,
'enableRateLimit': False,
'verbose': False
})
phemex.set_leverage(leverage, 'BTC/USD:BTC')
p={"type":"swap","code":coin}
response = phemex.fetch_balance(params=p)
positions = response['info']['data']['positions']
res = next((sub for sub in positions if sub['leverage']), None)
lever = res['leverage']
free_BTC = response[coin]['free']
used_BTC = response[coin]['used']
candles = phemex.fetch_ohlcv(symbol, timeframe = zeitraum, limit = limit)
candles = pd.DataFrame(candles, columns =['Datum', 'Open', 'High', 'Low', 'Close', 'Volume'])
candles['Datum'] = pd.to_datetime(candles['Datum'], unit='ms')
actual_BTC_value = candles["Close"].iloc[-1]
free_usd = actual_BTC_value * (free_BTC + used_BTC)
trade_amount = free_usd * 0.5 * leverage
amount = trade_amount
print(candles.tail(1))
print("we run the code at: ", pd.Timestamp.now())
结果是:
PS C:\Users\user\Desktop\BTC\Phemex> c:; cd 'c:\Users\user\Desktop\BTC\Phemex'; & 'C:\Users\user\.conda\envs\python36\python.exe' 'c:\Users\user\.vscode\extensions\ms-python.python-2022.0.1786462952\pythonFiles\lib\python\debugpy\launcher' '55429' '--' 'c:\Users\user\Desktop\BTC\Phemex\tets.py'
Datum Open High Low Close Volume
98 2022-02-09 08:30:00 43353.5 43353.5 43210.0 43315.0 13009953.0
we run the code at: 2022-02-09 09:40:03.614407
PS C:\Users\danis\Desktop\BTC\Phemex>