Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using variables to set StopLoss and ProfitTarget not woking

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

    Using variables to set StopLoss and ProfitTarget not woking

    Hello... I'm using the strategy builder to set a strategy, all logic apart, what it does is:
    • At some moment during the session it sets a value for the variable TargetPriceAbove, it will be the target price, (not currency, not ticks, the price).
    • Later, when the conditions indicate it is moment to create an order it does the following in this exact order:
      • Based on current price and target price (calculated before), it calculates the stop price and stores it in the variable StopPriceBelow, again, it is the actual price for the stop loss (not currency, not ticks).
      • It adds a message to the log indicating: Target, Stop and From (current price).
      • It creates a Long order with the tag "V26Up".
    • On the "Stops and Targets" it does the following in this order:
      • Uses SetStopLoss with tag "V26Up", calculation mode "Price" and the value is the variable StopPriceBelow (calculated some milliseconds ago).
      • Uses SetProfitTaget with tag "V26Up", calculation mode "Price" and the value is the variable TargetPriceAbove (calculated several minutes or hours ago).
    As you can see in the logs: It printed the correct values for the Target, Stop and current price using the values stored on the variables (1st log message at the bottom)... but at the moment of creating the StopLoss and the ProfitTarget, it looks that it is trying to create them with the prices $0.25 (that is the default value for those variables).

    Do you have a clue why the log uses the correct values for the variables but the SetStopLoss and SetProfitTarget functions do not use those values?

    The log output:
    Code:
    Time,Category,Message,
    9/30/2024 7:10:00 AM,Order,Order='125798094604/DEMO597393' Name='Stop loss' New state='Cancelled' Instrument='MES DEC24' Action='Sell' Limit price=0 Stop price=0.25 Quantity=1 Type='Stop Market' Time in force=DAY Oco='125798094595_1' Filled=0 Fill price=0 Error='No error' Native error='',
    9/30/2024 7:10:00 AM,Order,Order='125798094604/DEMO597393' Name='Stop loss' New state='Cancel submitted' Instrument='MES DEC24' Action='Sell' Limit price=0 Stop price=0.25 Quantity=1 Type='Stop Market' Time in force=DAY Oco='125798094595_1' Filled=0 Fill price=0 Error='No error' Native error='',
    9/30/2024 7:10:00 AM,Order,Order='125798094604/DEMO597393' Name='Stop loss' New state='Accepted' Instrument='MES DEC24' Action='Sell' Limit price=0 Stop price=0.25 Quantity=1 Type='Stop Market' Time in force=DAY Oco='125798094595_1' Filled=0 Fill price=0 Error='No error' Native error='',
    9/30/2024 7:10:00 AM,Order,Order='125798094606/DEMO597393' Name='Profit target' New state='Submitted' Instrument='MES DEC24' Action='Sell' Limit price=0.25 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='125798094595_1' Filled=0 Fill price=0 Error='No error' Native error='',
    9/30/2024 7:10:00 AM,Order,Order='125798094606/DEMO597393' Name='Profit target' New state='Working' Instrument='MES DEC24' Action='Sell' Limit price=0.25 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='125798094595_1' Filled=0 Fill price=0 Error='No error' Native error='',
    9/30/2024 7:10:00 AM,Order,Order='125798094606/DEMO597393' Name='Profit target' New state='Accepted' Instrument='MES DEC24' Action='Sell' Limit price=0.25 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='125798094595_1' Filled=0 Fill price=0 Error='No error' Native error='',
    9/30/2024 7:10:00 AM,Order,Order='125798094604/DEMO597393' Name='Stop loss' New state='Working' Instrument='MES DEC24' Action='Sell' Limit price=0 Stop price=0.25 Quantity=1 Type='Stop Market' Time in force=DAY Oco='125798094595_1' Filled=0 Fill price=0 Error='No error' Native error='',
    9/30/2024 7:10:00 AM,Position,Instrument='MES DEC24' Account='DEMO597393' Average price=0 Quantity=0 Market position=Flat Operation=Remove,
    9/30/2024 7:10:00 AM,Execution,Execution='125798094606_1' Instrument='MES DEC24' Account='DEMO597393' Exchange=Default Price=5779.75 Quantity=1 Market position=Short Operation=Operation_Add Order='125798094606' Time='9/30/2024 7:10 AM',
    9/30/2024 7:10:00 AM,Order,Order='125798094606/DEMO597393' Name='Profit target' New state='Filled' Instrument='MES DEC24' Action='Sell' Limit price=0.25 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='125798094595_1' Filled=1 Fill price=5779.75 Error='No error' Native error='',
    9/30/2024 7:10:00 AM,Order,Order='125798094604/DEMO597393' Name='Stop loss' New state='Submitted' Instrument='MES DEC24' Action='Sell' Limit price=0 Stop price=0.25 Quantity=1 Type='Stop Market' Time in force=DAY Oco='125798094595_1' Filled=0 Fill price=0 Error='No error' Native error='',
    9/30/2024 7:10:00 AM,Position,Instrument='MES DEC24' Account='DEMO597393' Average price=5780 Quantity=1 Market position=Long Operation=Operation_Add,
    9/30/2024 7:10:00 AM,NinjaScript,NinjaScript strategy 'V26LeaveNoOneBehind/336144117' submitting order,
    9/30/2024 7:10:00 AM,NinjaScript,NinjaScript strategy 'V26LeaveNoOneBehind/336144117' submitting order,
    9/30/2024 7:10:00 AM,Execution,Execution='125798094595_1' Instrument='MES DEC24' Account='DEMO597393' Exchange=Default Price=5780 Quantity=1 Market position=Long Operation=Operation_Add Order='125798094595' Time='9/30/2024 7:10 AM',
    9/30/2024 7:10:00 AM,Order,Order='125798094595/DEMO597393' Name='V26Up' New state='Filled' Instrument='MES DEC24' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=1 Fill price=5780 Error='No error' Native error='',
    9/30/2024 7:10:00 AM,Order,Order='125798094595/DEMO597393' Name='V26Up' New state='Working' Instrument='MES DEC24' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error='',
    9/30/2024 7:10:00 AM,Order,Order='125798094595/DEMO597393' Name='V26Up' New state='Accepted' Instrument='MES DEC24' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error='',
    9/30/2024 7:10:00 AM,Order,Order='125798094595/DEMO597393' Name='V26Up' New state='Submitted' Instrument='MES DEC24' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error='',
    9/30/2024 7:10:00 AM,NinjaScript,NinjaScript strategy 'V26LeaveNoOneBehind/336144117' submitting order,
    9/30/2024 7:10:00 AM,NinjaScript,V26 Long. Target: 5792.75 Stop: 5772.875 From: 5779.5,​
    Attached Files
    Last edited by juankvillegas; 09-30-2024, 09:45 AM.

    #2
    Hello juankvillegas,

    In the Strategy Builder calculated values or bar series values cannot be used with Stop losses and Profit targets.


    You would need to use Exit orders in the Actions window under Order management instead.

    Below is a link to an example.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your help.

      If I want to enter a long position immediately, and aditionally I want to add a STOP and a TARGET at specific price values, is this the correct way to do it?
      Click image for larger version

Name:	FgJ5a2J.png
Views:	54
Size:	3.0 KB
ID:	1319833

      Comment


        #4
        Hello juankvillegas,

        This would not be correct.

        The exit orders cannot be submitted until the entry order fills and would need to be resubmitted on each new bar. These could be submitted in a separate condition set. See the BreakEvenBuilderExample example I've linked in post # 2 on set 5.

        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I added the following and it worked:
          Click image for larger version

Name:	xA2861M.png
Views:	59
Size:	33.4 KB
ID:	1319872

          But...

          Originally posted by NinjaTrader_ChelseaB View Post
          The exit orders cannot be submitted until the entry order fills and would need to be resubmitted on each new bar.
          Is it necessary to resubmit the exit order on each new bar even if the Target and the Stop value doesn't change between bars? I mean, is the target and stop removed on each new bar and that's why I'd need to resubmit them? Or it must be resubmitted if I want to update de Stop or the Target? Like trying to have a Breakeven... in that case I understand the need to update the Stop value.

          Comment


            #6
            Hello juankvillegas,

            Yes, orders submitted without the isLiveUntilCancelled bool as true are automatically cancelled when the submission bar closes. Resubmitting them keeps them alive.
            The strategy builder does not use the isLiveUntilCancelled bool which defaults as false, so that orders can be cancelled.
            If you want to cancel an order, just don't submit the order on the new bar and it will be cancelled.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Wow... that's very interesting and opens a whole new world of opportunities... thank you for your help and the whole explanation.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Yesterday, 05:17 AM
              0 responses
              65 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              139 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