[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] 25 Least Square MA



PureBytes Links

Trading Reference Links

Hi All,

I've got some code to determine the bar number for a bar that 
corresponds to a previous date. What's strange is that I always get 
an array index error, eventhough the debug window shows the code 
executing successfully twice before it fails on a third execution -- 
all three executions coming from a single click of the Apply button 
in the editor window. The code is below, followed by the debug output 
for the trace statements. If I create a new indicator, paste the code 
in and click apply, I'll see the candle chart flash, and then I'll 
get an array index error. If I look at the debug output, everything 
looks fine through those first two executions of the code (why two?) 
and then suddenly -- with no user intervention whatsoever -- an array 
of datenums becomes empty on a third run through the script. 

Can anyone identify the issue? This is driving me nuts. I'm hoping I 
won't have to rewrite everything in vbscript of vb or something to 
avoid this craziness.

_TRACE(" ");
_TRACE("Starting...");
_TRACE(" ");

DN = DateNum();
nLastDateNum = LastValue(DN);
nLastBarNum = LastValue(BarIndex());

bContinue = False;
bLoop = True;
bDebug = True;

noBarsBack = 80;
startBar = 0;
nCurBar = 0;

// -- We want bar index for 10/19/1999
nYear = 1999; nMonth = 10; nday = 19;
nExploreDate = (10000 * (nYear - 1900)) + (100 * nMonth) + nDay;

if (nExploreDate > 0) {
    bLoop = True;
    bContinue = False;
    // -- There have to be enough bars 
    if (nLastDateNum >= nExploreDate 
        && nLastBarNum >= (noBarsBack+1)) {
        // -- keep moving 1/2 the direction 
        //    just moved toward desired bar
        idx = int(nLastBarNum /2);
        previdx = nLastBarNum;
        swapidx = 0;
        while (bLoop) {
            // -- Let's see in dbg view what I have 
            _TRACE("idx: " + idx);
            _TRACE("DN[idx]: " + WriteVal(DN[idx], 7.0));
            testDN = DN[idx];
            if (testDN == nExploreDate) {
               bLoop = False;
               if (idx >= (noBarsBack+1)) {
                  startBar = idx;
                  bContinue= True;
                }
            } else if (testDN < nExploreDate) {
               // -- move 1/2 the distance of last move forward
               swapidx = idx;
               idx = idx + int( abs((previdx - idx))/ 2);
               previdx = swapidx;
               if (idx==previdx) bLoop = False;
            } else {
               swapidx = idx;
               idx = idx - int(abs((previdx - idx))/ 2);
               previdx = swapidx;
               if (idx==previdx) bLoop = False;
            }
        }
     }
// -- if (nExploreDate > 0)
} else {
  startBar = nLastBarNum;
  noBarsRequired = noBarsBack+1;
  bProcess = True;
}

GraphXSpace=7;

// -- Plot basic candle chart
PlotOHLC(Open, High, Low, Close,
 "BIdx = " + BarIndex() +
 "\n" + "O = " + O + "\n"+"H = "+ H + "\n"+"L  = " + L
 + "\n"+"C ",
 colorBlack, styleCandle);

// *************************************

Debug output:

[22140]  
[22140] Starting...
[22140]  
[22140] idx: 721
[22140] DN[idx]: 1,010,814
[22140] idx: 360
[22140] DN[idx]: 1,000,309
[22140] idx: 180
[22140] DN[idx]:  990,623
[22140] idx: 270
[22140] DN[idx]:  991,029
[22140] idx: 225
[22140] DN[idx]:  990,826
[22140] idx: 247
[22140] DN[idx]:  990,928
[22140] idx: 258
[22140] DN[idx]:  991,013
[22140] idx: 263
[22140] DN[idx]:  991,020
[22140] idx: 261
[22140] DN[idx]:  991,018
[22140] idx: 262
[22140] DN[idx]:  991,019
[22140]  
[22140] Starting...
[22140]  
[22140] idx: 721
[22140] DN[idx]: 1,010,814
[22140] idx: 360
[22140] DN[idx]: 1,000,309
[22140] idx: 180
[22140] DN[idx]:  990,623
[22140] idx: 270
[22140] DN[idx]:  991,029
[22140] idx: 225
[22140] DN[idx]:  990,826
[22140] idx: 247
[22140] DN[idx]:  990,928
[22140] idx: 258
[22140] DN[idx]:  991,013
[22140] idx: 263
[22140] DN[idx]:  991,020
[22140] idx: 261
[22140] DN[idx]:  991,018
[22140] idx: 262
[22140] DN[idx]:  991,019
[22140]  
[22140] Starting...
[22140]  
[22140] idx: 721
[22140] DN[idx]: {EMPTY}


On the third run -- and I have no idea why the code executes three 
times for one button click -- DN[idx]: {EMPTY} is suddenly empty. 
This makes absolutely no sense to me. Thanks for your help on this.

Gordon



------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

Check AmiBroker web page at:
http://www.amibroker.com/

Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/