Announcement

Collapse
No announcement yet.

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.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      20 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      119 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      63 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      41 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      45 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X