• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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:

    https://docs.microsoft.com/en-us/dot...ramework-4.7.2

    https://ninjatrader.com/support/helpGuides/nt7/?add.htm

    https://ninjatrader.com/support/help...t7/?values.htm
    JoshNinjaTrader 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 Rmos22, Today, 02:20 PM
            0 responses
            2 views
            0 likes
            Last Post Rmos22
            by Rmos22
             
            Started by sawtooth500, Today, 12:59 PM
            1 response
            3 views
            0 likes
            Last Post NinjaTrader_Spencer  
            Started by Steve L, Yesterday, 09:36 PM
            1 response
            19 views
            0 likes
            Last Post NinjaTrader_Kate  
            Started by MichaelAlexander, 04-25-2017, 03:28 AM
            9 responses
            1,170 views
            0 likes
            Last Post dani neira  
            Started by uday12, Today, 11:34 AM
            2 responses
            9 views
            0 likes
            Last Post uday12
            by uday12
             
            Working...
            X