How to delete previously drawn object and not last one?  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
Zoli
Posts: 89
Joined: 12 Sep 2012
Has thanked: 24 times
Been thanked: 37 times

How to delete previously drawn object and not last one?

Postby Zoli » 01 Oct 2012

Hi,

I will give a simple example which will draw a trendline on every high. If 10 highs are drawn, the code should delete the first one and draw a new one when a new bar appears.
The problem is I cannot find a way to specify which trendline to delete.

Code: Select all

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

namespace PowerLanguage.Indicator
{
[SameAsSymbol(true)]
public class ST_Lines : IndicatorObject
{
public ST_Lines(object _ctx)
: base(_ctx)
{}

private int count_lines = 1;
private VariableSeries<ITrendLineObject> high_trendline;

protected override void Create()
{
high_trendline = new VariableSeries<ITrendLineObject>(this);
}

protected override void StartCalc()
{
high_trendline.DefaultValue = null;
}

protected override void CalcBar()
{
if (Bars.Status == EBarState.Close)
{
if (count_lines <= 10)
{
high_trendline.Value = DrwTrendLine.Create(new ChartPoint(Bars.Time[1], Bars.High[1]), new ChartPoint(Bars.Time[0], Bars.High[1]));
high_trendline.Value.ExtRight = true;
high_trendline.Value.Color = Color.Cyan;
count_lines++;
}
else
{
if (high_trendline.Value.ID == 1)
{
high_trendline.Value.Delete();
}
}
}
}
}
}

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2942 times

Re: How to delete previously drawn object and not last one?

Postby Henry MultiСharts » 02 Oct 2012

Hello Zoli,

You need to reference previous values and call "delete" for the drawing object (ITrendLineObject.Delete).
Try this example:

Code: Select all

high_trendline[10].Delete

Zoli
Posts: 89
Joined: 12 Sep 2012
Has thanked: 24 times
Been thanked: 37 times

Re: How to delete previously drawn object and not last one?

Postby Zoli » 03 Oct 2012

Hi Henry,

Thank you for the reply. I have tried in multiple ways, have assigned arrays for the trendlines to keep the ID, then I tried creating arrays for ITrendLineObject directly. Both work fine while drawing the trendlines and assigning the ID's, but when I try to delete one or all of them, it gives a "NullReferenceException: Object reference not set to an instance of an object" whether I check for null or not. I am definitely doing something wrong.

if (high_trendline[10].Exist && high_trendline[10] != null)
{
high_trendline[10].Delete();
}

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2942 times

Re: How to delete previously drawn object and not last one?

Postby Henry MultiСharts » 03 Oct 2012

Try this code:

Code: Select all

protected override void CalcBar()
{
if(Bars.CurrentBar == 1)
count_lines = 0;

if (Bars.Status == EBarState.Close)
{
if (count_lines <= 10)
{
high_trendline.Value = DrwTrendLine.Create(new ChartPoint(Bars.Time[1], Bars.High[1]), new ChartPoint(Bars.Time[0], Bars.High[1]));
high_trendline.Value.ExtRight = true;
high_trendline.Value.Color = Color.Cyan;
count_lines++;
}
else
{
if (!object.Equals(high_trendline[10], null))
{
if (high_trendline[10].Exist)
{
high_trendline[10].Delete();
count_lines--;
}
}
}
}
}

Zoli
Posts: 89
Joined: 12 Sep 2012
Has thanked: 24 times
Been thanked: 37 times

Re: How to delete previously drawn object and not last one?  [SOLVED]

Postby Zoli » 03 Oct 2012

Hi Henry,

Many thanks, fitted to my code and works perfectly.

joenetic
Posts: 21
Joined: 13 Jun 2013

Re: How to delete previously drawn object and not last one?

Postby joenetic » 13 Jun 2013

Hi Henry,

I tried your code but I found exception error, below are the error message, if I changed Bar.Time[1] and Bars.High[1] to [0], the error will gone, do you know the reason?



ManagedStudies.details._ELAPI_exception_Wrap_ was unhandled by user code
Message=在應用程式中發生錯誤。
Source=PLStudiesProxy
StackTrace:
於 ManagedStudies.details._ELAPI_exception_Wrap_Generator.raise(_ELAPI_exception_Wrap_Generator* )
於 PowerLanguage.?A0x75af4b10._raiser.RaiseBB(_raiser* , Int32 _bb)
於 _ELAPI_.variables.ISerIndex._index_ctx._check(_index_ctx* , Int32 _bb)
於 _ELAPI_.variables.ISerIndex._index_ctx.Prev(_index_ctx* , Int32 _barsBack)
於 PowerLanguage.IndexerImpl.get_Item(Int32 bars_ago)
於 PowerLanguage.PriceSeriesData.SeriaBase`1.get_value(Int32 bars_ago)
於 PowerLanguage.PriceSeriesData.Seria_System_DateTime.get_Item(Int32 bars_ago)
於 PowerLanguage.Indicator.test.CalcBar() 於 c:\ProgramData\TS Support\MultiCharts .NET SE\StudyServer\Techniques\CS\test.Indicator.CS: 行 105
於 PowerLanguage.CStudyAbstract.DoExecute()
於 ManagedStudies.details.CUserStudyImpl.Body(CUserStudyImpl* )
InnerException:

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2942 times

Re: How to delete previously drawn object and not last one?

Postby Henry MultiСharts » 14 Jun 2013

Hello joenetic,

Which version and build number of MultiCharts .NET do you use (Help tab->About)?
I don't have any problems with this code in MultiCharts .NET 8.7 beta 2.

joenetic
Posts: 21
Joined: 13 Jun 2013

Re: How to delete previously drawn object and not last one?

Postby joenetic » 14 Jun 2013

Hi Henry,

I am using MultiCharts .Net SE version 8.5 release (Build 7003)

Thanks!

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2942 times

Re: How to delete previously drawn object and not last one?

Postby Henry MultiСharts » 17 Jun 2013

Hello joenetic,

Please send me (support@multicharts.com) the following information for further investigation:
- workspace you are using;
- in QuoteManager select the symbol you are using, make a right click on it->Export data->Export instrument (with data). Send me the Qmd export file for analysis;
- in Power Language .Net editor->File->Export->export the study you are having problem with. Send me the study export file.

Jobauma
Posts: 104
Joined: 16 Apr 2013
Has thanked: 24 times
Been thanked: 4 times

Re: How to delete previously drawn object and not last one?

Postby Jobauma » 13 Jan 2019

Hi.

Code: Select all

if (!object.Equals( tl_TrendLine[10], null ))
{
if (tl_TrendLine[10].Exist)
tl_TrendLine[10].Delete();
}
First I though this code kept a precise number of trendlines within 10. :)

But I figured out that it deletes trendlines a set amount of bars back. I like this solution better:

Code: Select all

if (!object.Equals( tl_TrendLine[NumberOfBarsBack], null ))
{
if (tl_TrendLine[NumberOfBarsBack].Exist)
tl_TrendLine[NumberOfBarsBack].Delete();
}
Thanks.



Best regards,
Johannes Hillestad Baumann

Jobauma
Posts: 104
Joined: 16 Apr 2013
Has thanked: 24 times
Been thanked: 4 times

Re: How to delete previously drawn object and not last one?

Postby Jobauma » 14 Jan 2019

Hi.

I have a code that sometimes creates the same trendline object on the same bar ( the code has to be like that ). It's a code where several trendlines are created with calculation from left to right ( more than 1 in one go ) in different ways. Counters to keep track of which number the trendlines were created on is difficult ( to avoid duplicates ). It occurs sometimes, but not all the time. :)

It's easier to just delete the trendlines, when they're out of range. I've tried this:

Code: Select all

// Delete up to 3 TrendLines ( if Exist )

for ( int I = 0; I < 3; I++ )
{
if (!object.Equals( tl_TrendLine[117], null ))
{
if (tl_TrendLine[117].Exist)
tl_TrendLine[117].Delete();
}
}
But it doesn't work. ( Bars with more than 1 trendline passes "117". ) :)

Any clues?



Best regards,
Johannes Hillestad Baumann


Return to “MultiCharts .NET”