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

Public List<double>

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

    Public List<double>

    Hello, I'm trying to make a list of historical swing highs available for use in other indicators using List<double>, however I'm getting serious problems with my code returning an empty List, any idea what's wrong with my code, the current code returns no errors.

    For reference, 'HighLow.prevHighs' is an example of the List that contains all previous swing highs (which works, I checked) and I want to copy it to the new List<double> 'highs' to easily reference it.

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class ASR2 : Indicator
    {
    private HighLow highlow;
    List<double> highs = new List<double>();
    List<double> lows = new List<double>();


    private double avgRes;
    private double avgSupp;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"";
    Name = "ASR2";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    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;
    Swing = 5;
    SwingsToStore = 5;
    AddPlot(Brushes.Green, "Resistance");
    AddPlot(Brushes.Red, "Support");
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    highlow = HighLow(Swing, SwingsToStore);
    highs = HighLow(Swing, SwingsToStore).prevHighs;
    lows = HighLow(Swing, SwingsToStore).prevLows;
    }
    }

    protected override void OnBarUpdate()
    {
    Resistance[0] = (highlow.CurrentHigh[0] + highs[0]) / (2);
    //Support[0] = (highlow.CurrentLow[0] + highlow.prevLows.Sum()) / (SwingsToStore + 1);
    }

    region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Swing", Order=1, GroupName="Parameters")]
    public int Swing
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Swings To Store", Order=2, GroupName="Parameters")]
    public int SwingsToStore
    { get; set; }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Resistance
    {
    get { return Values[0]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Support
    {
    get { return Values[1]; }
    }
    #endregion

    }
    }​

    #2
    Hello KDTrade,

    You are assigning

    highs = HighLow(Swing, SwingsToStore).prevHighs;

    When the script starts.

    This is not a reference to the object in the indicator this is a copy of the object. You have copied an empty list from the indicator which has not processed any data yet, to 'highs'. 'highs' now has an empty list. It will not be updated when the original list is updated as they are separate objects.

    That said, why not just use the original list? Copying this to another variable is redundant when the original list can already be accessed by a variable from the indicator.

    I would assign HighLow to a variable.

    In the scope of the class:
    Code:
    private HighLow myHighLow;
    In State.DataLoaded:
    Code:
    myHighLow = HighLow(Swing, SwingsToStore);
    In OnBarUpdate() where you need to access the values from the list:
    Code:
    for (int index = 0; index < myHighLow.prevHighs; index++)
    {
    Print(string.Format("myHighLow.prevHigh[{1}]: {2}", index, myHighLow.prevHigh[index]));
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      Here's a sample of the updated code

      Resistance[0] = (highlow.CurrentHigh[0] + highlow.prevHighs[0]) / (2);

      This code returns nothing on my chart despite there being no errors, any ideas are welcome.

      Thanks,
      KD

      Comment


        #4
        Never mind, I found the error in the code, it was an index out of range error.

        Comment


          #5
          Hi All, KDTrade, I came across the same type of error, "index out of range", how did you fix that? Thanks!

          Comment


            #6
            Hello grimmer01,

            Below is a link to a forum post on indexing errors.
            Hello, I want to create an indicator that show data in a chart but calculate in other charttime different to the time of the chart where is showed. For example:
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by BarzTrading, Today, 07:25 AM
            2 responses
            15 views
            1 like
            Last Post BarzTrading  
            Started by devatechnologies, 04-14-2024, 02:58 PM
            3 responses
            20 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by tkaboris, Today, 08:01 AM
            0 responses
            4 views
            0 likes
            Last Post tkaboris  
            Started by EB Worx, 04-04-2023, 02:34 AM
            7 responses
            162 views
            0 likes
            Last Post VFI26
            by VFI26
             
            Started by Mizzouman1, Today, 07:35 AM
            1 response
            10 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Working...
            X