微信群是一个聚集志同道合朋友们的公共场所,量化交易圈子的微信群也不例外。最近有不少发明者量化的用户总是提问,如何可以随心所欲的向微信群推送消息,比如自己的策略发出了交易信号,但是又不想自动化交易,希望让机器人提供的信号作为参考发到微信群中。
为此大概看了一下微信接口,发现需要公众号才行,并不是特别的方便。所以决定另辟蹊径,尝试了一下以下方案。由于作者水平有限,仅仅只能完成此需求的最基本功能,各位看官仅当参考。
实现方案
github.com/go-vgo/robotgo
首先我们运行一个服务程序在自己电脑上,代码如下:
package main
import (
"fmt"
"time"
"github.com/go-vgo/robotgo"
"net/http"
"io/ioutil"
)
func postMsg (msg string) {
fmt.Println("开始执行任务!")
// process ids
processIds := "WeChat"
fpid, err3 := robotgo.FindIds(processIds)
robotgo.ActivePID(fpid[0])
time.Sleep(time.Millisecond * 2000)
if err3 == nil {
fmt.Println(fmt.Sprintf("find %s", processIds), "ids:", fpid)
/* 使用图片识别方式,获取点击区域坐标
arrPicFileName := []string{"pic1.png", "pic2.png", "pic3.png"}
for _, name := range arrPicFileName {
picPath := fmt.Sprintf("/xxx/xxx/Desktop/xxx/%s", name)
fmt.Println("picPath:", fmt.Sprintf("/xxx/xxx/Desktop/xxx/%s", name))
fx, fy := robotgo.FindPic(picPath)
fmt.Println("move to :", fx+10, fy+10)
robotgo.MoveMouseSmooth(fx+10, fy+10)
time.Sleep(time.Millisecond * 2000)
robotgo.MouseClick("left", false)
robotgo.TypeStr(msg)
time.Sleep(time.Second)
robotgo.KeyTap("enter")
time.Sleep(time.Second)
}
*/
// /* 固定区域坐标,屏幕右上角坐标是0,0
arrArea := []map[string]int{
map[string]int{
"x" : 190,
"y" : 200,
},
map[string]int{
"x" : 190,
"y" : 200+70,
},
map[string]int{
"x" : 190,
"y" : 200+70+70,
},
}
for _, area := range arrArea {
robotgo.MoveMouseSmooth(area["x"], area["y"])
time.Sleep(time.Millisecond * 2000)
robotgo.MouseClick("left", false)
robotgo.TypeStr(msg)
time.Sleep(time.Second)
robotgo.KeyTap("enter")
time.Sleep(time.Second)
}
// */
}
fmt.Println("任务执行完成!")
}
func Handle (w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
fmt.Println("req body:", string(b))
postMsg(string(b))
w.Write([]byte("finished!"))
}
func main () {
fmt.Println("listen http://127.0.0.1:9090")
http.HandleFunc("/data", Handle)
http.ListenAndServe("127.0.0.1:9090", nil)
}
postMsg
postMsgarrArea
发明者量化交易平台上的策略测试程序:
function main() {
var msg = {
"type" : "msg",
"robotId" : _G(),
"msg" : "hello fmz!"
}
var n = 0
while(true) {
if(n == 20) {
var ret = HttpQuery("http://127.0.0.1:9090/data", JSON.stringify(msg))
Log("Exit")
break
}
n++
LogStatus(_D(), "n:", n)
Sleep(1000)
}
}
http://127.0.0.1:9090/data
测试
发明者量化交易平台上的机器人运行(托管者也在本地运行):
本地服务运行:
微信窗口
推送的消息为:
{"type":"msg","robotId":130350,"msg":"hello fmz!"}
这个方案仅仅作为抛砖引玉,有更好方案的欢迎大家讨论。