Thursday, 2 February 2017

Moyenne Mobile Variable Amibroker

Ideally, you would like a filtered signal to be both smooth and lag-free. Lag causes delays in your trades, and increasing lag in your indicators typically result in lower profits. In other words, late comers get whats left on the table after the feast has already begun. Thats why investors, banks and institutions worldwide ask for the Jurik Research Moving Average (JMA). You may apply it just as you would any other popular moving average. However, JMAs improved timing and smoothness will astound you. The jagged gray line in the chart simulates price action that begins in a low trading range, then gaps to a higher trading range. Since no one likes waiting on the sidelines, a perfect noise reducing filter (green line) will move smoothly along the center of the first trading range and then jump to the center of the new trading range almost immediately. Do Adaptive Moving Averages Lead To Better Results Moving averages are a favorite tool of active traders. However, when markets consolidate, this indicator leads to numerous whipsaw trades, resulting in a frustrating series of small wins and losses. Analysts have spent decades trying to improve the simple moving average. In this article, we look at these efforts and find that their search has led to useful trading tools. (For background reading on simple moving averages, check out Simple Moving Averages Make Trends Stand Out .) Pros and Cons of Moving Averages The advantages and disadvantages of moving averages were summed up by Robert Edwards and John Magee in the first edition of Technical Analysis of Stock Trends . when they said and, it was back in 1941 that we delightedly made the discovery (though many others had made it before) that by averaging the data for a stated number of daysone could derive a sort of automated trendline which would definitely interpret the changes of trendIt seemed almost too good to be true. As a matter of fact, it was too good to be true. With the disadvantages outweighing the advantages, Edwards and Magee quickly abandoned their dream of trading from a beach bungalow. But 60 years after they wrote those words, others persist in trying to find a simple tool that would effortlessly deliver the riches of the markets. Simple Moving Averages To calculate a simple moving average. add the prices for the desired time period and divide by the number of periods selected. Finding a five-day moving average would require summing the five most recent closing prices and dividing by five. If the most recent close is above the moving average, the stock would be considered to be in an uptrend . Downtrends are defined by prices trading below the moving average. (For more, see our Moving Averages tutorial.) This trend-defining property makes it possible for moving averages to generate trading signals. In its simplest application, traders buy when prices move above the moving average and sell when prices cross below that line. An approach such as this is guaranteed to put the trader on the right side of every significant trade. Unfortunately, while smoothing the data, moving averages will lag behind the market action and the trader will almost always give back a large part of their profits on even the biggest winning trades. Exponential Moving Averages Analysts seem to like the idea of the moving average and have spent years trying to reduce the problems associated with this lag. One of these innovations is the exponential moving average (EMA). This approach assigns a relatively higher weighting to recent data, and as a result it stays closer to the price action than a simple moving average. The formula to calculate an exponential moving average is: EMA (Weight Close) ((1-Weight) EMAy) Where: Weight is the smoothing constant selected by the analyst EMAy is the exponential moving average from yesterday A common weighting value is 0.181, which is close to a 20-day simple moving average. Another is 0.10, which is approximately a 10-day moving average. Although it reduces the lag, the exponential moving average fails to address another problem with moving averages, which is that their use for trading signals will lead to a large number of losing trades. In New Concepts in Technical Trading Systems . Welles Wilder estimates that markets only trend a quarter of the time. Up to 75 of trading action is confined to narrow ranges, when moving-average buy-and-sell signals will be repeatedly generated as prices rapidly move above and below the moving average. To address this problem, several analysts have suggested varying the weighting factor of the EMA calculation. (For more, see How are moving averages used in trading ) Adapting Moving Averages to Market Action One method of addressing the disadvantages of moving averages is to multiply the weighting factor by a volatility ratio. Doing this would mean that the moving average would be further from the current price in volatile markets. This would allow winners to run. As a trend comes to an end and prices consolidate. the moving average would move closer to the current market action and, in theory, allow the trader to keep most of the gains captured during the trend. In practice, the volatility ratio can be an indicator such as the Bollinger Bandwidth, which measures the distance between the well-known Bollinger Bands. (For more on this indicator, see The Basics Of Bollinger Bands .) Perry Kaufman suggested replacing the weight variable in the EMA formula with a constant based on the efficiency ratio (ER) in his book, New Trading Systems and Methods . This indicator is designed to measure the strength of a trend, defined within a range from -1.0 to 1.0. It is calculated with a simple formula: ER (total price change for period) (sum of absolute price changes for each bar ) Consider a stock that has a five-point range each day, and at the end of five days has gained a total of 15 points. This would result in an ER of 0.67 (15 points upward movement divided by the total 25-point range). Had this stock declined 15 points, the ER would be -0.67. (For more trading advice from Perry Kaufman, read Losing To Win . which outlines strategies for coping with trading losses.) The principle of a trends efficiency is based on how much directional movement (or trend) you get per unit of price movement over a defined time period. An ER of 1.0 indicates that the stock is in a perfect uptrend -1.0 represents a perfect downtrend. In practical terms, the extremes are rarely reached. To apply this indicator to find the adaptive moving average (AMA), traders will need to calculate the weight with the following, rather complex, formula: C (ER (SCF SCS)) SCS 2 Where: SCF is the exponential constant for the fastest EMA allowable (usually 2) SCS is the exponential constant for the slowest EMA allowable (often 30) ER is the efficiency ratio that was noted above The value for C is then used in the EMA formula instead of the simpler weight variable. Although difficult to calculate by hand, the adaptive moving average is included as an option in almost all trading software packages. (For more on the EMA, read Exploring The Exponentially Weighted Moving Average .) Examples of a simple moving average (red line), an exponential moving average (blue line) and the adaptive moving average (green line) are shown in Figure 1. Figure 1: The AMA is in green and shows the greatest degree of flattening in the range-bound action seen on the right side of this chart. In most cases, the exponential moving average, shown as the blue line, is closest to the price action. The simple moving average is shown as the red line. The three moving averages shown in the figure are all prone to whipsaw trades at various times. This drawback to moving averages has thus far been impossible to eliminate. Conclusion Robert Colby tested hundreds of technical-analysis tools in The Encyclopedia of Technical Market Indicators . He concluded, Although the adaptive moving average is an interesting newer idea with considerable intellectual appeal, our preliminary tests fail to show any real practical advantage to this more complex trend smoothing method. This doesnt mean traders should ignore the idea. The AMA could be combined with other indicators to develop a profitable trading system. (For more on this topic, read Discovering Keltner Channels And The Chaikin Oscillator .) The ER can be used as a stand-alone trend indicator to spot the most profitable trading opportunities. As one example, ratios above 0.30 indicate strong uptrends and represent potential buys. Alternatively, since volatility moves in cycles, the stocks with the lowest efficiency ratio might be watched as breakout opportunities. How to optimize trading system NOTE: This is fairly advanced topic. Please read previous AFL tutorials first. The idea behind an optimization is simple. First you have to have a trading system, this may be a simple moving average crossover for example. In almost every system there are some parameters (as averaging period) that decide how given system behaves (i. e. is is well suited for long term or short term, how does is react on highly volatile stocks, etc). The optimization is the process of finding optimal values of those parameters (giving highest profit from the system) for a given symbol (or a portfolio of symbols). AmiBroker is one of the very few programs that allow you to optimize your system on multiple symbols at once. To optimize your system you have to define from one upto ten parameters to be optimized. You decide what is a minimum and maximum allowable value of the parameter and in what increments this value should be updated. AmiBroker then performs multiple back tests the system using ALL possible combinations of parameters values. When this process is finished AmiBroker displays the list of results sorted by net profit. You are able to see the values of optimization parameters that give the best result. Writing AFL formula Optimization in back tester is supported via new function called optimize. The syntax of this function is as follows: variable optimize( quot Description quot, default . min . max . step ) variable - is normal AFL variable that gets assigned the value returned by optimize function. With normal backtesting, scanning, exploration and comentary modes the optimize function returns default value, so the above function call is equivalent to: variable default In optimization mode optimize function returns successive values from min to max (inclusively) with step stepping. quot Descriptionquot is a string that is used to identify the optimization variable and is displayed as a column name in the optimization result list. default is a default value that optimize function returns in exploration, indicator, commentary, scan and normal back test modes min is a minimum value of the variable being optimized max is a maximum value of the variable being optimized step is an interval used for increasing the value from min to max AmiBroker supports upto 64 calls to optimize function (therefore upto 64 optimization variables), note that if you are using exhaustive optimization then it is really good idea to limit number of optimization variables to just few. Each call to optimize generate (max - min)step optimization loops and multiple calls to optimize multiply the number of runs needed. For example optimizing two parameters using 10 steps will require 1010 100 optimization loops. Call optimize function only ONCE per variable at the beginning of your formula as each call generates a new optimization loops Multiple-symbol optimization is fully supported by AmiBroker Maximum search space is 2 64 (10 19 10,000,000,000,000,000,000) combinations 1. Single variable optimization: sigavg Optimize ( Signal average. 9. 2. 20. 1 ) Buy Cross ( MACD ( 12. 26 ), Signal ( 12. 26. sigavg ) ) Sell Cross ( Signal ( 12. 26. sigavg ), MACD ( 12. 26 ) ) 2. Two-variable optimization (suitable for 3D charting) per Optimize ( per. 2. 5. 50. 1 ) Level Optimize ( level. 2. 2. 150. 4 ) Buy Cross ( CCI (per), - Level ) Sell Cross ( Level, CCI (per) ) 3. Multiple (3) variable optimization: mfast Optimize ( MACD Fast. 12. 8. 16. 1 ) mslow Optimize ( MACD Slow. 26. 17. 30. 1 ) sigavg Optimize ( Signal average. 9. 2. 20. 1 ) Buy Cross ( MACD ( mfast, mslow ). Signal ( mfast, mslow, sigavg ) ) Sell Cross ( Signal ( mfast, mslow, sigavg ), MACD ( mfast, mslow ) ) After entering the formula just click on Optimize button in quotAutomatic Analysisquot window. AmiBroker will start testing all possible combinations of optimization variables and report the results in the list. After optimization is done the list of result is presented sorted by the Net profit. As you can sort the results by any column in the result list it is easy to get the optimal values of parameters for the lowest drawdown, lowest number of trades, largest profit factor, lowest market exposure and highest risk adjusted annual return. The last columns of result list present the values of optimization variables for given test. When you decide which combination of parameters suits your needs the best all you need to do is to replace the default values in optimize function calls with the optimal values. At current stage you need to type them by hand in the formula edit window (the second parameter of optimize function call). Displaying 3D animated optimization charts To display 3D optimization chart, you need to run two-variable optimization first. Two variable optimization needs a formula that has 2 Optimize() function calls. An example two-variable optimization formula looks like this: per Optimize ( per. 2. 5. 50. 1 ) Level Optimize ( level. 2. 2. 150. 4 ) Buy Cross ( CCI (per), - Level ) Sell Cross ( Level, CCI (per) ) After entering the formula you need to click quotOptimizequot button. Once optimization is complete you should click on the drop down arrow on Optimize button and choose View 3D optimization graph . In a few seconds a colorful three-dimensional surface plot will appear in a 3D chart viewer window. An example 3D chart generated using above formula is shown below. By default the 3D charts display values of Net profit against optimization variables. You can however plot 3D surface chart for any column in the optimization result table. Just click on the column header to sort it (blue arrow will appear indicating that optimization results are sorted by selected column) and then choose View 3D optimization graph again. By visualizing how your systems parameters affect trading performance, you can more readily decide which parameter values produce quotfragilequot and which produce quotrobustquot system performance. Robust settings are regions in the 3D graph that show gradual rather than abrupt changes in the surface plot. 3D optimization charts are great tool to prevent curve-fitting. Curve-fitting (or over-optimization) occurs when the system is more complex than it needs to be, and all that complexity was focused on market conditions that may never happen again. Radical changes (or spikes) in the 3D optimization charts show clearly over-optimization areas. You should choose parameter region that produces a broad and wide plateau on 3D chart for your real life trading. Parameter sets producing profit spikes will not work reliably in real trading. 3D chart viewer controls AmiBrokers 3D chart viewer offers total viewing capabilities with full graph rotation and animation. Now you can view your system results from every conceivable perspective. You can control the position and other parameters of the chart using the mouse, toolbar and keyboard shortcuts, whatever you find easier for you. Below you will find the list. - to Rotate - hold down LEFT mouse button and move in XY directions - to Zoom-in, zoom-out - hold down RIGHT mouse button and move in XY directions - to Move (translate) - hold down LEFT mouse button and CTRL key and move in XY directions - to Animate - hold down LEFT mouse button, drag quickly and release button while dragging SPACE - animate (auto-rotate) LEFT ARROW KEY - rotate vert. left RIGHT ARROW KEY - rotate vert. right UP ARROW KEY - rotate horiz. up DOWN ARROW KEY - rotate horiz. down NUMPAD (PLUS) - Near (zoom in) NUMPAD - (MINUS) - Far (zoom out) NUMPAD 4 - move left NUMPAD 6 - move right NUMPAD 8 - move up NUMPAD 2 - move down PAGE UP - water level up PAGE DOWN - water level down Smart (non-exhaustive) optimization AmiBroker now offers smart (non-exhaustive) optimization in addition to regular, exhaustive search. Non-exhaustive search is useful if number of all parameter combinations of given trading system is simply too large to be feasible for exhaustive search. Exhaustive search is perfectly fine as long as it is reasonable to use it. Lets say you have 2 parameters each ranging from 1 to 100 (step 1). Thats 10000 combinations - perfectly OK for exhaustive search. Now with 3 parameters you got 1 million combinations - it is still OK for exhaustive search (but can be lenghty). With 4 parameters you have 100 million combinations and with 5 parameters (1..100) you have 10 billion combinations. In that case it would be too time consuming to check all of them, and this is the area where non-exhaustive smart-search methods can solve the problem that is not solvable in reasonable time using exhaustive search. Here is absolutely the SIMPLEST instruction how to use new non-exhaustive optimizer (in this case CMA-ES). 1. Open your formula in the Formula Editor 2. Add this single line at the top of your formula: OptimizerSetEngine(quotcmaequot) you can also use quotspsoquot or quottribquot here 3. (Optional) Select your optimization target in Automatic Analysis, Settings, quotWalk-Forwardquot tab, Optimization target field. If you skip this step it will optimize for CARMDD (compound annual return divided by maximum drawdown). Now if you run optimization using this formula, it will use new evolutionary (non-exhaustive) CMA-ES optimizer. How does it work The optimization is the process of finding minimum (or maximum) of given function. Any trading system can be considered as a function of certain number of arguments. The inputs are parameters and quotation data. the output is your optimization target (say CARMDD). And you are looking for maximum of given function. Some of smart optimization algorithms are based on nature (animal behavior) - PSO algorithm, or biological process - Genetic algorithms, and some are based on mathematical concepts derived by humans - CMA-ES. These algorithms are used in many different areas, including finance. Enter quotPSO financequot or quotCMA-ES financequot in Google and you will find lots of info. Non-exhaustive (or quotsmartquot) methods will find global or local optimum. The goal is of course to find global one, but if there is a single sharp peak out of zillions parameter combinations, non-exhaustive methods may fail to find this single peak, but taking it form traders perspecive, finding single sharp peak is useless for trading because that result would be instable (too fragile) and not replicable in real trading. In optimization process we are rather looking for plateau regions with stable parameters and this is the area where intelligent methods shine. As to algorithm used by non-exhaustive search it looks as follows: a) the optimizer generates some (usually random) starting population of parameter sets b) backtest is performed by AmiBroker for each parameter set from the population c) the results of backtests are evaluated according to the logic of algorithm and new population is generated based on the evolution of results, d) if new best is found - save it and go to step b) until stop criteria are met Example stop criteria can include: a) reaching specified maximum iterations b) stop if the range of best objective values of last X generations is zero c) stop if adding 0.1 standard deviation vector in any principal axis direction does not change the value of objective value d) others To use any smart (non-exhaustive) optimizer in AmiBroker you need to specify the optimizer engine you want to use in the AFL formula using OptimizerSetEngine function. The function selects external optimization engine defined by name . AmiBroker currently ships with 3 engines: Standard Particle Swarm Optimizer (quotspsoquot), Tribes (quottribquot), and CMA-ES (quotcmaequot) - the names in braces are to be used in OptimizerSetEngine calls. In addition to selecting optimizer engine you may want to set some of its internal parameters. To do so use OptimizerSetOption function. OptimizerSetOption(quotnamequot, value ) function The function set additional parameters for external optimization engine. The parameters are engine-dependent. All three optimizers shipped with AmiBroker (SPSO, Trib, CMAE) support two parameters: quotRunsquot (number of runs) and quotMaxEvalquot (maximum evaluations (tests)per single run). The behaviour of each parameter is engine-dependent, so same values may and usually will yield different results with different engines used. The difference between Runs and MaxEval is as follows. Evaluation (or test) is single backtest (or evaluation of objective function value). RUN is one full run of the algorithm (finding optimum value) - usually involving many tests (evaluations). Each run simply RESTARTS the entire optimization process from the new beginning (new initial random population). Therefore each run may lead to finding different local maxmin (if it does not find global one). So Runs parameter defines number of subsequent algorithm runs. MaxEval is the maximum number of evaluations (bactests) in any single run. If the problem is relatively simple and 1000 tests are enough to find global max, 5x1000 is more likely to find global maximum because there are less chances to be stuck in local max, as subsequent runs will start from different initial random population Choosing parameter values can be tricky. It depends on problem under test, its complexity, etc, etc. Any stochastic non-exhaustive method does not give you guarantee of finding global maxmin, regardless of number of tests if it is smaller than exhaustive. The easiest answer is to. specify as large number of tests as it is reasonable for you in terms of time required to complete. Another simple advice is to multiply by 10 the number of tests with adding new dimension. That may lead to overestimating number of tests required, but it is quite safe. Shipped engines are designed to be simple to use, therefore quotreasonablequot defaultautomatic values are used so optimization can be usually run without specifying anything (accepting defaults). It is important to understand that all smart optimization methods work best in continuous parameter spaces and relatively smooth objective functions. If parameter space is discrete evolutionary algorithms may have trouble finding optimum value. It is especially true for binary (onoff) parameters - they are not suited for any search method that uses gradient of objective function change (as most smart methods do). If your trading system contains many binary parameters, you should not use smart optimizer directly on them. Instead try to optimize only continuous parameters using smart optimizer, and switch binary parameters manually or via external script. SPSO - Standard Particle Swarm Optimizer Standard Particle Swarm Optimizer is based on SPSO2007 code that is supposed to produce good results provided that correct parameters (i. e. Runs, MaxEval) are provided for particular problem. Picking correct options for the PSO optimizer can be tricky therefore results may significantly vary from case to case. SPSO. dll comes with full source codes inside quotADKquot subfolder. Example code for Standard Particle Swarm Optimizer: (finding optimum value in 1000 tests within search space of 10000 combinations) OptimizerSetEngine(quotspsoquot) OptimizerSetOption(quotRunsquot, 1 ) OptimizerSetOption(quotMaxEvalquot, 1000 ) sl Optimize(quotsquot, 26, 1, 100, 1 ) fa Optimize(quotfquot, 12, 1, 100, 1 ) Buy Cross( MACD( fa, sl ), 0 ) Sell Cross( 0, MACD( fa, sl ) ) TRIBES - Adaptive Parameter-less Particle Swarm Optimizer Tribes is adaptive, parameter-less version of PSO (particle swarm optimization) non-exhaustive optimizer. For scientific background see: particleswarm. infoTribes2006Cooren. pdf In theory it should perform better than regular PSO, because it can automatically adjust the swarm sizes and algorithm strategy to the problem being solved. Practice shows that its performance is quite similar to PSO. The Tribes. DLL plugin implements quotTribes-Dquot (i. e. dimensionless) variant. Based on clerc. maurice. free. frpsoTribesTRIBES-D. zip by Maurice Clerc. Original source codes used with permission from the author Tribes. DLL comes with full source code (inside quotADKquot folder) Supported parameters: quotMaxEvalquot - maximum number of evaluations (backtests) per run (default 1000). You should increase the number of evaluations with increasing number of dimensions (number of optimization params). The default 1000 is good for 2 or maximum 3 dimensions. quotRunsquot - number of runs (restarts). (default 5 ) You can leave the number of runs at default value of 5. By default number of runs (or restarts) is set to 5. To use Tribes optimizer, you just need to add one line to your code: OptimizerSetOption(quotMaxEvalquot, 5000 ) 5000 evaluations max CMA-ES - Covariance Matrix Adaptation Evolutionary Strategy optimizer CMA-ES (Covariance Matrix Adaptation Evolutionary Strategy) is advanced non-exhaustive optimizer. For scientific background see: bionik. tu-berlin. deusernikocmaesintro. html According to scientific benchmarks outperforms nine other, most popular evolutionary strategies (like PSO, Genetic and Differential evolution). bionik. tu-berlin. deusernikocec2005.html The CMAE. DLL plugin implements quotGlobalquot variant of search with several restarts with increasing population size CMAE. DLL comes with full source code (inside quotADKquot folder) By default number of runs (or restarts) is set to 5. It is advised to leave the default number of restarts. You may vary it using OptimizerSetOption(quotRunsquot, N ) call, where N should be in range 1..10. Specifying more than 10 runs is not recommended, although possible. Note that each run uses TWICE the size of population of previous run so it grows exponentially. Therefore with 10 runs you end up with population 210 greater (1024 times) than the first run. There is another parameter quotMaxEvalquot. The default value is ZERO which means that plugin will automatically calculate MaxEval required. It is advised to NOT to define MaxEval by yourself as default works fine. The algorithm is smart enough to minimize the number of evaluations required and it converges very fast to solution point, so often it finds solutions faster than other strategies. It is normal that the plugin will skip some evaluations steps, if it detects that solution was found, therefore you should not be surprised that optimization progress bar may move very fast at some points. The plugin also has ability to increase number of steps over initially estimated value if it is needed to find the solution. Due to its adaptive nature, the quotestimated time leftquot andor quotnumber of stepsquot displayed by the progress dialog is only quotbest guess at the timequot and may vary during optimization course. To use CMA-ES optimizer, you just need to add one line to your code: This will run the optimization with default settings which are fine for most cases. It should be noted, as it is the case with many continouos-space search algorithms, that decreasing quotstepquot parameter in Optimize() funciton calls does not significantly affect optimization times. The only thing that matters is the problem quotdimensionquot, i. e. the number of different parameters (number of optimize function calls). The number of quotstepsquot per parameter can be set without affecting the optimization time, so use the finest resolution you want. In theory the algorithm should be able to find solution in at most 900(N3)(N3) backtests where quotNquot is the dimension. In practice it converges a LOT faster. For example the solution in 3 (N3) dimensional parameter space (say 100100100 1 million exhaustive steps) can be found in as few as 500-900 CMA-ES steps. Multi-threaded individual optimization Starting from AmiBroker 5.70 in addition to multiple-symbol multithreading. you can perform multi-threaded single-symbol optimization. To access this functionality, click on drop down arrow next to quotOptimizequot button in the New Analysis window and select quot Individual Optimize quot. quotIndividual Optimizequot will use all available processor cores to perform single-symbol optimization, making it much faster than regular optimization. In quotCurrent symbolquot mode it will perform optimization on one symbol. In quotAll symbolsquot and quotFilterquot modes it will process all symbols sequentially, i. e. first complete optimization for first symbol, then optimization on second symbol, etc. Limitations: 1. Custom backtester is NOT supported (yet) 2. Smart optimization engines are NOT supported - only EXHAUSTIVE optimization works. Eventually we may get rid of limitation (1) - when AmiBroker is changed so custom backtester does not use OLE anymore. But (2) is probably here to stay for long.


No comments:

Post a Comment