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

Error while getting access to trades statistics

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

    Error while getting access to trades statistics

    Hello,

    I am facing a problem when getting access to strategy performance (the issue happens not on all computers, however...).

    Here is my code:

    Code:
    DateTime currentDay = new DateTime(Time[0].Year,Time[0].Month,Time[0].Day);
    TradeCollection currentDayTrades = null;
    double netProfit = 0;
    try{
        currentDayTrades = (TradeCollection)SystemPerformance.AllTrades.ByDay[currentDay];
        netProfit = currentDayTrades.TradesPerformance.NetProfit;
    }
    catch(Exception ex){
        Print("Error in trades performance calculation: "+ex.ToString());
    }
    And this is the exception I receive even after a trade was executed and closed:

    Error in trades performance calculation:
    System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
    at System.ThrowHelper.ThrowKeyNotFoundException()
    at System.Collections.Generic.Dictionary`2.get_Item(T Key key)
    From the chart drop-down menu I am able to access the strategy performance, but programmatically - not. Can you please advise me on how I can solve this issue?

    Best regards,
    Alexei

    #2
    Hello Alexei,

    Thank you for your post.

    From the provided information I would be unsure why the key would be not found other than the key may have some other value which is different from what you used. have you at this point tried to iterate the collection to see what keys it has when this occurs?

    If your exception is being met and moving to your catch, you could use that logically to print out the keys of the collection after the error to see what the actual keys were. This would give better insight into what's happening on that use case.

    Code:
    foreach (KeyValuePair<object, TradeCollection> entry in SystemPerformance.AllTrades.ByDay) 
    {
        if(entry.Key is DateTime)
        {
            DateTime key = (DateTime)entry.Key ;
            Print(key);
        }
    }



    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Thank you, I have done as you have advised but it has appeared to be that the trades collection is empty even though there are trades opened and closed by the strategy. Is there anything I can try to debug this further?

      Best regards,
      Alexei

      Comment


        #4
        Hello akuntysh,

        Thank you for your reply.

        The collection being empty would explain why the key could not be found.

        Without more specific details on the use case, I wouldn't be sure why the collection would be empty when checked. Do you have a simple example script which demonstrates this along with the steps to see this?

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by kujista, Today, 06:23 AM
        0 responses
        1 view
        0 likes
        Last Post kujista
        by kujista
         
        Started by traderqz, Yesterday, 04:32 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by f.saeidi, Today, 05:56 AM
        1 response
        4 views
        0 likes
        Last Post Jltarrau  
        Started by Jltarrau, Today, 05:57 AM
        0 responses
        4 views
        0 likes
        Last Post Jltarrau  
        Started by Stanfillirenfro, Yesterday, 09:19 AM
        7 responses
        52 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X