The previous article, Hard-Core Information - Details and Thoughts on Automated Real Offering of Quantitative Trading System (I. Problems and Difficulties), talked about some basic problems of real trading in the currency circle. This article talks about the main purpose of the firm offer.

I think there are four main ones:

1. Implement the established logic of the strategy

There is no doubt about this. When there is a signal, corresponding operations must be made, opening and closing positions. This point requires that you try not to use limit orders. Otherwise, if the transaction cannot be completed, you will have to pursue the order. The price may have already gone too far. In the end, whether to pursue the order is also a question.

In the long run, the slippage caused by using market orders is roughly similar to the slippage caused by occasionally missing limit orders but chasing orders, so it is better to enter and exit the market directly, unless your strategy is very special.

In addition, if you use limit orders, your code will be more complicated because you have to manage the unfilled limit orders and then follow up on them. And what if only part of the limit order is filled? Should the remaining limit orders remain pending? Or should you issue another market order if part of the limit order is missed? This one-step operation generates multiple filled orders, which is troublesome to review later and easy to confuse. Therefore, it is better to use market orders directly. As long as the submission is successful, the transaction is basically guaranteed. If it is indeed a slippage-sensitive strategy, then only trade with targets with good liquidity.

Also, for contracts, the leverage should not be changed at will. It is best to use the ideal leverage for backtesting and then discount it by 20%. Don't predict the market subjectively and adjust leverage manually. Changing positions is also a taboo in automated trading.

It is best to have an automatic win-loss position. When the retracement reaches a certain level, you should reduce the leverage. Don't do the opposite, thinking that the wind is coming soon and increase the leverage (but you can increase the principal and reduce the leverage). The most important thing for the account is to survive. You can wait for the capital curve to pass the retracement period before adding leverage. However, if the compound interest model is adopted, it is already a simple automatic win-loss position.

2. Minimize Slippage

This has been mentioned in the previous article. Slippage cannot be eliminated, it can only be alleviated. This requires you to obtain market price information, various funds and position information in the exchange account at the first time, so that you can respond quickly.

In terms of the overall structure of the real offer, there are many sub-strategies and it requires timely acquisition of various information. Therefore, a dedicated websocket market center is needed to share the tasks. Equipped with Restful’s Plan B market center for backup. In this way, if the websocket is disconnected and cannot be connected, there will still be a relatively new price to judge whether it is necessary to leave the market. When running, you have to be fast. It is better to run in the wrong direction than to be stuck in a traffic jam and not know what the future holds.

3. Long-term operation

This has been mentioned in the previous article. The cryptocurrency industry runs 24/7, and your code cannot be shut down. This requires that the code will not be interrupted by various exceptions thrown, and there must be code to handle various unexpected situations. There must also be no bugs such as memory leaks, otherwise the program will crash over time, and even the entire server will be dragged down. Fortunately, Python is not prone to such problems, and it has an automatic memory recycling mechanism. If it really doesn't work, you can manually restart the trading program every day or every few days, anyway, it is all medium and low frequency trading. These are basic backend code development requirements and are easy to achieve.

Generally, quantitative trading uses Python. A major feature of Python, a scripting language, is dynamic compilation, which has slightly lower running efficiency because it is not easy to debug because it is compiled and executed at the same time (but it will not affect medium and low-frequency transactions at all). If there is a problem implicit in the logic of the code, it may not be easy to detect. For example, if there are 100 lines of code and there is a problem on line 99, but it only runs to line 95 every time, then the problem will not be discovered because the error must be found after running to line 99 and triggering the relevant code. But this It may be too late and the program will crash directly if you don't try catch exception.

Another feature is actually included in the above mentioned problem. Because Python is strongly typed, there are few implicit type conversions. If you trust the data type sent by the server too much, especially those numbers, you may suffer losses because sometimes the numbers will become strings. Different currencies of exchanges, different API versions, the same return data sent by websocket and rest may all be of different types. So in principle, you must force the conversion every time, or determine whether it needs to be converted first.

Also, don’t mix up the opening and closing positions between various strategies, especially those of the same currency, and do a good job of isolating them. Otherwise, in some extreme market conditions, strategy A will close the position of strategy B, which will eventually lead to misalignment. You may have to stop the real offer and start again, which will be incoherent and you may miss key market trends.

In a word, everything is for long-term fully automated and intervention-free operation of the real deal.

However, if the strategy is too complex, it may not be easy to achieve a completely intervention-free approach. Simplifying the strategy is also a starting point, don’t make it too complicated. For example, signals such as OBV should be used sparingly, because OBV needs to trace long-term price and volume information, and real trading needs to maintain too much data. Every interruption and restart is a troublesome matter. As a last resort, avoid such factors.

4. Risk control

This is the top priority of real-time requirements.

The most dangerous thing is not being able to close a position. The market went in the opposite direction and continued to go wild. As a result, your exit code was out of control. No matter what the reason, the position was closed with losses. In the currency circle, altcoins may rise several times in a short period of time. Therefore, in the case of short selling, even if you do not use leverage and still have a half position, thinking that you have nothing to worry about, the result may be that your position has been wiped out in the morning.

Therefore, it is necessary to ensure that the exit mechanism must take effect. If you miss the opportunity to open a position, at least it will not be fatal. If you miss the opportunity, it will be a pity at most, but if you miss the opportunity to close the position, it will cause big problems.

Here are two solutions briefly, and I will talk about them in detail later when I have time.

The first point is that it is best to have a hard stop loss, which is similar to a stop loss with a fixed loss ratio. For example, if the loss of a large coin exceeds 10% and a small coin 15%, the position should be closed immediately and the investor should run away.

For the hard stop loss method, the stop loss price should be set at the time of opening the position. Therefore, algorithmic stop-loss orders (some also called conditional orders) should be submitted as soon as possible after opening a position, and there is no need to change the price midway. In this way, the exchange will help you monitor the real-time price, and then help you submit a market order to exit the market after it is triggered. There may be a lot of slippage in this approach, but it is more likely to be more stable and reliable than the stop loss you set yourself. And even if something goes wrong and it is not triggered, you may still go to the exchange to claim rights (so you have to go to the top exchange) .

Of course, this kind of stop loss is used to cover the bottom and should be rarely triggered. It is best to control the timing of exit according to your own strategy, so that the retracement is likely to be smaller.

The second point is that the program will not open positions randomly. Don't open positions continuously but think they are not, and end up opening a large exposure. It's okay if the price doesn't fluctuate in a short period of time. It's possible to manually intervene to correct it. If it suddenly reverses, your position may be liquidated. It depends on how many positions you have opened. Of course this is a low-level error, but it does happen. Especially after iterating and upgrading the code midway, it may not be considered carefully.

In this case, you need to slow down the pace of opening a position first, and you must wait for the return of the position opening results. Also, you must check and confirm the account information at that time before opening a position. Do not open positions continuously. Of course, you can also record all operations locally, which will save the number of API requests and time for querying the exchange's own account status.

Also, more effectively, it is possible to limit the maximum leverage of an exchange in advance. This can be adjusted by yourself. Binance defaults to 20 times, but you can change it to 3 times or even 2 times (of course, this requires that your strategy itself does not use high leverage, otherwise you will not be able to open high leverage when you should). So no matter how crazy the code is, it is impossible to create a big exposure while you are sleeping. This is safer.

In short, sometimes the real offer is not only the transaction code, but also needs to lay out other lines of defense to prevent being penetrated by black swan events. Never take it lightly at any time and try to seal off blind spots.