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

Exit Bar 2 and Bar 3 and Bar 4 etc

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

    Exit Bar 2 and Bar 3 and Bar 4 etc

    // Set 98
    if ((Position.MarketPosition == MarketPosition.Long)
    && (LongTrades == true)
    && (TradeStableBars == true)
    && (Position.Quantity > 0)
    && (BarsSinceEntryExecution(0, "", 0) == 1)
    && (Close[1] > Open[1])
    && (SellPositionFIRSTBar == false)
    && (BarsSinceExitExecution(0, "", 0) != 0)
    && (UseExitScaleOutBar2thru5 == true))
    {
    ExitLong(Convert.ToInt32(ContractsorSharestoScaleO ut), @"L Scale2 Exit", @"");
    ProfitTargetMode1 = 5;
    }

    // Set 99
    if ((Position.MarketPosition == MarketPosition.Long)
    && (LongTrades == true)
    && (TradeStableBars == true)
    && (Position.Quantity > 0)
    && (BarsSinceEntryExecution(0, "", 0) == 2)
    && (Close[1] > Open[1])
    && (SellPositionFIRSTBar == false)
    && (UseExitScaleOutBar2thru5 == true)
    && (BarsSinceExitExecution(1, "", 0) == 0))
    {
    ExitLong(Convert.ToInt32(ContractsorSharestoScaleO ut), @"L Scale3 Exit", @"");
    ProfitTargetMode1 = 5;
    }


    ************************************************** ***********

    above i am beginning the exit process on bar 2 in the Set 98, this works as expected, how ever I cannot get the Next bar exit to work as expected

    the stategy is running OnPriceChange

    I am using this additional line of code to keep the second exit from sending out more that ONE exit order

    && (BarsSinceExitExecution(1, "", 0) == 0))

    ior this as well && (BarsSinceExitExecution(0, "", 0) == 0))

    either of these back test showing exits as expected, but in live data sim accounts only the First exit occurs, if i omit the && (BarsSinceExitExecution(1, "", 0) == 0)) line of code then the second exit will continue to send out orders until the complete position is sold

    #2
    Hello DTSSTS,

    Thank you for your post.

    If the expected trade(s) are not appearing, this would indicate that the condition to place the order is not evaluating as true or the order is being ignored for other reasons.

    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 values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    Below is a link to a forum post that demonstrates using prints to understand behavior and including a link to a video recorded using the Strategy Builder.
    https://ninjatrader.com/support/foru...121#post791121

    Please let me know if I may further assist
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      It is not sending unexpected order, It is just not sending the expected ones, SO if it does send a print

      the issues is the proper use of the condition indicated

      && (BarsSinceExitExecution(0, "", 0) == 0))

      what should this be to allow a traded just after the close of the previous bar *when conditions are meet using onpricechange strategy and NOT allow 2 trades

      also the bar prior to the previous bar (2 bars ago also made an exit( ** this is the key

      thanks

      Comment


        #4
        Hello DTSSTS,

        Thank you for your note.

        BarsSinceExitExecution() == 0 would check if the last exit execution occurred on the same bar.

        If you would like to check if 1 bar has passed since the last exit execution, you could call BarsSinceExitExecution() == 1. If you would like to check if 2 bars have passed since the exit order executed, you would use BarsSinceExitExecution() == 2, and so on.

        Note that a value of -1 will be returned for BarsSinceExitExecution() if a previous exit does not exist as stated in the help guide documentation.

        Ultimately, debugging prints should be used to see how the strategy is behaving. See the forum thread linked in my previous post regarding using prints to see how a strategy is behaving.

        See the help guide documentation below for more information and an example of using BarsSinceExitExecution().
        BarsSinceExitExecution() - https://ninjatrader.com/support/help...texecution.htm

        Let us know if we may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by rtwave, 04-12-2024, 09:30 AM
        4 responses
        29 views
        0 likes
        Last Post rtwave
        by rtwave
         
        Started by yertle, Yesterday, 08:38 AM
        7 responses
        28 views
        0 likes
        Last Post yertle
        by yertle
         
        Started by bmartz, 03-12-2024, 06:12 AM
        2 responses
        21 views
        0 likes
        Last Post bmartz
        by bmartz
         
        Started by funk10101, Today, 12:02 AM
        0 responses
        6 views
        0 likes
        Last Post funk10101  
        Started by gravdigaz6, Yesterday, 11:40 PM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Working...
        X