Example 24 – Complex Indicators and Strategies

NOTE: In NeuroShell Trader, open the chart named “Example 24 – Complex Indicators and Strategies” which is the basis for following example:

This example is like Example 3, except that we now show you how to make indicators in NeuroShell using languages other than the Indicator Wizard’s “point and click” language. Using something other than “point and click” is not necessary in NeuroShell for a vast number of indicators, but if you like really complex indicators or rules, you might want to use a written language some day. If you have no interest in writing your own indicators, you can skip this example. But don’t skip it just because you are not a programmer – you may be able to write code even if you don’t program. If you wrote code in FORTRAN or BASIC or some other language in school you can probably do this. If you have used some other trading program with its own built-in “language” or “script”, then your are already familiar with the concepts!

Writing code in another language may be necessary when you need to iterate through procedures, setting flags, and making decisions later based on those flags. (Having said that, our Advanced Indicator Set 3 add-on has indicators that perform many of those functions without programming). Code may also be desirable when you anticipate a huge number of complex indicators, even if they can be done in the Indicator Wizard, because they may run much faster in code than in the interpretative Indicator Wizard. The True Range indicator of example 3 certainly need not be coded, but is a nice simple indicator with which to train.

You can write in code that makes modules called DLLs. There are two ways to call a subroutine in a DLL from NeuroShell:

1. Byvalue – each time the subroutine is called, NeuroShell passes in all the inputs you have specified for the current bar only. So if there are 1000 bars in the chart, the subroutine is called 1000 times, once for each bar in the chart. Each time it is called, you will have access to only the values for the current bar.

2. ByArray – the subroutine is called only once. NeuroShell passes in all the inputs you have specified, each in its own array. Each array contains all the bars in the chart for that particular input, so if you are passing open, high, low, and close there will be an array for each. If there are 1000 bars, each array has 1000 elements in it. Each time the subroutine is called, therefore, you have access to ALL the bars in the chart for each input. In this mode, you need access to the number of the bars in the chart (i.e., the number in each array). That is a value you can pass into the subroutine when you call the indicator “External DLL call by array” . It is called “As the number of values in the time series input(s)”. You describe it where you describe the output.

We generally don’t use ByValue ourselves, because it is limited. So much more is possible with ByArray. Therefore, we’ll code this indicator ByArray. Indicators can be coded in several languages, but the easiest to use is probably PowerBasic for Windows (www.powerbasic.com). Of course C and C++ can be used for those who know them. We’ll show you the POWERBASIC code below. You will want to maximize this window so you can read the code easily.

Recall the definition of Wilder’s True Range indicator:

The maximum of the following three values:

1. The absolute value of the spread between the high and low

2. The absolute value of the spread between the high and yesterday’s close

3. The absolute value of the spread between the low and yesterday’s close

In ByArray, each input is passed as an array, not a single value, and so is the array in which to put the output.

Here is the code in PowerBasic for the TrueRange indicator in the ByArray format. Note the use of the @ in the code to load and store elements of arrays passed to and from the DLL. Also in PowerBasic the words CLOSE and OUT are keywords, so we changed them slightly.

————————————————————–

#COMPILE DLL

SUB TrueRange2 ALIAS “TrueRange2” (BYVAL high AS DOUBLE PTR, BYVAL low AS DOUBLE PTR, _

BYVAL cls AS DOUBLE PTR, BYVAL barcount AS LONG, BYVAL outp AS DOUBLE PTR) EXPORT

DIM value1 AS DOUBLE, value2 AS DOUBLE, value3 AS DOUBLE, missing AS DOUBLE

DIM currentbar AS LONG

missing=3.4e38 ‘ returning this tells NeuroShell to leave the bar blank

FOR currentbar=0 TO barcount-1 ‘ iterate through bars – one at a time

IF currentbar <1 THEN ‘ this is the oldest bar, and has no previous

@outp[currentbar] = missing ‘ output a blank

ELSE

value1 = ABS(@high[currentbar]-@low[currentbar])

value2 = ABS(@high[currentbar]-@cls[currentbar-1])

value3 = ABS(@cls[currentbar-1]-@low[currentbar])

‘ decide which of the three values calculated above

‘ should be put into the chart on this bar

IF value1>=value2 AND value1>=value3 THEN @outp[currentbar] = value1

IF value2>=value1 AND value2>=value3 THEN @outp[currentbar] = value2

IF value3>=value1 AND value3>=value2 THEN @outp[currentbar] = value3

END IF

NEXT currentbar ‘ go to the next bar

END SUB

————————————————————–

Once the code is compiled into a DLL, you store the DLL into the NeuroShell Template folder. We have done that for you and called the file TrueRange2.dll. The next step is to tell NeuroShell about the functions in the DLLs using the indicator called:

External DLL call by array

which is found in the category External Program and Library calls. This indicator tells NeuroShell exactly how to call your DLL – i.e., what the parameters are and what numeric type they are. When you later save your indicator as custom, you can also tell NeuroShell the name of your new indicator and the name of the category you want to store it into. A call to this indicator has been inserted into this chart. You can double click the indicator and then go backwards in the Wizard to explore how we built and parameterized it.

There are many more coding examples on www.ward.net.

Was this article helpful?

Related Articles