Hi all,
Being new to Power Language, I don't know how to solve my problem in getting a moving average to change color when its slope turns up or down.
Actually, I can get it to change color but the color change is plotted one bar late.
How do I get the color to change one bar sooner?
Thanks,
DC
Here's the code and a chart sample:
Inputs:
Price (Close),
SMALength (50);
Variables:
Iteration (0),
CloseSum (0),
LastSMAValue (0),
SMAValue (0);
CloseSum = 0;
for Iteration = 1 to SMALength
begin
CloseSum = CloseSum + Price[Iteration];
end;
if SMALength <> 0 then
SMAValue = CloseSum / SMALength;
if SMAValue > LastSMAValue then
SetPlotColor(1, darkgreen)
else
SetPlotColor(1, red);
Plot1(SMAValue, "Colorslope SMA");
LastSMAValue = SMAValue;
simple power language problem I don't know how to solve [SOLVED]
simple power language problem I don't know how to solve [SOLVED]
- Attachments
-
- slopeccolor.png
- (140.32 KiB) Downloaded 1020 times
- TJ
- Posts: 7743
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2223 times
Re: simple power language problem I don't know how to solve
Try this:
note SetPlotColor[1]
Code: Select all
if SMAValue > LastSMAValue then
SetPlotColor[1](1, darkgreen)
else
SetPlotColor[1](1, red);
note SetPlotColor[1]
- TJ
- Posts: 7743
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2223 times
Re: simple power language problem I don't know how to solve
TJ,Try this:
Code: Select all
if SMAValue > LastSMAValue then
SetPlotColor[1](1, darkgreen)
else
SetPlotColor[1](1, red);
note SetPlotColor[1]
I tried something similar, but all that does is displaces the moving average by one bar...
The color changes where it should, but how can it be done without the MA being displaced?
Thanks, DC
Re: simple power language problem I don't know how to solve
Gotcha.
Here's the original code again:
Code: Select all
Inputs:
Price (Close),
SMALength (50);
Variables:
Iteration (0),
CloseSum (0),
LastSMAValue (0),
SMAValue (0);
CloseSum = 0;
for Iteration = 1 to SMALength
begin
CloseSum = CloseSum + Price[Iteration];
end;
if SMALength <> 0 then
SMAValue = CloseSum / SMALength;
if SMAValue > LastSMAValue then
SetPlotColor(1, darkgreen)
else
SetPlotColor(1, red);
Plot1(SMAValue, "Burns 50 SMA");
LastSMAValue = SMAValue;
I've been looking through posts for examples, but not finding anything yet (or at least not understanding some of what I think might help - hey, I'm new to this coding stuff)...
DC
I guess I have to ask the question in a different way...
Is there a way to code a moving average that changes color when the slope changes? I can get it to change color, but it does it one bar later than the change of slope...
Is there an example somewhere to show how to do this in Power Language?
Is there an example somewhere to show how to do this in Power Language?
- TJ
- Posts: 7743
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2223 times
Re: I guess I have to ask the question in a different way...
Please USE the code I gave you in your previous thread.Is there a way to code a moving average that changes color when the slope changes? I can get it to change color, but it does it one bar later than the change of slope...
Is there an example somewhere to show how to do this in Power Language?
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: I guess I have to ask the question in a different way...
Like TJ said in the other topic use `SetPlotColor[1]()`.Is there a way to code a moving average that changes color when the slope changes? I can get it to change color, but it does it one bar later than the change of slope...
For example:
Code: Select all
Variables:
quickMA(0),
slowMA(0);
quickMA = XAverage(Close, 10);
slowMA = XAverage(Close, 20);
Plot1(quickMA, "QuickMA");
Plot2(slowMA, "SlowMA");
if (quickMA > slowMA) then begin
SetPlotColor[1](1, blue);
SetPlotColor[2](1, blue);
SetPlotColor[3](1, blue);
SetPlotColor[1](2, blue);
SetPlotColor[2](2, blue);
SetPlotColor[3](2, blue);
end else begin
SetPlotColor[1](1, red);
SetPlotColor[2](1, red);
SetPlotColor[3](1, red);
SetPlotColor[1](2, red);
SetPlotColor[2](2, red);
SetPlotColor[3](2, red);
end;
Re: I guess I have to ask the question in a different way...
TJ,Please USE the code I gave you in your previous thread.
I wasn't clear enough in my previous response to your suggestion; I DID use the code you gave me.
The code you offered made it LOOK like it solved my problem, but all it did was displace the moving average by one period.
I see that maxmax68 made a suggestion about the iteration values; I'll give that a try.
Re: I guess I have to ask the question in a different way...
Josh,Don't open multiple topics for the same thing, please. Or at least provide a link to the previous topic. Otherwise people have to search for your previous topic to see what was already discussed. It's more convenient to keep it all in one place.
Yeah, I didn't really want to ask the same question in another thread like that, but I hadn't seen any responses in several days. I need to develop more patience...
Re: I guess I have to ask the question in a different way...
Yup, using TJ's suggestion with yours did the trick...Probably the correct answer is:
change the iteration in your code from 0 to SMALength-1
I don't get why starting at zero matters, but I guess I don't need to know...
Thanks!
- TJ
- Posts: 7743
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2223 times
Re: I guess I have to ask the question in a different way...
Yes you need to know...::
I don't get why starting at zero matters, but I guess I don't need to know...
Thanks!
To find out, you should hand calculate this loop step by step...
Code: Select all
for Iteration = 1 to SMALength
begin
CloseSum = CloseSum + Price[Iteration];
end;
Step 2: Iteration = 2, CloseSum =
Step 3: Iteration = 3, CloseSum =
...
Re: I guess I have to ask the question in a different way...
I was clueless at first as to what you were pointing out; all I could see was that it was doing 50 iterations either way. Then it occurred to me that it starts the iterations at the current bar, which is considered to be "zero bars ago"; is that it? Please tell me that's it, or I'll really be confused...To find out, you should hand calculate this loop step by step...
Step 1: Iteration = 1, CloseSum = 0 + Price[ 1 ]Code: Select all
for Iteration = 1 to SMALength
begin
CloseSum = CloseSum + Price[Iteration];
end;
Step 2: Iteration = 2, CloseSum =
Step 3: Iteration = 3, CloseSum =
...
- TJ
- Posts: 7743
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2223 times
Re: I guess I have to ask the question in a different way...
That's it... [1] means ONE bar ago.I was clueless at first as to what you were pointing out; all I could see was that it was doing 50 iterations either way. Then it occurred to me that it starts the iterations at the current bar, which is considered to be "zero bars ago"; is that it? Please tell me that's it, or I'll really be confused...To find out, you should hand calculate this loop step by step...
Step 1: Iteration = 1, CloseSum = 0 + Price[ 1 ]Code: Select all
for Iteration = 1 to SMALength
begin
CloseSum = CloseSum + Price[Iteration];
end;
Step 2: Iteration = 2, CloseSum =
Step 3: Iteration = 3, CloseSum =
...
Your calculation used data starting from a bar ago, thus your results were always one bar behind.