vdemydiuk / mtapi

MetaTrader API (terminal bridge)
http://mtapi4.net/
MIT License
514 stars 277 forks source link

How can I obtain a tick listener for OnTick() in Python? #228

Closed eabase closed 3 years ago

eabase commented 3 years ago

Right now I am simply polling for new ticks by doing the following (in Python):

...
import MtApi as mt

mtc = mt.MtApiClient()
mtc.BeginConnect(ip, port);
#tic = mt.MqlTick()

while 1:
    rA = mtc.SymbolInfoTick(sym)
    # print bid/ask
    time.sleep(0.5)

This is stupid, and should be implement as a thread or something.

The problem is, I don't know how to emulate the OnTick() signal, to tell me when there is a new tick.

@vdemydiuk How can I make or implement a listener that will tell me when there is actually a new tick? Is there anything in the API I can use for this?

eabase commented 3 years ago

hmm, from #63

QuoteUpdate , QuoteUpdated ??

Maybe use this:

eabase commented 3 years ago

@vdemydiuk Does this mean that the tickevent is not avaiable in MT4 API?

vs.

eabase commented 3 years ago

How are the different DLL calls supposed to be used?

If we set c=MtApi.MtApiClient, then we have the following QuoteUpdate related attributes:

attr: QuoteAdded             type: <class 'CLR.EventBinding'>    doc: <n/a>
attr: QuoteRemoved           type: <class 'CLR.EventBinding'>    doc: <n/a>
attr: QuoteUpdate            type: <class 'CLR.EventBinding'>    doc: <n/a>
attr: QuoteUpdated           type: <class 'CLR.EventBinding'>    doc: <n/a>

attr: add_QuoteAdded         type: <class 'CLR.MethodBinding'>   doc: Void add_QuoteAdded(System.EventHandler`1[MtApi.MtQuoteEventArgs])
attr: add_QuoteRemoved       type: <class 'CLR.MethodBinding'>   doc: Void add_QuoteRemoved(System.EventHandler`1[MtApi.MtQuoteEventArgs])
attr: add_QuoteUpdate        type: <class 'CLR.MethodBinding'>   doc: Void add_QuoteUpdate(System.EventHandler`1[MtApi.MtQuoteEventArgs])
attr: add_QuoteUpdated       type: <class 'CLR.MethodBinding'>   doc: Void add_QuoteUpdated(MtApi.MtApiQuoteHandler)

attr: remove_QuoteAdded      type: <class 'CLR.MethodBinding'>   doc: Void remove_QuoteAdded(System.EventHandler`1[MtApi.MtQuoteEventArgs])
attr: remove_QuoteRemoved    type: <class 'CLR.MethodBinding'>   doc: Void remove_QuoteRemoved(System.EventHandler`1[MtApi.MtQuoteEventArgs])
attr: remove_QuoteUpdate     type: <class 'CLR.MethodBinding'>   doc: Void remove_QuoteUpdate(System.EventHandler`1[MtApi.MtQuoteEventArgs])
attr: remove_QuoteUpdated    type: <class 'CLR.MethodBinding'>   doc: Void remove_QuoteUpdated(MtApi.MtApiQuoteHandler)
eabase commented 3 years ago

I have solved the issue! The problem was over-complicating the delegates while trying to understand and use the far more complex nature of the underlying .NET code. The example VB code was trivial to implement, once it was understood.

To get OnTick() ticks, all you have to do is this:

...
import MtApi as mt

def printTick2(source, *args):
    print('source: {}\nargs: {}'.format(source, list(args)))

mtc = mt.MtApiClient()
mtc.BeginConnect('127.0.0.1', 8222);

mtc.QuoteUpdated += printTick2

# Run the loop forever waiting for and printing new ticks
while 1:
    pass
    time.sleep(0.1)
...

I will post a PR with more python code in a few days.