📜 ⬆️ ⬇️

Model alignment: what you need to know about creating strategies for trading on the exchange. Part II



On Habré and in the analytical section of our site, we write a lot about financial market trends and continue to publish a series of materials on creating strategies for trading on the stock exchange, based on the articles of the author of the blog Financial Hacker. In the past , we talked about the use of market inefficiencies on the example of a history with a price limit for the Swiss franc. This time, the author of the blog Financial Hacker decided to understand how the strategies focused on a particular model look like. We present to your attention the main theses of the second article in the series.

As mentioned last time, there are two main methods for building online trading strategies. The first is focused on a specific model (model-based), the second - on data mining (data-mining). Today we will talk about the first option. The algorithms built on its basis are surprisingly simple. But the process of creating them has its own pitfalls (otherwise everyone would feel like they were all too lazy). Even the obvious inefficiency of the market gives the system only a small gap, which can be penetrated with its own algorithm. And the most trifling mistake in his writing will turn a winning strategy into failure. And this failure can not always be seen on the trial run.
')
First you need to choose one of the market inefficiencies that the trader wants to use in his strategy. Inefficiency produces a price anomaly or price pattern that can be described using a quantitative or qualitative model. This model is designed to predict the current price y t , based on the previous value of the price y t-1 , to which is added some function f from a limited number of previous values, plus a noise variable ε.



Obviously, the higher the value of the predictable function in front of unpredictable noise, the more reliable the strategy. Some traders like to say that their method does not predict the behavior of the market, but "reacts to its changes." But systems that are not based on market forecasting should definitely count on luck. They only redistribute the risks. Change, for example, the high risk of small losses tolerable by large losses. But the probability of profit remains negative.

Not all price abnormalities are usable. The 1/16 dollar quotes limit is an obvious inefficiency, but the author honestly admits that he does not understand how it can be used for profit. However, working strategies based on models known to him can be divided into several categories. About each of them in order.

Following the trend


Momentum or price change dynamics - perhaps the most obvious and most frequently used anomaly. There are many methods of following a trend strategy. The classic version is the intersection of moving averages (moving average crossover). Under the link you can see the scripts for it on R and C.

There is one problem: momentum does not work in all markets all the time. Any assets may not trade for a long period. Random deviation may be positive or negative, but the momentum itself may be absent. Be that as it may, there is an undoubted advantage in the filter that identifies the real market mode. Here is an example of using a low pass filter to detect a reverse trend in a strategy written in the Zorro scripting language. The MMI indicator is needed to capture the moment of entry into the trend.

function run() { vars Price = series(price()); vars Trend = series(LowPass(Price,500)); vars MMI_Raw = series(MMI(Price,300)); vars MMI_Smooth = series(LowPass(MMI_Raw,500)); if(falling(MMI_Smooth)) { if(valley(Trend)) reverseLong(1); else if(peak(Trend)) reverseShort(1); } } 

The income curve of this strategy:



Return to average (mean reversion)


Believing that the market returns to a mean value implies a “real” or “fair” price. Traders buy when the current price is lower than what they think should be. And sell at a higher value. This, allegedly, causes the price to return to the average value more often than would have happened with random deviations. There is evidence that a return to the average occurs in 75% of cases (some of the pros are listed here in English). Therefore, everything above 75% should be related to market inefficiency. Calculation model:


,

Where y t , is the price on the scale t, y ^ is the fair price, λ is the half-life factor ( half-life factor ), ε is the noise.

The higher the half-life factor, the less likely it is to return to the average. Usually the average value in the price series is reached in the range from 50 to 200 bars. λ can be calculated through a linear regression between y t-1 and (y t-1 -y t ). To achieve an average value, price series may not be static because the fair price is free to change. Just this change should be less significant than in the case of random deviation. Usually, a return to the average is used by removing the trend from the price curve and normalizing the final result. Thus, we get a swing signal that will trigger trades when the upper or lower values ​​are reached. Here is a simple system script using this method:

 function run() { vars Price = series(price()); vars Filtered = series(HighPass(Price,30)); vars Signal = series(FisherN(Filtered,500)); var Threshold = 1.0; if(Hurst(Price,500) < 0.5) { // do we have mean reversion? if(crossUnder(Signal,-Threshold)) reverseLong(1); else if(crossOver(Signal,Threshold)) reverseShort(1); } } 

The high-pass filter attenuates the effect of all cycles with a value of more than 30 cycles, thus eliminating the trend from the price curve. The result is normalized through the Fisher transform, which leads to the Gaussian distribution. This allows you to define fixed boundaries of values ​​at 1 and -1, in order to separate the "tail" of the normal distribution. When the price reaches the fraction in any direction, a trade is initiated in the expectation that it will return to normal values ​​in the near future. To enter the mode of returning to the average values, the Hearst indicator set at a value of 0.5 for random deviation is used in the script.



Statistical arbitration


The strategy can be used similarity of two or more assets. This allows you to hedge the first asset through the reverse position in the second. Profit arises from the return to the average value of the difference in their prices.


,
Where y 1 and y 2 are the prices of two assets, the coefficients h 1 and h 2 determine the ratio for hedging. They are calculated on the assumption that the difference between the value of the assets is zero or is a constant. The easiest way to calculate is linear regression between y 1 and y 2 . Further, here we can also apply the strategy of returning to the mean values, which we analyzed above.

It is clear that assets must be of the same type. The standard arbitration system can be based on the price difference of the ETF index and its main shares. If y is not static, that is, its average value changes slowly, the hedging factor can be applied for real-time compensation.

Here is a simple example of an arbitration system from an R tutorial :

 require(quantmod) symbols <- c("AAPL", "QQQ") getSymbols(symbols) #define training set startT <- "2007-01-01" endT <- "2009-01-01" rangeT <- paste(startT,"::",endT,sep ="") tAAPL <- AAPL[,6][rangeT] tQQQ <- QQQ[,6][rangeT] #compute price differences on in-sample data pdtAAPL <- diff(tAAPL)[-1] pdtQQQ <- diff(tQQQ)[-1] #build the model model <- lm(pdtAAPL ~ pdtQQQ - 1) #extract the hedge ratio (h1 is assumed 1) h2 <- as.numeric(model$coefficients[1]) #spread price (in-sample) spreadT <- tAAPL - h2 * tQQQ #compute statistics of the spread meanT <- as.numeric(mean(spreadT,na.rm=TRUE)) sdT <- as.numeric(sd(spreadT,na.rm=TRUE)) upperThr <- meanT + 1 * sdT lowerThr <- meanT - 1 * sdT #run in-sample test spreadL <- length(spreadT) pricesB <- c(rep(NA,spreadL)) pricesS <- c(rep(NA,spreadL)) sp <- as.numeric(spreadT) tradeQty <- 100 totalP <- 0 for(i in 1:spreadL) { spTemp <- sp[i] if(spTemp < lowerThr) { if(totalP <= 0){ totalP <- totalP + tradeQty pricesB[i] <- spTemp } } else if(spTemp > upperThr) { if(totalP >= 0){ totalP <- totalP - tradeQty pricesS[i] <- spTemp } } } 

Price limits


Price constraints cause steady price drift or set its maximum lower or upper values. The most famous example here is the EUR / CHF (Euro / Franc) story, which we talked about in detail last time. But even after the cancellation of the limit, the restrictions in the pair remained. This time they were due not to the policy of the Swiss Central Bank, but to a serious asymmetry in the purchasing power of each of the currencies. Such things can be used in a trading strategy. Usually they give high returns in short periods of time when using the grid.

Cycles


If we are not talking about seasonal cycles, then they can be caused by the inverse reaction of the price curve to the actions of traders. When most players believe in some fair share price, they open positions on buying or selling, if the price reaches a certain value in case of deviation from this value. Or they close out winning positions when the preferred dynamic starts to slow down. Thus, a sufficient number of traders can synchronize their input-output cycles and cause steady price fluctuations over a certain period. Often, many such cycles are superimposed on a curve. For example:



If you know the period C i and phase D i of the dominant cycle, the trader will be able to determine the optimal moments for opening and closing positions while the cycle itself is stable. It is possible to determine the action of cycles through the functions of spectral analysis. Well, for example, through a fast Fourier transform or through a set of bandpass filters. Here is the frequency range for the euro / dollar pair for October 2015:



Dealing with cycles is harder than using a trend or a return to the average. It is necessary to take into account not only the duration of the cycle, but also its phase and amplitude (in order to understand whether it is worth paying attention to it at all). Here is an example of a basic script for this purpose:

 function run() { vars Price = series(price()); var Phase = DominantPhase(Price,10); vars Signal = series(sin(Phase+PI/4)); vars Dominant = series(BandPass(Price,rDominantPeriod,1)); ExitTime = 10*rDominantPeriod; var Threshold = 1*PIP; if(Amplitude(Dominant,100) > Threshold) { if(valley(Signal)) reverseLong(1); else if(peak(Signal)) reverseShort(1); } } 

The DominantPhase function determines the phase and duration of the cycle of the main maximum in the spectrum. It is designated by the variable rDominantPeriod . The phase is converted to a sine wave shifted by the π/4 value. So we get a curve that goes over the price curve. The only question is whether the price will follow our forecasts. We check this through bandpass filters centered on the dominant cycle on the price curve, and measure their amplitude (a i in the formula). If the amplitude exceeds the limit value, then the cycle is sufficiently stable. Since the cycles are short, the position opening time is limited to ExitTime with a maximum value of 10 cycles.



This graph shows that for the euro / dollar pair in 2012 and 2014 there were no long periods where there were stable cycles.

Clusters


The same effect, which causes prices to fluctuate, can lead to their uniting into groups at certain levels. In its extreme expression, it can even give the supply and demand lines (or support and resistance) - favorite objects of consideration at trading seminars. Experts at such seminars can identify support and resistance lines on any diagrams, without thinking of clusters, whether they are real stocks or the results of the baseball season. For all others, the evidence for the existence of these lines remains in question. There are only a few strategies aimed at their identification and use. Even less bring real income. Theoretically, clusters on price curves can be detected using a histogram, similar to a spectrogram, that we used to identify cycles.

Price curve patterns


Such patterns have their origins in the recurring behavior of traders. On the exchanges not only produce certain patterns, but also believe in the existence of such. Most of them, for example, the famous “head and shoulders”, the figure of price movement in technical analysis, which, allegedly, predicts a change in trend, are myths. At least, the author of the article has not met a single statistical confirmation of this pattern. However, some patterns, such as "cup" (cup) and "half cup" (half-cup), do exist and can predict the movement of the curve down and up. Curve patterns should not be confused with candlestick patterns. In general, they can be exploited using template definition methods. For example, the Frechet algorithm .

A special case is the breakout pattern, denoting a sudden momentum after a long movement in narrow limits of values. It can be caused, for example, by the desire of traders to place their stop loss orders at a short distance above or below the current plateau. The first closing speeds up the price movement until more and more such closings are initiated. This effect can be used by creating a system that captures a period of small fluctuations and goes into standby mode until the first movement in any direction.

Seasonality


Season in our case does not always mean the time of year. Demand and supply can follow monthly, weekly patterns, or even patterns of the same day. For example, the S & P500 index, as is commonly believed, goes up in the first days of each month or at the beginning of the opening of trading daily. Such things are easy to use for their own purposes. But they are often short-lived, unreliable, they cannot be recognized simply by looking at the price curve. But if you wish, for each day, month and week you can create your own profile of average price changes.

Gaps


If market participants have enough time to think about when to open or close a position, they often come to the same opinion. Time to think about them appears at night and on weekends. This may lead to the fact that when you open the auction, the starting price will be at a new level. The price at the opening and after the weekend is more predictable than during the trading day. This circumstance can also be used in the strategy.

Autoregression and heteroscedasticity


The last word means, according to Wikipedia, the heterogeneity of observations, which is expressed in the unequal (non-constant) variance of the random error of the regression (econometric) model. Or in our case, the prices are changing convulsively, and these changes vary over time.

The ARIMA and GARCH models are the first models with which you deal in financial analysis. They proceed from the assumption that future profits or future volatility can be determined through a linear combination of past earnings and past volatility. They have the status of theoretical models. But you can find them practical application. With their help, you can predict tomorrow's price, no worse than any other. Study at your leisure a correlogram with statistics of correlation of current and past incomes. Perhaps ARIMA can be customized for specific price series. There are two articles on the use of these models in existing trading strategies: “ ARIMA / GARCH trading strategy for S & P500 ” and “ Is it possible to predict prices through ARIMA / GARCH? "

news


Price shake-up usually happens on Mondays and Fridays, when many companies or organizations publish good or not so good news for the market. Even without knowing their content, one can predict the first price reaction with the help of the strategy and be on the winning side. But aerobatics, of course, is when the trader manages to interpret this news. Some information services produce news marked "good" or "bad." In order to figure out how to use public information more creatively and efficiently, you should get acquainted with the brilliant book “ The Index of Fear ” by Robert Harris, a must-have in the library of any advanced financier.

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


All Articles