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 habeebft, Today, 07:27 AM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_ChristopherS  
      Started by AveryFlynn, Today, 04:57 AM
      1 response
      12 views
      0 likes
      Last Post NinjaTrader_Erick  
      Started by Max238, Today, 01:28 AM
      4 responses
      37 views
      0 likes
      Last Post Max238
      by Max238
       
      Started by r68cervera, Today, 05:29 AM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by geddyisodin, Today, 05:20 AM
      1 response
      14 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Working...
      X