输入/搜索内容
4
关注
15
关注者
制作一个简易价格提醒机器人
经验交流
创建于 2022-03-27 15:44:13  更新于 2022-03-27 15:59:46
 5
 1592

之前给别人做过代写,发现很多朋友都要行情监控策略,出现特殊情况时能即时报警,但对于一直开着实盘又不太满意; 所以今天分享一个简易价格提醒demo供大家参考。
ps:语言使用的是python,通过钉钉接口报警,服务器的配置在这里不做介绍


一.准备

  1. 控制面板
    这里用到了宝塔面板,目的是将文件上传到服务器,当然也可以使用其他方式,记住路径即可(由于作者服务器是Ubuntu系统,所以以下的所有命令都以此为准,其他系统命令可自行查阅)
  • 下载
wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh

完成后得到网址并登录(如果网址无法打开则需要开放端口8888)

  • 获取权限
    打开安全-shh安全管理-打开shh密钥登录
    img
    img

  • 上传文件
    img

  1. Screen
    screen是linux下的一种视窗多重复用管理程序,目的是在终端shh连接后依然可以运行程序。
  • 下载
sudo apt-get install screen
  • 常见命令
    运行screen,打开路径运行文件后即可([name]标签可自行设置)。
screen -S [name]

退出screen快捷键

ctrl+a+d

查看screen在后台运行的进程

screen -ls

结束进程(pid号可通过进程查看)

kill -9 [pid号]

清理screen已经死亡的进程信息

screen -wipe
  1. 钉钉设置
    这里参考了Hukybo大佬的文章,原理实现不再赘述,只展示简易封装代码,仅供参考
    https://www.fmz.com/digest-topic/5840
# 钉钉输出接口 class DING: # 向钉钉群输出信息 def msg(self,text): token ="***" headers = {'Content-Type': 'application/json;charset=utf-8'} # 请求头 api_url = f"***={token}" json_text = { "msgtype": "text", # 信息格式 "text": { "content": text } } # 发送并打印信息 requests.post(api_url, json.dumps(json_text), headers=headers).content #拼接输出信息 def dd(self,logging): bj_dt = str(datetime.datetime.now()) bj_dt = bj_dt.split('.')[0] # 处理时间 text = f'{bj_dt}\n' # 定义信息内容 text = text + logging # 处理信息内容 log.msg(text) # 调用msg函数,输出信息

二.代码实现

行情通过币安api获取,使用的U本位合约fapi接口,以下代码简易封装了币安api,仅供参考

import requests,json,time,hmac,hashlib,datetime # APIKEY填写位置 apikey = '***' Secret_KEY = '***' #币安接口 class bian: #拼接请求参数 def param2string(self,param): s = '' for k in param.keys(): s += k s += '=' s += str(param[k]) s += '&' return s[:-1] # 参数为get,post请求方式,路径,body def IO(self,method,request_path,body): header = { 'X-MBX-APIKEY': apikey, } #选择请求方式 if body != '': #签名 body['signature'] = hmac.new(Secret_KEY.encode('utf-8'), self.param2string(body).encode('utf-8'), hashlib.sha256).hexdigest() if method == 'GET': body = self.param2string(body) tell = 'https://fapi.binance.com{0}?{1}'.format(request_path,body) response = requests.get(url=tell, headers=header).json() return response elif method == 'POST': response = requests.post(url='https://fapi.binance.com'+str(request_path), headers=header, data=body).json() return response else: response = requests.get(url='https://fapi.binance.com'+str(request_path), headers=header).json() return response

由于策略只用到获取价格接口,所以这里只进行简单演示,其他接口同理

#封装获取价格接口 def price(self,Name): body = {"symbol":str(Name)} info = self.IO("GET","/fapi/v1/ticker/price",body) for i in info: if i == "code": #设计一个接口错误容错功能 time.sleep(0.5) letgo = '调用price函数接口返回错误,再次尝试 返回错误代码:{0}'.format(str(info)) log.dd(str(letgo)) exchange.price(Name) return info["price"]

下面是行情监控代码实现

# 监控币种&&监控价格一一对应 ccy = ["BTCUSDT","ETHUSDT","LTCUSDT"] PriceTIME = ["100000;28000","500000000;1200","500;100"] #行情监控逻辑 def pricewarm(): #轮询获取当前价格 for i in range(len(PriceTIME)): info = exchange.price(str(PriceTIME[i])) priceindex = PriceTIME[i].find(";") #提取价格区间 #价格上限 priceup = PriceTIME[i][:priceindex] #价格下限 pricedown = PriceTIME[i][priceindex+1:] if float(info) >= float(priceup): #钉钉接口输出 letgo = f'当前价格{info}USDT大于所设定上限{priceup}USDT' log.dd(letgo) elif float(info) <= float(pricedown): letgo = f'当前价格{info}USDT小于等于设定下限{pricedown}USDT' log.dd(letgo) time.sleep(0.2) # 主函数 def main(): global exchange,log log = DING exchange = bian while True: try: pricewarm() time.sleep(1) except: time.sleep(1) if __name__ == "__main__": main()

三.运行

代码准备完成后记住路径,打开终端运行screen

screen -S [名称]
cd [路径]
python3 [文件名]

确认程序运行后退出即可。

策略地址:简易价格提醒机器人

相关推荐
评论
全部评论 (5)

    机器人运行后没反应怎么回事

    4 年前

    只有到价格才会提醒

    4 年前

    价格到了,设置条件达到了,机器人运行的,就是不提醒怎么回事呢?大佬v多少?请教一下

    4 年前

    qq 2700903954

    4 年前

    感谢分享经验。

    4 年前
  • 1
社区
回测系统
APP 下载
iPhone 下载
© 2015 - ∞ INVENTOR PTE LTD (SG)