22import datetime
33import pandas as pd
44import isbtchot
5+ from isbtchot .schemas .args import TypeTime
56
67
78CACHE_BTC_PATH = isbtchot .root_path / "cache" / "btc.csv"
@@ -20,9 +21,18 @@ def btc_historical_daily() -> pd.DataFrame:
2021 df = pd .read_csv (CACHE_BTC_PATH )
2122
2223 else :
23- data = requests .get (
24- "https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USD&allData=true"
25- ).json ()["Data" ]["Data" ]
24+ try :
25+ data = requests .get (
26+ "https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USD&allData=true"
27+ ).json ()["Data" ]["Data" ]
28+
29+ except Exception as _ :
30+ # Try without ssl enabled
31+ data = requests .get (
32+ "https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USD&allData=true" ,
33+ verify = False
34+ ).json ()["Data" ]["Data" ]
35+
2636 df = pd .DataFrame (data )
2737 df .to_csv (CACHE_BTC_PATH , index = False )
2838
@@ -31,7 +41,7 @@ def btc_historical_daily() -> pd.DataFrame:
3141 return df
3242
3343
34- def btc_pi (months ):
44+ def btc_pi (time_grouping : TypeTime , periods_back : int ):
3545 df = btc_historical_daily ()[["close" ]].rename ({"close" : "price" }, axis = 1 )
3646
3747 df ["sma111" ] = df ["price" ].rolling (window = 111 ).mean ()
@@ -42,14 +52,14 @@ def btc_pi(months):
4252 df = df .dropna ()
4353
4454 # Sell Indicator
45- mask_pi_sell = (df ["pi" ].shift (- 1 ) < 1 ) & (df ["pi" ] > = 1 )
55+ mask_pi_sell = (df ["pi" ].shift (- 1 ) > 1 ) & (df ["pi" ] < = 1 )
4656 df ["pi_sell" ] = mask_pi_sell
4757
4858 # Buy indicator
49- mask_pi_buy = (df ["pi" ].shift (- 1 ) > 0.35 ) & (df ["pi" ] < = 0.35 )
59+ mask_pi_buy = (df ["pi" ].shift (- 1 ) < 0.35 ) & (df ["pi" ] > = 0.35 )
5060 df ["pi_buy" ] = mask_pi_buy
5161
52- df = df .resample ("M" ).agg (
62+ df = df .resample (time_grouping . value ).agg (
5363 {
5464 "price" : "last" ,
5565 "pi" : "max" ,
@@ -61,25 +71,25 @@ def btc_pi(months):
6171 df = df .reset_index ()
6272 df .time = df .time .dt .strftime ("%d/%m/%Y" )
6373
64- if months :
65- df = df .iloc [- months :]
74+ if periods_back :
75+ df = df .iloc [- periods_back :]
6676
6777 return df
6878
6979
70- def btc_monthly ( months ):
80+ def btc_hist ( time_grouping : TypeTime , periods_back : int ):
7181 df = btc_historical_daily ()
7282 df = df .rename (
7383 {"open" : "Open" , "close" : "Close" , "high" : "High" , "low" : "Low" }, axis = 1
7484 )
75- df = df .resample ("M" ).agg (
85+ df = df .resample (time_grouping . value ).agg (
7686 {
7787 "Open" : "first" ,
7888 "High" : "max" ,
7989 "Low" : "min" ,
8090 "Close" : "last" ,
8191 }
8292 )
83- if months :
84- df = df .iloc [- months :]
93+ if periods_back :
94+ df = df .iloc [- periods_back :]
8595 return df
0 commit comments