last price line, anyone?

Questions about MultiCharts .NET and user contributed studies.
User avatar
arnie
Posts: 1593
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 511 times

last price line, anyone?

Postby arnie » 20 Jun 2015

Can someone code something similar to this?
inputs:
Color (white),
RightShift (50),
ExtendLeft (true),
ExtendRight (true);

variables:
firstBar (0),
intrabarpersist lastPrice (0);

if currentbar = 1 then
firstBar = currentbar;

lastPrice = close;

if ExtendLeft = true then begin
for value1 = 0 to (currentbar - firstBar) begin
plot1[value1](lastPrice,"lastPrice", Color);
end;
end;

if ExtendRight = true then begin
for value1 = 0 to RightShift begin
Plot1[-value1](lastPrice, "lastPrice", Color);
end;
end;

noplot(1);
I don't know how trendlines work over the .NET platform but the idea of plotting a line and not a trendline is because we can send it to the back of the bar and it updates in "realtime" when the last price changes whereas a trendline, in the standard platform,has that annoying delay...

This is one of those basic studies that should be built in MC.

Image
Attachments
line.png
line.png (11.72 KiB) Viewed 2524 times

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1502 times
Contact:

Re: last price line, anyone?

Postby JoshM » 21 Jun 2015

Can someone code something similar to this?

(...)

I don't know how trendlines work over the .NET platform but the idea of plotting a line and not a trendline is because we can send it to the back of the bar and it updates in "realtime" when the last price changes whereas a trendline, in the standard platform,has that annoying delay...
Here's my attempt. It's slightly different since I didn't translate the `NoPlot(1)` statement from your original code because I didn't understand why that's added to your code. :)

With the indicator's default options:
Image

With changed options:
Image
last_price_line_indicator.pln
(1.39 KiB) Downloaded 421 times
I tested the indicator on PlayBack data since the markets are closed today, so if it performs differently on real-time data than you'd like, let me know and I'll look into it.

****************

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
[SameAsSymbol(true), UpdateOnEveryTick(true)]
public class Last_Price_Line : IndicatorObject
{
[Input]
public Color Colour { get; set; }

[Input]
public int RightShift { get; set; }

[Input]
public int LineWidth { get; set; }

[Input]
public bool ExtendLeft { get; set; }

[Input]
public bool ExtendRight { get; set; }

public Last_Price_Line(object _ctx) : base(_ctx)
{
Colour = Color.LimeGreen;
RightShift = 50;
ExtendLeft = true;
ExtendRight = true;
LineWidth = 2;
}

private IPlotObject lastPrice;

protected override void Create()
{
lastPrice = AddPlot(new PlotAttributes("LastPrice", EPlotShapes.Line, Color.Red));
}

protected override void CalcBar()
{
// Only operate on the last bar of the chart
if (!Bars.LastBarOnChart)
return;

// Extend the horizontal line to the left
if (ExtendLeft)
{
for (int i = 0; i < Bars.CurrentBar; i++)
{
lastPrice.Set(i, Bars.Close[0], Colour, LineWidth);
}
}

// Extend the horizontal line to the right
if (ExtendRight)
{
// Don't access more bars in the chart's right margin than available
int barsForward = Math.Min(RightShift, ExecInfo.MaxBarsForward + 1);

for (int i = 0; i < barsForward; i++)
{
lastPrice.Set(-i, Bars.Close[0], Colour, LineWidth);
}
}
}
}
}
Attachments
lastPriceLine_otherOptions.png
lastPriceLine_otherOptions.png (6.89 KiB) Viewed 2546 times
lastPriceLine_defaultOptions.png
lastPriceLine_defaultOptions.png (7.07 KiB) Viewed 2533 times

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1502 times
Contact:

Re: last price line, anyone?

Postby JoshM » 22 Jun 2015

I've extended the indicator a little bit by having it indicate support and resistance too (with green and red by default).

I've defined 'support' as a bar that had a low lower than the recent price, but nonetheless succeeded to close above it (i.e., displayed strength). 'Resistance' is a bar that was higher than the recent price, but which couldn't hold that price level and moved away from it.

I've added this with the idea that it would make it easier to quickly see whether the recent price is around a level of support of resistance.

For example:

Image

By the way, if you have an idea to enhance this indicator Arnie, let me know and I'll add it.
Attachments
last_price_line_v2.pln
(1.63 KiB) Downloaded 432 times
scr.22-06-2015 09.20.52.png
scr.22-06-2015 09.20.52.png (6.85 KiB) Viewed 2517 times

User avatar
arnie
Posts: 1593
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 511 times

Re: last price line, anyone?

Postby arnie » 22 Jun 2015

Many thanks JoshM.

Plotting this study with the TPO study brings up some issues. I usually have 2 series plotted, one for RTH and the other only plotting 1 bar (today's) for the overnight alone.
If I plot the last line on the overnight series the line does not extends to the left. Since there's no bars back, since I'm plotting only 1 bar, the line doesn't extend.

Anyway to surpass this?

Image
Attachments
line02.png
line02.png (33.19 KiB) Viewed 2502 times

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1502 times
Contact:

Re: last price line, anyone?

Postby JoshM » 22 Jun 2015

Plotting this study with the TPO study brings up some issues. I usually have 2 series plotted, one for RTH and the other only plotting 1 bar (today's) for the overnight alone.
If I plot the last line on the overnight series the line does not extends to the left. Since there's no bars back, since I'm plotting only 1 bar, the line doesn't extend.

Anyway to surpass this?
It can be surpassed when you have a lot of bars on the chart. It's not ideal, and I also noticed this while making the script.

Perhaps Henry can tell us why this simply indicator needs several hundred of MaxBarsBack. When I add this to the code in my first post in the thread:

Code: Select all

Output.WriteLine("CurrentBar: {0}, FullSymbolData.Current: {1}, MaxBarsBack: {2}",
Bars.CurrentBar,
Bars.FullSymbolData.Current,
ExecInfo.MaxBarsBack);
I get this output:

Code: Select all

CurrentBar: 502, FullSymbolData.Current: 502, MaxBarsBack: 0
CurrentBar: 496, FullSymbolData.Current: 502, MaxBarsBack: 6
CurrentBar: 490, FullSymbolData.Current: 502, MaxBarsBack: 12
CurrentBar: 481, FullSymbolData.Current: 502, MaxBarsBack: 21
CurrentBar: 467, FullSymbolData.Current: 502, MaxBarsBack: 35
CurrentBar: 444, FullSymbolData.Current: 502, MaxBarsBack: 58
CurrentBar: 407, FullSymbolData.Current: 502, MaxBarsBack: 95
CurrentBar: 347, FullSymbolData.Current: 502, MaxBarsBack: 155
CurrentBar: 250, FullSymbolData.Current: 502, MaxBarsBack: 252
CurrentBar: 250, FullSymbolData.Current: 502, MaxBarsBack: 252
Since I only use `Bars.Close[0]` in the script (and `Bars.FullSymbolData`, but that's independent from MaxBarsBack) I don't understand why the script (probably `Set()`) needs to use half of the chart's bars when plotting.

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1502 times
Contact:

Re: last price line, anyone?

Postby JoshM » 22 Jun 2015

I don't know how trendlines work over the .NET platform but the idea of plotting a line and not a trendline is because we can send it to the back of the bar and it updates in "realtime" when the last price changes whereas a trendline, in the standard platform,has that annoying delay...
We can also make an indicator that uses trend lines (so we won't need a lot of bars before it plots) and that does not have a delay (on my computer and the instruments I tested, at least), but a trend line cannot be send back behind a bar. So I don't know if that's an option for you?

User avatar
arnie
Posts: 1593
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 511 times

Re: last price line, anyone?

Postby arnie » 22 Jun 2015

Many thanks for all the info.

This maxbarsback issue is identical on the PL version which can be quite annoying when we want to have such basic lines plotted.
If we you code a study that plot a fix price and plot a line for it, we get a horizontal line that encompasses the entire chart area but as soon as we link a line to a bar price it's impossible to have the same result.

Although the use of a trendline on bar charts might not be a big problem, if one want to use this study with footprint charts, trendlines become a big problem because they fall on top of the text.

I haven't tested your 2nd version yet but am curious about the s/r option.

User avatar
Henry MultiСharts
Posts: 9121
Joined: 25 Aug 2011
Has thanked: 1259 times
Been thanked: 2927 times

Re: last price line, anyone?

Postby Henry MultiСharts » 23 Jun 2015

Perhaps Henry can tell us why this simply indicator needs several hundred of MaxBarsBack. When I add this to the code in my first post in the thread:

Code: Select all

Output.WriteLine("CurrentBar: {0}, FullSymbolData.Current: {1}, MaxBarsBack: {2}",
Bars.CurrentBar,
Bars.FullSymbolData.Current,
ExecInfo.MaxBarsBack);
I get this output:

Code: Select all

CurrentBar: 502, FullSymbolData.Current: 502, MaxBarsBack: 0
CurrentBar: 496, FullSymbolData.Current: 502, MaxBarsBack: 6
CurrentBar: 490, FullSymbolData.Current: 502, MaxBarsBack: 12
CurrentBar: 481, FullSymbolData.Current: 502, MaxBarsBack: 21
CurrentBar: 467, FullSymbolData.Current: 502, MaxBarsBack: 35
CurrentBar: 444, FullSymbolData.Current: 502, MaxBarsBack: 58
CurrentBar: 407, FullSymbolData.Current: 502, MaxBarsBack: 95
CurrentBar: 347, FullSymbolData.Current: 502, MaxBarsBack: 155
CurrentBar: 250, FullSymbolData.Current: 502, MaxBarsBack: 252
CurrentBar: 250, FullSymbolData.Current: 502, MaxBarsBack: 252
Since I only use `Bars.Close[0]` in the script (and `Bars.FullSymbolData`, but that's independent from MaxBarsBack) I don't understand why the script (probably `Set()`) needs to use half of the chart's bars when plotting.
Most likely because the code tries to access Bars.FullSymbolData.Current - 1 when this data is not yet available in the series.

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1502 times
Contact:

Re: last price line, anyone?

Postby JoshM » 23 Jun 2015

Most likely because the code tries to access Bars.FullSymbolData.Current - 1 when this data is not yet available in the series.
I execute `CalcBar()` for the first time on the last bar of the chart with:

Code: Select all

if (!Bars.LastBarOnChart)
return;
So I think that `Bars.FullSymbolData` is fully loaded by then.

If I uncomment however the statement with the plot's `Set()` method, the MaxBarsBack are zero (as is expected with `Bars.FullSymbolData`):

Code: Select all

CurrentBar: 566, FullSymbolData.Current: 566, MaxBarsBack: 0
CurrentBar: 566, FullSymbolData.Current: 566, MaxBarsBack: 0
CurrentBar: 566, FullSymbolData.Current: 566, MaxBarsBack: 0
CurrentBar: 566, FullSymbolData.Current: 566, MaxBarsBack: 0
Is this expected behaviour? I couldn't find in the documentation that `Set()` requires MaxBarsBack, but perhaps it does.

By the way, `Set()` also required several hundred of bars back when I let it plot a literal value instead of using `Bars.Close[0]`.

User avatar
Henry MultiСharts
Posts: 9121
Joined: 25 Aug 2011
Has thanked: 1259 times
Been thanked: 2927 times

Re: last price line, anyone?

Postby Henry MultiСharts » 24 Jun 2015

That is expected behavior. Plots do have BarsBack and the displacement directly affects the BarsBack.

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1502 times
Contact:

Re: last price line, anyone?

Postby JoshM » 25 Jun 2015

Thanks Henry.
Although the use of a trendline on bar charts might not be a big problem, if one want to use this study with footprint charts, trendlines become a big problem because they fall on top of the text.
How do you want to proceed; use an indicator with a plot send to the back (but that requires a high MaxBarsBack) or a trend line that can be plot on all bars but that may overlay itself on the footprint charts?

(I'll check later if we can workaround that, but don't have time for that now).

User avatar
arnie
Posts: 1593
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 511 times

Re: last price line, anyone?

Postby arnie » 25 Jun 2015

How do you want to proceed; use an indicator with a plot send to the back (but that requires a high MaxBarsBack) or a trend line that can be plot on all bars but that may overlay itself on the footprint charts?

(I'll check later if we can workaround that, but don't have time for that now).
The best thing to do would be MC developers review how lines and trendlines are plotted and adapt them to the new requirements.

Well, since I have all my studies in the PL version and have adapted some of them for footprints charts, and since the last line study for the .NET is to be used mainly with TPO charts, if you can, it's preferable to use a trendline.
Since the TPO study does not update realtime it's a pain looking at it and having the red triangle that indicates the last price, with a 30 seconds delay.

You said that the trendline in the .NET does not have that annoying delay regarding the last price like the PL version has, correct? Like, the last price moves and the trendline only moves 1 second later or when the next tick gets in. For some people that is not a big issue, I get it, I understand, but for me, looking at a chart, see the last price moving and see the trendline that should be in sync with the last price moving with a second delay or so, man... it drives me nuts.

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1502 times
Contact:

Re: last price line, anyone?

Postby JoshM » 25 Jun 2015

(I'll check later if we can workaround that, but don't have time for that now).
By the way, if two trend lines are plotted on the same price, the second indicator (that's added last to the chart; green line in the image below) is placed on top of the other:

Image

I don't know if this also works the same with the TPO indicator, but you can try to add the last price indicator* first followed by the TPO indicator.

*: Not done yet.
Attachments
scr.25-06-2015 18.35.07.png
scr.25-06-2015 18.35.07.png (14.36 KiB) Viewed 2423 times

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1502 times
Contact:

Re: last price line, anyone?

Postby JoshM » 25 Jun 2015

You said that the trendline in the .NET does not have that annoying delay regarding the last price like the PL version has, correct? Like, the last price moves and the trendline only moves 1 second later or when the next tick gets in. For some people that is not a big issue, I get it, I understand, but for me, looking at a chart, see the last price moving and see the trendline that should be in sync with the last price moving with a second delay or so, man... it drives me nuts.
I understand that you find that lag annoying; I also won't like it. That lag also happens with MultiCharts .NET when the trend line is relocated to its new price when a new tick arrives.

But if we delete the trend line first and then draw it again, the lag doesn't happen (on my pc with the instruments I tested, as a disclaimer).

Here's the first version of the indicator to test:
horizontal_last_price_line_v1.pln
(1.79 KiB) Downloaded 435 times
It looks like this by default:
Image

With these input options:
Image

If you have any feedback I'd happily hear it, but I'm doubtful it can be made much quicker. So if a lag still occurs, we might have a problem. :]
Attachments
scr.25-06-2015 19.44.21.png
scr.25-06-2015 19.44.21.png (4.29 KiB) Viewed 2433 times
scr.25-06-2015 19.43.53.png
scr.25-06-2015 19.43.53.png (7.36 KiB) Viewed 2443 times

User avatar
arnie
Posts: 1593
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 511 times

Re: last price line, anyone?

Postby arnie » 25 Jun 2015

But if we delete the trend line first and then draw it again, the lag doesn't happen (on my pc with the instruments I tested, as a disclaimer).

If you have any feedback I'd happily hear it, but I'm doubtful it can be made much quicker. So if a lag still occurs, we might have a problem. :]
You lost me with that one, delete how? If I hit delete, I remove the study form the chart.
Yes, the lag is there, it seems it not so pronounced like in the PL, but exists. I'd like to test your way by deleting it...

Here's something interesting, I have 3 series, one for RTH only plotting 20 bars, one for globex only plotting one bar, today's (gray profile) and the last series plots the full session, RTH + globex which I use for the volume profile on the right.
So in terms of TPO's, I want to see the RTH's for the last couple of days, today's globex and today's full sessions volume.

The last line should be plotted over the full session bar so I can have it during the overnight and RTH session.
The thing is, since it's only 1 bar it does not plot, it hangs, indicating that it's calculating...
I could plot 10 full session bars but those bars will fall between each RTH bar which will separate each RTH profile even more.

Image

Fortunately MC has been capable in dealing with my unique chart specifications but there are a couple of things that can completely ruin the result I want to see. This continues to be one of them, loading a single bar and how studies plot over that single bar, with one or more series.
Attachments
tpo.png
tpo.png (34.22 KiB) Viewed 2420 times

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1502 times
Contact:

Re: last price line, anyone?

Postby JoshM » 26 Jun 2015

You lost me with that one, delete how? If I hit delete, I remove the study form the chart.
Sorry for not being more clear. I was talking about how it's implemented programmatically; the script deletes the trend line, you don't need to do that yourself.
Yes, the lag is there, it seems it not so pronounced like in the PL, but exists.
I'm out of ideas for how to prevent that lag. Perhaps someone can help us further by suggesting a different approach?
Here's something interesting, I have 3 series, one for RTH only plotting 20 bars, one for globex only plotting one bar, today's (gray profile) and the last series plots the full session, RTH + globex which I use for the volume profile on the right.
So in terms of TPO's, I want to see the RTH's for the last couple of days, today's globex and today's full sessions volume.

The last line should be plotted over the full session bar so I can have it during the overnight and RTH session.
The thing is, since it's only 1 bar it does not plot, it hangs, indicating that it's calculating...
That's true, the indicator needs at least 2 bars to plot. That's because a MC .NET trend line needs two different bar times to draw the line between.

User avatar
arnie
Posts: 1593
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 511 times

Re: last price line, anyone?

Postby arnie » 26 Jun 2015

I appreciate your help Josh, many thanks.

I understand that a line/trendline needs two points to be drawn but an horizontal line/trendline only needs one point. You pick up that point on the bar you want and extend that point to the left and right.

So for this to be resolved, MC developers would need to, a) allow us to code an horizontal line which if coded as a trendline and not as a simple plot, PLEASE CODE IT IN A WAY THAT DOES NOT PRODUCE THAT ANNOYING LAG or b) allow new options for the horizontal line tool. We actually can see this in many other platforms where inside the line settings we have several options to connect that line, for example, close, open, high, low, high+low+close/3, etc...

mno
Posts: 46
Joined: 11 Feb 2010
Has thanked: 16 times
Been thanked: 5 times

Re: last price line, anyone?

Postby mno » 11 May 2019

Can someone code something similar to this?

(...)

I don't know how trendlines work over the .NET platform but the idea of plotting a line and not a trendline is because we can send it to the back of the bar and it updates in "realtime" when the last price changes whereas a trendline, in the standard platform,has that annoying delay...
Here's my attempt. It's slightly different since I didn't translate the `NoPlot(1)` statement from your original code because I didn't understand why that's added to your code. :)

With the indicator's default options:
Image

With changed options:
Image

last_price_line_indicator.pln

I tested the indicator on PlayBack data since the markets are closed today, so if it performs differently on real-time data than you'd like, let me know and I'll look into it.

****************

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
[SameAsSymbol(true), UpdateOnEveryTick(true)]
public class Last_Price_Line : IndicatorObject
{
[Input]
public Color Colour { get; set; }

[Input]
public int RightShift { get; set; }

[Input]
public int LineWidth { get; set; }

[Input]
public bool ExtendLeft { get; set; }

[Input]
public bool ExtendRight { get; set; }

public Last_Price_Line(object _ctx) : base(_ctx)
{
Colour = Color.LimeGreen;
RightShift = 50;
ExtendLeft = true;
ExtendRight = true;
LineWidth = 2;
}

private IPlotObject lastPrice;

protected override void Create()
{
lastPrice = AddPlot(new PlotAttributes("LastPrice", EPlotShapes.Line, Color.Red));
}

protected override void CalcBar()
{
// Only operate on the last bar of the chart
if (!Bars.LastBarOnChart)
return;

// Extend the horizontal line to the left
if (ExtendLeft)
{
for (int i = 0; i < Bars.CurrentBar; i++)
{
lastPrice.Set(i, Bars.Close[0], Colour, LineWidth);
}
}

// Extend the horizontal line to the right
if (ExtendRight)
{
// Don't access more bars in the chart's right margin than available
int barsForward = Math.Min(RightShift, ExecInfo.MaxBarsForward + 1);

for (int i = 0; i < barsForward; i++)
{
lastPrice.Set(-i, Bars.Close[0], Colour, LineWidth);
}
}
}
}
}
Hello JoshM,

I am wondering if would be so nice to share same indicator for regular MC users. I really like the second version with SR with have no idea to make one on the MC. Thanks so much in advance!

mno


Return to “MultiCharts .NET”