Policy and status engine

Author: The Little Dream, Created: 2017-09-07 19:02:31, Updated:

Policy and status engine

  • I haven't written in a long time, I wanted to talk about strategy and statuses.

    Many friends have good trading strategies, but when implemented in a programmatic way, they often find that program control cannot be executed according to the previous thinking, and will encounter more open positions, more flat positions, even an unreasonable contract with more positions and empty positions at the same time. In fact, the root cause of these phenomena is that programmatic trading is not like subjective trading, in case of emergencies, it can be timely reacted and processed. For example, I do an operation when the opening of a strategy condition is met, but at this opening price position, or in the market environment at that time, can not be traded, then the program must do the withdrawal operation; and after the withdrawal, whether additional positions are opened, to reach the planned opening position, also requires program control.

    The state machine is a state transfer map, which distinguishes all the states of the order by introducing the state machine, and the program controls its logic in all the states without disturbing it.

    In the course of the transaction, we impose on the order actions: opening bid, withdrawing bid, closing bid, withdrawing bid; we receive feedback on the status of the order as a result of the action: not all transactions, all transactions, withdrawal (partially completed or not completed). Thus, we can mark this state as the state after the order is executed. For example, the action open bid indicates a state button > open bid or the bidding button is open.

  • The following is a simple example of a more classic single contract for the reader's reference:

    img

    In this way, in the process of trading a single contract, the order can be strictly distinguished from any state in which it is located; the process can also be processed according to its different state. For example, after the withdrawal of the overtime of the breakeven, the original price can be kept flat, or a certain slippage can be added, or the opponent's hanging order price can be kept flat, which can be controlled according to your strategy.

    Of course, you can also control the flow of the state according to your intention, for example: in the previous example, the pump withdraws the ing pump receives a return if the withdrawal is successful and there is an open trade, the state does not flow to the pump can be mediocre but instead choose a way to continue according to the previous target position continue to trade with the remaining position, and then flow to the pump open positioning pump.

  • This is a two-contract state machine. The state machine involves two contracts, i.e. the problem of flattening the legs.

    However, as mentioned above, we apply an action to the order, summarize the return of the order or the return of the transaction into several responses after the order is applied, and set the order state while waiting for the action response. With this in mind, we list all the actions in a row, then combine the two contract actions, and then add ing, and we list all the states in a row.

    The moves imposed on the contract: open, flat, withdraw, etc. The roll is because after one leg holds the position, according to the principle of the leverage, the other leg must be leveled. Therefore, one leg first establishes a good position, and must wait for the second leg to establish a good opposite position, which constitutes a leverage combination.

    In this case, the order status of the two contracts is included in the following table:

    img

    In this way, you can construct the state machine you need based on the feedback after the action is applied to the order. The following diagram is the state machine to consider when building a leverage portfolio for the reader's reference:

    img

    The logical and state flow structure of the flattening of the leverage portfolio is consistent with the basic and open position construction, which is not discussed here.

This post was originally published on Ronalgao's blog.


More