Question: Is there any open-source project/library, which can act as a "local exchange" for agents (algorithms), running on the same computer?
Clarification: by "local exchange" I mean, that the agents, who have the accounts on this exchange are "algorithms", running on the same computer. That is, in this question I don't speak about creation of any exchange market for other people or companies to trade on.
To clarify I will provide a couple of use-cases and a set desirable features. In what follows, the Exchange library stays for the hypothetical library I want to find.
Use-case 1: trading system
Imagine, that you have 4 algorithms, which could manage a certain amount of money on stock exchange market and (hopefully) generating some profit. Say you have 4000 Tögrögs and you want to reduce risks by diversification: you want to give 1000 Tögrögs to each algorithm, so if one fails, the most you can loose is 1000 Tögrögs.
Imagine, you have a stock exchange broker and you opened one account with it. You don't want to open 3 more accounts (e.g. because this would be too expensive for you). Instead, you want each of 4 algorithms to have a virtual account in the Exchange library, so they could send their orders to the Exchange library, and the library would 1. check if the account contains enough money; 2. if it does, forward the orders to the exchange market; 3. cancel the buying and selling orders between the accounts, when possible.
I started by saying, that the purpose of this scheme is diversification. However, there are 3 additional benefits:
- Providing a unified interface to the trading algorithms: of course, you need to deal with API of the broker to submit the trades. But in the scheme above, you need to do it once by writing a gateway for the Exchange library, instead of thinking about API while implementing each of 4 algorithms.
- Local cancellation of orders allows for otherwise impossible trades. Imagine, for example, that algorithm A wants to pay 10.00 Tögrögs for one stock WWD of the corporation "Worldwide". However the broker has a commission of 0.10 Tögrögs, so the Exchange library obtains an order and puts the BUY order at 9.90 Tögrögs (so if the order is matched, the total amount paid will be 10.00 = 9.90 + 0.10 commission). However it puts a BUY order at the price 10.00 to the "local" market. Later on, while order was not matched, algorithm B decides to sell 1 stock WWD at 9.95. Given commission, the SELL order could be sent to the broker only at 10.05 or higher, but the order can be locally cancelled with the order of algorithm A immediately.
- Trades of otherwise untradeable pairs. Imagine, algorithm A decides, that one stock of WWD is worth 25 Lunar Dollars, but it is traded only in Tögrögs (with a current price of 10.00). While you don't have access to any gateway, that could exchange Tögrögs to lunar dollars, algorithm D has 30 Lunar Dollars and is happy to exchange them to 10 Tögrögs. This trade was submitted to the Exchange library, but couldn't be forwarded anywhere. Given that algorithm A decides to exchange 1 stock of WWD to 10 Tögrögs, and then exchange 10 Tögrögs to 30 Lunar Dollars by matching D's order, generating an (assumed) profit of 5 Lunar Dollars.
Use-case 2: economic hypothesis testing
Imagine, you want to observe a phenomena, that if agents follow a certain strategy, market goes through 30-year cycles. You decide to model it on your computer. You implement a simplistic model, in which there are 7 types of goods and 10000 agents, which can buy, sell, produce and consume them. What you need is a marketplace, where buyers and sellers can meet. You could use the Exchange library as such a marketplace and have 10000 accounts for the agents and no external gateways. You could then run the experiment, plot the prices and check, if the phenomena can be observed.
- Two interfaces. The library should have an interface for interaction with agents (accounts), as well as an interface for interaction with external world (gateways). There should be a possibility to have multiple accounts and multiple gateways.
- Local exchange. The Exchange library should implement local "marketplace", where agents could exchange goods/stocks/currencies.
- Flexibility of gateways. E.g. if API's of two brokers allow automatic transfer of stocks between them, there should be a way to add this procedure as a gateway.
- Scalability. There should be a simple way to write the algorithms for the agents in C++, as it allows for efficient complex processing of Gigabytes of data. On the other hand, one should be able to implement simple agents with small amount of code.
- Linux. One should be able to run the programs, written using the Exchange library on Linux, because both servers and computers, used for heavy computations, use Linux.
- Reliability. E.g. while a transaction can fail for objective reasons, it should never happen twice. Some transactions (e.g. transfer of funds between brokers) could take as long as a week, so the fact, that a transaction is ongoing, shouldn't prevent us from shutting down the application and starting it later on with the same state. There should be a way to recover from crashes.
- Async/efficiency. E.g. while waiting for a response from one gateway, the library shouldn't become unresponsive or slow down processing of unrelated transactions.
Extended question: It feels to me, that this is a very natural library to write. That's why I'm asking if anyone have done at least something similar. It feels I'm inventing a bicycle, and there even if this wasn't implemented yet, there are at least things I could start with and add my own code on top. If not, I would like to know, why, because this would probably mean, that people think differently, than I am, and may be I'm missing something. So the extended question is:
Is there something similar to the library I've described above, and if there isn't, then why?