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

SetStopLoss problems

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

    SetStopLoss problems

    Hello! I have the strategy code:
    Code:
            protected void pBull()
            {
                if (!buyOn)
                    return;
                if (UseTrend && !IsRising(ValuesAlligatorTeeth))
                {
                    Log("Limit not setted. Outside trend for " + patternType + " time " + Time[0], LogLevel.Information);
                    Draw.Diamond(this, "NTrendBull" + Time[0], true, 0, Open[0] - TickSize * 35, System.Windows.Media.Brushes.Red);
                    return;
                }
                double price = Low[0];
                double bar = High[0] - Low[0];
                double sl, tp, entry;
                Log("Set Limit Order type " + patternType + " time " + Time[0], LogLevel.Information);
                for (int index = 0; index < fibo_count; index++)
                {
                    int half = 0;
                    Order order = null;
                    Order orderH = null;
                    entry = price + bar * fiboArr[index, 0] / 100;
                    if (entry >= GetCurrentAsk())
                        continue;
                    if (onFixSL)
                        sl = entry - fixSL * TickSize;
                    else
                        sl = price + bar * fiboArr[index, 1] / 100;
                    if (onFixTP)
                        tp = entry + fixTP * TickSize;
                    else
                        tp = price + bar * fiboArr[index, 2] / 100;
                    if (CloseHalf && lotCorr > 1)
                    {
                        half = lotCorr / 2;
                        orderH = EnterLongLimit(0, true, half, entry, signalCorr[index] + "_1");
                    }
                    order = EnterLongLimit(0, true, lotCorr - half, entry, signalCorr[index]);
                    if (orderH == null && order == null)
                    {
                        Log("null order " + Time[0], LogLevel.Information);
                        orderCorr[index] = null;
                        orderCorrHalf[index] = null;
                        continue;
                    }
                    if (sl == entry)
                        sl -= TickSize;
                    if (tp == entry)
                        tp += TickSize;
                    if (order != null)
                    {
                        SetStopLoss(signalCorr[index], CalculationMode.Price, sl, false);
                        SetProfitTarget(signalCorr[index], CalculationMode.Price, tp, MITTP);
                    }
                    if (orderH != null)
                    {
                        SetStopLoss(signalCorr[index] + "_1", CalculationMode.Price, sl, false);
                        SetProfitTarget(signalCorr[index] + "_1", CalculationMode.Price, tp, MITTP);
                    }
                    signalTP[index] = tp;
                    signalWork[index] = false;
                    orderCorr[index] = order;
                    orderCorrHalf[index] = orderH;
                    Log("Set Limit Order success. " + index + " time" + Time[0], LogLevel.Information);
                }
            }
    
            protected void pBear()
            {
                if (!sellOn)
                    return;
                if (UseTrend && !IsFalling(ValuesAlligatorTeeth))
                {
                    Log("Limit not setted. Outside trend for " + patternType + " time " + Time[0], LogLevel.Information);
                    Draw.Diamond(this, "NTrendBear" + Time[0], true, 0, Open[0] + TickSize * 50, System.Windows.Media.Brushes.Red);
                    return;
                }
                double price = High[0];
                double bar = High[0] - Low[0];
                double sl, tp, entry;
                Log("Set Limit Order type " + patternType + " time " + Time[0], LogLevel.Information);
                for (int index = 0; index < fibo_count; index++)
                {
                    int half = 0;
                    Order order = null;
                    Order orderH = null;
                    entry = price - bar * fiboArr[index, 0] / 100;
                    if (entry <= GetCurrentBid())
                        continue;
                    if (onFixSL)
                        sl = entry + fixSL * TickSize;
                    else
                        sl = price - bar * fiboArr[index, 1] / 100;
                    if (onFixTP)
                        tp = entry - fixTP * TickSize;
                    else
                        tp = price - bar * fiboArr[index, 2] / 100;
                    if (CloseHalf && lotCorr > 1)
                    {
                        half = lotCorr / 2;
                        orderH = EnterShortLimit(0, true, half, entry, signalCorr[index] + "_1");
                    }
                    order = EnterShortLimit(0, true, lotCorr - half, entry, signalCorr[index]);
                    if (orderH == null && order == null)
                    {
                        Log("null order " + Time[0], LogLevel.Information);
                        orderCorr[index] = null;
                        orderCorrHalf[index] = null;
                    }
                    if (sl == entry)
                        sl += TickSize;
                    if (tp == entry)
                        tp -= TickSize;
                    if (order != null)
                    {
                        SetStopLoss(signalCorr[index], CalculationMode.Price, sl, false);
                        SetProfitTarget(signalCorr[index], CalculationMode.Price, tp, MITTP);
                    }
                    if (orderH != null)
                    {
                        SetStopLoss(signalCorr[index] + "_1", CalculationMode.Price, sl, false);
                        SetProfitTarget(signalCorr[index] + "_1", CalculationMode.Price, tp, MITTP);
                    }
                    signalTP[index] = tp;
                    signalWork[index] = false;
                    orderCorr[index] = order;
                    orderCorrHalf[index] = orderH;
                    Log("Set Limit Order success. " + index + " time " + Time[0], LogLevel.Information);
                }
            }
    At first pBear worked for me. 2 positions of 2 lots were opened and stops were established for them. All perfectly. The second time pBear worked, and for the first position of 2 lots, stops were placed at 2 lots, and for the second position of 2 lots - at 4. And when the stops were executed, a 2-lot turn occurred. The log of work in the attachment.
    Attached Files
    Last edited by xasser; 05-29-2020, 02:33 PM.

    #2
    Hi xasser, thanks for your question.

    Please try calling SetStopLoss and SetProfitTarget before submitting the order. SetStopLoss and SetProfitTarget apply the protective order to any orders submitted after they are called.

    Please let me know if this does not resolve your inquiry.
    Chris L.NinjaTrader Customer Service

    Comment


      #3

      And why for the first time everything worked correctly? Everything works great on playback connection. But on real data - no

      Comment


        #4

        Even with partial execution of an order to open a position, stops are placed. Then the position opens and when changing stops an error is issued: 'Cannot change order '1192556598' because current order values already match.'

        Code:
         [TABLE="border: 0, cellpadding: 0, cellspacing: 0"]
        [TR]
        [TD="width: 575"]Disabling NinjaScript strategy 'XO1/202142948'[/TD]
         		[/TR]
        [TR]
        [TD]Order='26865dc1849640099da48e14d2131672/7PQ6868' Name='Profit target' New state='Cancelled' Instrument='ES 06-20' Action='Buy to cover' Limit price=0 Stop price=3004.5 Quantity=2 Type='MIT' Time in force=GTC Oco='314793671181' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Order='26865dc1849640099da48e14d2131672/7PQ6868' Name='Profit target' New state='Cancel submitted' Instrument='ES 06-20' Action='Buy to cover' Limit price=0 Stop price=3004.5 Quantity=2 Type='MIT' Time in force=GTC Oco='314793671181' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]7PQ6868, Cannot change order '1192556598' because current order values already match. affected Order: BuyToCover 1 StopMarket @ 3010.5[/TD]
         		[/TR]
        [TR]
        [TD]Strategy 'XO1/202142948' submitted an order that generated the following error 'Unable to change order'. Strategy has sent cancel requests, attempted to close the position and terminated itself.[/TD]
         		[/TR]
        [TR]
        [TD]Order='1192556598/7PQ6868' Name='Stop loss' New state='Working' Instrument='ES 06-20' Action='Buy to cover' Limit price=0 Stop price=3010.5 Quantity=1 Type='Stop Market' Time in force=GTC Oco='314793671181' Filled=0 Fill price=0 Error='Unable to change order' Native error='Cannot change order '1192556598' because current order values already match.'[/TD]
         		[/TR]
        [TR]
        [TD]Order='1192556598/7PQ6868' Name='Stop loss' New state='Accepted' Instrument='ES 06-20' Action='Buy to cover' Limit price=0 Stop price=3010.5 Quantity=1 Type='Stop Market' Time in force=GTC Oco='314793671181' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Order='26865dc1849640099da48e14d2131672/7PQ6868' Name='Profit target' New state='Trigger pending' Instrument='ES 06-20' Action='Buy to cover' Limit price=0 Stop price=3004.5 Quantity=2 Type='MIT' Time in force=GTC Oco='314793671181' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Order='26865dc1849640099da48e14d2131672/7PQ6868' Name='Profit target' New state='Change submitted' Instrument='ES 06-20' Action='Buy to cover' Limit price=0 Stop price=3004.5 Quantity=2 Type='MIT' Time in force=GTC Oco='314793671181' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Execution='314793676557' Instrument='ES 06-20' Account='7PQ6868' Exchange=Globex Price=3008 Quantity=1 Market position=Short Operation=Operation_Add Order='1192565567' Time='5/29/2020 11:29 AM'[/TD]
         		[/TR]
        [TR]
        [TD]Order='1192565567/7PQ6868' Name='Fibo_1_1' New state='Filled' Instrument='ES 06-20' Action='Sell short' Limit price=3008 Stop price=0 Quantity=2 Type='Limit' Time in force=GTC Oco='' Filled=2 Fill price=3008 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Instrument='ES 06-20' Account='7PQ6868' Average price=3008 Quantity=1 Market position=Short Operation=Operation_Add[/TD]
         		[/TR]
        [TR]
        [TD]Order='1192556598/7PQ6868' Name='Stop loss' New state='Working' Instrument='ES 06-20' Action='Buy to cover' Limit price=0 Stop price=3010.5 Quantity=1 Type='Stop Market' Time in force=GTC Oco='314793671181' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Order='1192556598/7PQ6868' Name='Stop loss' New state='Accepted' Instrument='ES 06-20' Action='Buy to cover' Limit price=0 Stop price=3010.5 Quantity=1 Type='Stop Market' Time in force=GTC Oco='314793671181' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Order='2dbb181d1adf4f14ae5cd9949dd4ffeb/7PQ6868' Name='Stop loss' New state='Submitted' Instrument='ES 06-20' Action='Buy to cover' Limit price=0 Stop price=3010.5 Quantity=1 Type='Stop Market' Time in force=GTC Oco='314793671181' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Order='26865dc1849640099da48e14d2131672/7PQ6868' Name='Profit target' New state='Trigger pending' Instrument='ES 06-20' Action='Buy to cover' Limit price=0 Stop price=3004.5 Quantity=1 Type='MIT' Time in force=GTC Oco='314793671181' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]NinjaScript strategy 'XO1/202142948' submitting order[/TD]
         		[/TR]
        [TR]
        [TD]NinjaScript strategy 'XO1/202142948' submitting order[/TD]
         		[/TR]
        [TR]
        [TD]Execution='314793671181' Instrument='ES 06-20' Account='7PQ6868' Exchange=Globex Price=3008 Quantity=1 Market position=Short Operation=Operation_Add Order='1192565567' Time='5/29/2020 11:29 AM'[/TD]
         		[/TR]
        [TR]
        [TD]Order='1192565567/7PQ6868' Name='Fibo_1_1' New state='Partially filled' Instrument='ES 06-20' Action='Sell short' Limit price=3008 Stop price=0 Quantity=2 Type='Limit' Time in force=GTC Oco='' Filled=1 Fill price=3008 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Order='1192565568/7PQ6868' Name='Fibo_1' New state='Working' Instrument='ES 06-20' Action='Sell short' Limit price=3008 Stop price=0 Quantity=2 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Order='1192565568/7PQ6868' Name='Fibo_1' New state='Accepted' Instrument='ES 06-20' Action='Sell short' Limit price=3008 Stop price=0 Quantity=2 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Order='1192565567/7PQ6868' Name='Fibo_1_1' New state='Working' Instrument='ES 06-20' Action='Sell short' Limit price=3008 Stop price=0 Quantity=2 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Order='1192565567/7PQ6868' Name='Fibo_1_1' New state='Accepted' Instrument='ES 06-20' Action='Sell short' Limit price=3008 Stop price=0 Quantity=2 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Order='6eeccbae48ce4acb82a8177da5a54873/7PQ6868' Name='Fibo_1' New state='Submitted' Instrument='ES 06-20' Action='Sell short' Limit price=3008 Stop price=0 Quantity=2 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Order='d6b2e8f79a2e44a8907f26288ba4a5f4/7PQ6868' Name='Fibo_1_1' New state='Submitted' Instrument='ES 06-20' Action='Sell short' Limit price=3008 Stop price=0 Quantity=2 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''[/TD]
         		[/TR]
        [TR]
        [TD]Set Limit Order success. 0 time 5/29/2020 11:20:00 AM[/TD]
         		[/TR]
        [TR]
        [TD]NinjaScript strategy 'XO1/202142948' submitting order[/TD]
         		[/TR]
        [TR]
        [TD]NinjaScript strategy 'XO1/202142948' submitting order[/TD]
         		[/TR]
        [TR]
        [TD]Set Limit Order type 2 time 5/29/2020 11:20:00 AM[/TD]
         		[/TR]
        [/TABLE]
        What could be the problem?

        Comment


          #5
          Hi xasser, thanks for your reply.

          Did you try printing the price variable you are using with the Print() method? Is it equal to the current price of the original stop?

          I look forward to hearing from you.
          Chris L.NinjaTrader Customer Service

          Comment


            #6

            Hello! I did not change the stop. I set a limit order on two lots using EnterShortLimit and put two stops on it using SetStopLoss and SetProfitTarget. With partial execution of the limit order, stops were established. OK. When the position was fully opened, this error occurred.

            Comment


              #7
              Hello xasser,

              The error received is a native error from the broker. The broker's order routing servers are returning an error when the order is attempted to be changed to the same price.

              To prevent the strategy from being deactivated, you can set the strategy's RealtimeErrorHandling property to IgnoreAllErrors and then trap the UnableToChangeOrder error in OnOrderUpdate. If the order could not be changed, and there is no cancel or fill seen for the order, you can expect it to still be protecting your position, and it would not be necessary to take any additional action.

              Another option would be to attempt moving the order to a different price and then back to the desired price so you can see confirmation of the order moving to your desired price.

              RealtimeErrorHandling - https://ninjatrader.com/support/help...orhandling.htm

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

              Please let me know if I can be of further assistance.
              Attached Files
              JimNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by andrewtrades, Today, 04:57 PM
              1 response
              10 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by chbruno, Today, 04:10 PM
              0 responses
              6 views
              0 likes
              Last Post chbruno
              by chbruno
               
              Started by josh18955, 03-25-2023, 11:16 AM
              6 responses
              436 views
              0 likes
              Last Post Delerium  
              Started by FAQtrader, Today, 03:35 PM
              0 responses
              9 views
              0 likes
              Last Post FAQtrader  
              Started by rocketman7, Today, 09:41 AM
              5 responses
              19 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X