Execute code Once - How

Hi, I use an afl which draws Daily, Weekly and Monthly Pivot points on the chart. I use real time data. I would like to know whether these Pivot lines are drawn repeatedly every time the new data comes in real time ? I noticed that drawing the Pivot lines is consuming a lot of CPU power and decreasing the efficiency. As these Pivots, once drawn, are non changing lines on the chart during the day, is there a way to draw them once I start Amibroker and then skip the execution of Pivot points code until I exit Amibroker ? I have searched the messages database but could not find an answer for this.

I apologize if my question is basic. I am not even familiar with software and code and so it is beyond me to understand how Amibroker works. I am just a layman user of Amibroker.
Thanks in advance.

Hi @bvpr
Without to see your code we dont know anything about it , if it is written correct or not? and much more.

At this moment at least You must follow two steps to start with

  1. open in the AFL editor your afl, and then go to tools / code check and profile.
    in there you will be able to see the Total execution time: , also you can see the function names and how many times they run.

  2. As for " How to Execute code Once " you can read it here
    AmiBroker Knowledge Base » How to run certain piece of code only once

1 Like

Thank you PanoS for your kind reply and taking out time to help me.
The code I use is as following ( this code I obtained by googling . I did not write it ) :slight_smile:

GraphXSpace = 5 ;
SetChartOptions(0,chartShowArrows|chartShowDates);
 
Plot(C,"Close",colorBlack, styleCandle);
ppl = ParamToggle("Plot Pivot Levels","Off|On",1);
 
numbars = LastValue(Cum(Status("barvisible")));
fraction= IIf(StrRight(Name(),3) == "", 3.2, 3.2);
hts = -33.5;
 
/* This code calculates the previous days high, low and close */
Hi1 = IIf(Day()!=Ref(Day(),-1),Ref(HighestSince(Day()!=Ref(Day(),-1),H,1),-1),0);
Hi = ValueWhen(Day()!=Ref(Day(),-1),Hi1,1);
Lo1 = IIf(Day()!=Ref(Day(),-1),Ref(LowestSince(Day()!=Ref(Day(),-1),L,1),-1),0);
Lo = ValueWhen(Day()!=Ref(Day(),-1),Lo1,1);
Cl1 = IIf(Day()!=Ref(Day(),-1),Ref(C,-1),0);
C1 = ValueWhen(Day()!=Ref(Day(),-1),Cl1,1);
 
//----------------------------------------------------------------------------------
 
/* This code calculates Daily Piovts */
 
rg = (Hi - Lo);
bp = (Hi + Lo + C1)/3; bpI = LastValue (bp,1);
r1 = (bp*2)-Lo; r1I = LastValue (r1,1);
s1 = (bp*2)-Hi; s1I = LastValue (s1,1);
r2 = bp + r1 - s1; r2I = LastValue (r2,1);
s2 = bp - r1 + s1; s2I = LastValue (s2,1);
r3 = bp + r2 - s1; r3I = LastValue (r3,1);
s3 = bp - r2 + s1; s3I = LastValue (s3,1);
r4 = bp + r2 - s2; r4I = LastValue (r4,1);
s4 = bp - r2 + s2; s4I = LastValue (s4,1);
 
if(ppl==1) {
Plot(bp,"",colorBlue,styleLine|styleDots|styleNoRescale);
Plot(s1,"",colorRed,styleLine|styleNoRescale);
Plot(s2,"",colorRed,styleLine|styleNoRescale);
Plot(s3,"",colorRed,styleLine|styleNoRescale);
Plot(s4,"",colorRed,styleLine|styleNoRescale);
Plot(r1,"",colorGreen,styleLine|styleNoRescale);
Plot(r2,"",colorGreen,styleLine|styleNoRescale);
Plot(r3,"",colorGreen,styleLine|styleNoRescale);
Plot(r4,"",colorGreen,styleLine|styleNoRescale);
PlotText(" Pivot = " + WriteVal(bp,fraction), LastValue(BarIndex())-(numbars/Hts), bpI +0.05, colorBlue);
PlotText(" r1 = " + WriteVal(r1,fraction), LastValue(BarIndex())-(numbars/Hts), r1I +0.05, colorGreen);
PlotText(" s1 = " + WriteVal(s1,fraction), LastValue(BarIndex())-(numbars/Hts), s1I +0.05, colorRed);
PlotText(" r2 = " + WriteVal(r2,fraction), LastValue(BarIndex())-(numbars/Hts), r2I +0.05, colorGreen);
PlotText(" s2 = " + WriteVal(s2,fraction), LastValue(BarIndex())-(numbars/Hts), s2I +0.05, colorRed);
PlotText(" r3 = " + WriteVal(r3,fraction), LastValue(BarIndex())-(numbars/Hts), r3I +0.05, colorGreen);
PlotText(" s3 = " + WriteVal(s3,fraction), LastValue(BarIndex())-(numbars/Hts), s3I +0.05, colorRed);
PlotText(" r4 = " + WriteVal(r4,fraction), LastValue(BarIndex())-(numbars/Hts), r4I +0.05, colorGreen);
PlotText(" s4 = " + WriteVal(s4,fraction), LastValue(BarIndex())-(numbars/Hts), s4I +0.05, colorRed);
}

I tried using the code at the link "AmiBroker Knowledge Base » How to run certain piece of code only once".

When I run the above with the Pivots code, it runs once and paints the lines but the lines disappear instantly and do not remain on the chart.

Any further help would be greatly appreciated.

Hello
I have the feeling you didn’t read or understood what I wrote above.
I check your “copy paste code from Internet” and as you see in the photo it is fine and running without using CPU power. Chart timing is 0.0159.

So I bet you maybe have more “copy paste code from Internet” and drain your CPU or a third party program. You have to find out what is the real problem. Is NOT the code that you provide here and you blame about it. So Any further help bout this code I donot thing is required.

Sorry but it is like that you telling me that, if you are NOT running this code you PC running fine? NO it is NOT. Try to find where else is the problem

pivots

Thanks for the reply PanoS. I am relieved that the Pivots code mentioned above is not the culprit in draining the CPU power. I will look for problems in the rest of the code as suggested by you.

"copy paste code from Internet" :slight_smile: Sorry. I have no other go because I am a novice as far as coding is concerned and I am just a layman user of the excellent Amibroker. At my age I suppose it would be difficult to learn coding but I will definitely make an attempt.

Thanks a ton .

Hello
Ooo no!!! dont remind my age now. i will start to cry :grinning: :rofl: me to i am older than 2 x 30 so you have many Amibroker users on your side.

Very well.
As for "copy paste code from Internet" An easy and quick way to check the afl you are going to run is to go to
Preferences / miscellaneous / and Click the Display chart timing......

with this way you will see quickly on the bottom of the chart how much time need to execute. After your finish dont forget to turn it off again.
Also try to find out if you run any third party plugin.

Thank you too.

PanoS, so nice of you to have taken time to reply. I will check my complete afl as you suggested. I am not using any third party software except the real time data plugin.

"Ooo no!!! dont remind my age now. i will start to cry :grinning:" :joy: :joy: :joy:
Thanks a ton again.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.