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

MarketAnalyzerColumns UnrealizedProfitLoss in Ticks script Background bug

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

    MarketAnalyzerColumns UnrealizedProfitLoss in Ticks script Background bug

    The FindResource("LongBackground") and FindResource("ShortBackground") display to the 1st row in addition to the correct row.

    Click image for larger version

Name:	wrong display.png
Views:	280
Size:	1.6 KB
ID:	1240892

    Expected result:

    The 1st row should not display the LongBackground or ShortBackground (only the row with the 3 value should display it).

    Can you reproduce the bug on your end?

    What fix do you suggest? thanks

    The script:

    //
    // Copyright (C) 2023, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //
    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.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    #endregion

    //This namespace holds Market Analyzer columns in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public class UnrealizedProfitLossInTicks : MarketAnalyzerColumn
    {
    private Cbi.Position position; // holds the position for the actual instrument

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    AccountName = DefaultAccountName;
    Description = NinjaTrader.Custom.Resource.NinjaScriptMarketAnaly zerColumnDescriptionUnrealizedProfitLoss;
    Name = "UnrealizedProfitLossInTicks";
    IsDataSeriesRequired = false;
    ShowInTotalRow = true;

    FormatDecimals = 0;
    }
    }

    protected override void OnConnectionStatusUpdate(Cbi.ConnectionStatusEvent Args connectionStatusUpdate)
    {
    BackColor = null; // reset color

    if (connectionStatusUpdate.Status == Cbi.ConnectionStatus.Connected && connectionStatusUpdate.PreviousStatus == Cbi.ConnectionStatus.Connecting)
    {
    Cbi.Account account = null;
    lock (connectionStatusUpdate.Connection.Accounts)
    account = connectionStatusUpdate.Connection.Accounts.FirstOr Default(o => o.DisplayName == AccountName);

    if (account != null)
    {
    lock (account.Positions)
    position = account.Positions.FirstOrDefault(o => o.Instrument.FullName == Instrument.FullName);
    }
    }
    else if (connectionStatusUpdate.Status == Cbi.ConnectionStatus.Disconnected && connectionStatusUpdate.PreviousStatus == Cbi.ConnectionStatus.Disconnecting)
    {
    if (position != null && position.Account.Connection == connectionStatusUpdate.Connection)
    {
    CurrentValue = 0;
    position = null;
    }
    }
    }

    protected override void OnMarketData(Data.MarketDataEventArgs marketDataUpdate)
    {
    CurrentValue = (position == null ? 0 : position.GetUnrealizedProfitLoss(Cbi.PerformanceUn it.Ticks));
    }

    protected override void OnPositionUpdate(Cbi.PositionEventArgs positionUpdate)
    {

    if (positionUpdate.Position.Account.DisplayName == AccountName && positionUpdate.Position.Instrument == Instrument)
    {
    position = (positionUpdate.Operation == Cbi.Operation.Remove ? null : positionUpdate.Position);
    CurrentValue = (position == null ? 0 : position.GetUnrealizedProfitLoss(Cbi.PerformanceUn it.Ticks));
    }
    }

    region Properties
    [NinjaScriptProperty]
    [TypeConverter(typeof(AccountDisplayNameConverter))]
    [Display(ResourceType = typeof(Resource), Name = "NinjaScriptColumnBaseAccount", GroupName = "NinjaScriptSetup", Order = 0)]
    public string AccountName
    { get; set; }
    #endregion

    region Miscellaneous

    public override string Format(double value)
    {
    if (CellConditions.Count == 0)
    BackColor = value == 0 ? null : value > 0 ? Application.Current.FindResource("LongBackground") as Brush : Application.Current.FindResource("ShortBackground" ) as Brush;

    return (value == 0 ? string.Empty : Core.Globals.FormatQuantity((long) value, false));
    }
    #endregion
    }
    }​



    #2
    NinjaTrader_ChelseaB Any luck taking a look at this issue? Thanks

    Comment


      #3
      Hello Cormick,

      As a tip, to export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
      1. Click Tools -> Export -> NinjaScript Add-on...
      2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
      3. Click the 'Export' button
      4. Enter a unique name for the file in the value for 'File name:'
      5. Choose a save location -> click Save
      6. Click OK to clear the export location message
      By default your exported file will be in the following location:
      • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
      Below is a link to the help guide on Exporting NinjaScripts.
      http://ninjatrader.com/support/helpG...-us/export.htm

      This will allow you to share a script for someone else to run, without having to add the text of an entire script to your post. Instead, you can add the text of just the single line (or few supporting lines) of code you like focused on.


      Regarding the resource dictionary keys 'LongBackground' and 'ShortBackground' these are in the BluePrint.xml skin file.

      <!-- Background colors for Flat, Long, or Short positions for the Position Display in order-entry windows -->
      <SolidColorBrush po:Freeze="true" x:Key="FlatBackground" Color="Black"/>
      <SolidColorBrush po:Freeze="true" x:Key="LongBackground" Color="#FF00D409"/>
      <SolidColorBrush po:Freeze="true" x:Key="ShortBackground" Color="#FFb70000"/>


      Are you asking about your own custom logic with nested ternary operations?:

      value == 0 ? null : value > 0 ? Application.Current.FindResource("LongBackground") as Brush : Application.Current.FindResource("ShortBackground" ) as Brush;

      Are you asking why this is returning null if the value is 0?

      Have you debugged your code by adding prints of each value and each section of the ternary operations separately?

      Can you provide the output from the prints?​​
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Hello Chelsea and thank you for the direction, much helpful.

        The zip in attachment.

        The few supporting lines:

        47-49
        Code:
                protected override void OnConnectionStatusUpdate(Cbi.ConnectionStatusEventArgs connectionStatusUpdate)
                {
                    BackColor    = null;        // reset color​
        96-105
        Code:
                #region Miscellaneous
        
                public override string Format(double value)
                {
                    if (CellConditions.Count == 0)
                        BackColor = value == null ? null : value == 0 ? null : value > 0 ? Application.Current.FindResource("LongBackground") as Brush : Application.Current.FindResource("ShortBackground") as Brush;
        
                    return (value == 0 ? string.Empty : Core.Globals.FormatQuantity((long) value, false));
                }
                #endregion​
        The problem is it colors the background of an additional row/cell when it shouldn't.
        For example, I have a position in row 4, with +3 ticks profit. It colors the background in green of the 4th row/cell with the 3 ticks input correctly. But in addition it also colors the 1st row/cell despite having no position in row 1.
        And all other rows (rows 2, 3, 5 etc.) aren't colored when no position which is as expected.

        The logic works so I'm not sure what to test for with prints.
        Can you please test on your end to see if you can reproduce the same issue?

        I copied the 2 above code snippets verbatim from built-in ninjascript PositionSize
        (
        namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
        {
        public class PositionSize : MarketAnalyzerColumn​
        )
        Attached Files

        Comment


          #5
          NinjaTrader_ChelseaB could you find time to replicate the issue? thanks

          Comment


            #6
            Hello Cormick,

            Reproducing the behavior on my end would not give more information about what is causing the behavior.

            The output from prints would be necessary to do any investigating, or to report any issue to our development team.


            However, as you have requested, I have run the script. It appears you are interested in the new code in post # 4, and you are no longer concerned about the code you posted in post # 1.



            Next, you will need to print all the values in the nested turnery from post # 1.

            Print if the variable named 'value' is null. If it is not null then ShortBackground is returned.

            If 'value' is null then you compare null to be greater than 0. Typically, I would suggest printing a label for 'value', the value of 'value', a label for greater than, and then 0. As this is what is being compared in the inner ternary condition, that is evaluated when 'value' is null.

            Note, your nested ternary cannot be null and greater than 0 at the same time. The inner ternary is only evaluated if 'value' is null. If 'value' is not null then the value from the resource dictionary with the key 'ShortBackground' is returned. If 'value' is null then you check if its greater than 0. Null cannot be greater than 0.


            Also, typically the BackColor would be assigned a value from OnMarketData() / OnPositionUpdate() and not from Format which is only intended to return a formatted string.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thank you for the video testing. Yes it does work in you video same as on my end, but testing with only 1 instrument begs the question.
              Would you please test as follow:
              0. Load 4 instruments into the Market Analyzer (same as you did but with 4 instruments instead of 1, for example, ES (1st row), GC (2nd row), NQ (3rd row), and ZB ( 4th row)).
              1. Add the Total row at the top, at row 0th.
              2. Then place a trade on the ZB/4th instrument (appearing in the 4th row in the Market Analyzer).
              3. Please confirm or infirm you're getting the same bug as in this demo:
              https://drive.google.com/file/d/1nP4...usp=share_link
              If you are getting it, please tip off the solution steps. thanks

              Comment


                #8
                Hello Cormick,

                Below is a link to a video of the test.


                The steps to a solution is to use prints as advised in post # 3 and post # 6..

                To move forward you will need to provide the output from the prints for me to assist with analyzing and understanding the behavior.

                If you are wanting to understand the behavior of the new script from post # 4 and not post # 1, print the values from the new script where BackColor is assigned.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  You did not test with added total row (right click Market Analyzer window > Properties > Show total row > tick check box > Ok).
                  I tested as you did without the total row row and the issue doesn't show. But again, that begs the question considering post 7 1. Add the Total row at the top, at row 0th. mention.

                  I need it to work with the Total row. Can you please test with the total Row enabled? So I know it's a platform issue or not before tinkering with prints? If it is a platform issue, can you fix it? If not, can you test again with total row row and offer the needed fix? thanks

                  Null or not, as long as a position is on on any instrument row, the 1st row (below the Total row row) gets background colored.
                  How am I supposed to print the 1st row?
                  Here's the test and prints of post 1 miscellaneous snippet:

                  Code:
                          #region Miscellaneous
                  
                          public override string Format(double value)
                          {
                              if (CellConditions.Count == 0)
                                  BackColor = value == 0 ? null : value > 0 ? Application.Current.FindResource("LongBackground") as Brush : Application.Current.FindResource("ShortBackground") as Brush;
                  
                                  Print("value : " + value +
                                          " BackColor : " + BackColor +
                                          " LongBackground : " + Application.Current.FindResource("LongBackground") +
                                          " ShortBackground : " + Application.Current.FindResource("ShortBackground") );
                  
                              return (value == 0 ? string.Empty : Core.Globals.FormatQuantity((long) value, false));
                          }
                          #endregion​
                  Code:
                  value : 0 BackColor :  LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0 BackColor :  LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0 BackColor :  LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0 BackColor :  LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : -0.999999999999091 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : -0.999999999999091 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : -0.999999999999091 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : -0.999999999999091 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : -0.999999999999091 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : -0.999999999999091 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0 BackColor :  LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0 BackColor :  LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0.999999999999091 BackColor : #FF00D409 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0.999999999999091 BackColor : #FF00D409 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0.999999999999091 BackColor : #FF00D409 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0.999999999999091 BackColor : #FF00D409 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0.999999999999091 BackColor : #FF00D409 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0.999999999999091 BackColor : #FF00D409 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0 BackColor :  LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0 BackColor :  LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0 BackColor :  LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : 0 BackColor :  LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : -2.00000000000045 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : -2.00000000000045 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : -2.00000000000045 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                  value : -2.00000000000045 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000​
                  Last edited by Cormick; 03-20-2023, 02:05 PM.

                  Comment


                    #10
                    Hello Cormick,

                    Below is a link to the video you have requested.


                    You will not be able to set the value or background of the total row.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Your video doesn't test it in order, again begging the question.
                      Take a look at this one and reproduce it as is in you next video.
                      https://drive.google.com/file/d/1ajG...usp=share_link

                      Then again please read what preceded thanks
                      I need it to work with the Total row. Can you please test with the total Row enabled? So I know it's a platform issue or not before tinkering with prints? If it is a platform issue, can you fix it? If not, can you test again with total row row and offer the needed fix? thanks

                      Null or not, as long as a position is on on any instrument row, the 1st row (below the Total row row) gets background colored.
                      How am I supposed to print the 1st row? (the ES row in the video above, right under the Total row).
                      How to get it not coloring the background when no position is on on the 1st row (the ES row in the video above, right under the Total row).
                      Last edited by Cormick; 03-20-2023, 02:26 PM.

                      Comment


                        #12
                        Hello Cormick,

                        Having me reproduce will not let you know if its a platform issue or not. I'm not quite certain why you are having me reproduce without doing any debugging, but if this somehow helps you I have done so.

                        Only the print output will inform what is going on. Even if there was a bug to be reported, we would still require the print output to report this.

                        To confirm, the instructions in post # 7 is to add ES, GC, NQ, and ZB.

                        Load 4 instruments into the Market Analyzer (same as you did but with 4 instruments instead of 1, for example, ES (1st row), GC (2nd row), NQ (3rd row), and ZB ( 4th row)).
                        I am attaching a screenshot of the to show this is the order the instruments are added.

                        This screenshot also shows the Total row is enabled in the video.

                        Click image for larger version  Name:	2023-03-20_14-01-16.png Views:	0 Size:	309.0 KB ID:	1241587

                        Apologies, the next instruction was to place an order only to ZB. I placed an order to each instrument instead of only ZB.

                        Then place a trade on the ZB/4th instrument (appearing in the 4th row in the Market Analyzer).
                        Below is a new video where I have only placed an order to ZB.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          You did reproduce it in your video, without begging the question, thank you.

                          The print output does not help, please see post 9 reproduced below
                          Code:
                          value : 0 BackColor : LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0 BackColor : LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0 BackColor : LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0 BackColor : LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : -0.999999999999091 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : -0.999999999999091 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : -0.999999999999091 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : -0.999999999999091 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : -0.999999999999091 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : -0.999999999999091 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0 BackColor : LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0 BackColor : LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0.999999999999091 BackColor : #FF00D409 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0.999999999999091 BackColor : #FF00D409 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0.999999999999091 BackColor : #FF00D409 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0.999999999999091 BackColor : #FF00D409 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0.999999999999091 BackColor : #FF00D409 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0.999999999999091 BackColor : #FF00D409 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0 BackColor : LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0 BackColor : LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0 BackColor : LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : 0 BackColor : LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : -2.00000000000045 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : -2.00000000000045 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : -2.00000000000045 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000
                          value : -2.00000000000045 BackColor : #FFB70000 LongBackground : #FF00D409 ShortBackground : #FFB70000​​
                          How to fix the bug of ES background coloring despite no ES position in your last video?

                          If you know of a way prints could shed light (beside the previously tested preceding suggestion), or if this issue is documented as it would make sense it to be, please do enlight.

                          Comment


                            #14
                            Hello Cormick,

                            The prints would be for the conditions in the ternary, not the colors.

                            Any time you want to know why a condition is evaluating as true or false, you print the values in the condition along with the comparison operators.

                            Below is a link to a forum post that demonstrates how to use prints to understand behavior.


                            Is value the expected value
                            s this matching CurrentValue?
                            Where are you setting CurrentValue?
                            Is this being set with the expected value?


                            Have you tried assigning the BackColor brush from OnMarketData() and OnPositionUpdate() as suggested in post # 6?
                            Does this also reproduce?
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              I did not request that. Please keep on the requested, not extraneous details.
                              The ternary operator statement has nothing to do with it, and the prints above show clearly the value with correct coloring so again begging the question.

                              Why not stop wasting time and start documenting the issues and commenting you built in scripts so we can use the needed methods without having to guess their syntax and semantic, do mindless trial and error with redundant debugging, or waste time with uncooperative or begin the question support interaction?

                              Please keep to the request without mindless digressive suggestion. thanks


                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by stafe, 04-15-2024, 08:34 PM
                              6 responses
                              31 views
                              0 likes
                              Last Post stafe
                              by stafe
                               
                              Started by adeelshahzad, Today, 03:54 AM
                              4 responses
                              26 views
                              0 likes
                              Last Post adeelshahzad  
                              Started by merzo, 06-25-2023, 02:19 AM
                              10 responses
                              823 views
                              1 like
                              Last Post NinjaTrader_ChristopherJ  
                              Started by frankthearm, Today, 09:08 AM
                              5 responses
                              17 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by jeronymite, 04-12-2024, 04:26 PM
                              3 responses
                              43 views
                              0 likes
                              Last Post jeronymite  
                              Working...
                              X