Skip to content

Commit cebfd53

Browse files
committed
add more parameters
1 parent 82193b3 commit cebfd53

File tree

4 files changed

+60
-29
lines changed

4 files changed

+60
-29
lines changed

src/isbtchot/__main__.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
from isbtchot import controller
2+
from isbtchot.schemas.args import TypeTime
23
import argparse
34

45
parser = argparse.ArgumentParser(description="BTC Hotness Index", prog="isbtchot")
56

67
# Add an optional positional argument named 'year' with default value of 6
78
parser.add_argument(
8-
"year",
9+
"-periods_back",
10+
"--p",
911
type=int,
10-
nargs="?",
11-
default=7,
12-
help="The year to be processed. Defaults to 6 if not provided.",
12+
default=85,
13+
help="Periods back to be processed. Defaults to 50 if not provided.",
1314
)
1415

15-
def main(year):
16-
controller.dashboard(months=12 * year)
16+
parser.add_argument(
17+
"-time",
18+
"--t",
19+
type=TypeTime,
20+
default=TypeTime.WEEK,
21+
help="Candle stick time to use. Defaults to W (Weekly),"
22+
)
1723

18-
if __name__ == "__main__":
24+
25+
def main():
26+
27+
# Parse args
1928
args = parser.parse_args()
20-
year = args.year
21-
main(year)
29+
time_grouping: TypeTime = args.t
30+
periods_back: int = args.p
31+
32+
controller.dashboard(periods_back=periods_back, time_grouping=time_grouping)
33+
34+
if __name__ == "__main__":
35+
main()

src/isbtchot/controller.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
from isbtchot import model
22
from isbtchot import view as v
3+
from isbtchot.schemas.args import TypeTime
4+
35

46
view = v.ViewPlotText()
57

6-
def btc_terminal(months):
7-
df = model.btc_monthly(months)
8+
def btc_terminal(time_grouping: TypeTime, periods_back: int):
9+
df = model.btc_hist(time_grouping, periods_back)
810
view.candle_stick_terminal(df, "BTC/USD")
911
view.plt.yscale("log")
1012

11-
def btc_pi_terminal(months):
12-
df = model.btc_pi(months)
13+
def btc_pi_terminal(time_grouping: TypeTime, periods_back):
14+
df = model.btc_pi(time_grouping, periods_back)
1315
view.chart_terminal(df.time, df.pi, "Hotness Index")
1416
view.add_horizontal_line(1, "red")
1517
view.add_horizontal_line(0.35, "green")
@@ -21,11 +23,11 @@ def btc_pi_terminal(months):
2123
view.plt.ylim(0.20, 1.1)
2224

2325

24-
def dashboard(months=None):
26+
def dashboard(time_grouping: TypeTime, periods_back: int):
2527
view.dimensions(0, -4)
2628
view.subplots(2,1)
2729
view.select_subplot(1,1)
28-
btc_terminal(months)
30+
btc_terminal(time_grouping, periods_back)
2931
view.select_subplot(2,1)
30-
btc_pi_terminal(months)
32+
btc_pi_terminal(time_grouping, periods_back)
3133
view.show()

src/isbtchot/model.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import datetime
33
import pandas as pd
44
import isbtchot
5+
from isbtchot.schemas.args import TypeTime
56

67

78
CACHE_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

src/isbtchot/schemas/args.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from enum import Enum
2+
3+
class TypeTime(Enum):
4+
MONTH = "M"
5+
WEEK = "W"

0 commit comments

Comments
 (0)