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 funk10101, Today, 09:43 PM
          0 responses
          6 views
          0 likes
          Last Post funk10101  
          Started by pkefal, 04-11-2024, 07:39 AM
          11 responses
          37 views
          0 likes
          Last Post jeronymite  
          Started by bill2023, Yesterday, 08:51 AM
          8 responses
          44 views
          0 likes
          Last Post bill2023  
          Started by yertle, Today, 08:38 AM
          6 responses
          26 views
          0 likes
          Last Post ryjoga
          by ryjoga
           
          Started by algospoke, Yesterday, 06:40 PM
          2 responses
          24 views
          0 likes
          Last Post algospoke  
          Working...
          X