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

Counter crossing the horizontal line

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

    Counter crossing the horizontal line

    Hello All. I have a indicator on easylanguage. I want to implement it on ninjascript. In the picture of his project.



    Code:
    input: count(3);
    var: x(0), oldest(0), recycle(0), size(199);
    array: id[199,3](0);  // 0=id, 1=price, 2=count, 3=age
    
    once for x=0 to size begin
        id[x,0]=tl_new(d,t,0,d,t,0);
        tl_setextright (id[x,0], True);
    end;
    
    if barstatus(1)=2 then begin
        oldest=0;
        
        for x=0 to size begin
        
            if L<=id[x,1] and H>=id[x,1] then begin
            
                id[x,2]=id[x,2]+1;
                TL_SetColor(id[x,0],red);
                tl_setstyle(id[x,0],1);
                if id[x,2] = count-1 then 
                    begin 
                        TL_SetColor(id[x,0],blue);
                        tl_setstyle(id[x,0],3); 
                    end;
                if id[x,2]=count then begin
                
                    id[x,1]=0;  id[x,2]=0;  id[x,3]=9999999;  tl_setend(id[x,0],d,t,0);  tl_setbegin(id[x,0],d,t,0);  // reset price, count and age
                end;
                
            end;
            
            if id[x,1]>0 then tl_setend(id[x,0],d,t,id[x,1]);
            
            id[x,3]=id[x,3]+1;
            
            if id[x,3]>oldest then begin
            
            if id[x,2]= count-1 then TL_SetColor(id[x,0],blue);
                oldest=id[x,3];  recycle=x;
            end;
            
        end;
        
        id[recycle,1]=c;  id[recycle,2]=0;  id[recycle,3]=0;  tl_setend(id[recycle,0],d,t,c);  tl_setbegin(id[recycle,0],d,t,c);
    end;
    He draws a horizontal line on the closing price of each bar. The horizontal line is displayed until it intersects the price of a certain number of times.

    First, I tried to display a horizontal line at each price:
    Code:
    for (int i=1; i<777; i++)
                    {
                         DrawHorizontalLine("tag1"+CurrentBar,Close[i], Color.Red);
                    }
    I could not get.

    Please advise how I can do it, and how to work with the horizontal line tag, to appeal to her?

    Thank.
    Last edited by doitagainforme; 05-02-2012, 02:46 AM.

    #2
    doitagainforme,

    Are you trying to implement this in NinjaTrader? NinjaScript would be used here. Unfortunately easy language is quite a bit different here.

    This here should be all that is required in your OnBarUpdate() method :

    DrawHorizontalLine("tag1"+CurrentBar,Close[0], Color.Red);

    What you are currently doing with the for loop, is redrawing a different line with the same tag over and over. OnBarUpdate() is called for each historical bar on your chart, and for all future bars. CurrentBar would change each time OnBarUpdate() is called on historical and future bars.

    You may also want to add :

    if ( CurrentBar < 1 )
    {
    return;
    }

    ... to your OnBarUpdate().
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Thank you, Adam. I realized I ninjascript another concept. In easylang zero bar on the right. In ninjascript zero bar on the left?

      Sorry if my questions are primitive. I am Russian-speaking, I have to work with the machine translator.

      Comment


        #4
        doitagainforme,

        Essentially, Close[0] means "the most current bars close price", but this may be accessed the same way on a historical bar. When NinjaTrader is generating indicators, it uses something called a data series which adds elements for each subsequent bar. When you initially put a indicator on a chart, it starts with the very first bar in the past and iterates through adding the next bar to the data series each time it goes onto the next bar.

        If you try this code :

        Print("Close price : "+Close[0]+" Current Bar : "+CurrentBar+" Time : "+ToTime(Time[0]));

        You can see what I mean in the Tools > Output window.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          At the moment my brain was able to generate the code. It compiles, but does not bring results, draws only one line. Maybe I need another array of lines? I will be happy with any prompts.

          Code:
          protected override void OnBarUpdate()
                  {
          
                  int[] count=new int[5000];
                  DrawHorizontalLine("tag1"+CurrentBar,Close[0], Color.Red);
                  for (int i=0; i<5000; i++)
                      {
                          
                          if ((Close[0]<=High[i]) || (Close[0]>=Low[i]))
                          {
                              count[CurrentBar]=count[CurrentBar]++;
                              if (count[CurrentBar] == 3)
                              {
                                  RemoveDrawObject("tag1"+CurrentBar);
                              }
                          }
                          
                          
                          
                      }
                      
                  }
          Last edited by doitagainforme; 05-02-2012, 02:16 AM.

          Comment


            #6
            Hello doitagainfrome,
            Thanks for your post and I am replying for Adam.

            You can use the below code to check if Close is above the horizontal line or not.

            Code:
            //draw a line at previous close
            IHorizontalLine hLine1 = DrawHorizontalLine("hLine", Close[1],  Color.Green);
            				
            int i  = MRO(delegate{return Close[0] > hLine1.Y ;}, 3, 20);
            
            if (i >= 0) //if close greater than horizontal line
            {
            	//do something
            }


            Please let me know if I can assist you any further.
            Last edited by NinjaTrader_Joydeep; 05-02-2012, 05:08 AM.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              anahronist with bigmiketrading forum, made ​​me this indicator

              Code:
              [COLOR=#000000][COLOR=#FF8000]#region Using declarations
              // add this
              [/COLOR][COLOR=#0000BB]using System[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Collections[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Generic[/COLOR][COLOR=#007700]; [/COLOR][COLOR=#FF8000]// needed for List class
              #endregion
              
              //...
              
                      #region Variables
                      // parameters
                      [/COLOR][COLOR=#007700]private [/COLOR][COLOR=#0000BB]int maxcount [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]3[/COLOR][COLOR=#007700]; [/COLOR][COLOR=#FF8000]// Default setting for Count
                      // other
                      [/COLOR][COLOR=#007700]private class [/COLOR][COLOR=#0000BB]linedata [/COLOR][COLOR=#007700]{
                          public [/COLOR][COLOR=#0000BB]string id[/COLOR][COLOR=#007700];
                          public [/COLOR][COLOR=#0000BB]int count[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]startbar[/COLOR][COLOR=#007700];
                          public [/COLOR][COLOR=#0000BB]double value[/COLOR][COLOR=#007700];
                          public [/COLOR][COLOR=#0000BB]linedata[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]int currentbar[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]double val[/COLOR][COLOR=#007700]) {
                              [/COLOR][COLOR=#0000BB]id [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#DD0000]"ln"[/COLOR][COLOR=#007700]+[/COLOR][COLOR=#0000BB]currentbar[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]ToString[/COLOR][COLOR=#007700]();
                              [/COLOR][COLOR=#0000BB]startbar [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]currentbar [/COLOR][COLOR=#007700]- [/COLOR][COLOR=#0000BB]1[/COLOR][COLOR=#007700];
                              [/COLOR][COLOR=#0000BB]value [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]val[/COLOR][COLOR=#007700];
                              [/COLOR][COLOR=#0000BB]count [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]0[/COLOR][COLOR=#007700];
                          }
                      }
                      private List<[/COLOR][COLOR=#0000BB]linedata[/COLOR][COLOR=#007700]> [/COLOR][COLOR=#0000BB]lines[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]to_remove[/COLOR][COLOR=#007700];
                      [/COLOR][COLOR=#FF8000]#endregion
              
                      /// <summary>
                      /// This method is used to configure the indicator and is called once before any bar data is loaded.
                      /// </summary>
                      [/COLOR][COLOR=#007700]protected [/COLOR][COLOR=#0000BB]override void Initialize[/COLOR][COLOR=#007700]()
                      {
                          [/COLOR][COLOR=#0000BB]lines [/COLOR][COLOR=#007700]= new List<[/COLOR][COLOR=#0000BB]linedata[/COLOR][COLOR=#007700]>();
                          [/COLOR][COLOR=#0000BB]to_remove [/COLOR][COLOR=#007700]= new List<[/COLOR][COLOR=#0000BB]linedata[/COLOR][COLOR=#007700]>();
                          [/COLOR][COLOR=#0000BB]Overlay [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]true[/COLOR][COLOR=#007700];
                          [/COLOR][COLOR=#0000BB]CalculateOnBarClose [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]true[/COLOR][COLOR=#007700];
                      }
              
                      [/COLOR][COLOR=#FF8000]/// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      [/COLOR][COLOR=#007700]protected [/COLOR][COLOR=#0000BB]override void OnBarUpdate[/COLOR][COLOR=#007700]()
                      {
                          if ([/COLOR][COLOR=#0000BB]CurrentBar [/COLOR][COLOR=#007700]== [/COLOR][COLOR=#0000BB]0[/COLOR][COLOR=#007700]) return;
                          [/COLOR][COLOR=#0000BB]lines[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Add[/COLOR][COLOR=#007700](new [/COLOR][COLOR=#0000BB]linedata[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]CurrentBar[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]Close[/COLOR][COLOR=#007700][[/COLOR][COLOR=#0000BB]1[/COLOR][COLOR=#007700]]));
                          [/COLOR][COLOR=#0000BB]to_remove[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Clear[/COLOR][COLOR=#007700]();
                          foreach([/COLOR][COLOR=#0000BB]linedata d in lines[/COLOR][COLOR=#007700]) {
                              if ([/COLOR][COLOR=#0000BB]d[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]value [/COLOR][COLOR=#007700]>= [/COLOR][COLOR=#0000BB]Low[/COLOR][COLOR=#007700][[/COLOR][COLOR=#0000BB]0[/COLOR][COLOR=#007700]] && [/COLOR][COLOR=#0000BB]d[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]value [/COLOR][COLOR=#007700]<= [/COLOR][COLOR=#0000BB]High[/COLOR][COLOR=#007700][[/COLOR][COLOR=#0000BB]0[/COLOR][COLOR=#007700]])
                                  ++[/COLOR][COLOR=#0000BB]d[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]count[/COLOR][COLOR=#007700];
                              if ([/COLOR][COLOR=#0000BB]d[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]count [/COLOR][COLOR=#007700]<= [/COLOR][COLOR=#0000BB]maxcount[/COLOR][COLOR=#007700])
                                  [/COLOR][COLOR=#0000BB]DrawLine[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]d[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]id[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]false[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]CurrentBar[/COLOR][COLOR=#007700]-[/COLOR][COLOR=#0000BB]d[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]startbar[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]d[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]value[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]0[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]d[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]value[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]Color[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Red[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]DashStyle[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Solid[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]1[/COLOR][COLOR=#007700]);
                              if ([/COLOR][COLOR=#0000BB]d[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]count [/COLOR][COLOR=#007700]>= [/COLOR][COLOR=#0000BB]maxcount[/COLOR][COLOR=#007700]) [/COLOR][COLOR=#FF8000]// we can stop extending this line
                                  [/COLOR][COLOR=#0000BB]to_remove[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Add[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]d[/COLOR][COLOR=#007700]);    [/COLOR][COLOR=#FF8000]// add line to removal queue
                          [/COLOR][COLOR=#007700]}
                          foreach([/COLOR][COLOR=#0000BB]linedata d in to_remove[/COLOR][COLOR=#007700]) [/COLOR][COLOR=#FF8000]// empty the removal queue
                              [/COLOR][COLOR=#0000BB]lines[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Remove[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]d[/COLOR][COLOR=#007700]);
                      }  
              [/COLOR][COLOR=#0000BB][/COLOR] [/COLOR]

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Gerik, Today, 09:40 AM
              2 responses
              7 views
              0 likes
              Last Post Gerik
              by Gerik
               
              Started by RookieTrader, Today, 09:37 AM
              2 responses
              11 views
              0 likes
              Last Post RookieTrader  
              Started by alifarahani, Today, 09:40 AM
              1 response
              7 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by KennyK, 05-29-2017, 02:02 AM
              3 responses
              1,285 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Started by AttiM, 02-14-2024, 05:20 PM
              11 responses
              186 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Working...
              X