Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
vicalloy committed Jun 13, 2023
1 parent ed8608f commit 3616a6f
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 6 deletions.
12 changes: 12 additions & 0 deletions Makefile
@@ -1,3 +1,6 @@
token ?= bark_token
datasource ?= qq

init-pre-commit:
git config --global url."https://".insteadOf git://
pre-commit install
Expand All @@ -13,6 +16,9 @@ test:
server:
python -m magpie server -r ./rules.json

check:
python -m magpie check -r ./rules.json -d $(datasource) --bark-token $(token)

build-docker-image:
docker build -f Dockerfile -t magpie:latest .

Expand All @@ -21,3 +27,9 @@ docker-run-server:
-v `pwd`/rules.json:/app/rules.json \
-p 8000:8000 magpie:latest \
python -m magpie server -r ./rules.json

docker-check:
docker run --rm \
-v `pwd`/rules.json:/app/rules.json \
magpie:latest \
python -m magpie check -r ./rules.json -d $(datasource) --bark-token $(token)
59 changes: 59 additions & 0 deletions README.cn.md
@@ -0,0 +1,59 @@
# Magpie

股票工具。设置股票的止损点和营收点,在到达止损点或营收点时发起消息推送。提供一个简易点web服务器用于查看相关股票的当前价格。

注:

- 目前只支持使用 [Bark](https://github.com/Finb/Bark/blob/master/README.en.md) 推送。
- 请在 iPhone 上安装 `Bark` 客户端,并获取对应的 `Token`

## 用法

### 编辑规则文件

提醒规则使用 `Json` 格式进行描述。编辑规则并保存为文件 `rule.json`

```
[
{
"stock_code": "sh000001",
"stock_name": "上证指数",
"base_price": 3200, # 基准价格,用于计算涨幅
"alarm_price_min": 3100, # 止损点
"alarm_price_max": 3400 # 营收点
},
{
"stock_code": "sz000333",
"stock_name": "美的",
"base_price": 54,
"alarm_percentage_min": 0.15, # 止损点 base_price * (1 - alarm_percentage_min)
"alarm_percentage_max": 0.15 # 营收点 base_price * (1 + alarm_percentage_max)
},
]
```
### 启动 Web 服务器

```shell
docker run --rm \
-v `pwd`/rules.json:/app/rules.json \
-p 8000:8000 vicalloy/magpie:latest \
python -m magpie server -r ./rules.json
```

在浏览器中访问网址 `http://localhost:8000/`

### 检查股价

```shell
docker run --rm \
-v `pwd`/rules.json:/app/rules.json \
magpie:latest \
python -m magpie check -r ./rules.json \
--datasource qq --bark-token $(token)
```

可以通过设置 `crontab` 的方式定时执行股价的检查。

```
10 9,10,11,12,13,14 * * * sudo docker run ....
```
64 changes: 63 additions & 1 deletion README.md
@@ -1,3 +1,65 @@
[中文](./README.cn.md)

# Magpie

A stock tool.
A stock tool. Set stop-loss and revenue points for stocks,
and initiate message pushes when stop-loss or revenue points are reached.

Provides a simple web server for viewing the current prices of relevant stocks.

Note:

- Currently only supports push notifications using [Bark](https://github.com/Finb/Bark/blob/master/README.en.md).
- Please install the `Bark` client on your iPhone and obtain the corresponding `Token`.

## Usage

### Edit the rule file

The rule is described in `Json` format.
Edit the rule and save it as a file named `rule.json`.

```
[
{
"stock_code": "sh000001",
"stock_name": "上证指数",
"base_price": 3200, # Base price for calculating the increase
"alarm_price_min": 3100, # Stop-loss point
"alarm_price_max": 3400 # Revenue point
},
{
"stock_code": "sz000333",
"stock_name": "美的",
"base_price": 54,
"alarm_percentage_min": 0.15, # Stop-loss point: base_price * (1 - alarm_percentage_min)
"alarm_percentage_max": 0.15 # Revenue point: base_price * (1 + alarm_percentage_max)
},
]
```
### Start the web server

```shell
docker run --rm \
-v `pwd`/rules.json:/app/rules.json \
-p 8000:8000 vicalloy/magpie:latest \
python -m magpie server -r ./rules.json
```

Access the website `http://localhost:8000/`.

### Check the stock price

```shell
docker run --rm \
-v `pwd`/rules.json:/app/rules.json \
magpie:latest \
python -m magpie check -r ./rules.json \
--datasource qq --bark-token $(token)
```

You can set up a `crontab` to perform stock price checks regularly.

```
10 9,10,11,12,13,14 * * * sudo docker run ....
```
13 changes: 10 additions & 3 deletions magpie/__main__.py
@@ -1,6 +1,6 @@
import argparse

from magpie.core import check_threshold, load_rules
from magpie.core import check_threshold, load_rules, set_datasource
from magpie.message import bark_send_message, set_bark_token
from magpie.server import add_server_arguments, run_server

Expand All @@ -12,7 +12,7 @@ def get_args():
default="check",
choices=["check", "server"],
nargs="?",
help="run http server or check threshold" "(default: %(default)s)",
help="run http server or check threshold(default: %(default)s)",
)
parser.add_argument(
"-r",
Expand All @@ -22,7 +22,13 @@ def get_args():
)
parser.add_argument(
"--bark-token",
help="bark token",
)
parser.add_argument(
"-d",
"--datasource",
default="qq",
choices=["qq", "tencent", "sina"],
help="datasource(default: %(default)s)",
)
add_server_arguments(parser)
return parser.parse_args()
Expand All @@ -33,6 +39,7 @@ def main():

rules = load_rules(args.rule)
set_bark_token(args.bark_token)
set_datasource(args.datasource)

if args.action == "server":
run_server(rules)
Expand Down
9 changes: 7 additions & 2 deletions magpie/core.py
Expand Up @@ -4,7 +4,12 @@

import easyquotation

quotation = easyquotation.use("qq")
_quotation = easyquotation.use("qq")


def set_datasource(datasource: str):
global _quotation
_quotation = easyquotation.use(datasource)


@dataclass
Expand Down Expand Up @@ -79,7 +84,7 @@ def load_rules(fn: str) -> dict[str, Rule]:


def check_threshold(rules: dict[str, Rule], send_msg_func: Callable) -> str:
stock_info_dict = quotation.stocks(list(rules.keys()), prefix=True)
stock_info_dict = _quotation.stocks(list(rules.keys()), prefix=True)
output = []
for stock_code, rule in rules.items():
price = stock_info_dict[stock_code]["now"]
Expand Down

0 comments on commit 3616a6f

Please sign in to comment.