Skip to content

Instantly share code, notes, and snippets.

@marcosan93
Created January 13, 2022 22:47
Show Gist options
  • Select an option

  • Save marcosan93/85a9ed9e1bef05cbe58ab08c42c12d63 to your computer and use it in GitHub Desktop.

Select an option

Save marcosan93/85a9ed9e1bef05cbe58ab08c42c12d63 to your computer and use it in GitHub Desktop.
def getIntradayPrices(crypto, n_hours, training_hours, mov_avg):
"""
Gets crypto prices from now to N days ago and training amount will be in addition
to the number of days to train. (According to the EOD documentation: The maximum
periods between ‘from’ and ‘to’ are 120 days for 1-minute interval, 600 days
for 5-minute interval and 7200 days for 1 hour interval.)
There also appears to be a time delay on the data of about 15-20 hours, which
is added to the from(ago) variable.
"""
# Getting the time from N hours ago
ago = datetime.utcnow() - timedelta(hours=n_hours+training_hours+15)
# Getting the unix timestamp format for the intraday data from the API
timestamp_unix = str(calendar.timegm(ago.utctimetuple()))
# Retrieving price data
resp = client.get_prices_intraday(
crypto+'-USD.CC',
interval="1h",
from_=timestamp_unix
)
# Removing the last time period for uniformity if necessary
if resp[-1]['datetime'][-5:-3] != "00":
resp = resp[:-1]
# Price formatting for the dataframe
prices = pd.DataFrame(resp)
# Filling NaNs with the most recent values for any missing data
prices = prices.fillna(method='ffill')
# Getting the N Day Moving Average and rounding the values for some light data preprocessing
prices['MA'] = prices[['open']].rolling(
window=mov_avg
).mean().apply(lambda x: round(x, 6))
# Resetting format for FBP
prices = prices.rename(
columns={"datetime": "ds", "MA": "y"}
)
return prices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment