Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

The position is not closed

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

    The position is not closed


    Good afternoon, I’ve encountered a problem, I’m setting positions using a strategy + setting take profit and stop loss. Several trades go well, but at some point the price may greatly exceed the take profit and stop loss, but the position is not closed. What could be the problem?
    The code looks like this:​
    if (State == State.DataLoaded)
    {

    if(Profit > 0)
    { //Profit
    SetProfitTarget(CalculationMode.Currency, Profit);
    }



    if(StopLoss>0)
    { //StopLoss
    SetStopLoss(CalculationMode.Currency, StopLoss);
    }
    }​​
    Last edited by Bogdan097; 01-23-2024, 09:22 AM.

    #2
    Hello Bogdan097,

    Thanks for your post.

    What exactly is 'Profit' and 'StopLoss' in the code you shared?

    If you are calling SetStopLoss() and SetProfitTarget() in OnStateChange(), you should do so when the State == State.Configure, as seen in the NinjaTrader help guide documentation sample code linked below.

    SetProfitTarget(): https://ninjatrader.com/support/help...ofittarget.htm
    SetStopLoss(): https://ninjatrader.com/support/help...etstoploss.htm

    You could likely call SetProfitTarget() and SetStopLoss() in State.Configure without having to use a condition to call these methods.

    Ultimately, debugging prints should be added to the script to understand exactly how your script is behaving and placing orders. Add prints to the script that print out all the values being used to place orders and print out the order object in OnOrderUpdate() to see how the orders are evaluating.

    Further, enable TraceOrders which is a useful property when debugging the behavior of your orders. With the use of this property, you can track orders placed, amended, and canceled.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior and talks about TraceOrders.
    https://ninjatrader.com/support/foru...121#post791121
    Brandon H.NinjaTrader Customer Service

    Comment


      #3

      Thanks for the answer, but I still don’t understand what the problem is. Initially, I created a strategy through strategy builder, then I simply unlocked the code and added some properties. Stop loss and take profit were generated automatically by the NinjaTrader.
      amespace NinjaTrader.NinjaScript.Strategies
      {
      public class StartOfVolatility : Strategy
      {
      private SMA SMA1;
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "StartOfVolatility";
      Calculate = Calculate.OnBarClose;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlat;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 20;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      Lots = 1;
      MA = 1;
      Profit = 1;
      Buy = false;
      Sell = false;
      StopLoss = 1;
      NormalDay = true;
      TradeStart = 10000;
      TradeEnd = 210000;
      OffHistory = true;
      }
      else if (State == State.Configure)
      {
      SetProfitTarget("", CalculationMode.Currency, Profit);
      SetStopLoss(CalculationMode.Currency, StopLoss);
      }
      if (State == State.DataLoaded)
      {
      SMA1= SMA(Close, Convert.ToInt32(MA));

      }
      }​
      Last edited by Bogdan097; 01-23-2024, 09:41 AM.

      Comment


        #4
        Hello Bogdan097,

        Thanks for your notes.

        The code you shared in post # 1 is not the same as the code you shared in post # 3.

        To clarify, which section of code that you shared is the code being used by the strategy?

        Post # 1:

        if (State == State.DataLoaded)
        {

        if(Profit > 0)
        { //Profit
        SetProfitTarget(CalculationMode.Currency, Profit);
        }

        if(StopLoss>0)
        { //StopLoss
        SetStopLoss(CalculationMode.Currency, StopLoss);
        }
        }​​


        Post # 3:

        else if (State == State.Configure)
        {
        SetProfitTarget("", CalculationMode.Currency, Profit);
        SetStopLoss(CalculationMode.Currency, StopLoss);
        }
        ​​

        Overall, you must add debugging prints to the script to understand exactly how your script is behaving and placing orders when it is not behaving as expected. Without debugging the script we would not know how exactly the script is behaving.

        In the strategy, add prints to the script that print out all the values being used to place orders, print out the Close price (Close[0]), the Time[0] of the bar, and print out the order object in OnOrderUpdate() to see how the orders are evaluating.

        OnOrderUpdate(): https://ninjatrader.com/support/help...rderupdate.htm

        Further, enable TraceOrders which is a useful property when debugging the behavior of your orders. With the use of this property, you can track orders placed, amended, and canceled.

        TraceOrders: https://ninjatrader.com/support/help...aceorders2.htm

        Below is a link to a forum post that demonstrates how to use prints to understand behavior and talks about TraceOrders.
        https://ninjatrader.com/support/foru...121#post791121
        Brandon H.NinjaTrader Customer Service

        Comment


          #5

          I'm sorry, the first code I posted was copied from another expert. When trading as shown in the screenshots above, I will use the second code.Perhaps the problem is that I set take profit and stop loss in dollars?​

          Comment


            #6
            Hello Bodgan097,

            Thanks for your notes.

            Without debugging the script I would not be able to say for certain if using CalculationMode.Currency in SetStopLoss()/SetProfitTarget() is causing the behavior to occur. Note that we do not provide debugging services in our support so it would be up to you to debug the script. If you need assistance with creating a certain print we would be happy to provide direction on that.

            From the help guide: CalculationMode.Currency is the PnL away from average entry. Calculated by the dollar per tick value for the order quantity used. When this mode is used, StopTargetHandling will automatically be set to ByStrategyPosition

            You could consider trying to use a different CalculationMode, such as CalculationMode.Ticks for placing stops and targets.

            Ultimately, you must debug your script by adding prints to the script and enable TraceOrders to understand exactly how your script is behaving. Post # 4 on this forum thread discusses how adding debugging prints and using TraceOrders is necessary to understand a strategy's behavior.

            Also, post # 4​ contains a forum thread link detailing how to use prints and TraceOrders to understand a script's behavior.

            SetProfitTarget(): https://ninjatrader.com/support/help...ofittarget.htm
            SetStopLoss(): https://ninjatrader.com/support/help...etstoploss.htm
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              I turn on the tracing and don’t see any errors; it happens that an order with a volume of 30 is closed in parts and some part just hangs.
              Attached Files

              Comment


                #8
                Hello Bogdan097,

                Thanks for your notes.

                I see that you shared the Orders or Executions tab of the Control Center, however,the traces when enabling TraceOrders displayed in the NinjaScript Output window (New > NinjaScript Output).

                Further, I do not see where you added debugging prints to the script to print out the values of the logic being used within the script.

                If the market price (Close[0]) does not reach the profit target or stop loss order then the profit and stop order would not be filled.

                The message highlighted in the screenshot you shared means that a change for the stop loss order was submitted. this could be a change in order quantity, price, etc. The Change Submitted message in the screenshot you shared seems to be a change in order quantity. The stop loss had an order quantity of 16, then an entry order for 14 Short was filled so the quantity of the stop loss changed to 30 (16 + 14 = 30).

                I suggest studying the forum thread linked in post # 2 and post # 4 and linked below which discusses how to add debugging prints to a strategy to understand how the logic is behaving.

                Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                https://ninjatrader.com/support/foru...121#post791121

                After studying the forum thread linked above, add prints to your strategy that print out all the values being used to place orders, print out the Close price (Close[0]), print out the Time[0] of the bar, and print out the order object in OnOrderUpdate() to see how the orders are evaluating.

                OnOrderUpdate(): https://ninjatrader.com/support/help...rderupdate.htm

                Prints will appear in a New > NinjaScript Output window.

                Also, enable TraceOrders if you have not done so which will also appear in the NinjaScript Output window.

                You could compare the Prints and TraceOrders to understand exactly how your strategy is behaving and handling orders.
                Brandon H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by fx.practic, 10-15-2013, 12:53 AM
                5 responses
                5,404 views
                0 likes
                Last Post Bidder
                by Bidder
                 
                Started by Shai Samuel, 07-02-2022, 02:46 PM
                4 responses
                95 views
                0 likes
                Last Post Bidder
                by Bidder
                 
                Started by DJ888, Yesterday, 10:57 PM
                0 responses
                7 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by MacDad, 02-25-2024, 11:48 PM
                7 responses
                159 views
                0 likes
                Last Post loganjarosz123  
                Started by Belfortbucks, Yesterday, 09:29 PM
                0 responses
                8 views
                0 likes
                Last Post Belfortbucks  
                Working...
                X