simple power language problem I don't know how to solve  [SOLVED]

Questions about MultiCharts and user contributed studies.
dennisc
Posts: 32
Joined: 25 Feb 2014
Has thanked: 16 times
Been thanked: 9 times

simple power language problem I don't know how to solve  [SOLVED]

Postby dennisc » 31 Aug 2014

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;
Attachments
slopeccolor.png
(140.32 KiB) Downloaded 1020 times

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: simple power language problem I don't know how to solve

Postby TJ » 31 Aug 2014

Try this:

Code: Select all


if SMAValue > LastSMAValue then
SetPlotColor[1](1, darkgreen)
else
SetPlotColor[1](1, red);


note SetPlotColor[1]

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: simple power language problem I don't know how to solve

Postby TJ » 31 Aug 2014

ps: [FAQ] How to Post Codes (that people can read)

viewtopic.php?f=16&t=11713

dennisc
Posts: 32
Joined: 25 Feb 2014
Has thanked: 16 times
Been thanked: 9 times

Re: simple power language problem I don't know how to solve

Postby dennisc » 31 Aug 2014

Try this:

Code: Select all

if SMAValue > LastSMAValue then
SetPlotColor[1](1, darkgreen)
else
SetPlotColor[1](1, red);

note SetPlotColor[1]
TJ,

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

dennisc
Posts: 32
Joined: 25 Feb 2014
Has thanked: 16 times
Been thanked: 9 times

Re: simple power language problem I don't know how to solve

Postby dennisc » 31 Aug 2014

ps: [FAQ] How to Post Codes (that people can read)

viewtopic.php?f=16&t=11713
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;
So how to get the color to plot one bar sooner without displacing the moving average?

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

dennisc
Posts: 32
Joined: 25 Feb 2014
Has thanked: 16 times
Been thanked: 9 times

I guess I have to ask the question in a different way...

Postby dennisc » 06 Sep 2014

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?

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: I guess I have to ask the question in a different way...

Postby TJ » 06 Sep 2014

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?
Please USE the code I gave you in your previous thread.

User avatar
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...

Postby JoshM » 07 Sep 2014

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...
Like TJ said in the other topic use `SetPlotColor[1]()`.

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;
PS: 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.

maxmax68
Posts: 163
Joined: 20 Nov 2012
Has thanked: 55 times
Been thanked: 48 times

Re: I guess I have to ask the question in a different way...

Postby maxmax68 » 07 Sep 2014

Probably the correct answer is:

change the iteration in your code from 0 to SMALength-1

maxmax68
Posts: 163
Joined: 20 Nov 2012
Has thanked: 55 times
Been thanked: 48 times

Re: simple power language problem I don't know how to solve

Postby maxmax68 » 07 Sep 2014

Probably the correct answer is:

change the iteration in your code from 0 to SMALength-1

dennisc
Posts: 32
Joined: 25 Feb 2014
Has thanked: 16 times
Been thanked: 9 times

Re: I guess I have to ask the question in a different way...

Postby dennisc » 07 Sep 2014

Please USE the code I gave you in your previous thread.
TJ,

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.

dennisc
Posts: 32
Joined: 25 Feb 2014
Has thanked: 16 times
Been thanked: 9 times

Re: I guess I have to ask the question in a different way...

Postby dennisc » 07 Sep 2014

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.
Josh,

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...

dennisc
Posts: 32
Joined: 25 Feb 2014
Has thanked: 16 times
Been thanked: 9 times

Re: I guess I have to ask the question in a different way...

Postby dennisc » 07 Sep 2014

Probably the correct answer is:

change the iteration in your code from 0 to SMALength-1
Yup, using TJ's suggestion with yours did the trick...

I don't get why starting at zero matters, but I guess I don't need to know...

Thanks!

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: I guess I have to ask the question in a different way...

Postby TJ » 07 Sep 2014

::
I don't get why starting at zero matters, but I guess I don't need to know...
Thanks!
Yes you need to know...

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 1: Iteration = 1, CloseSum = 0 + Price[ 1 ]
Step 2: Iteration = 2, CloseSum =
Step 3: Iteration = 3, CloseSum =
...

dennisc
Posts: 32
Joined: 25 Feb 2014
Has thanked: 16 times
Been thanked: 9 times

Re: I guess I have to ask the question in a different way...

Postby dennisc » 07 Sep 2014

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 1: Iteration = 1, CloseSum = 0 + Price[ 1 ]
Step 2: Iteration = 2, CloseSum =
Step 3: Iteration = 3, CloseSum =
...
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...

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: I guess I have to ask the question in a different way...

Postby TJ » 08 Sep 2014

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 1: Iteration = 1, CloseSum = 0 + Price[ 1 ]
Step 2: Iteration = 2, CloseSum =
Step 3: Iteration = 3, CloseSum =
...
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...
That's it... [1] means ONE bar ago.
Your calculation used data starting from a bar ago, thus your results were always one bar behind.


Return to “MultiCharts”