Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

How to make friends python with win32 api

342 views

Published on

How to use win32 broker API from python

Published in: Technology
  • Login to see the comments

How to make friends python with win32 api

  1. 1. How to make friends of Python and Win32 API Connecting the Python to a Win32 API provided by a stock broker
  2. 2. What was expected
  3. 3. Terms
  4. 4. Real world ?
  5. 5. Issues ● COM to pure Python object conversion ● Flow (events, async methods) handling ● Multithreading support ● Extensibility
  6. 6. Envisioned solution
  7. 7. Or...
  8. 8. What I’ve found ● pywin32 package on sourceforge.net by Mark Hammond ● python interpreter builds: ActiveState, Anaconda, Canopy with pywin32 ● Python Programming On Win32 by Mark Hammond and Andy Robinson ● no actual documentation on activestate.com
  9. 9. Pywin32 workflow Make Handle Events class Make client object with DispatchWithEven ts method Use API methods on python object
  10. 10. Python client structure
  11. 11. Event class class ClientEvents: def OnAddBar(self, row, nrows, symbol, interval, datetime, open, high, low, close, volume, open_int): pass # some logic …
  12. 12. Client clnt = client.DispatchWithEvents('COM.Server.1', ClientEvents) … clnt.GetBars('SBER', bar_interval, date_time_object, 10)
  13. 13. Cool tool Make folder python_folderLibsite-packageswin32comgen_py Run 'win32comclientmakepy.py' (eg, run it from the command window, or double-click on it) and a list will be presented. Select the Type Library. It will generate python wrapper classes from COM Library with all available methods.
  14. 14. Convert DateTime objects def pytime_2_datetime(pythime): return dt(year=pythime.year, month=pythime.month, day=pythime.day, hour=pythime.hour, minute=pythime.minute, second=pythime.second) def datetime_2_pytime(datetime): return pywintypes.Time(time .mktime(datetime.timetuple()))
  15. 15. Solution v. 0.1 ● COM to pure Python object conversion ● Flow (events, async methods) handling
  16. 16. Issues: ● Multithreading support ● Extensibility ● Not stable (?)
  17. 17. Manager
  18. 18. Workflow Add event queue Create custom Manager Make proxy to COM Object Get pure python Events queue Use proxy and queue from other threads
  19. 19. Modify Event class, part 1 class ClientEvents: def __init__(self): self.event_queue = Manager().Queue() def get_event_queue(self): return self.event_queue …
  20. 20. Modify Event class, part 2 class ClientEvents: … def OnAddBar(self, row, nrows, symbol, interval, datetime, open, high, low, close, volume, open_int): self.event_queue.put_nowait(('AddBar', row, nrows, symbol, interval, pytime_2_datetime(datetime), open, high, low, close, volume, open_int))
  21. 21. Use multiprocessing.Manager class ComManager(BaseManager): pass
  22. 22. Add client method def get_com_server(): CoInitializeEx(COINIT_MULTITHREADED) clnt = client.DispatchWithEvents('COM.Server.1', ClientEvents) CoUninitialize() return clnt
  23. 23. Register methods ComManager.register('get_com_server', callable=get_com_server, exposed=('CancelBidAsks', 'CancelOrder', 'GetBars', 'GetMoneyAccount', …, 'get_event_queue'))
  24. 24. Fix DateTime bug, part 1 class ClientEvents: def GetBarsSer(self, symbol, interval, since, count): self.GetBars(symbol, interval, datetime_2_pytime(since), count)
  25. 25. Fix DateTime bug, part 2 ComManager.register('get_com_server', callable=get_com_server, exposed=('CancelBidAsks', 'CancelOrder', 'GetBars', 'GetMoneyAccount', …, 'get_event_queue', 'GetBarsSer'))
  26. 26. Connection server solution
  27. 27. Use as server, server code if __name__ == '__main__': freeze_support() m = ComManager(address=('127.0.0.1', port, authkey='authkey').get_server() .serve_forever()
  28. 28. Use as server, client code … manager = ComManager(address='address', port, authkey='authkey') manager.connect() com_server =manager.get_com_server() com_event_queue = com_server.get_event_queue()
  29. 29. Stand-alone
  30. 30. In code use from package_mame import ComManager if __name__ == '__main__': freeze_support() manager = ComManager() manager.start() com_server = manager.get_com_server() com_event_queue = com_server.get_event_queue()
  31. 31. Solution v. 0.2
  32. 32. Contacts Linkedin: https://linkedin.com/in/eugene-lopatkin E-mail: eugenelopatkin@gmail.com Thank you! Any questions?

×