检测并防止连续重复Tradingview webhook json的处理



我对python不是很熟悉,但经过多次试验和错误,并以其他人的工作为基础,开发了一个简单可靠的Tradingview (TV)和Interactive Brokers (IB)之间的接口。

我有这样的情况,偶尔明显的重画电视图表导致重复的订单被提交。(是的,我已经研究了可以减少或消除这种重画的方法,但这些方法的效果大大降低了我的交易算法的已证明的盈利能力。)我试图修改我的代码来检测和防止按顺序接收的重复订单的处理,因为我从不打算在任何给定的安全和任何给定的时间内有多个订单工作。我的修改尝试(见下文)是相当初级的——简单地检测刚刚接收到的webhook json是否与之前接收到的基本相同,如果是,则忽略。

在这里,如果你愿意,你可以扔石头,告诉我去读"ver"、"let"one_answers"global"。我试了好几个小时,就是不"明白"。我70多岁了,来自Fortran、Cobol和Basic的时代,那时变量只是简单地声明(如果需要的话)、初始化和使用。有人可以帮助我简单地得到一些开始值进入两个变量,xticker和xorder,在下面的代码?或者更好的是,告诉我一个更好的方法来实现我的目标?

非常感谢您的帮助!

[编辑]-上面提到的重画可能是由于我设置了"calc_on_order_fills=true"和/或"process_orders_on_close ="true"同时也设置了&;calc_on_every_tick=true&;我已经将前两个设置更改为"false",但没有办法确定这样做是否可以摆脱电视警报/webhook重复。但不管这些脚本修改是否有益,我仍然想添加一个"检测并忽略重复"。如所述,将我的电视连接到IB桥。

#Create root
@app.route('/', methods=['GET','POST'])
async def root(request):
return response.text('online')
#Listen for signals and execute orders
@app.route('/webhook', methods=['GET','POST'])
async def webhook(request):
if request.method == 'POST':
await checkIfReconnect()
#Parse alert data
alert = request.json

order = MarketOrder(alert['order_action'],alert['order_contracts'],account='aannnnnnn')  
ticker = alert['ticker']
# Attempt to detect and prevent processing of duplicate webhook json
# Fails since xticker and xorder are undefined and have no value.
# Need to initialize xticker='dummy' and xorder='dummy' at start
# Where/how to place these two initializing statements?
if not ticker == xticker and xorder == order:

#Do stuff (process and submit valid order)
#then ...

xticker = ticker
xorder = order

return HTTPResponse("ok", 200)
return HTTPResponse("", 405)

我尝试过直接使用=,var和let设置xticker和xorder,并且尝试过但未能理解局部和全局变量赋值在Python中的工作原理。如果没有人提供帮助,我的下一个努力就是利用read &将操作写入外部文件,以便绘制初始值,然后跟踪最后有效的json数据-粗糙但可能可行-并且必须有许多更简单和更好的方法,所有这些都超出了我的知识范围。

在网上找到一些例子后解决了我自己使用全局变量的问题。解决方案按预期工作,顺序重复的订单被忽略。相关代码摘录如下:

#Create root
@app.route('/', methods=['GET','POST'])
async def root(request):
return response.text('online')
#Listen for signals and execute orders
@app.route('/webhook', methods=['GET','POST'])
async def webhook(request):
if request.method == 'POST':
await checkIfReconnect()
#Parse alert data
alert = request.json

order = MarketOrder(alert['order_action'],alert['order_contracts'],account='aannnnnnn')
ticker = alert['ticker']
action = alert['order_action']
source = alert['source']

if not (ticker == xticker and xaction == action and xsource == source):
def inner():
global xticker
xticker = ticker
global xaction
xaction = action
global xsource
xsource = source
inner() 

#Do stuff (prepare and submit order to broker)

return HTTPResponse("ok", 200)
return HTTPResponse("", 405)
#Reconnect if needed
async def checkIfReconnect():
if not app.ib.isConnected() or not app.ib.client.isConnected():
app.ib.disconnect()
app.ib = IB()
app.ib.connect('127.0.0.1',7497,clientId=int(int((time.time()*1000)-1667740000000)/1000000))

#Run app
if __name__ == '__main__':
#Connect to IB
app.ib = IB()
xticker = "ticker"
xaction = "action"
xsource = "source"
app.ib.connectAsync('127.0.0.1',7497,clientId=int(int((time.time()*1000)-1667740000000)/1000000))
app.run(port=5000)

最新更新