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

Opening price strategy

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

    Opening price strategy

    Hello, Im trying to develop a simple strategy, and I need the enter to be above the opening price of the day, (9:30, not the open session price). The indicator that comes with ninja (current day OHL, doesn't display the 9:30 price, so I downloaded the OpeningPrice indicator, but is not working well when adding it to the strategy.

    I decide to make my own indicator so I went to GhatGPT for some help.
    The indicator it gave me is this one:


    Code:
    using System;
    using System.ComponentModel;
    using System.Windows.Media;
    using System.Windows.Shapes;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.NinjaScript.Indicators;
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
    [Description("Displays a horizontal line at the price of the instrument at 9:30 am")]
    public class OpeningPriceIndicator : Indicator
    {
    private double openingPrice;
    private System.Windows.Shapes.Line line;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Displays a horizontal line at the price of the instrument at 9:30 am";
    Name = "OpeningPriceIndicator";
    IsOverlay = true;
    line = new System.Windows.Shapes.Line
    {
    Stroke = Brushes.White,
    StrokeThickness = 2,
    StrokeDashArray = new DoubleCollection(new double[] { 4, 3 }),
    };
    AddChartObject(line);
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (Bars == null || CurrentBar < 1)
    return;
    
    if (Time[0].TimeOfDay == new TimeSpan(9, 30, 0))
    {
    openingPrice = Open[0];
    line.Y1 = openingPrice;
    line.Y2 = openingPrice;
    }
    }
    
    private void AddChartObject(ChartControl chartControl, ChartPanel chartPanel, ChartScale chartScale, ChartAnchor chartAnchor, object chartObject)
    {
    chartControl.Dispatcher.InvokeAsync(() => chartControl.AddChartObject(chartPanel, chartScale, chartAnchor, chartObject));
    }
    }
    }

    When I try to compile it, the following error appears: "The type or namespace name 'ChartAnchor' could not be found"

    ChatGPT suggest the following:


    "I apologize for the continued issues. In this case, I believe the issue may be related to the version of NinjaTrader you're using.

    In some versions of NinjaTrader, ChartAnchor is defined in the NinjaTrader.Gui.Chart namespace. In other versions, ChartAnchor is defined in the NinjaTrader.NinjaScript namespace.

    To work around this issue, you can try using the ChartAnchor enum directly without the namespace. This should work for any version of NinjaTrader.

    Please try replacing the AddChartObject method with the following:

    csharpCopy code
    private void AddChartObject(ChartControl chartControl, ChartPanel chartPanel, ChartScale chartScale, ChartAnchor chartAnchor, object chartObject) { chartControl.Dispatcher.InvokeAsync(() => chartControl.AddChartObject(chartPanel, chartScale, (int)chartAnchor, chartObject)); }

    This should resolve the issue and allow you to compile the indicator. If you're still having issues, please let me know the specific error message you're seeing, and I'll do my best to help you resolve it."

    I did it but the problem remains. Do you have any solution for this code error?. Or a way to solve the strategy issue? I just need to buy below 9:30 price and sell above it.

    Thanks!


    #2
    Hello, thanks for writing in. You can read the Time value by using the Time[0] array e.g.

    if (Times[0][0].TimeOfDay == new TimeSpan(9, 30, 0))
    {
    //Place orders at 9:30
    }​

    Your account can not be in a long and short position at the same time, so the code would need to make a decision to either place a buy or a sell order on either side of the market, but not both. The managed approach will not allow a buy and a sell order on either side of the market (See the Order handling rules)


    Kind regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick response. Is there a way that that can be done with the strategy builder?

      Comment


        #4
        Hi Joaqvil, the time condition I wrote above can be done in the builder see here:

        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Yes, I already use that to limit the trading time windows. But what I need is to get the price the instrument has at 9:30am, and then use that price the rest of the day, as an imaginary line, to make enters below and above that line. I mean, the price being above/below opening price, is one of many conditions that need to happen in order to enter the trades through the day.

          Comment


            #6
            Hi Joaqvil, You can create a double variable in the Inputs and Variables section and then set that variable to the Open[0] price at 9:30. In the Actions you will have an option to set your custom variable.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Sorry but I don't get it. Can you gave me an example? what number would you put to the variable?

              Comment


                #8
                Hello, thanks for the follow up. There is an full example on how to use the Input and Variables section here. I recommend going through all of these strategy builder examples if you are just starting out:


                In the Inputs and Variables section, create a double type variable. In your 9:30AM condition check, set the variable to a price value. This will save the 9:30 price value for later use.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Addendum: The Example I linked shows how to set up an Input, you will need to set up a Variable instead.
                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    Chris, Thanks for your patience, but Im not getting it. I know how to use inputs and variables, i've done a couple of strategy's before. But I don't see what that has to do with getting the opening price of the day. I can set the variable to a price value, and manually every morning put the opening price before turning on the strategy, but I would like to be automatic, so I can backtest the strategy. And I don't get what you want me to put on the conditions tab. Time value (9:30), equals, variable??

                    Comment


                      #11
                      Hello, thanks for the follow up. You asked how to get the open or close price at 9:30AM so it can be used at a later time. I attached my test script if you would like to try it out.

                      Attached Files
                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        That seems to work, THANKS A LOT. But there is a little problem.. it only works on 1 minute chart, you know why this may be?

                        Comment


                          #13
                          Hi, thanks for the reply. The time stamp needs to line up with your time definitions. E.g. If you run your script on a tick based chart, then there is no guarantee that a bar will close at exactly 9:30.
                          Chris L.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Noerclou, Today, 04:55 AM
                          0 responses
                          1 view
                          0 likes
                          Last Post Noerclou  
                          Started by llanqui, Yesterday, 09:59 AM
                          2 responses
                          16 views
                          0 likes
                          Last Post llanqui
                          by llanqui
                           
                          Started by ThoriSten, Today, 03:56 AM
                          0 responses
                          6 views
                          0 likes
                          Last Post ThoriSten  
                          Started by PhillT, 04-19-2024, 02:16 PM
                          3 responses
                          20 views
                          0 likes
                          Last Post mangel2000  
                          Started by TraderBCL, Today, 02:37 AM
                          0 responses
                          4 views
                          0 likes
                          Last Post TraderBCL  
                          Working...
                          X