Price by day to Price by Week on Python Dict - python

I have a dictionary of some stock price for the last 252 business days. This dictionary contains 252 items with the structure timestamp=price.
year_prices = {'1584662400000': 85.5059967041, '1584921600000': 86.858001709, '1585008000000': 101.0, '1585094400000': 107.8499984741, '1585180800000': 105.6320037842, '1585267200000': 102.8720016479, '1585526400000': 100.4260025024, '1585612800000': 104.8000030518, '1585699200000': 96.31199646, '1585785600000': 90.8939971924, '1585872000000': 96.0019989014, '1586131200000': 103.2480010986, '1586217600000': 109.0899963379, '1586304000000': 109.7679977417, '1586390400000': 114.5999984741, '1586736000000': 130.1900024414, '1586822400000': 141.9779968262, '1586908800000': 145.966003418, '1586995200000': 149.0420074463, '1587081600000': 150.7779998779, '1587340800000': 149.2720031738, '1587427200000': 137.3439941406, '1587513600000': 146.4219970703, '1587600000000': 141.1260070801, '1587686400000': 145.0299987793, '1587945600000': 159.75, '1588032000000': 153.824005127, '1588118400000': 160.1020050049, '1588204800000': 156.3760070801, '1588291200000': 140.2640075684, '1588550400000': 152.2380065918, '1588636800000': 153.641998291, '1588723200000': 156.5160064697, '1588809600000': 156.0079956055, '1588896000000': 163.8840026855, '1589155200000': 162.2579956055, '1589241600000': 161.8820037842, '1589328000000': 158.1920013428, '1589414400000': 160.6660003662, '1589500800000': 159.8339996338, '1589760000000': 162.7259979248, '1589846400000': 161.6020050049, '1589932800000': 163.1119995117, '1590019200000': 165.5200042725, '1590105600000': 163.3760070801, '1590451200000': 163.7740020752, '1590537600000': 164.046005249, '1590624000000': 161.1620025635, '1590710400000': 167.0, '1590969600000': 179.6199951172, '1591056000000': 176.31199646, '1591142400000': 176.5919952393, '1591228800000': 172.8760070801, '1591315200000': 177.1320037842, '1591574400000': 189.9839935303, '1591660800000': 188.1340026855, '1591747200000': 205.0099945068, '1591833600000': 194.5679931641, '1591920000000': 187.0559997559, '1592179200000': 198.1799926758, '1592265600000': 196.425994873, '1592352000000': 198.358001709, '1592438400000': 200.7920074463, '1592524800000': 200.1799926758, '1592784000000': 198.8639984131, '1592870400000': 200.3560028076, '1592956800000': 192.1699981689, '1593043200000': 197.1959991455, '1593129600000': 191.9479980469, '1593388800000': 201.8699951172, '1593475200000': 215.9620056152, '1593561600000': 223.925994873, '1593648000000': 241.7319946289, '1593993600000': 274.3160095215, '1594080000000': 277.9719848633, '1594166400000': 273.175994873, '1594252800000': 278.8559875488, '1594339200000': 308.9299926758, '1594598400000': 299.4119873047, '1594684800000': 303.3599853516, '1594771200000': 309.2019958496, '1594857600000': 300.1279907227, '1594944000000': 300.1679992676, '1595203200000': 328.6000061035, '1595289600000': 313.6719970703, '1595376000000': 318.466003418, '1595462400000': 302.6140136719, '1595548800000': 283.3999938965, '1595808000000': 307.9200134277, '1595894400000': 295.2980041504, '1595980800000': 299.8219909668, '1596067200000': 297.4979858398, '1596153600000': 286.1520080566, '1596412800000': 297.0, '1596499200000': 297.3999938965, '1596585600000': 297.0039978027, '1596672000000': 297.9159851074, '1596758400000': 290.5419921875, '1597017600000': 283.7139892578, '1597104000000': 274.8779907227, '1597190400000': 310.9519958496, '1597276800000': 324.200012207, '1597363200000': 330.141998291, '1597622400000': 367.1279907227, '1597708800000': 377.4179992676, '1597795200000': 375.7059936523, '1597881600000': 400.3659973145, '1597968000000': 409.9960021973, '1598227200000': 402.8399963379, '1598313600000': 404.6679992676, '1598400000000': 430.6340026855, '1598486400000': 447.75, '1598572800000': 442.6799926758, '1598832000000': 498.3200073242, '1598918400000': 475.049987793, '1599004800000': 447.3699951172, '1599091200000': 407.0, '1599177600000': 418.3200073242, '1599523200000': 330.2099914551, '1599609600000': 366.2799987793, '1599696000000': 371.3399963379, '1599782400000': 372.7200012207, '1600041600000': 419.6199951172, '1600128000000': 449.7600097656, '1600214400000': 441.7600097656, '1600300800000': 423.4299926758, '1600387200000': 442.1499938965, '1600646400000': 449.3900146484, '1600732800000': 424.2300109863, '1600819200000': 380.3599853516, '1600905600000': 387.7900085449, '1600992000000': 407.3399963379, '1601251200000': 421.200012207, '1601337600000': 419.0700073242, '1601424000000': 429.0100097656, '1601510400000': 448.1600036621, '1601596800000': 415.0899963379, '1601856000000': 425.6799926758, '1601942400000': 413.9800109863, '1602028800000': 425.299987793, '1602115200000': 425.9200134277, '1602201600000': 434.0, '1602460800000': 442.299987793, '1602547200000': 446.6499938965, '1602633600000': 461.299987793, '1602720000000': 448.8800048828, '1602806400000': 439.6700134277, '1603065600000': 430.8299865723, '1603152000000': 421.9400024414, '1603238400000': 422.6400146484, '1603324800000': 425.7900085449, '1603411200000': 420.6300048828, '1603670400000': 420.2799987793, '1603756800000': 424.6799926758, '1603843200000': 406.0199890137, '1603929600000': 410.8299865723, '1604016000000': 388.0400085449, '1604275200000': 400.5100097656, '1604361600000': 423.8999938965, '1604448000000': 420.9800109863, '1604534400000': 438.0899963379, '1604620800000': 429.950012207, '1604880000000': 421.2600097656, '1604966400000': 410.3599853516, '1605052800000': 417.1300048828, '1605139200000': 411.7600097656, '1605225600000': 408.5, '1605484800000': 408.0899963379, '1605571200000': 441.6099853516, '1605657600000': 486.6400146484, '1605744000000': 499.2699890137, '1605830400000': 489.6099853516, '1606089600000': 521.8499755859, '1606176000000': 555.3800048828, '1606262400000': 574.0, '1606435200000': 585.7600097656, '1606694400000': 567.5999755859, '1606780800000': 584.7600097656, '1606867200000': 568.8200073242, '1606953600000': 593.3800048828, '1607040000000': 599.0399780273, '1607299200000': 641.7600097656, '1607385600000': 649.8800048828, '1607472000000': 604.4799804688, '1607558400000': 627.0700073242, '1607644800000': 609.9899902344, '1607904000000': 639.8300170898, '1607990400000': 633.25, '1608076800000': 622.7700195312, '1608163200000': 655.9000244141, '1608249600000': 695.0, '1608508800000': 649.8599853516, '1608595200000': 640.3400268555, '1608681600000': 645.9799804688, '1608768000000': 661.7700195312, '1609113600000': 663.6900024414, '1609200000000': 665.9899902344, '1609286400000': 694.7800292969, '1609372800000': 705.6699829102, '1609718400000': 729.7700195312, '1609804800000': 735.1099853516, '1609891200000': 755.9799804688, '1609977600000': 816.0399780273, '1610064000000': 880.0200195312, '1610323200000': 811.1900024414, '1610409600000': 849.4400024414, '1610496000000': 854.4099731445, '1610582400000': 845.0, '1610668800000': 826.1599731445, '1611014400000': 844.549987793, '1611100800000': 850.450012207, '1611187200000': 844.9899902344, '1611273600000': 846.6400146484, '1611532800000': 880.799987793, '1611619200000': 883.0900268555, '1611705600000': 864.1599731445, '1611792000000': 835.4299926758, '1611878400000': 793.5300292969, '1612137600000': 839.8099975586, '1612224000000': 872.7899780273, '1612310400000': 854.6900024414, '1612396800000': 849.9899902344, '1612483200000': 852.2299804688, '1612742400000': 863.4199829102, '1612828800000': 849.4600219727, '1612915200000': 804.8200073242, '1613001600000': 811.6599731445, '1613088000000': 816.1199951172, '1613433600000': 796.2199707031, '1613520000000': 798.1500244141, '1613606400000': 787.3800048828, '1613692800000': 781.299987793, '1613952000000': 714.5, '1614038400000': 698.8400268555, '1614124800000': 742.0200195312, '1614211200000': 682.2199707031, '1614297600000': 675.5, '1614556800000': 718.4299926758, '1614643200000': 686.4400024414, '1614729600000': 653.200012207, '1614816000000': 621.4400024414, '1614902400000': 597.950012207, '1615161600000': 563.0, '1615248000000': 673.5800170898, '1615334400000': 668.0599975586, '1615420800000': 699.5999755859, '1615507200000': 693.7299804688, '1615766400000': 707.9400024414, '1615852800000': 676.8800048828, '1615939200000': 701.8099975586, '1616025600000': 653.1599731445, '1616112000000': 654.8699951172}
Now I need to process this dict and group their values by week, so that I get a 52 items long dictionary (of structure timestamp:price) being price the average price for that week from the original dict.
I can't figure out a way of getting the last 52 weeks dates in python, and also I have dubts on how to go from having the dates to getting the prices by week on a pythonic way.

There are (at least) two possible solutions that will work.
Solution 1
By using pandas you can use the following steps:
Convert the data to a DataFrame
Cast the timestamp columns to a datetime object
Filter the dates (filter/select rows of pandas dataframe by timestamp column)
Code
import pandas as pd
df = pd.DataFrame(year_prices.items(), columns=['timestamp', 'price'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
# Example filtering times
df = df[(df['timestamp'] > '2020-07-01') & (df['timestamp'] < '2021-03-21')]
Solution 2
You can iterate over the data yourself, and use the built in datetime libary.
See:
How do I create a datetime in Python from milliseconds?
Code
import datetime
from pprint import pprint
start_date = datetime.datetime(2020, 7, 2)
end_date = datetime.datetime(2021, 3, 21)
records = {}
for milliseconds, price in year_prices.items():
date = datetime.datetime.fromtimestamp(int(milliseconds) / 1000.)
if start_date < date < end_date:
records[date] = price
pprint(records)

Related

The python snscrape modules facing problem in request

import snscrape.modules.twitter as sntwitter
import pandas as pd
import datetime
query = "elonmusk"
limit = 10000
start_date = datetime.datetime(2023, 1, 27)
end_date = datetime.datetime(2023, 1, 28)
tweets = sntwitter.TwitterSearchScraper(query).get_items()
index = 0
df = pd.DataFrame(columns=['Date','Username' ,'Tweet'])
for tweet in tweets:
# filter by date
if ((start_date.date() <= tweet.date.date()) and (end_date.date() >= tweet.date.date())):
# hit the limit to quit
if index == limit:
break
df2 = {'Date': tweet.date, 'Username': tweet.user.username, 'Tweet': tweet.rawContent}
df = pd.concat([df, pd.DataFrame.from_records([df2])])
index = index + 1
# out out date to quit
elif (start_date.date() > tweet.date.date()):
break
# Converting time zone from UTC to GMT+8
df['Date'] = df['Date'].dt.tz_convert('Etc/GMT+8')
print(df)
when i use the snscrape it cant work, and i have cheked the version is new snscrape 0.5.0.20230113, but it still have error.
i checked the snscrape version, here is the error information:
Error retrieving https://api.twitter.com/2/search/adaptive.json?include_profile_interstitial_type=1&include_blocking=1&include_blocked_by=1&include_followed_by=1&include_want_retweets=1&include_mute_edge=1&include_can_dm=1&include_can_media_tag=1&skip_status=1&cards_platform=Web-12&include_cards=1&include_ext_alt_text=true&include_quote_count=true&include_reply_count=1&tweet_mode=extended&include_entities=true&include_user_entities=true&include_ext_media_color=true&include_ext_media_availability=true&send_error_codes=true&simple_quoted_tweets=true&q=elonmusk&tweet_search_mode=live&count=100&query_source=spelling_expansion_revert_click&pc=1&spelling_corrections=1&ext=mediaStats%2ChighlightedLabel: non-200 status code
4 requests to https://api.twitter.com/2/search/adaptive.json?include_profile_interstitial_type=1&include_blocking=1&include_blocked_by=1&include_followed_by=1&include_want_retweets=1&include_mute_edge=1&include_can_dm=1&include_can_media_tag=1&skip_status=1&cards_platform=Web-12&include_cards=1&include_ext_alt_text=true&include_quote_count=true&include_reply_count=1&tweet_mode=extended&include_entities=true&include_user_entities=true&include_ext_media_color=true&include_ext_media_availability=true&send_error_codes=true&simple_quoted_tweets=true&q=elonmusk&tweet_search_mode=live&count=100&query_source=spelling_expansion_revert_click&pc=1&spelling_corrections=1&ext=mediaStats%2ChighlightedLabel failed, giving up.

Time difference between two timedate columns without considering Non-business hours

I want to calculate difference between two time columns without considering non-business hours. I have used pyholidays, which worked totally fine. But even when i define starttime and endtime for Business-duration, Result still includes Non-Business Hours as you shown in attached photos.
for index, row in df.iterrows():
first=row['New']
second=row['Assigned']
third=row['In Progress']
if(pd.notnull(second)):
starttime = (8,0,0)
endtime = (17,0,0)
holidaylist = pyholidays.Germany()
unit='hour'
row['AP'] = businessDuration(first,second,holidaylist=holidaylist,unit=unit)
else:
starttime = (8,0,0)
endtime = (17,0,0)
holidaylist = pyholidays.Germany()
unit='hour'
row['AP'] = businessDuration(first,third,holidaylist=holidaylist,unit=unit)
ap.append(row['AP'])
DataFrame
Printed Result
Thank you for your suggestion. I have tried your method, i have also defined calendar instance. Later i was getting 'relativedelta' error which i have somehow solved by 'dateutil'. Now i am at final stage to compute business-hour difference between two columns.
`de_holidays = pyholidays.Germany()
cal = Calendar(holidays=de_holidays, weekdays=['Saturday', 'Sunday'])
df['rp'] = df.apply(lambda row: compute_bizhours_diff(row['Resolved'], row['Pending'], cal=cal, biz_open_time = time(8, 0, 0), biz_close_time = time(17, 0, 0)), axis=1)`
Now i am getting error about month number, which can not be nan. I have also attached photo of errors.
Pic1
Pic2
I do not know if this works, but try this:
# == Imports needed ===========================
from __future__ import annotations
from typing import Any
import pandas as pd
import holidays as pyholidays
from datetime import time
from bizdays import Calendar
from dateutil.relativedelta import relativedelta
# == Functions ==================================
def is_null_dates(*dates: Any) -> bool:
"""Determine whether objects are valid dates.
Parameters
----------
dates : Any
Variables to check whether they hold a valid date, or not.
Returns
-------
bool
True, if at least one informed value is not a date.
False otherwise.
"""
for date in dates:
if pd.isna(pd.to_datetime(date, errors='coerce')):
return True
return False
def compute_bizhours_diff(
start_date: str | pd.Timestamp,
end_date: str | pd.Timestamp,
biz_open_time: datetime.time | None = None,
biz_close_time: datetime.time | None = None,
cal: bizdays.Calendar | None = None,
) -> float:
"""Compute the number of business hours between two dates.
Parameters
----------
start_date : str | pd.Timestamp
The first date.
end_date : str | pd.Timestamp
The final date.
biz_open_time : datetime.time | None
The beginning hour/minute of a business day.
biz_close_time : datetime.time | None
The ending hour/minute of a business day.
cal : bizdays.Calendar | None
The calendar object used to figure out the number of days between `start_date`
and `end_date` that are not holidays. If None, consider every day as a business day,
except Saturdays, or Sundays.
Returns
-------
float
The total number of business hours between `start_date`, and `end_date`.
Examples
--------
>>> import holidays as pyholidays
>>> from datetime import time
>>> from bizdays import Calendar
>>> # 2022-09-07 is a national holiday in Brazil, therefore only
>>> # the hours between 2022-09-08 09:00:00, and 2022-09-08 15:48:00
>>> # should be considered. This should equal 6.8 hours.
>>> start_date = pd.to_datetime('2022-09-07 15:55:00')
>>> end_date = pd.to_datetime('2022-09-08 15:48:00')
>>> BR_holiday_list = pyholidays.BR(years={start_date.year, end_date.year}, state='RJ')
>>> cal = Calendar(holidays=BR_holiday_list, weekdays=['Saturday', 'Sunday'])
>>> print(compute_bizhours_diff(start_date, end_date, cal=cal))
6.8
>>> # Both dates in the next example are holidays, therefore, the result should be 0.0
>>> start_date = pd.to_datetime('2022-09-07 15:55:00')
>>> end_date = pd.to_datetime('2022-09-07 15:48:00')
>>> print(compute_bizhours_diff(start_date, end_date, cal=cal))
0.0
>>> # What if the end_date preceeds start_date by mistake?
>>> # In such cases, we switch start_date to end_date, and vice-versa.
>>> start_date = pd.to_datetime('2022-09-02 00:00:00')
>>> end_date = pd.to_datetime('2022-09-01 15:55:00')
>>> print(compute_bizhours_diff(start_date, end_date, cal=cal))
2.0833333333333335
>>> # What if the start_date, and end_date begin and finish on the same day, but they both have timestamps that end before
>>> # or after the business hours?
>>> # In such cases, the total number of hours is equal to 0.0
>>> start_date = pd.to_datetime('2022-09-02 00:00:00')
>>> end_date = pd.to_datetime('2022-09-02 8:00:00')
>>> print(compute_bizhours_diff(start_date, end_date, cal=cal))
0.0
"""
if is_null_dates(start_date, end_date):
return pd.NA
if biz_open_time is None:
biz_open_time = time(9, 0, 0)
if biz_close_time is None:
biz_close_time = time(18, 0, 0)
if cal is None:
cal = Calendar(weekdays=['Saturday', 'Sunday'])
open_delta = relativedelta(hour=biz_open_time.hour, minute=biz_open_time.minute)
end_delta = relativedelta(hour=biz_close_time.hour, minute=biz_close_time.minute)
start_date = pd.to_datetime(start_date)
end_date = pd.to_datetime(end_date)
_end_date = max(start_date, end_date)
_start_date = min(start_date, end_date)
start_date = _start_date
end_date = _end_date
start_date = (
start_date if cal.isbizday(start_date) else cal.following(start_date) + open_delta
)
end_date = (
end_date if cal.isbizday(end_date) else cal.preceding(end_date) + end_delta
)
if end_date < start_date:
return 0.00
start_date_biz = max(start_date, start_date + open_delta)
end_first_day = start_date_biz + end_delta
end_date_biz = min(
end_date,
end_date + end_delta
)
start_last_day = end_date_biz + open_delta
if start_last_day > end_date:
end_date_biz = start_last_day
if end_first_day < start_date:
end_first_day = start_date_biz
if end_first_day.date() == end_date_biz.date():
return (end_date_biz - start_date_biz).seconds / 3600
return (
(end_first_day - start_date_biz).seconds
+ (end_date_biz - start_last_day).seconds
+ (
max((len(list(cal.seq(start_date, end_date))) - 2), 0)
* (end_first_day - (start_date + open_delta)).seconds
)
) / 3600
Before running the preceding code, you need to install the following packages, if you do not already have them:
pip install holidays bizdays
Link to both packages' documentation:
bizdays
python-holidays
Examples
Here is how you can use compute_bizhours_diff:
import pandas as pd
import holidays as pyholidays
from datetime import time
from bizdays import Calendar
# OPTIONAL: define custom start, and end to your business hours.
biz_open_time = time(9, 0, 0)
biz_close_time = time(18, 0, 0)
# Define your start, and end dates.
start_date = pd.to_datetime('2022-09-07 04:48:00')
end_date = pd.to_datetime('2022-09-10 15:55:00')
# Create a list of holidays, and create a Calendar instance.
BR_holiday_list = pyholidays.BR(years={start_date.year, end_date.year}, state='RJ')
# For German holidays, you can use something like:
German_holiday_list = pyholidays.Germany(years={start_date.year, end_date.year})
# Define the Calendar instance. Here, we use the German holidays, excluding Saturday, and Sunday from weekdays.
cal = Calendar(holidays=German_holiday_list, weekdays=['Saturday', 'Sunday'])
# Finally, compute the total number of working hours between your two dates:
compute_bizhours_diff(start_date, end_date, cal=cal)
# Returns: 27.0
You can also use the function with pandas dataframes, using apply:
df['working_hours_delta'] = df.apply(lambda row: compute_bizhours_diff(row[START_DATE_COLNAME], row[END_DATE_COLNAME], cal=cal), axis=1)
Notes
The function compute_bizhours_diff is far from perfect. Before using it in any production environment, or for any serious use case, I strongly recommend refactoring it.
Edit
I made some changes to the original answer, to account for instances where start_date, or end_date have null or invalid representations of dates.
Using the example dataframe from your question it now runs fine:
de_holidays = pyholidays.Germany()
cal = Calendar(holidays=de_holidays, weekdays=['Saturday', 'Sunday'])
df = pd.DataFrame(
{
'Assigned': [None, '2022-07-28 10:53:00', '2022-07-28 18:08:00', None, '2022-07-29 12:56:00'],
'In Progress': ['2022-08-01 10:53:00', '2022-08-02 09:32:00', '2022-07-29 12:08:00', '2022-08-02 10:23:00', '2022-07-29 14:54:00'],
'New': ['2022-07-27 15:01:00', '2022-07-28 10:09:00', '2022-07-28 13:37:00', '2022-07-29 00:12:00', '2022-07-29 09:51:00'],
}
).apply(pd.to_datetime)
df['rp'] = df.apply(
lambda row: compute_bizhours_diff(
row['Assigned'], row['In Progress'], cal=cal, biz_open_time = time(8, 0, 0), biz_close_time = time(17, 0, 0)
), axis=1
)
print(df)
# Prints:
# Assigned In Progress New rp
# 0 NaT 2022-08-01 10:53:00 2022-07-27 15:01:00 <NA>
# 1 2022-07-28 10:53:00 2022-08-02 09:32:00 2022-07-28 10:09:00 25.65
# 2 2022-07-28 18:08:00 2022-07-29 12:08:00 2022-07-28 13:37:00 4.133333
# 3 NaT 2022-08-02 10:23:00 2022-07-29 00:12:00 <NA>
# 4 2022-07-29 12:56:00 2022-07-29 14:54:00 2022-07-29 09:51:00 1.966667

Get prices of all Cryptocurrencies

I I am trying to make a Crypto Barometer. I have a little piece of code that gets the price in USD for each symbol. Now I want to add them up and get the total of these coins (the prices of one of each coin). I got the realtime prices, but I don't know how to add them up. I also want the price of each symbol one, four, eight and 24 hours ago...
In the end it should look like this :
Current 1Hour ... 24Hours
BTCUSDT $49343.34 BTCUSDT $49133.12 BTCUSDT $48763.34
... ... ..
ETHUSDT $2123.84 ETHUSDT $2087.53 ETHUSDT $1987.23
sum : $6255422.23 Sum : $6249983m92 Sum : 6187291.51
Here is my code so far:
import requests
import json
import datetime
import time
api_request = requests.get('https://api.binance.com/api/v3/ticker/price')
api = json.loads(api_request.content)
for x in api:
print(x['symbol'], "${0:.4f}".format(float(x['price'])))
# THE PART WHERE I GOT DIFFERENT TIMES
while True:
dt = datetime
cur_time = (dt.datetime.now().strftime('%d-%m %H:%M'))
one_hour = (dt.datetime.now() - dt.timedelta(hours=1)).strftime('%d-%m %H:%M')
four_hours = (dt.datetime.now() - dt.timedelta(hours=4)).strftime('%d-%m %H:%M')
eight_hours = (dt.datetime.now() - dt.timedelta(hours=8)).strftime('%d-%m %H:%M')
one_day = (dt.datetime.now() - dt.timedelta(hours=24)).strftime('%d-%m %H:%M')
print(cur_time)
print(one_hour)
print(four_hours)
print(eight_hours)
print(one_day)
time.sleep(60)
there is a API library to get prices of nearly every crypto
import cryptocompare
def crypto_price('BTC'):
coin_acronym = str(acronyms['BTC'])
price_crypto = cryptocompare.get_price(coin_acronym, currency='USD', full=True).get('RAW').get(coin_acronym).get(
'USD').get(
'PRICE')
return price_crypto

How can i sort Binance historical candles for multiple pairs across multiple timeframes

I'm downloading historical candlestick data for multiple crypto pairs across different timeframes from the binance api, i would like to know how to sort this data according to pair and timeframe and check which pair on which timeframe executes my code, the following code is what i use to get historical data
import requests
class BinanceFuturesClient:
def __init__(self):
self.base_url = "https://fapi.binance.com"
def make_requests(self, method, endpoint, data):
if method=="GET":
response = requests.get(self.base_url + endpoint, params=data)
return response.json()
def get_symbols(self):
symbols = []
exchange_info = self.make_requests("GET", "/fapi/v1/exchangeInfo", None)
if exchange_info is not None:
for symbol in exchange_info['symbols']:
if symbol['contractType'] == 'PERPETUAL' and symbol['quoteAsset'] == 'USDT':
symbols.append(symbol['pair'])
return symbols
def initial_historical_data(self, symbol, interval):
data = dict()
data['symbol'] = symbol
data['interval'] = interval
data['limit'] = 35
raw_candle = self.make_requests("GET", "/fapi/v1/klines", data)
candles = []
if raw_candle is not None:
for c in raw_candle:
candles.append(float(c[4]))
return candles[:-1]
running this code
print(binance.initial_historical_data("BTCUSDT", "5m"))
will return this as the output
[55673.63, 55568.0, 55567.89, 55646.19, 55555.0, 55514.53, 55572.46, 55663.91, 55792.83, 55649.43,
55749.98, 55680.0, 55540.25, 55470.44, 55422.01, 55350.0, 55486.56, 55452.45, 55507.03, 55390.23,
55401.39, 55478.63, 55466.48, 55584.2, 55690.03, 55760.81, 55515.57, 55698.35, 55709.78, 55760.42,
55719.71, 55887.0, 55950.0, 55980.47]
which is a list of closes
i want to loop through the code in such a manner that i can return all the close prices for the pairs and timeframes i need and sort it accordingly, i did give it a try but am just stuck at this point
period = ["1m", "3m", "5m", "15m"]
binance = BinanceFuturesClient()
symbols = binance.get_symbols()
for symbol in symbols:
for tf in period:
historical_candles = binance.initial_historical_data(symbol, tf)
# store values and run through strategy
You can use my code posted below. It requires python-binance package to be installed on your environment and API key/secret from your Binance account. Method tries to load data by weekly chunks (parameter step) and supports resending requests on failures after timeout. It may helps when you need to fetch huge amount of data.
import pandas as pd
import pytz, time, datetime
from binance.client import Client
from tqdm.notebook import tqdm
def binance_client(api_key, secret_key):
return Client(api_key=api_key, api_secret=secret_key)
def load_binance_data(client, symbol, start='1 Jan 2017 00:00:00', timeframe='1M', step='4W', timeout_sec=5):
tD = pd.Timedelta(timeframe)
now = (pd.Timestamp(datetime.datetime.now(pytz.UTC).replace(second=0)) - tD).strftime('%d %b %Y %H:%M:%S')
tlr = pd.DatetimeIndex([start]).append(pd.date_range(start, now, freq=step).append(pd.DatetimeIndex([now])))
print(f' >> Loading {symbol} {timeframe} for [{start} -> {now}]')
df = pd.DataFrame()
s = tlr[0]
for e in tqdm(tlr[1:]):
if s + tD < e:
_start, _stop = (s + tD).strftime('%d %b %Y %H:%M:%S'), e.strftime('%d %b %Y %H:%M:%S')
nerr = 0
while nerr < 3:
try:
chunk = client.get_historical_klines(symbol, timeframe.lower(), _start, _stop)
nerr = 100
except e as Exception:
nerr +=1
print(red(str(e)))
time.sleep(10)
if chunk:
data = pd.DataFrame(chunk, columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_av', 'trades', 'tb_base_av', 'tb_quote_av', 'ignore' ])
data.index = pd.to_datetime(data['timestamp'].rename('time'), unit='ms')
data = data.drop(columns=['timestamp', 'close_time']).astype(float).astype({
'ignore': bool,
'trades': int,
})
df = df.append(data)
s = e
time.sleep(timeout_sec)
return df
How to use
c = binance_client(<your API code>, <your API secret>)
# loading daily data from 1/Mar/21 till now (your can use other timerames like 1m, 5m etc)
data = load_binance_data(c, 'BTCUSDT', '2021-03-01', '1D')
It returns indexed DataFrame with loaded data:
time
open
high
low
close
volume
quote_av
trades
tb_base_av
tb_quote_av
ignore
2021-03-02 00:00:00
49595.8
50200
47047.6
48440.7
64221.1
3.12047e+09
1855583
31377
1.52515e+09
False
2021-03-03 00:00:00
48436.6
52640
48100.7
50349.4
81035.9
4.10952e+09
2242131
40955.4
2.07759e+09
False
2021-03-04 00:00:00
50349.4
51773.9
47500
48374.1
82649.7
4.07984e+09
2291936
40270
1.98796e+09
False
2021-03-05 00:00:00
48374.1
49448.9
46300
48751.7
78192.5
3.72713e+09
2054216
38318.3
1.82703e+09
False
2021-03-06 00:00:00
48746.8
49200
47070
48882.2
44399.2
2.14391e+09
1476474
21500.6
1.03837e+09
False
Next steps are up to you and dependent on how would you like to design your data structure. In simplest case you could store data into dictionaries:
from collections import defaultdict
data = defaultdict(dict)
for symbol in ['BTCUSDT', 'ETHUSDT']:
for tf in ['1d', '1w']:
historical_candles = load_binance_data(c, symbol, '2021-05-01', timeframe=tf)
# store values and run through strategy
data[symbol][tf] = historical_candles
to get access to your OHLC you just need following: data['BTCUSDT']['1d'] etc.

Easier Way to get dt obj attributes

I am trying to get detailed calendar information on all my birthdays to 2024(i.e. week #, day of week etc...). I noticed Pandas as date_range function/method, but am trying to do it using time/datetime because I couldn't get "freq=" to work. This is what I have so far, and I think I can get what I need from myBirthdays list, but am wondering if there is/was an easier way? Seems like a lot of extra work.
TIA.
#import pandas as pd
from datetime import date
import time
def BdayList(birthdate, enddate):
print(birthdate, type(birthdate), endDate, type(endDate))
#print(birthdate.weekday(), endDate.isocalendar())
myMonth = date.strftime(birthdate, "%m")
myDay = date.strftime(birthdate, "%d")
myBirthDays = []
daysDelta = (enddate - birthdate)
daysDeltaInt = daysDelta.days / 365
for year in range(int(date.strftime(birthdate, "%Y")), int(date.strftime(enddate, "%Y"))): #13148
year = str(year)
myBirthday = time.strptime(year+" "+myMonth+" "+myDay, "%Y %m %d")
print(myBirthday)
myBirthDays.append(myBirthday)
#dateRange = pd.date_range(start, periods = NumPeriods, freq="A")
return myBirthDays#DaysDelta, type(DaysDelta)
myBday = date(1988, 12, 22)
endDate = date(2024, 12, 22)
BdayList(myBday, endDate)
time.struct_time(tm_year=1988, tm_mon=12, tm_mday=22, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=357, tm_isdst=-1)
Because it is possible to just replace the year in original birth_date, there is no need to switch between dates and strings. (Note that I have also PEP8'd the code and used slightly different variable names + added type hints)
from datetime import date
from typing import List
from pprint import pprint
def get_birthdays(birth_date: date, end_date: date) -> List[date]:
birthday_list = list()
while birth_date <= end_date:
birthday_list.append(birth_date)
birth_date = birth_date.replace(year=birth_date.year + 1)
return birthday_list
if __name__ == "__main__":
birthdays = get_birthdays(
birth_date=date(1988, month=12, day=22),
end_date=date(2024, month=12, day=22)
)
pprint([(x.strftime("%Y-%m-%d %A, week: %U")) for x in birthdays])
The output should be:
['1988-12-22 Thursday, week: 51',
'1989-12-22 Friday, week: 51',
'1990-12-22 Saturday, week: 50',
'1991-12-22 Sunday, week: 51',
'1992-12-22 Tuesday, week: 51',
'1993-12-22 Wednesday, week: 51',
'1994-12-22 Thursday, week: 51',
'1995-12-22 Friday, week: 51']
To format output, please check datetime documentation. Hopefully this helps!

Categories

Resources