我尝试使用API和ccxt包在phemex上进行交易。我这样连接到phemex:
phemex = ccxt.phemex({
'apiKey': API_KEY,
'secret': API_SECRET,
'adjustForTimeDifference': False,
'enableRateLimit': False,
'verbose': False})
现在我可以像这样处理:
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分钟?
附言:我只每5分钟运行一次代码,这样我就不会太频繁地获取数据。
我希望有人能帮我解决这个问题。
谢谢!
谨致问候,Daniel
编辑完整代码:
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:UsersuserDesktopBTCPhemex> c:; cd 'c:UsersuserDesktopBTCPhemex'; & 'C:Usersuser.condaenvspython36python.exe' 'c:Usersuser.vscodeextensionsms-python.python-2022.0.1786462952pythonFileslibpythondebugpylauncher' '55429' '--' 'c:UsersuserDesktopBTCPhemextets.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:UsersdanisDesktopBTCPhemex>
这不是10分钟的延迟。事实上,蜡烛的每个"基准"都是蜡烛开始的日期时间,因此"2022-02-09 08:30:00"的蜡烛实际上是在08:30:00打开并在08:35:00关闭的蜡烛=>然而,正如你在这里所说的延迟是5分钟,你可以通过等待超过5分钟来抵消这一延迟(意思是蜡烛"8:35:00"开始于08:35:00->08:40:00(