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

Export trades to CSV in real-time

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

    Export trades to CSV in real-time

    Hi guys,

    I wanted to save every occurring trade made by a strategy into a CSV file as they happen (upon new trade -> open file -> save trade -> close file). To achieve this, I wrote a new PerformanceMetric script because it provides a convenient method that one can override: protected override void OnAddTrade(Cbi.Trade trade).

    The Cbi.Trade object contains everything I need.

    Now, my questions/concerns are:
    1. Is this a good approach? I tried to write AddOns and Indicators but was unable to get the same results. There is always some piece of data missing.
    2. I tried this with a backtest only. Will this performance metric script work once I start the strategy in real-time?
    3. How can I get the strategy name from within the performance metric script? I would help to create one CSV file per strategy.
    Please find attached the performance metric script and also the resulting CSV file with the trades.

    Thank you!

    Damián.
    Attached Files

    #2
    Hello damianfanaro,

    Using this approach would only work for backtesting, performance metrics are only calculated in a backtest/optimization.

    This type of logging would generally be best added directly to the strategy which is placing the trades. You can use the OnExecutionUpdate to know when executions happen, if you specifically wanted the paired Trade from the performance you can use the AllTrades collection to get the last trade in the collection: https://ninjatrader.com/support/help...tsub=alltrades
    To drive the logic you could use OnBarUpdate or OnExecutionUpdate if you want to log exactly when an order is filled. https://ninjatrader.com/support/help...ub=onexecution

    If you migrate this to a strategy the name can be found using Name. I am otherwise not aware of a way to get the name of the strategy from just the Trade infomration, that is more of a raw data processing override which doesn't rely on a specific strategy.


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

    Comment


      #3
      Thanks NinjaTrader_Jesse for your advice.

      I finally wrapped this logic up within an indicator because I didn't want to mix this logging logic with the strategy's logic.

      Sharing the code with the community in case somebody finds it useful.

      Cheers,

      Attached Files

      Comment


        #4
        damianfanaro How does the indicator work so that you see the trades afterwards? I tried it but didn't know where to find the CSV file afterwards.

        Comment


          #5
          Also damianfanaro, do you know how to code a simple breakeven trigger into a strategy when you are up 30 ticks from your initial position?

          Comment


            #6
            what is the location of this generated file

            Comment


              #7
              This is cool thank you!

              Comment


                #8
                Hi Cubanaso24 / FernandoParra / moontrader1 ,

                Once you import the TradesExporter indicator, the instantiation within the strategy is as follows:

                1. Within the OnStateChange method:

                Click image for larger version

Name:	TradesExporter-Instantiation.png
Views:	696
Size:	29.7 KB
ID:	1188827

                2. Override the OnExecutionUpdate method:

                Click image for larger version

Name:	TradesExporter-OnExecutionUpdate.png
Views:	660
Size:	53.6 KB
ID:	1188828

                You can remove the TradesExporterActivated variable.
                It's just a flag I've added to activate/deactivate the indicator from the GUI.

                3. The path of the exported CSV file is at:

                Click image for larger version

Name:	TradesExporter-FileLocation.png
Views:	669
Size:	23.0 KB
ID:	1188829

                Of course you can change it from the indicator as you see fit.

                Cheers,
                Damián.

                Comment


                  #9
                  hey damian Thanks for your replay, Im try to modify the line that you put overthere bus I have some errors, can you upload the file again with the modification thanks a lot men!!!

                  Comment


                    #10
                    Hi FernandoParra - If you are referring to the strategy that uses the indicator, here it goes. It's just a non-sense strategy but useful to showcase this.
                    Attached Files

                    Comment


                      #11
                      I would like to thank damianfanaro for posting this - very useful code.

                      Upon OnExecution event, is it possible to write indicator values at the time of the trade to a csv file? Regards and thx
                      Last edited by elliot5; 11-22-2022, 04:22 AM.

                      Comment


                        #12
                        Thank you very much for sharing this! Very useful for me.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by wzgy0920, 04-20-2024, 06:09 PM
                        2 responses
                        26 views
                        0 likes
                        Last Post wzgy0920  
                        Started by wzgy0920, 02-22-2024, 01:11 AM
                        5 responses
                        32 views
                        0 likes
                        Last Post wzgy0920  
                        Started by wzgy0920, Yesterday, 09:53 PM
                        2 responses
                        49 views
                        0 likes
                        Last Post wzgy0920  
                        Started by Kensonprib, 04-28-2021, 10:11 AM
                        5 responses
                        191 views
                        0 likes
                        Last Post Hasadafa  
                        Started by GussJ, 03-04-2020, 03:11 PM
                        11 responses
                        3,230 views
                        0 likes
                        Last Post xiinteractive  
                        Working...
                        X