Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position not being closed in strategy analyzer

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

    Position not being closed in strategy analyzer


    I selected UseDynamicSLTP as exit strategy but in several cases in the stategy analyzer shorts were not closed. I added 1 tick data to the strategy for accuracy. What can i do to prevent this?(it maybe because of take profit ? its by ATR and it may get filled actually)
    else if (State == State.Configure)
    {
    Calculate = Calculate.OnPriceChange;
    AddDataSeries(Data.BarsPeriodType.Tick, 1);
    }​

    protected override void OnBarUpdate()
    {

    if (CurrentBar < 8) return;
    if (BarsInProgress!=0) return;​
    ............................... variables
    if( StochSwingShort && StochFastSwing
    && (Position.MarketPosition == MarketPosition.Flat)
    && (BarsSinceExitExecution(0,"SLS",0) > 1 || BarsSinceExitExecution(0,"SLS",0) == -1)
    && (BarsSinceExitExecution(0,"PTS",0) > 1 || BarsSinceExitExecution(0,"PTS",0) == -1)
    && (BarsSinceExitExecution(0,"Parabolic stop",0) > 1 || BarsSinceExitExecution(0,"Parabolic stop",0) == -1)​
    EnterShort(PositionSize, "StochFS");


    if (Position.MarketPosition == MarketPosition.Short && myFreeTradeShort == true || StochSwingShort == true)

    {

    if (UseDynamicSLTP)
    {
    if(stopShort >= ask){
    ExitShortStopMarket(1, true, Position.Quantity, stopShort, "SLS", "StochFS");
    ExitShortLimit(1, true, Position.Quantity, Position.AveragePrice - (TickSize * tpByATR), "PTS", "StochFS");
    }

    }

    else if (UseFixedSLTP)
    {
    if(Position.AveragePrice + (TickSize * StopLossTicks) >= ask){
    ExitShortStopMarket(1, true, Position.Quantity, Position.AveragePrice + (TickSize * StopLossTicks), "SLS", "StochFS");
    ExitLongLimit(1, true, Position.Quantity, Position.AveragePrice - (TickSize * ProfitTargetTicks), "PTS", "StochFS");
    }
    }

    else if (UseParabolicStop)
    {
    SetParabolicStop("StochFS", CalculationMode.Ticks, ParabolilcSarSL, false, Acc, AccStep, AccMax);
    }


    Click image for larger version  Name:	image.png Views:	0 Size:	1.01 MB ID:	1259269
    Last edited by tkaboris; 07-07-2023, 05:36 AM.

    #2
    Hello tkaboris,

    Implementing 1 tick intra-bar granularity would be the right move, so don't change that.

    Enable TraceOrders and print the order object from OnOrderUpdate so that we can see if the orders are being submitted, ignored, or if they become working and at what price.

    Save the output to a text file and attach this to your next post so I may have a look.

    We are just focusing on the last entry and exits, is this correct?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Yes just last entry and exits. strategy called StochAlgoTrader..
      I am seeing these types of messages
      2023-07-06 15:25:35:741|3|4|Strategy 'StochAlgoTrader/-1': An order has been ignored since the stop price ‘15174.25’ near the bar stamped ‘06/14/2023 12:10:26’ is invalid based on the price range of the bar. This is an invalid order and subsequent orders may also be ignored.

      Comment


        #4
        Hello tkaboris,

        The ignored message indicates an invalid order price. May I confirm that the stop price is less than the bid for a sell or greater than the ask for a buy, when submitted?
        Hi I have a sellshort StopLimit order that i change if unfilled. I tick below the last bar's low. I keep getting the stop price can't be changed above the market but the price value it quotes ( the one I want) is below the market. Currently I am on the simulated feed. I appreciate in real market situations this could happen


        You have provided your log file which was not requested. I've removed this from your post as this can contain sensitive information.

        TraceOrders and Print() will appear in the NinjaScript Output window. Control Center -> New -> NinjaScript Output. This is the window that is showing in your screenshot in your first post of this thread.

        Below is a link to a forum post on using TraceOrders and Print() to understand behavior.


        And links to the help guide.



        Right-click the NinjaScript Output window and select Save As to save this to a text file.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          uploaded file

          Comment


            #6
            One question for for
            parabolicstops, do we need to confirm stopprice is less the a bid for sell? in parabolicstop i use ParabolicSarSL which is doulbe like 40 ticks

            if (UseParabolicStop){
            if(ParabolilcSarSL >= ask){
            EnterShort(PositionSize, "StochFS");
            SetParabolicStop("StochFS", CalculationMode.Ticks, ParabolilcSarSL, false, Acc, AccStep, AccMax);
            }
            }​

            Comment


              #7
              hi It seems my exits working after i edited (BarsSinceExitExecution(0,"PTS",0) > 1 || BarsSinceExitExecution(0,"PTS",0) == -1) , except the UseparabolicStop one.
              After your previous reply i noticed that you say stopprice is less then a bid for a sell, and i have stopShort is greater then ask. So my code is not correct and i should use stopShort is less then a bid?

              If i look in the first post screenshot, sell order is filled at 8:55 around 15039 price and it shows stopprice is at 15044.

              My code is provided in the first post for it. If i choose UseParabolicStop as my exit then stoploss and take profits orders not getting filled in strategy analyzer. I added 1 Tick to make it more accurate. I tried setting parabolic stop in state.configure state but it was giving same results too
              I tried to upload traceorders .txt but file was too big, so i just added one line which is excactly all lines are.


              You have reached the maximum threshold of the NinjaScript Output window. Some of your output messages have been suppressed.
              6/16/2023 3:53:22 PM Strategy 'Stoch Algo Trader/-1': Amended parabolic stop order orderId='NT-00037-1035' account='Backtest' name='Parabolic stop' orderState=Working instrument='MNQ 09-23' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=15217.5 quantity=1 tif=Gtc oco='NT-00032-1035' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-16 05:36:35' gtd='2099-12-01' statementDate='2023-07-07' stopPriceChanged=15217.5​

              Thank you for your help
              Last edited by tkaboris; 07-07-2023, 05:33 AM.

              Comment


                #8
                Hello tkaboris,

                With the text file being to large, you can use file transfer service like wetransfer or google drive, and then provide a link.
                Or you can backtest over a smaller data set to get less output in the output window. (Clear the output window before re-running the test)

                When using CalculationMode.Ticks, you won't have to check the stop price. However, you do need to call set methods before calling the entry method.

                For a sell stop order, yes, the stop price must be less than the bid price, not greater.

                With the parabolic stop, are you seeing that the market price dropped below the price of the stop? (15217.50)
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9

                  Ok i got a link to trace file


                  I am not sure about parabolic, i see that with shorts order for short gets open but setparabolicstop is not working and position gets closed at the end of the session

                  Comment


                    #10
                    hi if i have the following logic then it doesnt set TP or SL
                    region Stop Offset

                    stopAreaLong = Low[1];
                    stopAreaShort = High[1];
                    tickCalcStop = TickOffsetStop * TickSize;

                    stopLong = stopAreaLong - tickCalcStop;
                    stopShort = stopAreaShort + tickCalcStop;

                    #endregion​
                    if (UseDynamicSLTP)
                    {
                    if(stopShort <= bid){
                    ExitShortStopMarket(1, true, Position.Quantity, stopShort, "SLS", "StochFS");
                    ExitShortLimit(1, true, Position.Quantity, Position.AveragePrice - (TickSize * tpByATR), "PTS", "StochFS");

                    }

                    }​

                    But if i keep this it works fine
                    if (UseDynamicSLTP)
                    {
                    if(stopShort >= ask){
                    ExitShortStopMarket(1, true, Position.Quantity, stopShort, "SLS", "StochFS");
                    ExitShortLimit(1, true, Position.Quantity, Position.AveragePrice - (TickSize * tpByATR), "PTS", "StochFS");

                    }

                    }​

                    Can you please confirm it needs to be stopShor less then bid?
                    Does this expression needs to be supplied for both entry and exits? if(stopShort >= ask){

                    Comment


                      #11
                      Also strange that in output window there was three trades although it was only one trade ​short. Could it be because of short timeframe like 100 ticks and it triggers multiple orders?
                      Click image for larger version  Name:	image.png Views:	0 Size:	836.1 KB ID:	1259374
                      Last edited by tkaboris; 07-07-2023, 07:53 AM.

                      Comment


                        #12
                        here is a screenshot example of what I have in analyzer, sometimes parabolic sets ok sometimes it doesnt and waits like sevearl hours before finally closes, even though price should have closed much earlier at setparabolic stop

                        Click image for larger version

Name:	image.png
Views:	133
Size:	915.8 KB
ID:	1259388

                        Comment


                          #13
                          Hello tkaboris,

                          May I confirm you are also printing the order object from OnOrderUpdate() as directed?

                          I am seeing the parabolic stop was amended to 15207. Did the price reach 15207 while the position was open?
                          6/14/2023 3:07:50 PM Strategy 'Stoch Algo Trader/-1': Amended parabolic stop order orderId='NT-00010-6734' account='Backtest' name='Parabolic stop' orderState=Working instrument='MNQ 09-23' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=15207 quantity=1 tif=Gtc oco='NT-00008-6734' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-06-14 14:22:16' gtd='2099-12-01' statementDate='2023-07-07' stopPriceChanged=15207
                          To understand why there were only 3 trades, print the time of the bar and the values used in the conditions along with labels for each value and comparison operator to understand when the conditions are evaluating as true or false.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Yes price reached 15207 when it was open
                            I enabled traceorders but i am confused about this line
                            Print(order.ToString());
                            order does not exist
                            should this line go right after i submit an order?
                            Last edited by tkaboris; 07-07-2023, 08:56 AM.

                            Comment


                              #15
                              I am attaching copy of strategy just in case
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              59 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              134 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              75 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              45 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              50 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X