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

How to know if my order has hit stop / take profit

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

    How to know if my order has hit stop / take profit

    I'm unable to figure out a way to know when a trade has been 'completed' and because of this I get a huge problem (just because of the logic of my strategy) where it sends an order out every tick because it doesn't know that the trade has already been taken.

    My problematic code is shown. The trade is triggered when an inside bar gets it's high taken out by the following bar. This causes the issue because I have to enter when the previous high breaks meaning I am checking if there is a new high in the code but with no way of knowing if the order has been made it does it in an infinite loop on every tick until the strategy is terminated.

    Is there any simple way of keeping track of orders that have hit target / stop loss or do I need to re-logic my strategy to work around this?


    EDIT : Solution

    Create a variable called "tradedBar" when you make a trade on a bar and want to restrict any further trades from ocurring set the tradedBar variable to CurrentBar then do a simple
    if(tradedBar != CurrentBar)
    and voila
    Attached Files
    Last edited by AlexsOptions; 02-23-2021, 07:15 PM.

    #2
    The general case is usually done with MarketPosition.

    EDIT: Enter a new trade only if Position.MarketPosition == MarketPosition.Flat.

    If you want to specifically know if your last trade was a winner or loser,
    (aka, hit stop loss or profit target), you would look at the SystemPerformance.

    EDIT: The examples for TradeCollection should get you started.
    Last edited by bltdavid; 02-23-2021, 10:20 PM.

    Comment


      #3
      Hello AlexsOptions,

      Thanks for your post.

      It looks like you have found a solution.

      When using Calculate.OnEachTick or Calculate.OnPriceChange you do need additional logic to prevent sending orders on each tick while a condition is true. Using an int variable and storing the current bar and checking for it matching the current bar is a common approach.
      Paul H.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by ttodua, 06-18-2024, 12:33 PM
      4 responses
      13 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by lepticellreviews, Today, 05:12 AM
      0 responses
      8 views
      0 likes
      Last Post lepticellreviews  
      Started by lepticellreviews, Today, 05:10 AM
      0 responses
      3 views
      0 likes
      Last Post lepticellreviews  
      Started by AcidaBurnCA, Today, 04:54 AM
      0 responses
      2 views
      0 likes
      Last Post AcidaBurnCA  
      Started by TanyaD, 06-17-2024, 10:07 AM
      5 responses
      22 views
      0 likes
      Last Post TanyaD
      by TanyaD
       
      Working...
      X