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

Function returning multiple values to OnBarUpdate()

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

    Function returning multiple values to OnBarUpdate()

    Hi i have a function returning a DataSerie Type (i.e PriceP4[0]) to the OnBarUpdate() but i would like to be able to return multiple DataSeries from that function.
    Here below some code:

    Code:
    private DataSeries StikePattern(DataSeries StrikeId, int BarID, string Label, double phase1a,double phase1b, Color Col, Color Area) {                                
    
         PriceP4.Set(0);
         PriceP5.Set(0);
    
         if(some logic){  
              PriceP4.Set(1);
              PriceP5.Set(1);
         }
         return PriceP4;  //AND I want to return PriceP5 also
    }
    
    protected override void OnBarUpdate(){
    
          StikePattern(Barracuda, BarNbID, "Signal", 0.350, 1.620, Color.Lime, Color.Red)
    
          ////This function returns PriceP4 for further logic in the OnBarUpdate.
         //// How can i also get PriceP5????
    }

    #2
    Hello cbadr,
    Thanks for your post.

    I believe the easiest way to do this in NinjaTrader 7 will be to store your two price values in a data series array and then return the array. For simplicity I also think it would be easier to use the Values array to set your prices. Something like the following snippet would return both PriceP4 and PriceP5 and then print their values from OnbarUpdate()

    Code:
    protected override void Initialize()
    {
        Add(new Plot(Color.Blue, "PriceP4Plot"));
        Add(new Plot(Color.Blue, "PriceP5Plot"));
    }
    
    private DataSeries[] StikePattern() 
    {                                
        Values[0].Set(4);//This is PriceP4
        Values[1].Set(5);//This is PriceP5
        DataSeries[] myDataSeriesArray = new DataSeries[2] { Values[0], Values[1] };
        return myDataSeriesArray; 
    }
    
    protected override void OnBarUpdate()
    {
        DataSeries[] a =  StikePattern();
        Print("PriceP4 value : "+ a[0][0] );
        Print("PriceP5 value : "+ a[1][0] ;
    }

    Please see the following public links for more information on the concepts I used here:

    Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays in the common language runtime.




    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Great thanks!

      Comment


        #4
        Originally posted by cbadr View Post
        Hi i have a function returning a DataSerie Type (i.e PriceP4[0]) to the OnBarUpdate() but i would like to be able to return multiple DataSeries from that function.
        Here below some code:

        Code:
        private DataSeries StikePattern(DataSeries StrikeId, int BarID, string Label, double phase1a,double phase1b, Color Col, Color Area) {
        
        PriceP4.Set(0);
        PriceP5.Set(0);
        
        if(some logic){
        PriceP4.Set(1);
        PriceP5.Set(1);
        }
        return PriceP4; //AND I want to return PriceP5 also
        }
        
        protected override void OnBarUpdate(){
        
        StikePattern(Barracuda, BarNbID, "Signal", 0.350, 1.620, Color.Lime, Color.Red)
        
        ////This function returns PriceP4 for further logic in the OnBarUpdate.
        //// How can i also get PriceP5????
        }
        Why return anything at all?
        After calling StrikePattern just access PriceP4[0] and PriceP5[0] to get the values you've 'Set' into them.

        Comment


          #5
          Well the problem actually came from the fact that i don't want DataSeries type that will attribute a value for each bar.
          I wanted to extract just "SOME" values. At the end i move toward the List<T> using System.Collections.Generic.
          Very handy to handle variable rather than Arrays of objects or DataSeries.
          Something like that below.
          I'm still struggling with the logic and the coding but the idea is there.

          If you know a better way to improve this coding please share !!

          Code:
          private List<DataList> lists;
          
          protected override void Initialize(){
          lists     = new List<DataList>();
           }
          public class DataList
                  {
                      public int ip1    {get;set;}
                      public int ip2    {get;set;}
          
                      public double y1    {get;set;}
                      public double y2    {get;set;}
          
                      public string StrikeId {get;set;}
                  }
          
          private List<DataList> StrikeFunction (String txt, DataSeries StrikeId){
          
          <<SOME LOGIC>>
          
          if(lists.Exists(x => x.StrikeId.Contains(StrikeName))==false){
                                                  lists.Add(new DataList()     
                                                      {ip1 = P1, ip2 = P2,
                                                      y1 = High[P1], y2 = Low[P2],
                                                      StrikeId= StrikeName});
                                              }
                                              else{
                                                  idx = lists.FindIndex(x => x.StrikeId.Contains(StrikeName));
                                                  DataList Dlist = lists[idx];
          
                                                  Dlist.ip1 = P1;
                                                  Dlist.ip2 = P2;
                                                  Dlist.y1 = High[P1];
                                                  Dlist.y2 = Low[P2];
                                              }
          
                                              StrikeId.Set(1);
                                              }
          
          private List<DataList> UpdateFunction (String txt, DataSeries StrikeId, List<DataList> lists){
                      StrikeName = StrikeId.ToString();
          
                      if(lists.Exists(x => x.StrikeId.Contains(StrikeName))==true){
          
                          idx = lists.FindIndex(x => x.StrikeId.Contains(StrikeName));
                          DataList Dlist = lists[idx];
          
                          if (Low[0]>=Dlist.y4 && High[0]<Dlist.y5){
                                  StrikeId.Set(1);
                          }
                          if (Low[0]<Dlist.y4 ){
                              StrikeId.Set(0);
          //                  CallFunction(txt,StrikeId);
                          }    
                          if (High[1]>=Dlist.y5){
                              StrikeId.Set(0);
          //                  CallFunction(txt,StrikeId);
                          }
                      }
                      return lists;
          
                  }

          Comment


            #6
            I just realised an issue here.
            How do i get the string name for a Dataseries??
            BecauseStrikeName = StrikeId.ToString(); in the example above, returns the value not the Name "StrikeId"...

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by yertle, Today, 08:38 AM
            5 responses
            14 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by frankthearm, Today, 09:08 AM
            2 responses
            4 views
            0 likes
            Last Post frankthearm  
            Started by adeelshahzad, Today, 03:54 AM
            3 responses
            16 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by bill2023, Yesterday, 08:51 AM
            6 responses
            27 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by NinjaTrader_ChelseaB, 01-08-2017, 06:59 PM
            80 responses
            19,667 views
            5 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X