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

auto breakeven

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

    auto breakeven

    Code:
    // Once the price is greater than entry price +8 ticks, set stop loss to breakeven:
    if (Close[0] > Position.AveragePrice + 8 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice);
    }
    How come we cant use "SetStopLoss" in strat builder so we can build a break even? Can that be added so we dont have to unlock our code?


    Code:
    Position.MarketPosition = MarketPosition.Long
    Position.GetUnrealizedProfitLoss(PerformanceUnit.Ticks, Default input[0]) > 8
    DO:
    ExitLongStopMarket(DefaultQuantity, (Position.AveragePrice +5) "", "");
    How come this wont work?

    The reason I have to do it through the strategy is because the ATM doesnt always come on for some reason? thanks!

    #2
    Hello ezrollin,

    Thanks for your post.

    "How come we cant use "SetStopLoss" in strat builder so we can build a break even? Can that be added so we dont have to unlock our code?"
    In the strategy Builder the Set methods are created before the bars are loaded so they would not have access to the data. The reason for this is because if the Set methods are used within the OnBarUpdate() it would be possible to create a situation where the set methods are not properly reset and this would lead to strategy errors. the goal of the strategy builder is to generate code that does not generate strategy errors as much as possible. In Ninjascript editor, when the Set methods are used within the OnBarUpdate(), the programmer has to take responsibility to ensure that the set method is "reset" before the next entry, to prevent rejected orders. unlocking a strategy builder strategy and working directly in NInjascript is always an option if you really want to use the set methods that way.

    How come this wont work?
    In the code lines you quoted, if you use them in the Strategy Builder you have to understand that the ExitLongStopMarket order is a resting order that if not filled on the bar it is submitted it will be automatically canceled. This is true for all resetting order types. So any time the line "Position.GetUnrealizedProfitLoss(PerformanceUnit. Ticks, Default input[0]) > 8" is not true, then the order is not resubmitted and would be canceled automatically.

    What you can do here is to remove the order from the action of this set and in its place set a bool variable to be true, then in another set you check the market position is long and that the bool is true and then submit the ExitLongStopMarket. This arrangement will set the stop and will maintain it until either the stop is hit (or a profit target, if any, is hit and no longer in a long market position) or you set the bool to false which would cause the order to not be resubmitted.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Click image for larger version

Name:	Untitled.png
Views:	55
Size:	36.7 KB
ID:	1180107
      Click image for larger version

Name:	Untitled.png
Views:	54
Size:	37.1 KB
ID:	1180108

      Ok, I can confirm it doesnt work. Stop stays the same. What did I do wrong?? Thanks for the info!

      Comment


        #4
        Thats the short side.
        Do I need to also reset the breakeven boolean back to false upon the exit? You didnt tell me to so I assume, No. tks

        Comment


          #5
          Hello ezrollin,

          Thanks for your reply.

          Please check the "Log" tab of the control center for any error messages related to the strategy.

          Are you also using SetStopLoss? (this would cause the exit order to be ignored, which would be an error message in the log)

          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Yes, SetStopLoss is in my (State == State.DataLoaded)
            I have to have a stop loss when I enter the trade so what should I do?

            Comment


              #7
              Hello ezrollin,

              Thanks for your reply.

              The Set methods (SetStopLoss, SetProfitTaregt, etc.) will prevent resting (Limit/stop) exit orders from being used as this violates the "Internal Order Handling Rules that Reduce Unwanted Positions", please see: https://ninjatrader.com/support/help...antedPositions

              If you want to continue to use the SetStopLoss then you can change from using a resting type order to using logic to monitor the current price and when it reaches the entry position to then use a market order to exit (this does not violate the rules), however in order to closely monitor price you would need to change the strategy Calculate to Calculate.OnPriceChange (if not already using). Using Calculate.OnPriceChanges means every time price changes, your strategy code is run giving a quick response to changes and in this case, you would be looking at a price relative to your entry. You would check that you are in a position and then compare the current Price>Close[0] price to Strategy>Avertage position Price and call ExitLong() or ExitShort() as needed.

              If you want to use resting type orders then you would need to replace (remove) the SetStopLoss with a resting order that starts at your desired stop distance and then gets moved to your breakeven when the conditions permit. You would also have to replace the SetProfitTarget with a limit type order. Here is a link to an example thread where I've posted a working example of using limit and stop type orders in place of the set methods: https://ninjatrader.com/support/foru...on#post1174873
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                That sounds like its easily doable and sounds good but Resting seems safer in case of disconnections or something! thanks

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by slimcactus, Today, 10:26 AM
                1 response
                12 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by Monares, Today, 10:49 AM
                3 responses
                17 views
                0 likes
                Last Post NinjaTrader_JustinD  
                Started by d122y, Today, 10:20 AM
                1 response
                12 views
                0 likes
                Last Post NinjaTrader_ChrisL  
                Started by trader3000a, Today, 06:41 AM
                1 response
                14 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by trader3000a, Today, 09:23 AM
                5 responses
                19 views
                0 likes
                Last Post trader3000a  
                Working...
                X