Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Creating two or more custom brushes

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

    Creating two or more custom brushes

    Dear support,

    I created a new strategy with two conditions. If condition 1 is true than Backbrush.Green and if condition 2 is true than Backbrush.Red. Both conditions at the same time can`t be true. So I tried:

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 161)
    return;

    if (condition 1)
    {
    BackBrush = Brushes.Green;
    }
    if (condition 2)
    {
    BackBrush = Brushes.Red;
    }
    }

    And it worked fine. Now I want to change the opacity. So I tried:

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 161)
    return;

    if (condition 1)
    {
    BackBrush = new SolidColorBrush(Colors.Green) {Opacity = 0.25};
    BackBrush.Freeze();
    }
    if (condition 2)
    {
    BackBrush = new SolidColorBrush(Colors.Red) {Opacity = 0.25};
    BackBrush.Freeze();
    }
    }

    But this don't work. If have only one color. How can I create two different custom brushes and use them?
    Last edited by Andreas189; 09-06-2021, 06:20 AM.

    #2
    Hello Andreas189,

    Thanks for your post.

    The brush is likely already in a frozen state, we get an error when trying to assign an unfrozen brush that it is in a read only state.

    Creating a new brush and assigning it is working for me:

    Code:
    Brush MyBrush = new SolidColorBrush(Colors.Green);
    MyBrush.Opacity = 0.25;
    MyBrush.Freeze();
    BackBrush = MyBrush;
    Let me know if there is anything else we can do to help.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim,

      thank you for your help. I tried you recommendation.

      protected override void OnBarUpdate()
      {
      if (case1)
      Brush MyBrushr = new SolidColorBrush(Colors.Red) {Opacity = 0.25};
      MyBrushr.Freeze();
      BackBrush = MyBrushr;

      if(case2)
      Brush MyBrushg = new SolidColorBrush(Colors.Green) {Opacity = 0.25};
      MyBrushg.Freeze();
      BackBrush = MyBrushg;
      }

      No error message but in in both cases the bars are red. The strategy can't make a different between the both custom brushes.

      Comment


        #4
        Hello Andreas189,

        Are you using curly braces here? Is the script reloaded after it has been recompiled? The first line after your condition may be controlled by the condition, but the other lines would be hit regardless if the condition is true/false if you are not using curly braces there.

        The below gives me the attached result:

        Code:
        if (Close[0] > Open[0])
        {
            Brush MyBrushr = new SolidColorBrush(Colors.Red) {Opacity = 0.25};
            MyBrushr.Freeze();
            BackBrush = MyBrushr;
        }
        
        if(Open[0] > Close[0])
        {
            Brush MyBrushg = new SolidColorBrush(Colors.Green) {Opacity = 0.25};
            MyBrushg.Freeze();
            BackBrush = MyBrushg;
        }
        Attached Files
        JimNinjaTrader Customer Service

        Comment


          #5
          Hi Jim,
          sorry, I forgot the curly braces in my post but not in the script. The script is reloaded. I tried your code in my strategy but I get only one color. I also created a new indicator with this complete code:

          #region Using declarations
          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.ComponentModel.DataAnnotations;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
          using System.Windows;
          using System.Windows.Input;
          using System.Windows.Media;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Gui;
          using NinjaTrader.Gui.Chart;
          using NinjaTrader.Gui.SuperDom;
          using NinjaTrader.Gui.Tools;
          using NinjaTrader.Data;
          using NinjaTrader.NinjaScript;
          using NinjaTrader.Core.FloatingPoint;
          using NinjaTrader.NinjaScript.DrawingTools;
          #endregion

          //This namespace holds Indicators in this folder and is required. Do not change it.
          namespace NinjaTrader.NinjaScript.Indicators
          {
          public class MyCustomIndicator : Indicator
          {
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Geben Sie hier die Beschreibung für die neue benutzerdefinierte Indikator.";
          Name = "MyCustomIndicator";
          Calculate = Calculate.OnBarClose;
          IsOverlay = false;
          DisplayInDataBox = true;
          DrawOnPricePanel = true;
          DrawHorizontalGridLines = true;
          DrawVerticalGridLines = true;
          PaintPriceMarkers = true;
          ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
          //Disable this property if your indicator requires custom values that cumulate with each new market data event.
          //See Help Guide for additional information.
          IsSuspendedWhileInactive = true;
          }
          else if (State == State.Configure)
          {
          }
          }

          protected override void OnBarUpdate()
          {
          if (Close[0] > Open[0])
          {
          Brush MyBrushr = new SolidColorBrush(Colors.Red) {Opacity = 0.25};
          MyBrushr.Freeze();
          BackBrush = MyBrushr;
          }

          if(Open[0] > Close[0])
          {
          Brush MyBrushg = new SolidColorBrush(Colors.Green) {Opacity = 0.25};
          MyBrushg.Freeze();
          BackBrush = MyBrushg;

          }
          }
          }
          }

          #region NinjaScript generated code. Neither change nor remove.

          namespace NinjaTrader.NinjaScript.Indicators
          {
          public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
          {
          private MyCustomIndicator[] cacheMyCustomIndicator;
          public MyCustomIndicator MyCustomIndicator()
          {
          return MyCustomIndicator(Input);
          }

          public MyCustomIndicator MyCustomIndicator(ISeries<double> input)
          {
          if (cacheMyCustomIndicator != null)
          for (int idx = 0; idx < cacheMyCustomIndicator.Length; idx++)
          if (cacheMyCustomIndicator[idx] != null && cacheMyCustomIndicator[idx].EqualsInput(input))
          return cacheMyCustomIndicator[idx];
          return CacheIndicator<MyCustomIndicator>(new MyCustomIndicator(), input, ref cacheMyCustomIndicator);
          }
          }
          }

          namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
          {
          public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
          {
          public Indicators.MyCustomIndicator MyCustomIndicator()
          {
          return indicator.MyCustomIndicator(Input);
          }

          public Indicators.MyCustomIndicator MyCustomIndicator(ISeries<double> input )
          {
          return indicator.MyCustomIndicator(input);
          }
          }
          }

          namespace NinjaTrader.NinjaScript.Strategies
          {
          public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
          {
          public Indicators.MyCustomIndicator MyCustomIndicator()
          {
          return indicator.MyCustomIndicator(Input);
          }

          public Indicators.MyCustomIndicator MyCustomIndicator(ISeries<double> input )
          {
          return indicator.MyCustomIndicator(input);
          }
          }
          }

          #endregion

          And you can see the result below. I have no idea where the mistake is.
          Attached Files
          Last edited by Andreas189; 09-07-2021, 12:09 AM.

          Comment


            #6
            Hello Andreas189,

            I do not get this result, and I see both colors on my chart testing the code given.

            I have attached an export of my indicator. Could you test setting this up in a clean environment?

            Clean Environment Test:

            Creating a clean environment can be done by following the steps below:
            1. Close NinjaTrader 8, and rename the "NinjaTrader 8" folder in My Documents to something like: "NinjaTrader 8 OLD" Do not delete this folder.
            2. Uninstall NinjaTrader from the Windows Control Panel
            3. Delete the C:\Program Files (x86)\NinjaTrader 8 folder
            4. Reinstall using the installer from http://ninjatrader.com/PlatformDirect
            5. Add and test the attached indicator
            If you ever need to switch back to your original platform, you may do so by closing NinjaTrader and swapping the platform folder names.

            For example, Close NinjaTrader and rename the new "NinjaTrader 8" folder to "NinjaTrader 8 NEW" and the "NinjaTrader 8 OLD" folder to "NinjaTrader 8." Then restart the platform. Simply put: NinjaTrader 8 will always load the "NinjaTrader 8" folder in My Documents.
            Attached Files
            JimNinjaTrader Customer Service

            Comment


              #7
              Hi Jim,

              I'm so confused. I created a new clean environment as you recommended. And your TestForAndreas and my strategy worked perfect. Both colors were shown in the chart. After creation of a new chart the error happend again. Your script and my shows only one color.

              Comment


                #8
                Hello Andreas189,

                Could you reach out to me at scriptingsupport [at] ninjatrader [dot] com with the text "Attn Jim 3251515" and include a link to this thread?

                I would like to schedule a remote support session with you to observe this happening in the clean environment after making a new chart.

                I am available to schedule a call as early as 9AM EST and as late as 4:30PM EST. Please also let me know a time that works as well as a phone number I can use to reach you.
                Last edited by NinjaTrader_Jim; 09-20-2021, 07:28 AM.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Hi Jim,

                  sorry for my late feedback. I spent so much time on figure out why this error happend. I have installed a new VM with Windows 10 and installed a new Ninjatrader. First it worked fine and than the same error happend again. But if think this is weird, wait for this.
                  It depent on which timeframe the chart is. On 7 minutes chart or higher your testindicator works absolut normal. And my strategy works on 19 minutes chart and higher. Below there is only one color.
                  But when I change the count of days to load, the timeframe change. With only 31 days to load my strategy works in a 1 minute chart. And 80 days your indicator works in a 1 minute chart. Where is the connection?

                  Comment


                    #10
                    Hello Andreas189,

                    I will really need to see the test indicator in action when the issue comes up as I am not able to reproduce any issues with my test script, and the data series settings listed are not making a difference for me on my end.

                    I have received your email and am trying to schedule a time for us to get connected. Please reach back to me over email with a time to schedule and connect with you.

                    I would just ask that if your PC and platform are in another language than English, that we switch the language to English so I may better navigate the PC and platform for any necessary troubleshooting.


                    JimNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by set2win, 08-04-2021, 09:23 AM
                    39 responses
                    1,000 views
                    0 likes
                    Last Post WaleeTheRobot  
                    Started by md4866, Today, 08:15 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post md4866
                    by md4866
                     
                    Started by mjbatts91, Yesterday, 04:48 PM
                    2 responses
                    23 views
                    0 likes
                    Last Post mjbatts91  
                    Started by pibrew, Today, 06:10 PM
                    1 response
                    19 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by actualfacts.2021, 07-25-2021, 03:11 PM
                    8 responses
                    1,187 views
                    0 likes
                    Last Post linkcou
                    by linkcou
                     
                    Working...
                    X