CSI's Unfair Advantage How-To Guide: How do I use UA to trade/develop a system?
20040116

From the Bollinger Bands example, we know now to add a study to a chart, but what about studies which have trade signals?
This page takes us through such an example.

1.  Follow the instructions for applying the Bollinger Bands to a chart, but choose Moving Averages, then MACross instead of  BollingerBands.  Here I have chosen a short Back-Adjusted Corn contract.



2. If we open the crosshairs with the crosshairs button: . And move the mouse to someplace where there is a red line



The indicator itself is in that blue-green color which can be seen by looking in the crosshairs box.  The blue line is the open equity, and the gray line is the closed equity.  The blue errors are entry points.  The red line shows the entry price so that you can gauge whether you are ahead or behind (without counting commission and slippage.)  The red arrow shows the exit point of the trade.

3. To simplify matters, we can remove the equity lines.  This is done by right clicking on the graph and choosing display system equity to uncheck it.



4. Now that we can see the indicator and the trades graphically, we would like to know the performance statistics.  To start with, lets close the crosshairs box by clicking the crosshairs toolbar button : .  Then right click on the chart and select "View System Performance Statistics".  Notice that if the series you have selected, does not have any trades, this option will be grayed-out.



5.  The View System Performance window opens to the CurrentStatus page.  This page shows what systems are on the chart, whether they are long or short and what orders are pending.  At the bottom of the table, it shows how many systems are long and how many are short.  If we click on the Summary tab, we can view the statistic about each system on this chart.



6.  The statistics for each system are reported one after the other.  For descriptions of the various fields, please see the UA manual.  If we click on the Trade-By-Trade tab for study 1, we can see what happened.



7. This shows for each trade, the entry and exit and whether it is open or closed.  The profit or loss depend on the contract size.  This view and the graphical view are the key views for system development.  (Don't do it now, but one can double-click on a trade to hide the table and see center the graph on the trade chosen.)  If you see a trade which doesn't fit your expectations, you can always look at the order list by clicking on the Order List tab.



8.  Now that we have a system applied to a chart and we know how to look at the orders, how do we improve the performance?  First, let's look at how you optimize the parameters.  Close the performance table by clicking the OK button.  This should bring you back to the chart.  We need to edit the study, so we open the study toolbar by clicking the Study Toolbar button: .



9.  Now we want to click on the "e" button next to MACross on the graph: .  The study control buttons may be hidden behind the study toolbar so try moving the study toolbar to the left if you can't find the "e" button.



10.  Now we click on the Optimize button in the lower left.



11. Suppose that we want to optimize both parameters, the 3-day moving average, and the 5-day moving average.  Then in the Expression edit box, we would enter MACross($1,$2) where $1 and $2 are variables we are going to change.  Then press the tab key on the keyboard, or click on the table in the middle of the graph.



12. Now fill-out the table to describe the range of acceptible parameter values.  For example,



13.  To change the basis of comparison, open the left drop down, and change it to read



We are not going to use "Smart Optimize" in this example, because it is a dangerous option.  Smart Optimize will find the best set of parameters much more quickly, but it does so by giving-up on unproductive combinations very easily.  For complex systems with lots of history, it is for "Smart Optimize" to get lost in some mediocre parameter combinations since it doesn't try to push-through avenues which are not immediately more profitable.  The "Smart Optimize" is good at finding reasonable parameters quickly which is useful when given a system about which you are unfamiliar or which has so many parameters that even finding a starting point is difficult.

14.  Instead we are going to press the Run button.



15.  UA will now run the system with every combintation of $1 and $2 which are within the range specified.  If this process is taking unreasonably long, try making the range of parameters smaller or the step size bigger.  If the optimized parameters come-out near the reduced limits, then run the optimization again with the previous optimium at the center of the second run.  Also you can optimize first a set of trigger parameters for example, then a set of stop parameters, and then go back to the trigger parameters until you are satisfied you have found the best combination.  When it is done, you should be asked:



16. Ordinarily you will answer this question with No, because we don't know yet, if these optimized parameters are any good, but sometimes you will want to change the parameters in the study toolbar to match the optimized values.  For now, press the No button.



17. Now close the study toolbar, right click on the chart, and choose the menu option View System Optimization Report.



18. This opens the View Optimization Performance form which has a table with one parameter combination per row, and all of the performance statistics have a column.  By default it is sorted by the optimization parameter, but you can sort any column by clicking on the column heading.  What we are interested here is whether similar parameters 2,6 produce about the same results as 1,6 which should be about the same a 1,7.  What we see is that 1,6 made 1600, while 1,7 only made 275.  This tells us that the optimium found is not robust. 

History never repeats itself exactly so if a system only produces good results for the exact situation offered, and fails miserably on a slightly different situation, then more likely if you trade this system into the future, it will produce much worse results than it did on the optimization set. 

Since we didn't find any parameter combinations we liked, perhaps we need to change the code.  Close the View Optimization Performance form, and open the study toolbar.  Press the "e" button again:.



19.  To edit the code, we need to be able to see it, so press the "More >>" button.



20. Since we don't like the parameter combination anyhow, reselect Moving Average -> MACross by right clicking on the expression memo box (the top white box).



21. UA automatically brings you to the start of the code for your selection in the library memo box (the lower white box.) 

Let's review the code for a moment:

function MACross(p1, p2) This line declares the study to the VBScript interpreter
'example Moving Averages\MACross(3,5) = 100*(MA3 - MA5)/(MA3 + MA5) This line tells UA how to describe the study in the righ-click menu of the expression memo box.
dim ma1, ma2, indicator, TargetPosition This line declares four variables.  "ma1" and "ma2" will be the first and second moving average.  "indicator" will hold the relative difference.  "TargetPosition" represents how the indicator value should be traded.
if(getNumDays()<max(p1,p2)) then exit function This says that if there aren't enough data of data available, then the study won't report anything.  This will be a blank area at the beginning of the graph in the chart.
ma1 = MAOfField(p1,4) We compute the first moving average using a standard function.
ma2 = MAOfField(p2,4) We compute the second moving average using a standard function.
UA.SharePriceScale = 0 This tells UA that the indicator is not a price, and should not share the chart's price scale.  Without this the price graph would shift up, and the indicator would be plotted as a zero-line since the prices are approx 300 and the indicator is approx 0.5.
indicator = 100*(ma1 - ma2)/(ma1 + ma2) This defined the indicator, a relative difference between them.
if indicator<0 then The system tries to be short if the indicator is negative and long if the indicator is positive, and out of the market if the indicator ever were zero.  (Zero being highly unlikely.)
TargetPosition = -1
elseif indicator>0 then
TargetPosition = +1
else
TargetPosition = 0
end if
StandardOrderGenerator(TargetPosition) We can call a standard function which just puts in the market orders for the next day to change the current Position (1=long, -1=short, or 0=neutral) to the TargetPosition.
MACross = indicator This tells the VBScript interpreter the that return value of the study is indicator.
end function This tells the VBScript interpreter that the study is done.

Since we don't want to change the standard code, let's create a new study MACross2 which is the same expect for the name.

Put the cursor just before the phrase "Function MACross" and paste the following into the Code Editor.

22. The most obvious problem with this system, is that the indicator is that the system makes a bet long or short based on the slightest excuse.  Surely having the 25 day moving average 1/2 % higher than th 50 day moving average is not a statistically significant fact, and as such is likely to produce losses.  The simplest way to change this is to require that the indicator be more than 0.1 rather than just positive.  I have made the changes as follows.  Just copy it from this document and paste it into UA.

function MACross2(p1, p2, threshold)
  'example MACross2(3,5,0.1) = 100*(MA3 - MA5)/(MA3 + MA5)
  dim ma1, ma2, indicator, TargetPosition
  if(getNumDays()<max(p1,p2)) then exit function
  ma1 = MAOfField(p1,4)
  ma2 = MAOfField(p2,4)
  UA.SharePriceScale = 0
  indicator = 100*(ma1 - ma2)/(ma1 + ma2)
  if indicator< -threshold then
    TargetPosition = -1
  elseif indicator> threshold then
    TargetPosition = +1
  else
    TargetPosition = 0
  end if
  StandardOrderGenerator(TargetPosition)
  MACross2 = indicator
end function




23.  Now that we have done that, right click on the express memo box (the upper white box) and choose MACross2 (not Moving Averages -> MACross2).



24. Now press the Optimize button.



25. Changed the fields to match the following:



26. When you are finished, press the Run button.



27. When faced with the question



again answer No.



28.  If we close the Study Toolbar, we can easily open the "View System Optimization Report".



The nice thing is that similar inputs produce similar result.  To bad thay all lost so much money though!

Well that is just the nature of system development.  You try changes which you think might help, and see what the results are.