Skip to content

Commit

Permalink
Update automonitor and add qbot_main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Charmve committed Mar 23, 2023
1 parent 1fc22ed commit 338dbdc
Show file tree
Hide file tree
Showing 7 changed files with 1,430 additions and 133 deletions.
19 changes: 17 additions & 2 deletions .github/scripts/run_all.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
#!/bin/bash

###
# @Author: Charmve [email protected]
# @Date: 2023-01-20 23:48:48
# @LastEditors: Charmve [email protected]
# @LastEditTime: 2023-03-12 18:23:29
# @FilePath: /Qbot/.github/scripts/run_all.sh
# @Version: 1.0.1
# @Blogs: charmve.blog.csdn.net
# @GitHub: https://github.com/Charmve
# @Description:
#
# Copyright (c) 2023 by Charmve, All Rights Reserved.
# Licensed under the MIT License.
###

set -euo pipefail

TOP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../" && pwd -P)"
Expand All @@ -22,8 +37,8 @@ function parse_cmdline_args() {
-u | --update) ;;

-t | --test)
python "${TOP_DIR}"/qbot/bt_boll.py
python "${TOP_DIR}"/qbot/bt_bigger_than_EMA.py
python "${TOP_DIR}"/qbot/strategies/bigger_than_ema_bt.py
python "${TOP_DIR}"/qbot/strategies/boll_strategy_bt.py
;;
-h | --help)
usage
Expand Down
5 changes: 3 additions & 2 deletions auto_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def show_notification_2(title, text):
stocks_pool = [
{"code": "sz000063", "name": "中兴通讯", "min_threshold": "26", "max_threshold": "38"},
{"code": "sh000016", "name": "上证50"},
{"coce": "601318", "name": "中国平安"},
]


Expand Down Expand Up @@ -148,5 +149,5 @@ def check(code, low, high):
# if MacOs
os.system(f"afplay {sounds_file}")

exit()
time.sleep(1)
# exit()
time.sleep(2)
9 changes: 0 additions & 9 deletions qbot/main.py

This file was deleted.

82 changes: 82 additions & 0 deletions qbot/qbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
'''
Author: Charmve [email protected]
Date: 2023-03-23 15:07:38
LastEditors: Charmve [email protected]
LastEditTime: 2023-03-24 02:42:20
FilePath: /Qbot/qbot/qbot.py
Version: 1.0.1
Blogs: charmve.blog.csdn.net
GitHub: https://github.com/Charmve
Description:
Copyright (c) 2023 by Charmve, All Rights Reserved.
Licensed under the MIT License.
'''

import tushare as ts
import talib
import pandas as pd
import numpy as np
import time

# 设置股票代码和起止日期
# code = '600000.SH'
code = '601318'
start_date = '20230320'
end_date = time.strftime('%Y%m%d',time.localtime(time.time()))

def get_data(code, start="2023-02-01", end="2023-03-21"):
df = ts.get_k_data(code, autype="qfq", start=start, end=end)
df.index = pd.to_datetime(df.date)
df["openinterest"] = 0
df = df[["open", "high", "low", "close", "volume", "openinterest"]]
return df

# 从tushare获取股票数据
# data = ts.get_k_data(code, start=start_date, end=end_date)
# print(data)

data = get_data("600018")

# 计算均线、BIAS和BOLL指标
data['ma5'] = talib.MA(data['close'], timeperiod=5)
data['ma10'] = talib.MA(data['close'], timeperiod=10)
data['ma20'] = talib.MA(data['close'], timeperiod=20)
data['bias1'] = (data['close'] - data['ma5']) / data['ma5'] * 100
data['bias2'] = (data['close'] - data['ma10']) / data['ma10'] * 100
data['bias3'] = (data['close'] - data['ma20']) / data['ma20'] * 100
data['upper'], data['middle'], data['lower'] = talib.BBANDS(data['close'], timeperiod=20, nbdevup=2, nbdevdn=2, matype=0)
data['k'], data['d'] = talib.STOCH(data['high'], data['low'], data['close'], fastk_period=9, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
data['rsi'] = talib.RSI(data['close'], timeperiod=14)

# 初始化买卖信号
signals = np.zeros(len(data))

# 第一个策略:均线和BIAS指标信号
condition1 = (data['ma5'] > data['ma10']) & (data['ma5'] > data['ma20']) & (data['bias1'] > data['bias2']) & (data['bias1'] > data['bias3'])
signals += 0.2 * condition1.astype(int)

# 第二个策略:股价低于BOLL线底
condition2 = (data['close'] < data['lower'])
signals += 0.3 * condition2.astype(int)

# 第三个策略:K线上穿D线
condition3 = (data['k'] > data['d']) & (data['k'].shift() < data['d'].shift())
signals += 0.2 * condition3.astype(int)

# 第四个策略:RSI指标信号
condition4 = (data['rsi'] > 80) | (data['rsi'] < 20)
signals += 0.3 * condition4.astype(int)

# print("signals: ", signals)

# 根据信号生成交易指令,并计算收益率
orders = np.zeros_like(signals)
orders[signals > 0] = 1 # 买入信号
orders[signals < 0] = -1 # 卖出信号
returns = np.diff(data['close']) * orders[:-1]

# 输出结果
print('Total returns:', returns.sum())
print('Positive trades:', len(returns[returns > 0]))
print('Negative trades:', len(returns[returns < 0]))
Loading

0 comments on commit 338dbdc

Please sign in to comment.