Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy Shows Entry one Bar to Late

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

    #16
    Thank you but the Stop and Profit Target ar far away from the actual Bid or Ask and there are not the wrong way. as you can see in the Code, the stop for Short is the High of the las 2 Bars + Ticksize and for Long other wise. there should be enough space as i thnk.
    now i have read the follow at your documentary (should be called prior to submitting the associated entry order to ensure an initial level is set.​)

    Maybe this is the Reason because i set the Stop and ProfitTarget after the Position are opend?
    Bo so i can not become the entryprice for the Stop and Profit Calculation or shoul i use the bid or ask for that?
    Many confusion now why its so hard to do that.

    Comment


      #17
      Hello orbito,

      Thanks for your notes.

      Debugging prints should be used to confirm the price that the orders are being submitted to.

      When using SetProfitTarget() and SetStopLoss() dynamically, you should call SetStopLoss() and SetProfitTarget() prior to your Entry order to submit the indicator stop/target orders. Since they are submitted upon receiving an execution, the Set method should be called prior to submitting the associated entry order to ensure an initial level is set as noted in the help guide documentation.

      SetProfitTarget();
      SetStopLoss();
      EnterLong();

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

      Then, you could dynamically change SetStopLoss() and SetProfitTarget() within your script. Note that when you call these methods to dynamically change the stop loss or profit target price in the strategy, you should always reset the stop loss price / offset value in OnBarUpdate() when your strategy is flat, otherwise, the last price/offset value set will be used to generate your stop loss order on your next open position

      This reference sample demonstrates dynamically modifying the price of stop loss or profit target orders: https://ninjatrader.com/support/help..._lo.htm​

      You could also consider using Exit methods such as ExitLongLimit()/ExitLongStopMarket() instead of Set methods in your script for the protective orders to control the stop/target orders.
      Last edited by NinjaTrader_BrandonH; 09-13-2023, 10:56 AM.
      <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

      Comment


        #18
        thank you i dont want change the stop and the target afterwards its fix after first submit.

        (ExitLongLimit()/ExitLongStopMarket())
        Interesting if i use this before or after Entry? is that also automatic OCO Orders?

        Comment


          #19
          Hello orbito,

          Thanks for your notes.

          ExitLongLimit() and ExitLongStopMarket() would be used after your strategy has entered a Long position.

          For example, in OnBarUpdate() you could create a condition that checks if Position.MarketPosition == MarketPosition.Long and then call your Exit order methods.

          Position.MarketPosition: https://ninjatrader.com/support/help...etposition.htm
          Managed Approach Order Methods: https://ninjatrader.com/support/help...d_approach.htm

          Or, in OnExecutionUpdate() you could check if the OrderState of the entry order object is OrderState.Filled and call your Exit order methods.

          This reference sample demonstrates using Exit order methods in OnExecutionUpdate(): https://ninjatrader.com/support/help...and_onexec.htm

          OnExecutionUpdate(): https://ninjatrader.com/support/help...tionupdate.htm

          Note that Exit order methods are not tied by an OCO ID. You could use SignalName and FromEntrySignal to tie the exit orders to the entry.

          See the 'Order Entry Methods' and 'How to close a position' sections of the Managed Approach help guide page linked above.
          Last edited by NinjaTrader_BrandonH; 09-13-2023, 11:32 AM.
          <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

          Comment


            #20
            Ohhhh i have testet the new (Target & Stop Before Entry) and in Playback its working now

            Comment


              #21
              I have now Changed my Script to ExitStopMarkt and ExitLimit orders like your Example so that i can dynamicly change that on chart. But its not working on the Chart so long i use the myBool (My is ShortTrigger) Parameter the Strategy shows allways one position open because no stops or targets not working. if i delete the my Bol Part ist working but than i can not adjust the stop and the Stop always spring to the last Position (x Candles ago) it actualy it self so to speak.
              I dont know what wrong here.


              EDIT: I Think Know the Problem. Is use now intabar entry end the HighestBar option for Stio use the Current Bar too so the Stop Rise after every Bar.
              Is there a way to ident the Highest High & Low with a Bar Shift? i use LowestBar() and HighestBar() But there is no Shift Option. I wish to know the Highest Bar of High[2] to High[4] for example. Excluding the Actual Bar


              EDIT: i have found the MAX Function but its still the same not working with the myBool = true; or my ShortTrigger=true;​
              How can i do this?

              if (Position.MarketPosition == MarketPosition.Flat)
              {
              EnterShort(1, "MyEntry");

              myBool = true;
              }

              if (Position.MarketPosition == MarketPosition.Short && myBool == true)
              {
              ExitShortStopLimit(0, true, Position.Quantity, prevClose + 20*TickSize, prevClose + 10*TickSize, "MyStop", "MyEntry");
              ExitShortLimit(0, true, Position.Quantity, Position.AveragePrice - 10 * TickSize, "MyTarget", "MyEntry");

              myBool = false;
              }​



              My Code are in the OnBarUpdate

              if(Condition){
              // Enter Short Trade
              EnterShort(1, "SystemShort");

              ShortTrigger=true;​
              }

              if(Position.MarketPosition == MarketPosition.Short && ShortTrigger==true){

              Print ("Set Stop Orders");
              ExitShortStopMarket((High[HighestBar(High,StopBars)] + TickSize), "StopShort", "SystemShort");
              ExitShortLimit(0, true, Position.Quantity, (Position.AveragePrice-(((High[HighestBar(High,StopBars)]-Position.AveragePrice)*FlexTarget)) - TickSize), "ShortTarget", "SystemShort");
              ShortTrigger=false;
              }​
              Last edited by orbito; 09-14-2023, 08:11 AM.

              Comment


                #22
                Hello orbito,

                Thanks for your note.

                HighestBar() returns the number of bars ago the highest price value occurred within the specified look-back period. LowestBar() returns the number of bars ago the lowest price value occurred within a specified look-back period. There are methods available to get the HighestBar() from High[2] to High[4]. You would have to add your own custom logic to calculate those values in your NinjaScript strategy.

                I could submit a feature request to the Development team for detecting the HighestBar and LowestBar for a specific range of barsAgo if you would like.

                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.

                Also, keep an eye on the Log tab of the Control Center when testing your strategy to see if any errors appear when running the strategy.

                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 how to use prints to understand behavior.
                https://ninjatrader.com/support/foru...121#post791121​​

                I have attached a simple sample strategy you could view demonstrating how to submit Exit orders and control trades made by using a bool.
                Attached Files
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment


                  #23
                  Thank you i have that integrated. The Eror Are in my Stop and Target Orders but they are Correct. I have Printed out that und its correct.
                  Its Courios, why is
                  shortTarget=currentBid-(shortStop-currentBid) - 10 * TickSize;
                  working and
                  shortTarget=currentBid-(shortStop-currentBid) - 1 * TickSize;
                  not?

                  The Target are far away from Entry as you can see. i dont kndow whats wrong at the calculation

                  Comment


                    #24
                    Hello orbito,

                    Thanks for your notes.

                    What exactly is the error message that you are referring to?

                    "why is shortTarget=currentBid-(shortStop-currentBid) - 10 * TickSize; working and shortTarget=currentBid-(shortStop-currentBid) - 1 * TickSize; not?"

                    The first value shortTarget=currentBid-(shortStop-currentBid) - 10 * TickSize;​ is being placed further away from the entry order compared to shortTarget=currentBid-(shortStop-currentBid) - 1 * TickSize; ​which is being placed closer to the entry order.

                    Ultimately, it would be up to you to further debug the script to determine exactly what value is being evaluated for your order when using ​​shortTarget=currentBid-(shortStop-currentBid) - 10 * TickSize;​ compared to shortTarget=currentBid-(shortStop-currentBid) - 1 * TickSize; ​in your script.

                    You would need to print out the price being passed into the order method one line above the order method and print out the GetCurrentBid() and GetCurrentAsk() along with the Time[0] to understand exactly how your logic is behaving compared to the current market price.

                    TraceOrders could also be used when debugging to get more information about how an order behaves.

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

                    See the forum thread linked on post # 22 detailing how to use prints and TraceOrders to understand a script's behavior.

                    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                    Comment


                      #25
                      I have Printed Out the Values iof nthe Orders and they all fine. I know that the Different is that the firts a more away. The Problem is that with the second Line, the System not working. they placed no order on the chart but no error also. not in the output or in the Log.
                      My Question was why the first line work and the scond not. it seems i musst add over 5 Tick to every exitStopMarket order or Limit exit order to be working. if i reduce the amount of Ticks under 5 the orders still not working. thats my Problem. as you can see the limit order is far away from entry so it should be notm a Problem to place it with smaller Ticks or even without a tick over the stop. normaly i would place the stops over the highest last 2 candles + 1 Tick visaversa the Target. But so i must add 5 Tick minimum that the order still work.

                      Comment


                        #26
                        Hello orbito,

                        Thanks for your notes.

                        If you are referring to the same error you noted in post # 14 of this forum thread, the error message in the screenshot you shared indicates that a buy stop or buy stop limit order was placed on the wrong side of the market. Since the order was placed on the wrong side of the market, the order was rejected.

                        If this is due to market volatility then there isn't really a way to 100% avoid this occurring, as in volatile markets the market could move so far and fast that this would occur.

                        You could consider using RealtimeErrorHandling.IgnoreAllErrors to determine the behavior of a strategy when the strategy places an order that is returned "Rejected". The default behavior is to stop the strategy, cancel any remaining working orders, and then close any open positions.

                        Please note that setting this property value to IgnoreAllErrors can have serious adverse affects on a running strategy unless you have programmed your own order rejection handling in the OnOrderUpdate() method. To do this you could trap the rejected order by checking if the OrderState is Rejected within OnOrderUpdate() followed by defining your own order rejection handling behavior for the rejected order.

                        Please see the example in the help guide link below that demonstrates using RealtimeErrorHandling and trapping a rejected order in OnOrderUpdate().

                        RealtimeErrorHandling — https://ninjatrader.com/es/support/h...orhandling.htm

                        If the strategy is not behaving as expected then it would be up to you to further debug your custom strategy's logic by reducing code, adding prints, and enabling TraceOrders to understand exactly how it is behaving. Note that we do not provide debugging services in our support. Note that you could print the Order object in OnOrderUpdate() to see how the order is behaving.

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

                        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                        https://ninjatrader.com/support/foru...121#post791121​​​
                        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by NullPointStrategies, Today, 05:17 AM
                        0 responses
                        20 views
                        0 likes
                        Last Post NullPointStrategies  
                        Started by argusthome, 03-08-2026, 10:06 AM
                        0 responses
                        119 views
                        0 likes
                        Last Post argusthome  
                        Started by NabilKhattabi, 03-06-2026, 11:18 AM
                        0 responses
                        63 views
                        0 likes
                        Last Post NabilKhattabi  
                        Started by Deep42, 03-06-2026, 12:28 AM
                        0 responses
                        41 views
                        0 likes
                        Last Post Deep42
                        by Deep42
                         
                        Started by TheRealMorford, 03-05-2026, 06:15 PM
                        0 responses
                        45 views
                        0 likes
                        Last Post TheRealMorford  
                        Working...
                        X