@@ -18,19 +18,23 @@ def from_context(ctx: click.Context) -> QfApp:
1818 return ctx .obj # type: ignore
1919
2020
21- @click .command ()
22- def exit () -> None :
23- """Exit the program"""
24- raise click .Abort ()
21+ @click .group (invoke_without_command = True )
22+ @click .pass_context
23+ def stocks (ctx : click .Context ) -> None :
24+ """Stocks commands"""
25+ if ctx .invoked_subcommand is None :
26+ app = from_context (ctx )
27+ app .print ("Welcome to the stocks commands!" )
28+ app .print (ctx .get_help ())
2529
2630
27- @click .command ()
31+ @stocks .command ()
2832@click .argument ("symbol" )
2933@click .pass_context
3034def profile (ctx : click .Context , symbol : str ) -> None :
3135 """Company profile"""
3236 app = from_context (ctx )
33- data = asyncio .run (get_profile (symbol ))
37+ data = asyncio .run (get_profile (app , symbol ))
3438 if not data :
3539 raise click .UsageError (f"Company { symbol } not found - try searching" )
3640 else :
@@ -40,18 +44,18 @@ def profile(ctx: click.Context, symbol: str) -> None:
4044 app .print (df_to_rich (df ))
4145
4246
43- @click .command ()
47+ @stocks .command ()
4448@click .argument ("text" )
4549@click .pass_context
4650def search (ctx : click .Context , text : str ) -> None :
4751 """Search companies"""
4852 app = from_context (ctx )
49- data = asyncio .run (search_company (text ))
53+ data = asyncio .run (search_company (app , text ))
5054 df = pd .DataFrame (data , columns = ["symbol" , "name" , "currency" , "stockExchange" ])
5155 app .print (df_to_rich (df ))
5256
5357
54- @click .command ()
58+ @stocks .command ()
5559@click .argument ("symbol" )
5660@click .option (
5761 "-h" ,
@@ -76,9 +80,13 @@ def search(ctx: click.Context, text: str) -> None:
7680 default = "" ,
7781 help = "Frequency of data - if not provided it is daily" ,
7882)
79- def chart (symbol : str , height : int , length : int , frequency : str ) -> None :
83+ @click .pass_context
84+ def chart (
85+ ctx : click .Context , symbol : str , height : int , length : int , frequency : str
86+ ) -> None :
8087 """Symbol chart"""
81- df = asyncio .run (get_prices (symbol , frequency ))
88+ app = from_context (ctx )
89+ df = asyncio .run (get_prices (app , symbol , frequency ))
8290 if df .empty :
8391 raise click .UsageError (
8492 f"No data for { symbol } - are you sure the symbol exists?"
@@ -87,16 +95,16 @@ def chart(symbol: str, height: int, length: int, frequency: str) -> None:
8795 print (plot (data , {"height" : height }))
8896
8997
90- async def get_prices (symbol : str , frequency : str ) -> pd .DataFrame :
91- async with FMP () as cli :
98+ async def get_prices (app : QfApp , symbol : str , frequency : str ) -> pd .DataFrame :
99+ async with app . fmp () as cli :
92100 return await cli .prices (symbol , frequency )
93101
94102
95- async def get_profile (symbol : str ) -> list [dict ]:
96- async with FMP () as cli :
103+ async def get_profile (app : QfApp , symbol : str ) -> list [dict ]:
104+ async with app . fmp () as cli :
97105 return await cli .profile (symbol )
98106
99107
100- async def search_company (text : str ) -> list [dict ]:
101- async with FMP () as cli :
108+ async def search_company (app : QfApp , text : str ) -> list [dict ]:
109+ async with app . fmp () as cli :
102110 return await cli .search (text )
0 commit comments