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

If last trade was a loosing trade stop trading for today

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

    If last trade was a loosing trade stop trading for today

    hello

    i would like to stop (not terminate) my automated strategy for today if the last trade was a loosing one, but when i add this code from the help section i get an INDEX OUT OF RANGE error

    other parts of my strategy require this to be set:
    if (CurrentBar < 6)
    return;

    here is the code i am using from the help section:

    Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];

    if (lastTrade != null && lastTrade.ProfitPercent <= 0)
    allowtrading == false;

    on firstbarofsession is set "allowtrading" to true

    on every entry i check if allowtrading == true then enterlong

    #2
    This is likely your line in error: Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];

    Where in the help guide does it use Performance.AllTrades.Count - 1? If you want the last trade you would want to use an index of 0.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      thx

      about the performance.alltrades
      is there a function to print the largest loosing trade from my trade history?

      Originally posted by NinjaTrader_Josh View Post
      This is likely your line in error: Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];

      Where in the help guide does it use Performance.AllTrades.Count - 1? If you want the last trade you would want to use an index of 0.

      Comment


        #4
        You will want to loop through the TradeCollection class and determine it through your own code.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          tradereight,

          Are you still getting the "index out of range" issue? It has been brought to my attention that Performance.AllTrades[Performance.AllTrades.Count - 1] is actually the last trade. A reason you may have hit the "index out of range" is if there are no trades at the point in time when you access it.
          Josh P.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by bortz, 11-06-2023, 08:04 AM
          47 responses
          1,610 views
          0 likes
          Last Post aligator  
          Started by jaybedreamin, Today, 05:56 PM
          0 responses
          9 views
          0 likes
          Last Post jaybedreamin  
          Started by DJ888, 04-16-2024, 06:09 PM
          6 responses
          19 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by Jon17, Today, 04:33 PM
          0 responses
          6 views
          0 likes
          Last Post Jon17
          by Jon17
           
          Started by Javierw.ok, Today, 04:12 PM
          0 responses
          16 views
          0 likes
          Last Post Javierw.ok  
          Working...
          X