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: 8508
Joined: 25 Aug 2011
Has thanked: 1213 times
Been thanked: 2733 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: 8508
Joined: 25 Aug 2011
Has thanked: 1213 times
Been thanked: 2733 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--;
                        }
                    }
                }
            }
        }
These users thanked the author Henry MultiСharts for the post (total 2):
Zolismath

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: 19
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: 8508
Joined: 25 Aug 2011
Has thanked: 1213 times
Been thanked: 2733 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: 19
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: 8508
Joined: 25 Aug 2011
Has thanked: 1213 times
Been thanked: 2733 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.


Return to “MultiCharts .NET”