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

Undesired stop loss placement

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

    Undesired stop loss placement

    I am having an issue where the stoploss is moving between the initial swing location and the subsequent swing. It is also exiting in some cases at prices not indicated by the swings.

    I have an initial stop for longs go below the most current swing low - 35 ticks. Then its supposed to go under the rest of the swing points - 5 ticks. In the replay it is exiting at an undesired price. Tracelog and details below. Please help.


    Stop losses are initially set to the prior swing low minus 35 ticks.

    Then they are set to the next swing low minus 5 ticks.

    The screenshot shows the order being exited at the open of the next candle. It clearly should still be in a trade as the stop should now be below the most current swing low minus 5 ticks.


    First the traceorder show the initial stop of 1.1363 (35 ticks below the swing low, correct)

    Then it moves to 1.1378 (5 ticks below the swing low, correct)

    Finally instead of moving the stop to the most recent swing low print, it attempts to move the stop above price to 1.3925 (incorrectly exits trade early).

    The order is exited at the open of the next candle.


    Trace Order:

    4/4/2016 6:55:04 AM Entered internal PlaceOrder() method at 4/4/2016 6:55:04 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='L1' FromEntrySignal=''

    4/4/2016 6:55:04 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='L1' Mode=Price Value=1.1363 Currency=0 Simulated=False

    4/4/2016 7:00:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='L1' Mode=Price Value=1.1378 Currency=0 Simulated=False

    4/4/2016 7:00:00 AM Amended stop order: Order='bda4d9d50725419b89f71dca4601bfcf/Replay101' Name='Stop loss' State=Accepted Instrument='6E 06-16' Action=Sell Limit price=0 Stop price=1.1378 Quantity=1 Strategy='FTRVSA2' Type=Stop Tif=Gtc Oco='9d7b9e8bba224047ab9b4334357329b5-7326' Filled=0 Fill price=0 Token='bda4d9d50725419b89f71dca4601bfcf' Gtd='12/1/2099 12:00:00 AM'

    4/4/2016 7:00:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='L1' Mode=Price Value=1.13925 Currency=0 Simulated=False

    4/4/2016 7:00:00 AM Amended stop order: Order='bda4d9d50725419b89f71dca4601bfcf/Replay101' Name='Stop loss' State=Accepted Instrument='6E 06-16' Action=Sell Limit price=0 Stop price=1.13925 Quantity=1 Strategy='FTRVSA2' Type=Stop Tif=Gtc Oco='9d7b9e8bba224047ab9b4334357329b5-7326' Filled=0 Fill price=0 Token='bda4d9d50725419b89f71dca4601bfcf' Gtd='12/1/2099 12:00:00 AM'


    My setup is as follows:

    The script contains a 30 minute higher time frame also. I did not find any details pertaining to barsarray or indexing for the swing indicator. Everything else however has the proper modifications for the dataseries index.


    private int swingStrength = 2; //default swing strength
    private int initialSwingPeriod = 1; //initial swing period
    private int swingPeriod = 2; //subsequent swing period
    private int spacer = 1; //space in ticks above or below swing
    private int initialSpacer = 35; //ticks above or below initial swing

    CalculateOnBarClose = true;


    if (long entry conditions met == true)
    {
    EnterLong(DefaultQuantity,"L1");
    double initialStop = Swing(swingStrength).SwingLow[initialSwingPeriod]-initialSpacer*TickSize;
    SetStopLoss("L1",CalculationMode.Price,initialStop , false);
    }

    //subsequent stop modification
    if (Position.MarketPosition == MarketPosition.Long)

    {
    double swingLow = Swing(swingStrength).SwingLow[swingPeriod]-spacer*TickSize;
    SetStopLoss("L1",CalculationMode.Price,swingLow, false);
    }

    Last edited by brucelevy; 04-30-2016, 10:41 PM.

    #2
    Hello,

    Thank you for the question.

    Just to simplify a bit, you have a stop that is being modified while there is a long position and it had two modifications that were correct until it had a third modification which placed it at the incorrect price and filled. Have I understood correctly?

    Based on the values shown, it seems this may indicate the logic that controls the third move may have not been a value you had intended. The trace output will show the order of events but would not really explain what had happened regarding the price. For this you may need to utilize Prints to determine why the price you have shown was being used.

    Based on the syntax provided it seems you are only checking if the position is long and if so to execute the SetStopLoss each OnBarUpdate, have you Printed the value of the Swing at this point to verify the value being used is expected? For example if the third event had a swing value that was that price, the logic would be working as expected based on what is provided.

    I would suggest if you have not yet, try the following Print over the same situation to see if the resulting value is valid for the order placement:

    {
    double swingLow = Swing(swingStrength).SwingLow[swingPeriod]-spacer*TickSize;
    Print("swingLow " + swingLow );
    SetStopLoss("L1",CalculationMode.Price,swingLow, false);
    }

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Here is the log

      Yes I am only checking to see if the position is long or short before initiating the subsequent swing stops. The only difference between the initial swing stop (entered along with the entry) and the subsequent (adjusted once long or short) is the initial stop is farther away from the swing to allow for price movement during the initial entry. After the next swing is created it should then go closer to the swings from then on.

      I added the prints, in this log the strategy enters a short position. Initial stop goes to correct price, the next stop goes to the correct price. Then as new swings are developed the stop loss goes backwards to different swing points.

      Lets forget about the random stop location for now and see if we can find why the stop placement is shifting between the swings.

      Thanks.



      4/4/2016 10:50:00 AM Entered internal PlaceOrder() method at 4/4/2016 10:50:00 AM: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='S1' FromEntrySignal=''

      4/4/2016 10:50:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.63 Currency=0 Simulated=False

      initialStop 38.63
      swingHigh 38.29


      4/4/2016 10:55:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False

      4/4/2016 10:55:00 AM Amended stop order: Order='4d48796eac034f7fa96e30c967d5a3dc/Replay101' Name='Stop loss' State=Accepted Instrument='CL 06-16' Action=BuyToCover Limit price=0 Stop price=38.29 Quantity=1 Strategy='FTRVSA2' Type=Stop Tif=Gtc Oco='505b5a9e198e41d3852b68a4179eb99d-8176' Filled=0 Fill price=0 Token='4d48796eac034f7fa96e30c967d5a3dc' Gtd='12/1/2099 12:00:00 AM'

      swingHigh 38.29

      4/4/2016 11:00:03 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False

      swingHigh 38.39 (Here the stop goes backwards to a higher price)

      4/4/2016 11:00:03 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.39 Currency=0 Simulated=False

      4/4/2016 11:00:03 AM Amended stop order: Order='4d48796eac034f7fa96e30c967d5a3dc/Replay101' Name='Stop loss' State=Accepted Instrument='CL 06-16' Action=BuyToCover Limit price=0 Stop price=38.39 Quantity=1 Strategy='FTRVSA2' Type=Stop Tif=Gtc Oco='505b5a9e198e41d3852b68a4179eb99d-8176' Filled=0 Fill price=0 Token='4d48796eac034f7fa96e30c967d5a3dc' Gtd='12/1/2099 12:00:00 AM'

      swingHigh 38.29

      4/4/2016 11:05:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False
      4/4/2016 11:05:00 AM Amended stop order: Order='4d48796eac034f7fa96e30c967d5a3dc/Replay101' Name='Stop loss' State=Accepted Instrument='CL 06-16' Action=BuyToCover Limit price=0 Stop price=38.29 Quantity=1 Strategy='FTRVSA2' Type=Stop Tif=Gtc Oco='505b5a9e198e41d3852b68a4179eb99d-8176' Filled=0 Fill price=0 Token='4d48796eac034f7fa96e30c967d5a3dc' Gtd='12/1/2099 12:00:00 AM'


      swingHigh 38.29

      4/4/2016 11:10:03 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False

      swingHigh 38.29

      4/4/2016 11:15:02 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False

      swingHigh 38.29

      4/4/2016 11:20:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False

      swingHigh 38.29

      4/4/2016 11:25:04 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False

      swingHigh 38.29

      4/4/2016 11:30:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False

      swingHigh 38.43 (Stop goes backwards to a higher price)

      4/4/2016 11:30:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.43 Currency=0 Simulated=False

      4/4/2016 11:30:00 AM Amended stop order: Order='4d48796eac034f7fa96e30c967d5a3dc/Replay101' Name='Stop loss' State=Accepted Instrument='CL 06-16' Action=BuyToCover Limit price=0 Stop price=38.43 Quantity=1 Strategy='FTRVSA2' Type=Stop Tif=Gtc Oco='505b5a9e198e41d3852b68a4179eb99d-8176' Filled=0 Fill price=0 Token='4d48796eac034f7fa96e30c967d5a3dc' Gtd='12/1/2099 12:00:00 AM'

      swingHigh 38.29
      Last edited by brucelevy; 05-02-2016, 09:31 PM.

      Comment


        #4
        Originally posted by brucelevy View Post
        Here is the log

        Yes I am only checking to see if the position is long or short before initiating the subsequent swing stops. The only difference between the initial swing stop (entered along with the entry) and the subsequent (adjusted once long or short) is the initial stop is farther away from the swing to allow for price movement during the initial entry. After the next swing is created it should then go closer to the swings from then on.

        I added the prints, in this log the strategy enters a short position. Initial stop goes to correct price, the next stop goes to the correct price. Then as new swings are developed the stop loss goes backwards to different swing points.

        Lets forget about the random stop location for now and see if we can find why the stop placement is shifting between the swings.

        Thanks.



        4/4/2016 10:50:00 AM Entered internal PlaceOrder() method at 4/4/2016 10:50:00 AM: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='S1' FromEntrySignal=''

        4/4/2016 10:50:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.63 Currency=0 Simulated=False

        initialStop 38.63
        swingHigh 38.29


        4/4/2016 10:55:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False

        4/4/2016 10:55:00 AM Amended stop order: Order='4d48796eac034f7fa96e30c967d5a3dc/Replay101' Name='Stop loss' State=Accepted Instrument='CL 06-16' Action=BuyToCover Limit price=0 Stop price=38.29 Quantity=1 Strategy='FTRVSA2' Type=Stop Tif=Gtc Oco='505b5a9e198e41d3852b68a4179eb99d-8176' Filled=0 Fill price=0 Token='4d48796eac034f7fa96e30c967d5a3dc' Gtd='12/1/2099 12:00:00 AM'

        swingHigh 38.29

        4/4/2016 11:00:03 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False

        swingHigh 38.39 (Here the stop goes backwards to a higher price)

        4/4/2016 11:00:03 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.39 Currency=0 Simulated=False

        4/4/2016 11:00:03 AM Amended stop order: Order='4d48796eac034f7fa96e30c967d5a3dc/Replay101' Name='Stop loss' State=Accepted Instrument='CL 06-16' Action=BuyToCover Limit price=0 Stop price=38.39 Quantity=1 Strategy='FTRVSA2' Type=Stop Tif=Gtc Oco='505b5a9e198e41d3852b68a4179eb99d-8176' Filled=0 Fill price=0 Token='4d48796eac034f7fa96e30c967d5a3dc' Gtd='12/1/2099 12:00:00 AM'

        swingHigh 38.29

        4/4/2016 11:05:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False
        4/4/2016 11:05:00 AM Amended stop order: Order='4d48796eac034f7fa96e30c967d5a3dc/Replay101' Name='Stop loss' State=Accepted Instrument='CL 06-16' Action=BuyToCover Limit price=0 Stop price=38.29 Quantity=1 Strategy='FTRVSA2' Type=Stop Tif=Gtc Oco='505b5a9e198e41d3852b68a4179eb99d-8176' Filled=0 Fill price=0 Token='4d48796eac034f7fa96e30c967d5a3dc' Gtd='12/1/2099 12:00:00 AM'


        swingHigh 38.29

        4/4/2016 11:10:03 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False

        swingHigh 38.29

        4/4/2016 11:15:02 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False

        swingHigh 38.29

        4/4/2016 11:20:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False

        swingHigh 38.29

        4/4/2016 11:25:04 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False

        swingHigh 38.29

        4/4/2016 11:30:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.29 Currency=0 Simulated=False

        swingHigh 38.43 (Stop goes backwards to a higher price)

        4/4/2016 11:30:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='S1' Mode=Price Value=38.43 Currency=0 Simulated=False

        4/4/2016 11:30:00 AM Amended stop order: Order='4d48796eac034f7fa96e30c967d5a3dc/Replay101' Name='Stop loss' State=Accepted Instrument='CL 06-16' Action=BuyToCover Limit price=0 Stop price=38.43 Quantity=1 Strategy='FTRVSA2' Type=Stop Tif=Gtc Oco='505b5a9e198e41d3852b68a4179eb99d-8176' Filled=0 Fill price=0 Token='4d48796eac034f7fa96e30c967d5a3dc' Gtd='12/1/2099 12:00:00 AM'

        swingHigh 38.29
        It is doing what you told it to do. If you do not want the stop to move backwards then you must tell it not to do so.

        Pseudocode:
        Code:
        //Short
        SetStopLoss(Math.Min(newStop, currentStop)); //use correct syntax. 
        
        //Long
        SetStopLoss(Math.Max(newStop, currentStop)); //use correct syntax.

        Comment


          #5
          Thanks... would you be able to provide any examples of how to add that in.

          Comment


            #6
            Originally posted by brucelevy View Post
            Thanks... would you be able to provide any examples of how to add that in.
            If you are moving the stop, then you must be calculating the stop. What is the code that you are using to calculate the new stop and move the stop?

            Comment


              #7
              Originally posted by brucelevy View Post
              Thanks... would you be able to provide any examples of how to add that in.
              I just remembered that you showed your code in the original post, so take a look at this, and use/modify as needed.
              Code:
              private int swingStrength = 2; //default swing strength
              private int initialSwingPeriod = 1; //initial swing period
              private int swingPeriod = 2; //subsequent swing period
              private int spacer = 1; //space in ticks above or below swing
              private int initialSpacer = 35; //ticks above or below initial swing 
              [COLOR="blue"]private double storedSwingLow = -1.0;[/COLOR]
              
              CalculateOnBarClose = true;
              
              
              if (long entry conditions met == true)
              {
              EnterLong(DefaultQuantity,"L1");
              double initialStop = Swing(swingStrength).SwingLow[initialSwingPeriod]-initialSpacer*TickSize;
              SetStopLoss("L1",CalculationMode.Price,initialStop , false);
              }
              
              //subsequent stop modification
              if (Position.MarketPosition == MarketPosition.Long)
              
              {
              double swingLow = Swing(swingStrength).SwingLow[swingPeriod]-spacer*TickSize;
              [COLOR="Blue"]double stopPrice = Math.Max(storedSwingLow, swingLow);[/COLOR]
              SetStopLoss("L1",CalculationMode.Price, stopPrice, false);
              [COLOR="blue"]storedSwingLow = stopPrice; [/COLOR]//store the value for later comparison
              }
              You probably want to implement a boolean gate to control access to the SetStopLoss() modification block, so that the block only runs when a new SwingLow occurs. As written, that code block will run on every tick update, and so is rather inefficient.
              Last edited by koganam; 05-03-2016, 09:49 AM.

              Comment


                #8
                Thank you,

                Should I be using the following for storing the SwingHigh value:
                private double storedSwingHigh = 1.0;

                Comment


                  #9
                  Originally posted by brucelevy View Post
                  Thank you,

                  Should I be using the following for storing the SwingHigh value:
                  private double storedSwingHigh = 1.0;
                  I would not know: I do not see anything about SwingHigh's in your code write up.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by elirion, Today, 01:36 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post elirion
                  by elirion
                   
                  Started by gentlebenthebear, Today, 01:30 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post gentlebenthebear  
                  Started by samish18, Yesterday, 08:31 AM
                  2 responses
                  9 views
                  0 likes
                  Last Post elirion
                  by elirion
                   
                  Started by Mestor, 03-10-2023, 01:50 AM
                  16 responses
                  389 views
                  0 likes
                  Last Post z.franck  
                  Started by rtwave, 04-12-2024, 09:30 AM
                  4 responses
                  31 views
                  0 likes
                  Last Post rtwave
                  by rtwave
                   
                  Working...
                  X