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

Custom Trade logging

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

    Custom Trade logging

    Hello,

    I would like to log each trade as they are closed=(entry + exit). For this, I plan on using the Trade object. When and where to place the code ? I'm thinking of OnOrderUpdate(), OnExecutionUpdate(). Is there a way to access the Trade object only when it gets a trade added to it ?

    Thank you.
    Anthony

    #2
    Hello Anthony_0709,

    Thank you for your post.

    When using the Trade object, Entry will get an Execution object representing the entry. Exit will get an Execution object representing the exit. Since both of these get Execution objects, OnExecutionUpdate() should be used. OnExecutionUpdate() is called on each incoming execution of an order managed by a strategy.

    Please see the documentation below for more information about Trade and OnExecutionUpdate().

    Trade - https://ninjatrader.com/support/help.../nt8/trade.htm
    OnExecutionUpdate - https://ninjatrader.com/support/help.../execution.htm

    Let us know if we may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hello,

      How do I know when the Trade object has a complete entry ? i.e. it has received the Entry and Exit objects ?

      Thank you.
      Anthony

      Comment


        #4
        Hello Anthony_0709,

        Thank you for your note.

        You could use the Trade object to get the last Entry and Exit objects and print them to the Output window. In OnBarUpdate() we would check if there are Realtime trades followed by using the Trade object to get the last trade and assign it to a variable named lastTrade. Once we have the last trade, we can get the Entry and Exit by calling lastTrade.Entry and lastTrade.Exit.

        The code to do so would look something like this.

        protected override void OnBarUpdate()
        {
        if (SystemPerformance.RealTimeTrades.Count > 0)
        {
        // Check to make sure there is at least one trade in the collection
        Trade lastTrade = SystemPerformance.RealTimeTrades[SystemPerformance.RealTimeTrades.Count - 1];

        Execution lastEntry = lastTrade.Entry;
        Print("Last Entry: " + lastEntry);

        Execution lastExit = lastTrade.Exit;
        Print("Last Exit: " + lastExit);
        }
        }

        Please see this help guide link for more information - https://ninjatrader.com/support/help.../nt8/trade.htm
        Let us know if we may assist further.
        Last edited by NinjaTrader_BrandonH; 11-27-2020, 10:22 AM.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Hello,

          For Trade.Entry (Execution): "lastEntry" doesn't exist, same for Trade.Exit. I planned on going with OnExecutionUpdate() and accessing the Trade object from within, but as I was going through the help guides I stumbled upon OnAddTrade(). Can you add something extra as to what the help guide say about it ? which is very limited (to me).

          protected override void OnAddTrade(Cbi.Trade trade)

          {

          Values[(int)Cbi.PerformanceUnit.Currency] += trade.ProfitCurrency;

          Values[(int)Cbi.PerformanceUnit.Percent] += trade.ProfitPercent;

          Values[(int)Cbi.PerformanceUnit.Pips] += trade.ProfitPips;

          Values[(int)Cbi.PerformanceUnit.Points] += trade.ProfitPoints;

          Values[(int)Cbi.PerformanceUnit.Ticks] += trade.ProfitTicks;

          }

          What is all the sufff inside the curly brackets ? Am I safe with deleting it and putting my own stuff there ?

          Thank you.
          Anthony

          Comment


            #6
            Hello Anthony_0709,

            Thank you for that information.

            I noticed an error in the code sample in my previous post and have corrected it. 'lastEntry = lastTrade.Entry;' had an undeclared variable and should be 'Execution lastEntry = lastTrade.Entry;'. The same goes for 'lastExit'.

            Please note that Entry and Exit are Executions but they are not a property of Execution. These would need to be called in OnBarUpdate().

            This help guide documentation lists all the properties for Execution - https://ninjatrader.com/support/help.../execution.htm

            And this help guide documentation lists all the properties for Trade - https://ninjatrader.com/support/help.../nt8/trade.htm

            OnAddTrade() is for making custom Performance Metrics, not for use in strategies. The Performance Metrics code between the curly braces gets the profit value for a trade for different PerformanceUnits (Currency, Percent, etc..).

            NinjaScript uses the C# programming language and curly braces are part of how C# is structured. To learn more about that, you could use the new MSDN learning portal linked below.
            MSDN Learning Portal - https://dotnet.microsoft.com/learn/csharp

            Please let us know if we may assist further.

            Brandon H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by ScottWalsh, 04-16-2024, 04:29 PM
            7 responses
            34 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by cls71, Today, 04:45 AM
            0 responses
            5 views
            0 likes
            Last Post cls71
            by cls71
             
            Started by mjairg, 07-20-2023, 11:57 PM
            3 responses
            214 views
            1 like
            Last Post PaulMohn  
            Started by TheWhiteDragon, 01-21-2019, 12:44 PM
            4 responses
            547 views
            0 likes
            Last Post PaulMohn  
            Started by GLFX005, Today, 03:23 AM
            0 responses
            3 views
            0 likes
            Last Post GLFX005
            by GLFX005
             
            Working...
            X