| 9 | CurrentConfig.ONLINE_HOST = "" |
| 10 | |
| 11 | def plot_trading(data, |
| 12 | save_path, |
| 13 | now_date = None, |
| 14 | width=3.5, |
| 15 | opacity=0.8, |
| 16 | path=None): |
| 17 | dates = data['date'][:-1] |
| 18 | closing_prices = data['price'][:-1] |
| 19 | returns = data['total_profit'][1:] |
| 20 | actions = data['action'][:-1] |
| 21 | |
| 22 | min_y = min(closing_prices) |
| 23 | max_y = max(closing_prices) |
| 24 | delta = max_y - min_y |
| 25 | lowerbound = round(min_y - delta * 0.1, 2) |
| 26 | upperbound = round(max_y + delta * 0.1, 2) |
| 27 | if delta > 5: |
| 28 | lowerbound = int(lowerbound) |
| 29 | upperbound = int(upperbound) |
| 30 | |
| 31 | markers = [ |
| 32 | opts.MarkPointItem( |
| 33 | coord=[date, price - (delta * 0.08 if action == 'BUY' else 0)], |
| 34 | value=action, |
| 35 | symbol_size=45 if action == 'BUY' else 60, |
| 36 | symbol="diamond" if action == 'BUY' else "pin", |
| 37 | itemstyle_opts=opts.ItemStyleOpts(color="green" if action == 'BUY' else "red"), |
| 38 | ) for date, price, action in zip(dates, closing_prices, actions) if action in ['BUY', 'SELL'] |
| 39 | ] |
| 40 | if now_date: |
| 41 | index = dates.index(now_date) |
| 42 | closing_price_at_now_date = closing_prices[index] |
| 43 | markers.append( |
| 44 | opts.MarkPointItem( |
| 45 | coord=[now_date, closing_price_at_now_date], |
| 46 | value=now_date, |
| 47 | symbol_size=120, |
| 48 | symbol="pin", |
| 49 | itemstyle_opts=opts.ItemStyleOpts(color="grey"), |
| 50 | )) |
| 51 | |
| 52 | signal_line = ( |
| 53 | Line() |
| 54 | .set_global_opts( |
| 55 | tooltip_opts=opts.TooltipOpts(is_show=False), |
| 56 | xaxis_opts=opts.AxisOpts(type_="category"), |
| 57 | yaxis_opts=opts.AxisOpts( |
| 58 | type_="value", |
| 59 | min_=lowerbound, |
| 60 | max_=upperbound, |
| 61 | axistick_opts=opts.AxisTickOpts(is_show=True), |
| 62 | splitline_opts=opts.SplitLineOpts(is_show=True), |
| 63 | ), |
| 64 | legend_opts=opts.LegendOpts(orient="horizontal", pos_top="2%"), |
| 65 | ) |
| 66 | .add_xaxis(xaxis_data=dates) |
| 67 | .add_yaxis( |
| 68 | series_name="Adj Close Prices", |