Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problems coding breakeven exit.

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

    Problems coding breakeven exit.

    Good morning,

    I'm having trouble exiting my breakeven position once it has reached a certain price level.

    Usage:

    Calculate = Calculate.OnEachTick;
    EntryHandling = EntryHandling.UniqueEntries;
    EntriesPerDirect = 1;
    SetStopLoss("SALE", CalculationMode.Price, stopSales, false);
    EnterShortLimit(Closes[303][0],"SALE");


    ...to enter the market. And once I have the position entered, I use the breakeven to enter:

    if(Position.MarketPosition == MarketPosition.Short)
    {
    if(Closes[0][0] <= (Position.AveragePrice - 15*TickSize))
    {
    SetStopLoss("SALE",CalculationMode.Price(Position. AveragePrice - TickSize), false);
    }
    }


    I use two different SetStopLoss() commands.

    The problem is that when I run the program, it positions itself correctly, but when it exits the position, it immediately places another short position without fulfilling the entry requirement.

    Why is this?

    If you find the brakeven programming incorrect, could you tell me how to do it correctly?

    Thank you very much in advance;​

    #2
    Hello BIOK.NT

    Thanks for your message.



    First, I recommend you check out our sample code for modifying the price of a StopLoss and how to use that for a breakeven.


    https://developer.ninjatrader.com/docs/desktop/modifying_the_price_of_stop_loss_and_profit_target _orders


    This will provide you with a working sample of a break-even script demonstrating the use of SetStopLoss() method and how it is being modified to a break-even after the position goes up 20 ticks.

    We also highly recommend that you read the following support guide article, which goes over steps to troubleshoot your script.


    Developer Guide - Debugging using Print() and TraceOrderss


    To fully understand the behavior of your script and why it is placing unexpected orders, we recommend you start by adding prints to the script that print the values used throughout the logic of the script to understand how the script is evaluating.
    • Enable TraceOrders
    • Print the time of the bar and all variables and values used in the conditions that submit entry orders
    • Include labels for all values and comparison operators
    • Save the output to a text file
      • Right-click the output window, select Save as
      • Attach this to your initial inquiry when contacting NinjaTrader Support or posting on the forums for assistance

    From the code sample you shared, I want to confirm where exactly you are calling your EnterShortLimit() method from?

    It seems that it is being called under OnStateChage(), on State.SetDefaults .

    Only by seeing your full script would we be able to determine if there are any methods or properties that are out of place.

    You can export your script directly from the platform. On the Control Center, go to Tools>Export>NinjaScript Add-on. Select the desired strategy.



    Feel free to reach out with any other questions or concerns.

    Comment


      #3
      Hi again!

      Answering your inquire:

      From the code sample you shared, I want to confirm where exactly you are calling your EnterShortLimit() method from?
      It seems that it is being called under OnStateChage(), on State.SetDefaults .


      I´m calling it from OnBarUpdate. I want a dynamic StopLoss.
      I belive that, since I´m using an EnterShortLimit() order (that needs to be cancelled) and changing the StopLoss() during the performance of the script, that entry order needs to be cancelled anyway.

      ​Is there a way to cancel the entry signal without using Advanced Handling?
      Could I place an ExitShort() order once the market has reached the breakeven price and the market is flat?

      Thank you,​

      Comment


        #4
        Hello BIOK.NT,

        Thanks for your reply.



        We need a little bit more clarity to fully understand what is happening with your script and what you are trying to accomplish.

        Why do you want to cancel this EnterShortLimit() order?

        In your initial post, you stated that it does "exits the position". Does that mean that the script enters a position initially, and it enters a new position once a StopLoss or ProfitTarget gets triggered?

        Do you want to enter this short position only once?


        You also stated that the second short position is being called without fulfilling the entry requirements. If an order method is called from inside a condition, it can only be called if that condition is true. Only by examining your prints and TraceOrders can we fully understand this behavior.

        This link provides a guide on how to create prints and how to enable TraceOrders.

        How to debug your script using Print() and TraceOrders

        Before you create your Print(), ensure that:
        • Enable TraceOrders
        • Print the time of the bar and all variables and values used in the conditions that submit entry orders
        • Include labels for all values and comparison operators

        Once you have the Prints in the output window, save the output to a text file:
          • Right-click the output window, select Save as

        If needed, you can export your script directly from the NinjaTrader platform.

        In the Control Center, go to Tolls>Export>NinjaScript Add-on. Select the desired script and click "Export".

        Please, attach this to your initial inquiry when contacting NinjaTrader Support or posting on the forums for assistance



        We'll be waiting for your response.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        50 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        126 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        69 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        42 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X