advertisement
TradingViewWebHook alarm directly connected to FMZ robot
Recently, more and more TradingView users have connected the TradingView chart signal to the FMZ platform (FMZ.COM) and let the robot strategy on FMZ execute the trade according to the chart signal, which saves a lot of code writing and design work. Directly, the indicators can be used for programmatic and automated trading, which reduces the barriers for many programmatic and quantitative trading developments. There are several design schemes for performing automatic trading in TradingViewWebHook.
The previous solution: https://www.fmz.com/digest-topic/5533.
The previous plan was to extend the API interface of the FMZ platform to send instructions to the robot. Today, let's take a look at another solution. Let TradingView's alarm WebHook request be sent directly to the FMZ platform robot, so that it can directly send instructions and order trades from the robot.
Robot strategy source code
The strategy is written in Python. Once the robot is created and started using this strategy, the robot will create a thread that will start a service to monitor the established port. Waiting for external requests and processing. When I tested it, the host on the server tested it and the device where the host is located should be accessible from the outside. When the robot executes the transaction, it uses the market order interface. Additionally, this strategy can also be modified to implement limit order order logic. To make it easy to understand and simplify, the market order is used here, so the exchange must support the market order.
'''
Request format: http://x.x.x.x:xxxx/data?access_key=xxx&secret_key=yyy&type=buy&amount=0.001
Strategy Robot Parameters:
- Type: encrypted string, AccessKey, SecretKey. You can use the low-privilege API KEY from the FMZ platform or you can generate the KEY yourself.
- Type: string, Contract ID, Contract Type
- Type: numeric value, port number, Port
'''
import _thread
import json
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import parse_qs, urlparse
def url2Dict(url):
query = urlparse(url).query
parameters = parse_qs(query)
result = {key: params[key][0] for key in parameters}
return result
Class Executor (BaseHTTPRequestHandler):
def make_POST(self):
test:
self.send_response(200)
self.send_header("Content type", "application/json")
self.end_headers()
dictParam = url2Dict(self.path)
# check
if len(dictParam) == 4 and dictParam["access_key"] == AccessKey and dictParam["secret_key"] == SecretKey:
del dictParam["password"]
del dictParam["secret_key"]
Log("Request received", "parameter:", dictParam, "#FF0000")
'''
map[access_key:xxx quantity:0.001 secret_key:yyy type:buy]
'''
isSpot = True
if exchange.GetName().find("Futures") != -1:
if Contract Type != "":
exchange.SetContractType (Contract Type)
isSpot = False
more:
raise "No futures contract has been established"
if isSpot and dictParam["type"] == "buy":
exchange.Buy(-1, float(dictParam["amount"]))
Login (exchange.GetAccount())
elif isSpot and dictParam["type"] == "sell":
exchange.Sell(-1, float(dictParam["amount"]))
Login (exchange.GetAccount())
elif not isSpot and dictParam["type"] == "long":
exchange.SetDirection("buy")
exchange.Buy(-1, float(dictParam["amount"]))
Log("Hold Position:", exchange.GetPosition())
elif not isSpot and dictParam["type"] == "short":
exchange.SetDirection("sell")
exchange.Sell(-1, float(dictParam["amount"]))
Log("Hold Position:", exchange.GetPosition())
elif not isSpot and dictParam["type"] == "cover_long":
exchange.SetDirection("closepurchase")
exchange.Sell(-1, float(dictParam["amount"]))
Log("Hold Position:", exchange.GetPosition())
elif not isSpot and dictParam["type"] == "cover_short":
exchange.SetDirection("closesale")
exchange.Buy(-1, float(dictParam["amount"]))
Log("Hold Position:", exchange.GetPosition())
# Write data response
advertisement
Related Articles
advertisement