TrendLines disappear on Live Data (Indicator)  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
Jobauma
Posts: 25
Joined: 16 Apr 2013
Has thanked: 8 times
Been thanked: 1 time

TrendLines disappear on Live Data (Indicator)

Postby Jobauma » 28 Jan 2018

Hi.



I have no clue to why this happens.. The trendlines are created on live data, but they disappear right after they are created. If they are created right before a new bar appears, they do not disappear. I created the indicator for simulated trading, so the lines are representing the position, stop loss and take profit. I've also added "Buy" "Sell" toolbar buttons, but I've also noticed that I have to press them several times, before price changes, for the lines to appear. I wonder why this happens too. :)

I've attached the indicator, so if someone knows how to fix this problem, or discover a bug here, I'm thankful for that. :P
Attachments
Jobauma BuySell.pln.zip
(2.21 KiB) Downloaded 2 times

Jobauma
Posts: 25
Joined: 16 Apr 2013
Has thanked: 8 times
Been thanked: 1 time

Re: TrendLines disappear on Live Data (Indicator)

Postby Jobauma » 28 Jan 2018

Code: Select all

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;






namespace PowerLanguage.Indicator
{
   [SameAsSymbol(true)]

   public class _Jobauma_BuySell : IndicatorObject
   {
      private bool m_Plot_Buy;
      private bool m_Plot_Sell;

      private Color m_BGColor;
      private Color m_Color_Position;
      private Color m_Color_SL;
      private Color m_Color_TP;



      private ITrendLineObject tl_;



      public _Jobauma_BuySell( object _ctx ) : base(_ctx)
      {
         Point = 0.00001;

         StopLoss = 50;
         TakeProfit = 210;

         Color_Position = Color.FromArgb( 0, 153, 0 );
         Color_SL = Color.FromArgb( 153, 0, 0 );
         Color_TP = Color.FromArgb( 102, 0, 204 );
         Color_Opacity = 75;
      }

      [Input]
      public double Point { get; set; }
      [Input]
      public string _ { get; set; }
      [Input]
      public int StopLoss { get; set; }
      [Input]
      public int TakeProfit { get; set; }
      [Input]
      public string __ { get; set; }
      [Input]
      public Color Color_Position { get; set; }
      [Input]
      public Color Color_SL { get; set; }
      [Input]
      public Color Color_TP { get; set; }
      [Input]
      public int Color_Opacity { get; set; }



      protected override void Create()
      {
      }



      public static Color Blend( Color Color1, Color Color2, double Amount )
      {
         Amount = Amount * 2.55;
         byte R = (byte) ( ( Color1.R * Amount / 255 ) + Color2.R * ( 255 - Amount ) / 255 );
         byte G = (byte) ( ( Color1.G * Amount / 255 ) + Color2.G * ( 255 - Amount ) / 255 );
         byte B = (byte) ( ( Color1.B * Amount / 255 ) + Color2.B * ( 255 - Amount ) / 255 );
         return Color.FromArgb( R, G, B );
      }

      public void OpenPosition( int Direction )
      {
         {
            ChartPoint Point1 = new ChartPoint( Bars.Time[1], Bars.Close[0] );
            ChartPoint Point2 = new ChartPoint( Bars.Time[0], Bars.Close[0] );
            tl_ = DrwTrendLine.Create( Point1, Point2, true );
            tl_.Color = m_Color_Position;
            tl_.Style = ETLStyle.ToolDashed;
            tl_.ExtRight = true;
            tl_.Locked = true;
         }

         {
            ChartPoint Point1 = new ChartPoint( Bars.Time[1], Bars.Close[0] - ( Point * StopLoss * Direction ) );
            ChartPoint Point2 = new ChartPoint( Bars.Time[0], Bars.Close[0] - ( Point * StopLoss * Direction ));
            tl_ = DrwTrendLine.Create( Point1, Point2, true );
            tl_.Color = m_Color_SL;
            tl_.Style = ETLStyle.ToolDashed;
            tl_.ExtRight = true;
            tl_.Locked = true;
         }

         {
            ChartPoint Point1 = new ChartPoint( Bars.Time[1], Bars.Close[0] + ( Point * TakeProfit * Direction ) );
            ChartPoint Point2 = new ChartPoint( Bars.Time[0], Bars.Close[0] + ( Point * TakeProfit * Direction ) );
            tl_ = DrwTrendLine.Create( Point1, Point2, true );
            tl_.Color = m_Color_TP;
            tl_.Style = ETLStyle.ToolDashed;
            tl_.ExtRight = true;
            tl_.Locked = true;
         }
      }

      private void AddItem2ToolStrip( ToolStrip tb, ToolStripItem item )
      {
         item.Tag = this;
         tb.Items.Add(item);
      }

      private bool tool_bar_inited = false;

      protected override void StartCalc()
      {
         if ( Point < 0.00001 ) Point = 0.00001;



         if (!tool_bar_inited)
         {
            ChartToolBar.AccessToolBar( tb=>
            {
               {
                  var _tsi3 = new ToolStripButton { Text = "Buy" };
                  _tsi3.Click += Button1_Buy;
                  AddItem2ToolStrip( tb, _tsi3 );
               }

               AddItem2ToolStrip(tb, new ToolStripSeparator());

               {
                  var _tsi3 = new ToolStripButton { Text = "Sell" };
                  _tsi3.Click += Button2_Sell;
                  AddItem2ToolStrip( tb, _tsi3 );
               }

               AddItem2ToolStrip(tb, new ToolStripSeparator());
            });
            tool_bar_inited = true;
         }




         m_BGColor = Environment.BGColor;

         if ( Color_Opacity > 100 ) Color_Opacity = 100;
         else if ( Color_Opacity < 0 ) Color_Opacity = 0;
         m_Color_Position = Blend( Color_Position, m_BGColor, Color_Opacity );
         m_Color_SL = Blend( Color_SL, m_BGColor, Color_Opacity );
         m_Color_TP = Blend( Color_TP, m_BGColor, Color_Opacity );
      }

      private void Button1_Buy( object sender, EventArgs e )
      {
         m_Plot_Buy = true;
         m_Plot_Sell = false;
      }

      private void Button2_Sell( object sender, EventArgs e )
      {
         m_Plot_Sell = true;
         m_Plot_Buy = false;
      }



      protected override void CalcBar()
      {
         if ( Bars.LastBarOnChart )
         {
            if ( m_Plot_Buy || m_Plot_Sell )
            {
               int Direction = 0;
               if (m_Plot_Buy) Direction = 1;
               else if (m_Plot_Sell) Direction = -1;

               OpenPosition(Direction);

               m_Plot_Buy = false;
               m_Plot_Sell = false;
            }
         }
      }



      protected override void Destroy()
      {
         if (tool_bar_inited)
         {
            ChartToolBar.AccessToolBar( tb =>
            {
               var _for_erase = new List<ToolStripItem>();
                                       
               foreach ( ToolStripItem item in tb.Items )
                  if (ReferenceEquals( this, item.Tag ))
                     _for_erase.Add(item);
                                       
               foreach ( var item in _for_erase )
                  tb.Items.Remove(item);
            });
         }
      }
   }
}

Jobauma
Posts: 25
Joined: 16 Apr 2013
Has thanked: 8 times
Been thanked: 1 time

Re: TrendLines disappear on Live Data (Indicator)

Postby Jobauma » 28 Jan 2018

By the way: I tried to make the buttons trigger a method directly to create the lines, but this caused an error unfortunately. Is it possible to do this without live data to create the lines?

User avatar
ABC
Posts: 538
Joined: 16 Dec 2006
Has thanked: 113 times
Been thanked: 300 times
Contact:

Re: TrendLines disappear on Live Data (Indicator)  [SOLVED]

Postby ABC » 29 Jan 2018

Jobauma,

take a look at the "RecoverDrawings" attribute. This will prevent trendlines from disappearing in case they are created intrabar.

Regards,

ABC
These users thanked the author ABC for the post:
Jobauma

Jobauma
Posts: 25
Joined: 16 Apr 2013
Has thanked: 8 times
Been thanked: 1 time

Re: TrendLines disappear on Live Data (Indicator)

Postby Jobauma » 29 Jan 2018

Thanks, man! It worked! :D


Return to “MultiCharts .NET”