Programming Indicators in a DLL using PowerBASIC

However, this approach works when creating indicators and systems from scratch, but is extremely cumbersome when converting EL code into DLLs.

I have not really tried using the other possible way in my DLLs – the bar by bar approach, because I do not know an easy way of preserving the already calculated values. I assume that in this approach the DLL is called again by every bar, so the previous values get “forgotten” before a new bar will be calculated.

Using the array approach makes the programming often very complex and nesting, at least the way how I do it, and I find myself easily losing track what the code really does on a given line. This is especially true, when EL uses functions inside its code, and one should put that in a loop when using the arrays approach. (I have no education in programming)

My version of Average True Range coded in Power Basic (attached) shows clearly what I mean. Do you have any hints for me how to convert the EL code any easier?

Sincerely, Mike (not his real name)

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

Dear Mike:

Yes, we do have suggestions. We can see from your code why you think the array method is complex, and we have suggestions that will clear up the complexity.

First of all, consider building the easier indicators in the Indicator Wizard. Average True Range is one example that would be quite easily done in there. Takes only a couple of minutes. You first make the True Range, and then take the moving average of that. Think modularly.

In fact if you will log into www.ward.net with your serial number, and look under tips and techniques you will find a complete description of how to build that indicator in the Wizard!

If you don’t feel comfortable with the Wizard, go over the Trader videos again, especially where they talk about the “top down” and “bottom up” approaches. The Indicator Wizard feels hard at first, but after a while it seems easy.

Of course, many things involving looping can’t be done in the Indicator Wizard. That’s where C and Power Basic come in for DLLs. (In fact, even though True Range is easily done in the Indicator Wizard, we have an example of the True Range indicator written in Power Basic; look on ward.net in “New and Updated Examples” for “Power Basic DLL Examples.”)

The bar by bar approach is usually easiest where there is no lookback. However, if the lookback is only a bar or two, you can feed in the lags as indicators and get the lookback values that way. When you make a custom indicator out of the whole thing, you can map several variables (eg, the close) into just one.

Now if you have longer lookback, especially involving loops, the array method is indeed the most flexible.

If you have functions that are applied to the time series, don’t code them in the indicator code itself. Code them as separate functions in the DLL and call them from your indicator code.

The array method is just one big loop that looks at each bar. For example, if making an indicator that averages the high and low, your code would be similar to:

For i=0 to totalbars-1

output=(high+low)/2

Next i

where high and low are input arrays you set up using the array pointers, and output is the indicator output array.

Now if you want lags, high[i-1] is the one bar lag of the high. If you want the close 3 bars back it is close[i-3]. You just have to make sure you output the chart null value until i is greater than the longest lookback. So if you were averaging the current and the previous two closes the loop would be:

For i=0 to totalbars-1

if i < 2 then ' the lookback is two bars output[i]= 3.4e38 ' that is the null value else output[i]=(close[i]+close[i-1]+close[i-2])/3 end if Next i Note that 3.4e38 is the null value that puts nothing on the chart. Since the lags aren't known until i=2, the first two outputs are set to this value. For most indicators, you will want the lookback amount to be fed in as an indicator input so you can optimize it in the NeuroShell Trader or NeuroShell DayTrader Pro. For three examples done in Power Basic look on ward.net in "New and Updated Examples" for "Power Basic DLL Examples."

Was this article helpful?

Related Articles