How To Subscribe To A .net Event In A Python Listener Using Pythonnet?
Solution 1:
After a few days and some long hours I found a combination of mistake(s). As always, a combination of 2-3 simple mistakes can make your life miserable for a long time. But the biggest mistake of all, was being fooled to think that a listner (aka. delegate) had to be complicated with a bunch of __init__
and __iadd__
functions. Wrong! Python.NET just works most of the time but the errors you get (if any at all) are pretty useless if you make any small mistake.
There are 1.5 problems with my original code.
There were 2 different Quote functions, namely:
QuoteUpdate
which returns sender and an object named "MtQuoteEventArgs" in .NETQuoteUpdated
which returns sender and 3 arguments.Therefore we need to fix both the printTick() function and the listener line.
The corrected code is:
defprintTick(source, symbol, ask, bid):
print('Tick: Symbol: {} Ask: {:.5f} Bid: {:.5f}'.format(symbol, ask, bid))
...
mtc.QuoteUpdates += printTick
For additional info on C# events & delegates:
Solution 2:
According to the mtapi documentation you linked, the code should be:
defprintTick(sender, args):
print(str(args.Quote.Instrument))
mtc = mt.MtApiClient()
res = mtc.BeginConnect('127.0.0.1', 8222)
mtc.QuoteUpdate += printTick # Subscribe & handle new repeated events
rA = mtc.SymbolInfoTick(SYM) # Make a request to get a one-time tick data# Need wait loop here
Post a Comment for "How To Subscribe To A .net Event In A Python Listener Using Pythonnet?"