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

Re: [amibroker] Need Help With Looping Code



PureBytes Links

Trading Reference Links



Here is a version that will match the 50 MA from Amibroker:
Sum50 = 0;
MA50 = Null;

for(i = 1; i < BarCount; i++)
 {
  Sum50[i] = Sum50[i-1] + Close[i];
   
  if(i > 50) Sum50[i] = Sum50[i] - Close[i - 50];
   
  if(i >= 50) MA50[i] = SUM50[i] / 50;  
 }  


Plot(MA50,"50 MA via Looping", colorRed, styleLine); 
Plot(MA(C,50),"50 MA via Built in Function", colorBlack, styleLine); 


On Sat, May 2, 2009 at 4:37 PM, Pete <dryheat3@xxxxxxx> wrote:


Ok, first off, I apparently don't have the first clue about how to right code that loops through an AB database. I am pretty fluent with looping through MS Access databases using VBA but when it comes to AB I am completely lost.

So in my endeavor to educate myself on the ins and outs of looping in AB I decided to try creating a very simple 50 period moving average using a for loop. My results are below but it does not work.

I am defeated and at my wits end. Despite all this time and effort I have gained absolutely nothing which helps understand what is happening at each bar during a loop beyond measuring a single value for a single bar. What I am trying to understand is how to accumulate values over a period of bars and output the accumulated value for each and plot it on a chart.

Seems simple enough, add the closes of 50 consecutive bars, store the result in a variable and divide that variable by 50. I can either do this for all bars or one bar but have not been able to figure out how to do it for EVERY bar.

Thanks for taking the time to look at this.

Pete :-)
//Code Begin@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Cnt[0] = 0;
SumOfClose[0] = 0;
for( i = 1; i < BarCount; i++ )
{
if(Cnt[i] < 50)//check if counter is less than 50
{
//if counter less than 50 add current bar's
//close to the previous running total
SumOfClose[i] = (SumOfClose[i-1] + C[i]);
//increment counter by + 1 each bar
Cnt = Cnt + 1;
}
if(Cnt[i] == 50)//check if counter is equal to 50
{
//if counter = 50 than compute
//the 50 period MA
MAline[i] = SumOfClose[i]/50;
//reset counter to zero
Cnt = 0;
}
}
//plot the result
Plot(MAline, "MA 50", colorBlue, styleLine);
//Code End@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@





__._,_.___


**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/





Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe

__,_._,___