Replies: 6 comments 1 reply
-
Question: you mention oandapyV20 but I get the impression you mean the V20 lib of OANDA, since you mention a method:
in your example. Thing is that oandapyV20 uses request classes. There is no such method as list_accounts() So, is your question for oandapyV20 or for V20? |
Beta Was this translation helpful? Give feedback.
-
Apologies for my confusing approach. The example was from the source code for what's now been published as pyfx.dispatch.oanda. The API used there was generated with the OpenAPI Generator from an OpenAPI specification for v20 published by OANDA, last updated some years ago - more details available in the readme for the linked project. At the time when I'd created the issue ticket asking about AIO support, I hadn't been able to get the generated source code running here. After restarting the iPython session and a little more debugging, however, it works now though - for the most part. Not every API method in the Oanda v20 OpenAPI specification is actually available as a REST endpoint, but I've managed to figure out how to use the API to print FX quotes information. I appreciate that this was converted to a discussion item. Apologies for my rambling in the initial post. |
Beta Was this translation helpful? Give feedback.
-
To answer your question: I leave things to the developers to program async. code the way they like it. So, short term there will be no changes to oandapyV20 specifically related to asyncio It is easy to wrap oandapyV20 in async. approaches. Personally I've used gevent a lot over the past years for async. coding. An example to fetch prices using async (just a demo!):
Result:
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the example! fwiw I wasn't able to figure out where all the pricing endpoints are as yet, lol. After getting the generated code running, I'd been seeing some errors about unavailable endpoints with the couple of pricing endpoints I'd tried, which were described in the original OANDA OpenAPI v20 specification, e.g Once connecting with an HTTP/2 client, using HTTPX/HTTPCore, even the I'm not sure how active their support might be, for this aspect of the OANDA framework. MT5 remains unavailable with brokers in the US, will keep hacking on something though lol. Thx for the example! |
Beta Was this translation helpful? Give feedback.
-
FWIW: I'm currently looking into making a change to have the API client also support async. requests. |
Beta Was this translation helpful? Give feedback.
-
Super! I guess asyncio isn't necessarily all the thing, in all things? Reading a little about green futures & green threads implementations recently, after the eventlet integration in futurist. If one could share some ideas: Futurist itself offers a green threads alternative to the multiprocessing Thread Pool Executor, based on eventlets. This is how I'd found the project, after looking for something to address a certain behavior of a Thread Pool Executor producing an atexit behavior that prevents the process from exiting. (There is a patch for this using system threads, or one could use green threads) Futurist is probably portable to the alternate greenlets API. These green APIs don't seem to use asyncio for scheduling. Perhaps these green approaches may not add a lot of additional concerns for thread-safety, furthermore, as juxtaposed to the one-to-one threads and loops correlation in asyncio. For green/asyncio interoperability there is an async layer available, based on greenlets. It's probably portable to gevent which also uses greenlets. After the initial question, I'd developed something of a futures-oriented approach, it ultimately needed a concurrent.futures.Future implementation in addition to asyncio futures. Requests are sent under one (thread, loop) pair using a common httpx client and transport for http/2 request pooling when possible. Responses are each processed under a thread pool worker, with an asyncio loop pre-initilaized to each worker, then asyncio json processing under each loop using ijson (maybe kind of odd here, as the worker would be running at most one processing routine, I'd hoped it might help to balance the processing load for the other threads though, to use asyncio in the worker threads too) Of course, the loops and futures under asyncio aren't rated "Thread-safe", and will often throw an exception if something sets a result or exception state from the wrong thread. So, there was the addition of a concurrent.futures.Future. This could be used to pass either an exception or a deserialized object back to the request caller, from within a response processor under a worker thread. The Future might even implement an With the greenlets API, gevent offers a system thread pool. The thread-safety caveats might be less limiting here. I'm not sure if there's HTTP/2 client support in gevent itself. Maybe it could be fairly straightforward to wrap a synchronous HTTP/2 client in greenlet calls? Hoping this helps. There are other alternatives for concurrent processing? |
Beta Was this translation helpful? Give feedback.
-
Has there perhaps been any discussion about introducing asyncio support for oandapyv20?
Alternate Approaches ??
As one hypothetically possible approach for asyncio support with the Oanda REST API in Python, today I was taking a look at using the Python (client) generator from the OpenAPI-generator project, starting with the v20-openapi spec from Oanda. The resulting Python code - as produced with templates in the OpenAPI generator Java libraries - it had a sort of optional asyncio feature. It was able to contact the Oanda server, but it looks like it may not have just matched the request URLs expected by the server. Candidly I haven't tried to debug this further, still quite new to this framework. The code doesn't work, as yet, but for purpose of example I might at least try to publish the tooling for the build.
If evaluating the approach of using OpenAPI-generator to produce AIO code: There is a shell script. It needs Java, Maven 3, and jq. When the script's
generator
command is applied as follows:...then the generator will produce a complete Python project, including a
pyproject.toml
and test files, etc, in the current working directory. In the example, that would beapi/client
when the script is calledThe generated code is usable for e.g
list_accounts()
in the following,I'd seen some errors from this, previously, thus alternately the question about aio support here
After restarting the ipython session, it seems to work though. I'll try to publish the build configuration for this, perhaps it's not an entirely straightforward toolchain albeit.
Applications ?
Towards applying aio support for the Oanda REST API: Candidly, I was thinking about trying to mix asyncio with PySide6 (Qt for Python) starting with the Oanda REST support.
The idea here has been to develop an alternative to MetaTrader 5. While the MetaTrader 5 platform might offer some improvements for calculations, compared to MT4, and it offers support for OpenCL and DirectX but it seems that - perhaps due to the absence of broker support - MetaTrader 5 is principally unavailable for live trading with FX brokers licensed in the US.
afaict the earlier MT4 platform might not do really well with a moderate amount of data- and number-crunching for technical indicators. MT4 does OK with line drawing, but after some technical analysis methods developed by John F. Ehlers, MT4 may almost literally start to crunch.
I understand that it should be possible to use synchronous I/O throughout. Considering the possibility of opening a Qt GUI with a tab for each of a set of trading instruments, I thought it might be useful to start out with AIO if possible, whatever hacks might be needed to keep the Python aio parts synchronized with the Qt GUI.
On the Qt GUI side, there's also PythonQwt. It could probably be extended for a presentation of OHLC time series, as well as tick volume series and time-series data from technical indicators, in a Qt chart window.
Considering the possible complexity of the GUI features, I thought it might be conducive to start with the I/O features first, however.
Synchronous and Asynchronous Methods in the Same API ?
For the I/O components, considering the approach used in the code generated with OpenAPI-generator when applied as in the previous, perhaps it may be possible to implement both aio and synchronous I/O client methods in parallel. The code may seem to rely on the aspect that an
async def
method would return an async generator, such that can be applied as like an AIO Future under anawait
expression within some calling function, or otherwise applied within the aio framework.So, perhaps it may be possible to implement that in parallel with a synchronous I/O implementation.
With apologies for if this may resemble something like a blog article: Thus the question of aio support ...?
Beta Was this translation helpful? Give feedback.
All reactions