MCPcopy Create free account
hub / github.com/DVampire/FinAgent / plot_kline

Function plot_kline

finagent/plots/kline.py:23–237  ·  view source on GitHub ↗
(df,
               title,
               save_path,
               now_date,
               width = 3.5,
               opacity = 0.8,
               path= None,
               mode = "train")

Source from the content-addressed store, hash-verified

21 return macd_line
22
23def plot_kline(df,
24 title,
25 save_path,
26 now_date,
27 width = 3.5,
28 opacity = 0.8,
29 path= None,
30 mode = "train"):
31
32 if not mode == "train":
33 df = df[df.index <= now_date]
34
35 df = df.rename(columns={'open': 'Open', 'high': 'High', 'low': 'Low', 'close': 'Close', 'volume': 'Volume', 'timestamp': 'Date'})
36
37 format = "%Y-%m-%d"
38 date = df.index.strftime(format).tolist()
39 values = df[["Open", "Close", "Low", "High"]].values.tolist()
40 volumes = [[i, row[1]["Volume"], 1 if row[1]["Open"] > row[1]["Close"] else -1] for i, row in enumerate(df.iterrows())]
41
42 # Calculate the indicators
43 # df['sma_3'] = ta.sma(df["Close"], length=3)
44 df['sma_5'] = ta.sma(df["Close"], length=5)
45 # df['sma_7'] = ta.sma(df["Close"], length=7)
46 bbands = ta.bbands(df["Close"], length=5)
47 df['bbl'] = bbands.iloc[:, 0]
48 df['bbu'] = bbands.iloc[:, 2]
49 df['bbp'] = bbands.iloc[:, 4]
50
51 macd = cal_macd(df, 7, 14)
52 max_macd = max(abs(x) for x in macd)
53 max_volume = max(df["Volume"])
54 adj_macd = [number * max_volume / max_macd for number in macd]
55
56 kline = (
57 Kline()
58 .add_xaxis(xaxis_data=date)
59 .add_yaxis(
60 series_name=title,
61 y_axis=values,
62 itemstyle_opts=opts.ItemStyleOpts(color="#00da3c", color0="#ec0000", border_color="#00da3c", border_color0="#ec0000"),
63 markpoint_opts=opts.MarkPointOpts(
64 data=[
65 opts.MarkPointItem(
66 coord=[now_date, df.loc[now_date, 'High']],
67 value=now_date,
68 symbol_size=100,
69 symbol="pin",
70 itemstyle_opts=opts.ItemStyleOpts(color="grey"),
71 )
72 ]
73 )
74 )
75 .set_global_opts(
76 legend_opts=opts.LegendOpts(
77 is_show=True
78 ),
79 yaxis_opts=opts.AxisOpts(
80 is_scale=True,

Callers 1

plot_klineMethod · 0.90

Calls 2

cal_macdFunction · 0.85
addMethod · 0.45

Tested by

no test coverage detected