Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

persistence of custom brush colors / settings

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    persistence of custom brush colors / settings

    Hi, I'm trying to get custom brush colors (set from the indicator settings) to persist when the NinjaScript reloads. This appears to impact Templates as well.

    For example, the default color is Blue, but I add an indicator and change the color to Red. When I save as a template, and then load the template elsewhere, the color reverts to blue.

    Here some sample code that draws a simple line at certain times. Bolded parts are the likely important lines. Can someone help me here or point to some documentation which would lead me in the right direction? Thanks.



    region Using declarations
    using NinjaTrader.Data;
    using NinjaTrader.Gui;
    using SharpDX.Direct2D1;
    using System;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Windows.Media;
    using System.Xml.Serialization;
    #endregion

    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators.MyIndicators
    {
    public class D_draw : Indicator
    {

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"PriceDraw";
    Name = "PriceDraw";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    PaintPriceMarkers = true;
    IsSuspendedWhileInactive = true;

    MyBrush = Brushes.Blue;


    }
    else if (State == State.Configure)
    {
    AddPlot(new Stroke(MyBrush, DashStyleHelper.Solid, 1), PlotStyle.Dot, "D_draw");
    AddDataSeries(BarsPeriodType.Minute, 1);
    }
    }

    private double _plotValue = 0;

    protected override void OnBarUpdate()
    {

    if (CurrentBars[1] < 1000)
    { return; }

    if (BarsPeriod.BarsPeriodType == BarsPeriodType.Minute && BarsPeriod.Value == 1) //Only look at 1Min bars regardless of chart
    {
    int BarMinute = Time[0].Minute;
    int BarHour = Time[0].Hour;
    int BarDay = Time[0].Day;
    int BarMonth = Time[0].Month;
    int BarYear = Time[0].Year;
    int open = CurrentBar - Bars.GetBar(new DateTime(BarYear, BarMonth, BarDay, PlotHour, 0, 0)); //Open Time example 800 cst

    // check to see if matches
    if (PlotHour == BarHour & PlotMinute == BarMinute)
    {
    _plotValue = Closes[1][0];
    Print("Plotvalue: " + BarDay.ToString() + " day & value: " + _plotValue.ToString());

    }

    if(_plotValue > 0)
    {
    Values[0][0] = _plotValue;
    }

    }

    }


    region Properties

    [Range(0, int.MaxValue)]
    [NinjaScriptProperty]
    [Display(Name = "EventHour", Description = "Hour of Event", Order = 1, GroupName = "Parameters")]
    public int PlotHour
    { get; set; }

    [Range(0, int.MaxValue)]
    [NinjaScriptProperty]
    [Display(Name = "EventMinute", Description = "Minute of Event", Order = 2, GroupName = "Parameters")]
    public int PlotMinute
    { get; set; }




    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public double TheOpen
    {
    get; set;
    }

    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public double o
    {
    get; set;
    }
    [XmlIgnore]
    [Display(Name = "MyBrush", Description = "Color of the Event Marker", Order = 1, GroupName = "Parameters")]
    public System.Windows.Media.Brush MyBrush
    { get; set; }




    #endregion

    }

    #2
    Hello mohawkTrader,


    Thank you for your post.


    In order to properly serialize the Brush, you will need to add code to convert the public "MyBrush" to a string type which can then be processed by the serialization routines, meaning it can be saved and then loaded from a template.



    Code:
    [Browsable(false)]
    
    public string MyBrushSerialize
    
    {
    
    get { return Serialize.BrushToString(MyBrush); }
    
    set { MyBrush = Serialize.StringToBrush(value); }
    
    }


    Please see the following Help Guide page.


    Working with Brushes- Using brushes defined on the user interface





    Please let me know if you have any other questions.
    Last edited by NinjaTrader_Gaby; 09-28-2023, 11:53 AM.

    Comment


      #3
      Thank you. This is exactly the information needed.

      Comment


        #4
        Originally posted by NinjaTrader_Gaby View Post
        Code:
        [Browsable(false)]
        
        public string MyBrushSerialize
        
        {
        
        get { return Serialize.BrushToString(MyBrush); }
        
        set { MyBrush = Serialize.StringToBrush(value); }
        
        }
        Hey Gaby

        If I created this "MyBrushSerialize", I need to manually call it for it to be effective correct? Otherwise It has no use? Can you provide an example for how to use this specific property?

        PS: NVM I understand, so basically the default behavior of NT8 is to serialize all the public properties and deserialized all of them when reload, hence when NT8 try to serialize MyBrushSerialize, it calls its get function, and when it deserialized it calls its set function.
        Last edited by Curerious; 05-01-2025, 12:18 PM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        637 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        366 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        107 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        569 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        572 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X