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 vecnopus, Today, 06:15 AM
          0 responses
          1 view
          0 likes
          Last Post vecnopus  
          Started by Aviram Y, Today, 05:29 AM
          0 responses
          5 views
          0 likes
          Last Post Aviram Y  
          Started by quantismo, 04-17-2024, 05:13 PM
          3 responses
          27 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by ScottWalsh, 04-16-2024, 04:29 PM
          7 responses
          36 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by cls71, Today, 04:45 AM
          0 responses
          6 views
          0 likes
          Last Post cls71
          by cls71
           
          Working...
          X