TradingViewWebHook is a direct link policy

Author: The Little Dream, Date: 2020-08-03 09:58:19
Tags:

Related articles:https://www.fmz.com/bbs-topic/5969

Since the HTTPServer itself has some pitfalls, consider using ThreadingHTTPServer instead. See also:https://docs.python.org/3.7/library/http.server.htmlIt requires Python version 3.7.

The HTTPSserver problem is described below:https://www.zybuluo.com/JunQiu/note/1350528


'''
请求格式:http://x.x.x.x:xxxx/data?access_key=xxx&secret_key=yyy&type=buy&amount=0.001
策略机器人参数:
- 类型:加密字符串,AccessKey , SecretKey ,可以用FMZ平台的低权限的API KEY,或者自己生成KEY也可以。
- 类型:字符串,合约ID,ContractType
- 类型:数值,端口号,Port
'''

import re
import _thread
import json
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler
from urllib.parse import parse_qs, urlparse

def url2Dict(url):
    query = urlparse(url).query  
    params = parse_qs(query)  
    result = {key: params[key][0] for key in params}  
    return result

class Executor(BaseHTTPRequestHandler):
    def do_GET(self):
        try:
            dictParam = url2Dict(self.path)
            Log("测试", dictParam)
        except Exception as e:
            Log("Provider do_GET error, e:", e)
    def do_POST(self):
        try:
            self.send_response(200)
            self.send_header("Content-type", "application/json")
            self.end_headers()
            dictParam = url2Dict(self.path)
            
            # 测试POST请求Body信息            
            data = self.rfile.read(200)   # 指定了读取长度
            Log("data:", data)            # 打印POST请求的数据,可以根据请求中的数据具体再让机器人执行对应的操作
            
            # 校验
            if len(dictParam) == 4 and dictParam["access_key"] == AccessKey and dictParam["secret_key"] == SecretKey:
                del dictParam["access_key"]
                del dictParam["secret_key"]
                Log("接收到请求", "参数:", dictParam, "#FF0000")
                '''
                map[access_key:xxx amount:0.001 secret_key:yyy type:buy]
                '''
                isSpot = True
                if exchange.GetName().find("Futures") != -1:
                    if ContractType != "":
                        exchange.SetContractType(ContractType)
                        isSpot = False 
                    else :
                        raise "未设置期货合约"
                                
                q = None
                if exchange.GetName() == "Futures_CTP" and UseMarketOrderForCTP == False:
                    q = ext.NewTaskQueue()
                
                if isSpot and dictParam["type"] == "buy":
                    exchange.Buy(-1, float(dictParam["amount"]))
                    Log(exchange.GetAccount())
                elif isSpot and dictParam["type"] == "sell":
                    exchange.Sell(-1, float(dictParam["amount"]))
                    Log(exchange.GetAccount())
                elif not isSpot and dictParam["type"] == "long":
                    exchange.SetDirection("buy")
                    if not q:
                        exchange.Buy(-1, float(dictParam["amount"]))
                    else :
                        q.pushTask(exchange, ContractType, "buy", float(dictParam["amount"]), lambda task, ret: Log(task["desc"], ret, "#FF0000"))
                    Log("持仓:", exchange.GetPosition())
                elif not isSpot and dictParam["type"] == "short":
                    exchange.SetDirection("sell")
                    if not q:
                        exchange.Sell(-1, float(dictParam["amount"]))
                    else :
                        q.pushTask(exchange, ContractType, "sell", float(dictParam["amount"]), lambda task, ret: Log(task["desc"], ret, "#FF0000"))
                    Log("持仓:", exchange.GetPosition())
                elif not isSpot and dictParam["type"] == "cover_long":
                    exchange.SetDirection("closebuy")
                    if not q:
                        exchange.Sell(-1, float(dictParam["amount"]))
                    else :
                        q.pushTask(exchange, ContractType, "closebuy", float(dictParam["amount"]), lambda task, ret: Log(task["desc"], ret, "#FF0000"))
                    Log("持仓:", exchange.GetPosition())
                elif not isSpot and dictParam["type"] == "cover_short":
                    exchange.SetDirection("closesell")
                    if not q:
                        exchange.Buy(-1, float(dictParam["amount"]))
                    else :
                        q.pushTask(exchange, ContractType, "closesell", float(dictParam["amount"]), lambda task, ret: Log(task["desc"], ret, "#FF0000"))
                    Log("持仓:", exchange.GetPosition())
                
                if q is not None:
                    while q.size() > 0:
                        q.poll()
                        Sleep(500)
            
            # 处理body数据
            if isDealBodyMsg:
                if exchange.GetName().find("Futures") != -1:
                    Log("data:", data.decode('utf-8'))  # 测试
                    if re.search(r'buy', data.decode('utf-8')):
                        Log("触发buy")
                        exchange.SetContractType(ct)
                        exchange.SetDirection("buy")
                        exchange.Buy(-1, amount)
                    elif re.search(r'sell', data.decode('utf-8')):
                        Log("触发sell")
                        exchange.SetContractType(ct)
                        exchange.SetDirection("sell")
                        exchange.Sell(-1, amount)
            
            # 写入数据应答
            self.wfile.write(json.dumps({"state": "ok"}).encode())
        except Exception as e:
            Log("Provider do_POST error, e:", e)


def createServer(host):
    try:
        server = ThreadingHTTPServer(host, Executor)
        Log("Starting server, listen at: %s:%s" % host)
        server.serve_forever()
    except Exception as e:
        Log("createServer error, e:", e)
        raise Exception("stop")

def main():
    # 开启一个线程
    try:
        _thread.start_new_thread(createServer, (("0.0.0.0", Port), ))         # VPS服务器上测试           
    except Exception as e:        
        Log("错误信息:", e)
        raise Exception("stop")    
    if exchange.GetName().find("Futures") != -1:
        exchange.SetContractType(ContractType)
    Log("账户资产信息:", _C(exchange.GetAccount))
    while True:
        if exchange.GetName() == "Futures_CTP":
            if exchange.IO("status"):
                LogStatus(_D(), "CTP连接")
            else:
                LogStatus(_D(), "CTP未连接")
        else:
            LogStatus(_D())
        Sleep(2000)

More

eth8888/upload/asset/2457669d9c32568732cb7.jpg Open the hard drive to show this error, where is the error?

eth8888/upload/asset/2457669d9c32568732cb7.jpg Open the hard drive to show this error, where is the error?

The Sword of Hua ShanTeacher: Can I add a message?

The Sword of Hua Shan2021/11/21 11:53:33 CMD 199866 buy=0.1, Administrator received this message and there is no activity in the bot

superonI've been looking at it all night and I don't understand it, can you translate it?

What is it?Can you write a complete example of how to get the information in the body of a TV signal?

DietQuantTeacher, how can we change to add dual currency on a single disk?

Dreams are worth eight figures.Oh my God, can you make an OKv5 simulation disc?

ssxxxdTeacher, I have a question. Is version 3.7 the version of the deployment administrator? How do I update it? 2021-08-11 11:08:55 Traceback (most recent call last): File "", line 999, in __init_ctx__ File "", line 4, in ImportError: cannot import name 'ThreadingHTTPServer' 2021-08-11 11:08:55 Information Commodity futures trading library was successfully loaded 2021-08-11 11:08:55 Info The host you are using for the python compilation environment is the python version 3.5.

qq3390214355I've been reading it all night and I still can't understand it, can you translate it, how to contact me?

I'm not sure.Hello, how do you write the webhook address of the direct alert on TV?

lanbnGood, thank you teacher, I'll try it!

lanbnTeacher, hello, can you give me some details about the contract code, programming is really hard, thank you!

The Little DreamThe FMZ API documentation has a function for OKEX switching analog drives at the end.

wbe3- small potato chipsHow do I switch the analogue disc? It always reminds me that the environment is not compatible.

The Little DreamThe error is due to Python not having a related module ThreadingHTTPServer

key986Did you solve this problem?

eth8888Let's get this straight.

The Little DreamIt is not recommended to create a service to listen to TV requests in this way, use FMZ's extension API, there is an article in the library.

The Little DreamThis strategy is used as an example of commodity futures, if you need to set a contract code to run a digital currency.

The Little DreamThis tactical example is straightforward, where a tactical robot builds a service to listen in on signal requests from the TV. What's up? # Test POST request for body information data = self.rfile.read ((200) # Specifies the length of the read Log (("data:", data) # Prints the data of the POST request, which can be specified based on the data in the request and then the robot can perform the corresponding operation What's up? There is already an annotation in the code, here is the body information for processing the request from the TV.

The Little DreamIn addition to the above, I am also very happy to see that you have been able to change your strategies according to your needs.

The Little DreamThe API KEY to configure the OKEX analog disk on FMZ is then added to the code with a word ``exchange.

The Little DreamI don't understand, I guess it's where I made a mistake when I used it, check it out.

I'm not sure.Thank you, if my webhook address says buy, but the tv strategy alert suggests sell?

The Little Dreamhttps://www.fmz.com/bbs-topic/5969 There are examples in articles and strategies.

The Little Dreamhttps://www.fmz.com/api#exchange.setcontracttype... is in the function description, you can see it here.

PY008What is a contract code?

The Little DreamI'm not being polite.

The Little DreamIf it's a contract for an exchange, write the webhook address on the TV: I'm going to make another deal: What's up? This is a list of all the different ways Access_key=xxx&secret_key=yyy&type=long&amount=1 is credited in the database. What's up? I'm not going to say that I'm not. What's up? This is a list of all the different ways Access_key=xxx&secret_key=yyy&type=cover_long&amount=1 is credited in the database. What's up? Remember to set the contract code on the policy parameter.