I am not very well acquainted with python, but have by much trial and error, and building upon the work of others, developed a simple working and reliable interface between Tradingview (TV) and Interactive Brokers (IB).
I have situation where occasional apparent repainting of TV charts results in duplicate orders being submitted. (Yes, I have researched methods whereby such repainting can be reduced or eliminated, but the effect of these methods greatly reduces the proven profitability of my trading algo.) I am attempting to modify my code to detect and prevent processing of duplicate orders received in sequence, as I never intend to have more than one order working for any given security and any given time. My modification attempt (see below) is quite rudimentary--simply detect whether the just-received webhook json is essentially the same as that received just previously, and ignore if so.
This is where, if you wish, you can throw stones and tell me to go read about 'ver' and 'let' and 'global'. I've tried--for hours, and just don't "get it". I'm in my 70s and come from the era of Fortran, Cobol, and Basic, when variables were simply declared (if necessary), initialized, and used. Could someone kindly help me simply to get some starting values into the two variables, xticker and xorder, in the code below? Or better yet, show me a better way to accomplish my objective?
Many thanks for any help offered!
[Edit] - Repainting noted above may possibly have resulted from my setting "calc_on_order_fills=true" and/or "process_orders_on_close="true" while also setting "calc_on_every_tick=true". I have changed the first two of these settings to "false", but have no way to ascertain if doing so gets rid of the TV alert/webhook duplicates. But irrespective of whether these script modifications are beneficial, I would still like to add a "detect and ignore duplicates" feature to my TV to IB bridge as described.
#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 initiali开发者_JS百科zing 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)
I have tried setting xticker and xorder directly using =, var, and let, and have tried but failed to understand how local and global variable assignment works within Python. My next effort, if no assistance is offered, will be to make use of read & write operations to an external file in order to draw in initial values and then to keep track of the last valid json data-- Crude but perhaps workable-- and there must be a great many simpler and better methods, all of which are beyond my ken.
精彩评论