0

I try to trade on phemex using the API and ccxt package. I connect to the phemex like this:

phemex = ccxt.phemex({
   'apiKey': API_KEY,
   'secret': API_SECRET,
   'adjustForTimeDifference': False,
   'enableRateLimit': False,
   'verbose': False})

Now I can fethc_ohlcv like this:

candles = phemex.fetch_ohlcv('BTCUSD', timeframe = '5m', limit = 100)

and I can get the current time like this:

print("we run the code at: ", pd.Timestamp.now())

But now I get from fetch_ohlcv this data:

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

So the latest timestamp is 5 pm ( I am located in Germany) But the timestamp i run this code is:

we run the code at:  2022-02-07 18:10:00.473305

so it is 6.10 pm. I think there is 1 hour difference because ccxt is showing UTC timestamp which is ok. But why there are 10 min delay by fetching the data?

PS: I only run the code every full 5 minutes, that I do not fetch the data too often.

I hope someone can help me with this issue.

Thank you!

Best regards, Daniel

Edit full code:

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())

The resulsts are:

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> 
Dani
  • 29
  • 1
  • 8

1 Answers1

0

It's not 10 minutes delay. In fact, every 'Datum' of the candle is the DateTime of the start of the candle so the candle of '2022-02-09 08:30:00' is actually the candle opened at 08:30:00 and closed at 08:35:00 => the delay as you say here is of 5 minutes, however, you can counteract by waiting more than exactly 5 minutes to get the close data of that candle ( meaning candle'08:35:00' 'starting 08:35:00 -> 08:40:00')

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ran A
  • 746
  • 3
  • 7
  • 19