Announcement

Collapse
No announcement yet.

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, 09: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 sidlercom80, Today, 02:40 AM
            0 responses
            3 views
            0 likes
            Last Post sidlercom80  
            Started by b16_aln, Today, 01:52 AM
            0 responses
            7 views
            0 likes
            Last Post b16_aln
            by b16_aln
             
            Started by junkone, Yesterday, 08:55 PM
            1 response
            9 views
            0 likes
            Last Post NinjaTrader_Jason  
            Started by adaeiqendqw, 01-16-2021, 02:56 AM
            1 response
            15 views
            0 likes
            Last Post NinjaTrader_Jason  
            Started by [email protected], Yesterday, 06:10 PM
            5 responses
            16 views
            0 likes
            Last Post NinjaTrader_ChrisSch  
            Working...
            X