Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close position

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

    Close position

    Hi everyone

    It must be just me, but I am unable so far to create this simple strategy for automation.
    I plan to enter manually but have my strategy close my position when a condition is met.

    I'll use the MACD histogram for simplicity.
    I enter my position long and when the MACD (diff) histogram closes lower than the previous bar MACD histogram (diff) Ninja should close my position.

    If all:

    MACD/Diff Less than MACD/Diff 1 bar ago
    Position Quantity greater or equal to 1

    Do the following:

    Exit Long position

    A second tab with the inverse exists for short positions.

    It seems like a no brainer, but my strategy does absolutely nothing.

    Any ideas what I am doing wrong?

    #2
    Hello notenufftime,

    Thank you for your post.

    To understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

    The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

    The debugging print output should clearly show what the condition is, what time the conditions are being compared, all values being compared, and how they are being compared.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

    After enabling TraceOrders remove the instance of the strategy from the Configured list in the Strategies window and add a new instance of the strategy from the Available list.

    I am happy to assist you with analyzing the output from the output window.

    Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

    Below is a link to a support article that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.

    https://support.ninjatrader.com/s/ar...nd-TraceOrders
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Hi Gaby V.

      Thank you for your quick and comprehensive reply. I am using Strategy Builder, it is compiling, adding and enabling with no issues. Unfortunately, it simply does nothing.
      I've followed your link and will add the necessary code in editor. When I am able to harvest the output, I will return.

      Edit:
      I've found the area where I can do it in Builder and will proceed that way.
      Last edited by notenufftime; 10-08-2024, 10:10 AM.

      Comment


        #4
        I followed Chelseas method in the video and it worked fine however it does not cover Trace orders. I made a copy of the strategy with the print function and then unlocked the strategy in Builder, edited the trace to true and ran it.
        I ran the strategy a few times and despite several opportunities where it should have closed, it did not.
        Please let me know if this is an effective log.
        Attached Files

        Comment


          #5
          Hello,

          Unfortunately, these prints aren't descriptive enough to be able to tell what is being compared in these conditions. The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

          If you need assistance creating a print, please provide one of your conditions and I can create an example print for you.

          TraceOrders needs to be set to true from State.SetDefaults. After editing the code, you'll need to remove the old strategy instance and add a new instance to pull the new defaults.

          Gaby V.NinjaTrader Customer Service

          Comment


            #6
            Hi Gaby

            I used MACD in my example because it is easily available to everyone however, the indicator I am actually using is one that you've provided:

            VAcc (Velocity And Acceleration) – November 2023 S&C

            There are several factors required for my manual entry but I intend to use your indicator to close my position with the histogram being the trigger.
            Attached is a text file that contains the body of the strategy with print and trace functions that output the previous log.

            I removed the strategy and replaced it with the new one having print and trace enabled to provide the requested log. I thought I did good haha.

            Thank you.
            Attached Files
            Last edited by notenufftime; 10-08-2024, 04:46 PM.

            Comment


              #7
              Here is an example print for your first condition:

              Code:
              Print(Times[0][0] + " VAcc21.AccPlot[0]: " + VAcc21.AccPlot[0] + " < VAcc21.AccPlot[1]: " + VAcc21.AccPlot[1]) + " & Position.Quantity: " + Position.Quantity + " >= 1");
              if ((VAcc21.AccPlot[0] < VAcc21.AccPlot[1])
              && (Position.Quantity >= 1))
              {
              ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
              }
              You need to create a similar print for your second condition, the print should be placed 1 line above the condition as per the example above. ​
              Gaby V.NinjaTrader Customer Service

              Comment


                #8
                I am a novice at best, please accept my gratitude for your help and your patience as I work through this. I'll figure this out and post the results when it's working.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NasdaqAnalytica, Today, 03:33 PM
                1 response
                4 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by Auvrayphil, Today, 03:44 PM
                0 responses
                4 views
                0 likes
                Last Post Auvrayphil  
                Started by jamesbhardwaj, Today, 04:41 AM
                1 response
                15 views
                0 likes
                Last Post NinjaTrader_LuisH  
                Started by diorfo, Today, 12:48 PM
                5 responses
                24 views
                0 likes
                Last Post NinjaTrader_ChristopherJ  
                Started by cp202822, Today, 01:38 PM
                2 responses
                14 views
                0 likes
                Last Post cp202822  
                Working...
                X