Question re: Plot function  [SOLVED]

Questions about MultiCharts and user contributed studies.
PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Question re: Plot function

Postby PD Quig » 03 Apr 2019

Here is a simple code for plotting an moving average:

Code: Select all

inputs:
MA_Length (20),
Sensitivity (1),
Color_Up (green),
Color_Down (red),
Color_Neutral (yellow);

variables:
MA (0),
MA_Rising (false),
MA_Falling (false);

MA = round(XAverage(Close, MA_Length), 1);

MA_Rising = MA > MA[Sensitivity];
MA_Falling = MA < MA[Sensitivity];

if MA_Rising then Plot1(MA, "MA", Color_Up)
else if MA_Falling then Plot1(MA, "MA", Color_Down)
else Plot1(MA, "MA", Color_Neutral);
The problem with using Plot is that it is lagging. It will plot the color of the next line as a result of the calculation on the prior data points. Here's how it looks:
2019-04-03_1434.png
(34.68 KiB) Downloaded 214 times
Notice how some up-sloping lines are red, some down-sloping lines are green, and how flat lines don't plot as yellow.

Here's how I think it should look:
2019-04-03_1435.png
(34.35 KiB) Downloaded 214 times
Can Plot be used to do this?

-pdq

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

Re: Question re: Plot function  [SOLVED]

Postby TJ » 03 Apr 2019

For line plots,
you should always add an offset code:

Code: Select all


SetPlotColor[1]( 1, mycolor );

PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Re: Question re: Plot function

Postby PD Quig » 03 Apr 2019

Awesome! Thanks so much, TJ.

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

Re: Question re: Plot function

Postby TJ » 03 Apr 2019

This is how I would code this snippet:

Code: Select all


inputs:
MA_Length (20),
Sensitivity (1),
Color_Up (green),
Color_Down (red),
Color_Neutral (yellow);

variables:
MA.Color(0),
MA (0),
MA_Rising (false),
MA_Falling (false);

MA = round(XAverage(Close, MA_Length), 1);

MA_Rising = MA > MA[Sensitivity];
MA_Falling = MA < MA[Sensitivity];

if MA_Rising then MA.Color = Color_Up
else if MA_Falling then MA.Color = Color_Down
else MA.Color = Color_Neutral;

Plot1( MA, "MA", MA.Color);
SetPlotColor[1]( 1, MA.Color);

PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Re: Question re: Plot function

Postby PD Quig » 03 Apr 2019

Clean.


Return to “MultiCharts”