Evolving Trading Systems

AI is coming, and we are not afraid of it. I suggest puzzling him by earning cabbage on the stock exchange. For starters, we'll see.


An agent is a program that has accounts in various currencies on one exchange, whose work should lead to an increase in their total value.

Agent asset - a list of accounts with the availability of funds on them A = [A1, A2, ..] where A1, A2, ... the amount in the corresponding currency.

Asset value. We must have a universal measure of the asset that allows us to measure the asset. Take for granted that this is $. A $ (account) is part of the asset. We also assume that we have an algorithm-ability to estimate the value of each account in the list through $.

An agent can be closed (rely only on its own asset) or open (borrow). For simplicity, we assume that we have a closed one.

Each agent operation has its own value So (Ax-> Ay, Sum) which we will measure with our measure $ and time To (Ax-> Ay).

Agent environment - other agents or people with similar or dissimilar work algorithms + any information that is valid at that moment.

Agent strategy. This is an algorithm that receives information on the status of accounts, the history of the exchange’s activity, its activity, the current exchange’s activity (glasses), and, in response, performs or does not perform a set of actions.

Strategies can only be based on the current activity of the exchange (primitive),
take into account information from other exchanges (medium), take into account the history of activity and detected statistical patterns (deep). Pessimistic or optimistic - differ in the assessment of the risk of operations. Partial or complete. A complete strategy covers all aspects of the functioning of the agent. Partial can be combined to form a complete one.
Optimality of strategies and their choice. Normally, the agent has several strategies, switching between them occurs with some stable (more than a certain time interval) change in the environment.

Agent inertia - how long the agent ignores a change in environment. Or a strategy that determines its resistance to environmental disturbances. A more inert agent can either win or lose, not succumbing to the “panic” of the environment.

Suppose we have three sets of incomplete strategies:

St   = [1,2,3,4] St   = [5,6,7,8] St  = [9,10,11,12] 

The combination of these strategies provides 64 unique agent behaviors that
It turns out to be correct (useful) for environments [O1, O2, O3] and useless for [O4, O5, O6] or harmful for [O7, ..]

Analyzing the work of strategies and their combinations in various environments
We can build a metastrategy that will switch the agent’s behavior depending on the environment being detected.

The agent platform (hereinafter referred to as the platform, MetaBot) is a specific agent that creates, coordinates, deletes agents on connected exchanges. Like an agent, it architecturally consists of a set of control strategies that can be combined to improve the functioning of the whole system. The platform stores the history of all operations and other useful data that allow its component strategies to effectively (in $) manage subordinate agents, analyzing general statistical patterns and the history of the impact of strategy combinations on success in specific environments. The platform is driven by metastrategy, whose parameters evolve over time to maximize profits.

The environment of the exchange agent consists of other agents and current information. Environment changes for the agent are divided into optimal, normal, critical, catastrophic, etc. by the degree of success of operations. Notifying the platform of its condition and operations, the agent acts as long as the platform is 'Satisfied' with the state and dynamics of its accounts, otherwise it closes it. The platform differently evaluates total assets and can make decisions on transferring an asset from one agent to another, which can operate on one exchange or on different ones. The task of the platform is to maximize the total asset held by all agents.

Consider the contents of a simple implementation of an agent strategy (on Scala) for processing an Orderbook exchange:

 //    var minDelayBetweenOperationsSec: Long //      // 0,     > 0      // < 0      def action(rateVolume: TimeRate): Double //     def middleValues() : Seq[Double] 

We bring in a genome that will develop during the operation of the system.

StrategyParams is the bot strategy genome that contains a lot of Gene to parameterize the strategy:

 case class StrategyParams(low: IntGene, middle: IntGene, high: IntGene, intense: DoubleGene, threshold: DoubleGene, delay: IntGene) 

where low, middle, high are the periods in minutes for the asset values ​​averaged over it.
They are used to predict the movement of value.

Intense means the activity or aggressiveness of the bot. With large parameter values, the bot makes transactions for large amounts and (or) evaluates predictions with great optimism.
Threshold means the minimum confidence of the bot in the prediction, which prompts him to complete the operation. Delay - inertia, the minimum time between activations.

It is postulated that there is an optimal bot strategy for any period described by StrategyParams parameters that can be calculated from the history of the asset’s rate. Its parameters can be calculated using the evolutionary genome selection algorithm. The calculation has the form:

 def evalBots(trs: Seq[TimeRate]) = EvolutionFactory( candidate = defaultBotStrategyParams, populationConfig = PopulationConfig(500), evolutionConfig = EvolutionConfig(100), errorRate = (c: StrategyParams) => {errorBot(c, trs)} ). 

trs - Orderbook history,
populationConfig - how many bots at the same time compete for survival among themselves, evolutionConfig - how many iterations of life and reproduction to do. In the process of iterations, bots with fewer assets are less likely to produce offspring with other bots, which ultimately, with a sufficiently large population and the number of degeneration-evolution, should lead us to the ideal bot (bots).

Having received and calculated such bot (s) for a certain period (history), you need to somehow use it for effective trading in the future. Platform-Metabot and its MetaStrategy will deal with this issue.

Consider the contents of a simple platform strategy implementation:

 MetaStrategy(rateSensitivity: DoubleGene, volumeSensitivity: DoubleGene, profitSensitivity: DoubleGene, criticalForce: DoubleGene, volumesSize: IntGene, delaySwitching: IntGene) 

where rateSensitivity, volumeSensitivity, profitSensitivity describe how sensitive the behavior of the Platform is to changes in the parameters of the asset price, trading volume, and a decrease in the expected profit from operations.

criticalForce sets a threshold value based on a comprehensive assessment of the above parameters, exceeding which is a signal of inefficiency of the current bot strategy (not the platform) and calculating its (current bot strategy) more successful version.

Fragment of implementation:

 val force = rateForce * volumeForce * profitForce if (force >= strategy.criticalForce.value.get) changeBot(force / strategy.criticalForce.value.get, extremeBot, tr) 

Having described the platform strategy algorithm using the MetaStrategy parameters, we can start a similar evolutionary process for growing a population of effective platform metabot-bots, the most effective of which will become the basis of the trading system.

To test the effectiveness of these ideas, a prototype cryptocurrency trading system was created that can be used as a testing ground. The landfill requires data on the history of courses and their average values ​​over the periods, which are stored in Influx and generated by the landfill itself by extraction through the open bitfinex exchange API. See object HistoryExtractor.

Test results

During the runs, we managed to find some simple strategies for agents who, without the use of the Metabot manager, showed 14% profit per year for a test 2.5-year history. Although impressive, it does not guarantee the same interest on future periods. The behavior of the system in real time was not tested. Using Metabot strategies designed for short periods (a month or less) did not make a profit, but for large periods effective meta strategies are quite possible. Their calculation was difficult due to the great computational complexity (months of uninterrupted operation of an average PC). Chaotic cryptocurrency rates can only be adjusted to a small extent, but ... using this approach to trade other assets, whose volatility is not so chaotic and has periods of seasonality, can give better results.

Polygon sources are available here .

Source: https://habr.com/ru/post/463357/

All Articles