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

Ray line programmer

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

    Ray line programmer

    Hi,

    I'm looking to draw a ray line on the high, between a specific time frame every day. Can anyone please recommend me someone who can create it, since there's big selection on the ninjatrader script consultants. Thanks

    #2
    Thank you for your question Bdg820. The code you are describing is simple enough we can create an educational sample out of two pieces of code in the help guide.

    On the ToTime page we find

    Code:
    [FONT=Courier New]
    // http://ninjatrader.com/support/helpGuides/nt8/en-us/totime.htm
    // Only trade between 7:45 AM and 1:45 PM
    if (ToTime(Time[0]) >= 74500 && ToTime(Time[0]) <= 134500)
    {
        // Strategy logic goes here
    }[/FONT]
    In Draw.Ray we find

    Code:
    [FONT=Courier New]
    // http://ninjatrader.com/support/helpGuides/nt8/en-us/draw_ray.htm
    // Draws a lime green ray from 10 bars back through the current bar
    Draw.Ray(this, "tag1", 10, 1000, 0, 1001, Brushes.LimeGreen);[/FONT]
    Putting our Draw.Ray code inside our time filter code gives us

    Code:
    [FONT=Courier New]
    if (ToTime(Time[0]) >= 74500 && ToTime(Time[0]) <= 134500)
    {
        Draw.Ray(this, "tag1", 10, 1000, 0, 1001, Brushes.LimeGreen);
    }[/FONT]
    Once we have this, the next step will be to set up input times. We can generate time input code with the New -> Strategy Builder, on the Inputs and Variables page, by pressing Add in the Inputs section. I have done so and have received the following example code when I later unlocked my code.


    Code:
    [FONT=Courier New]        #region Properties
            [NinjaScriptProperty]
            [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKey")]
            [Display(ResourceType = typeof(Custom.Resource), Name="StartTime", Order=1, GroupName="NinjaScriptStrategyParameters")]
            public DateTime StartTime
            { get; set; }
    
            [NinjaScriptProperty]
            [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKey")]
            [Display(ResourceType = typeof(Custom.Resource), Name="EndTime", Order=2, GroupName="NinjaScriptStrategyParameters")]
            public DateTime EndTime
            { get; set; }
            #endregion[/FONT]

    Finally, we can use these variables in our code sample from earlier :


    Code:
    [FONT=Courier New]if (ToTime(Time[0]) >= [B]ToTime(StartTime)[/B] && ToTime(Time[0]) <= [B]ToTime(EndTime) && StartTime < EndTime[/B])
    {[/FONT]
    [FONT=Courier New][B]    // http://ninjatrader.com/support/helpGuides/nt8/en-us/getbar.htm[/B]
    [/FONT]
    [FONT=Courier New][B]    // Draw.Ray(NinjaScriptBase owner, string tag, DateTime startTime, double startY, DateTime endTime, double endY, Brush brush)[/B][/FONT]
    [FONT=Courier New]    Draw.Ray(this, "tag1", [B]StartTime[/B], [B]High[CurrentBar - Bars.GetBar(StartTime)][/B], [B]EndTime[/B], [B]High[CurrentBar - Bars.GetBar(EndTime)][/B], Brushes.LimeGreen);
    }[/FONT]

    Please let us know if there are any questions we can answer, or if any questions come up using this educational sample in your own code.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hi Jessica,

      Is it possible for you to send me the code as a attachment for the Ray line with time. Thanks

      Comment


        #4
        While the code I gave you previously is more flexible, since you would like a complete code solution, I must provide a context for this code, and so I needed to make the following modifications to, for example, make sure all our indexes were valid in both historical and live processing, and so you'd see results right away if you tried to run this. I recommend using the solution provided in the post rather than that in the provided sample for this reason in your own code. However, this will contain additional information on setting up custom DateTime objects so that the year, month, and day match the currently processed ymd, while still using a user specified hour, minute, and second (hms).

        Code:
        [FONT=Courier New]
                protected override void OnBarUpdate()
                {[B]
                    int StartBar = [/B]CurrentBar - Bars.GetBar ([B] new DateTime (
                        Time[0].Year, Time[0].Month, Time[0].Day
                        ,StartTime.Hour, StartTime.Minute, StartTime.Second
                        )[/B])[B];
                    int EndBar   = [/B]CurrentBar - Bars.GetBar ([B] new DateTime (
                        Time[0].Year, Time[0].Month, Time[0].Day
                        ,EndTime.Hour,   EndTime.Minute,   EndTime.Second
                        )[/B])[B];
        
                    if (StartBar < 0)
                    {
                        StartBar = 0;
                    }
                    if (EndBar < 0)
                    {
                        EndBar = 0;
                    }[/B]
        
                    // http://ninjatrader.com/support/helpGuides/nt8/en-us/totime.htm
                    if (ToTime(Time[0]) >= ToTime(StartTime) && ToTime(Time[0]) <= ToTime(EndTime) && StartTime < EndTime)
                    {
                        // http://ninjatrader.com/support/helpGuides/nt8/en-us/draw_ray.htm
                        // http://ninjatrader.com/support/helpGuides/nt8/en-us/getbar.htm
                        // Draw.Ray(NinjaScriptBase owner, string tag, DateTime startTime, double startY, DateTime endTime, double endY, Brush brush)
                        Draw.Ray(this, "tag1"[B] + CurrentBar[/B], [B]StartBar[/B], High[[B]StartBar[/B]], [B]EndBar[/B], High[[B]EndBar[/B]], Brushes.LimeGreen);
                    }
                }[/FONT]
        Code samples we provide are for educational purposes, and are not intended for live trading, and are not guaranteed to accomplish any user goal or to be maintained.
        Attached Files
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Hi Jessica,

          Thanks for the code, but I'm having problems with it, it's plotting a lot of Ray lines, is it possible for it to plot only 1 high horizontal line between TWO timeframes. Thanks

          Comment


            #6
            While this is possible Bdg820, code samples we provide are for educational purposes, and are not intended for live trading, and are not guaranteed to accomplish any user goal or to be maintained. Since the provided samples already demonstrates all the concepts needed to create the indicator you described, and it is easy to modify the code to meet your exact specifications, I will leave this code up so that a member of the community can assist further if they so choose. Please let us know if there are any other ways we can help.
            Jessica P.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by sidlercom80, 10-28-2023, 08:49 AM
            170 responses
            2,271 views
            0 likes
            Last Post sidlercom80  
            Started by Irukandji, Yesterday, 02:53 AM
            2 responses
            17 views
            0 likes
            Last Post Irukandji  
            Started by adeelshahzad, Today, 03:54 AM
            0 responses
            3 views
            0 likes
            Last Post adeelshahzad  
            Started by CortexZenUSA, Today, 12:53 AM
            0 responses
            3 views
            0 likes
            Last Post CortexZenUSA  
            Started by CortexZenUSA, Today, 12:46 AM
            0 responses
            1 view
            0 likes
            Last Post CortexZenUSA  
            Working...
            X