Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

cvs file no way to write all trades info

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

    cvs file no way to write all trades info

    Hi all,

    i am triying to write trade info in a .cvs file in backtest mode (then i want to use these method with a i live account), (understanding real time writing trade info when position is closed). I've created a method (logTradeToCVS) that writes this info in a .cvs file, my problem is the last 3 trades are never written in .cvs file.

    This is part of my code just to show you where i am calling the method...

    Code:
     
    protected override void Initialize()
    {
     //code
    }
     
    protected override void OnBarUpdate()
    {
       // Exit positions
       if (Position.MarketPosition == MarketPosition.Long)
       { 
          ExitLong();
          logTradeToCVS();
       }
       if (Position.MarketPosition == MarketPosition.Short)
       {
          ExitShort();
          logTradeToCVS();
       }
     }
     
    Print (Performance.AllTrades.Count); // prints 546
    If i print Performance.AllTrades.Count there are 546 trades in the collection (the same number is in the tab trades when the backtest ends) but in my .cvs file there are just 543 trades, no way to write this 3 last trades. What is happening? Could you help me?.

    PD: logTradeToCVS method is using try-catch blocks and no errors, i mean, trade info is correctly written arround 543 trades but 3 last trades. 546 in the collection, 543 in my .cvs file...

    Sorry about my english,
    Vince.
    Last edited by vibarco; 03-12-2012, 07:55 AM.

    #2
    Hello Vince,
    Thanks for writing in and I am happy to assist you.

    You might be getting the historical trades right but not the realtime trades. Can you verify that that your realtime trades are logged in correctly? Please verify the realtime trade counts with Performance.RealtimeTrades.Count

    I look forward to assisting you further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Vibarco,

      I'm not sure how you're doing this, but have you tried setting "AutoFlush" to true?

      VT

      Comment


        #4
        Twice?

        Thanks @VTrader (Autoflush no needed) and @NinjaTrader_Joydeep (no RealtimeTrades needed, trades are generated from backtest),

        trades are written ok now, but... Twice!, trades are generated from a backtest and if we have 'Executions', 'Trades' or 'Orders' tab selected in Strategy Analyzer then trades are written in the .cvs twice... the same in the output window, it seems like strategy (backtest) executes twice.

        Test this...
        Code:
        protected override void OnBarUpdate()
        {
        	if (Performance.AllTrades.Count > 0) && Performance.AllTrades.Count != countOld)
        	{
                        if (Performance.AllTrades.Count != countOld)
        		{
                        countOld = Performance.AllTrades.Count;
        		Print(Performance.AllTrades.Count + ";" + Performance.AllTrades[Performance.AllTrades.Count - 1].ProfitCurrency);
                        }
        	}
                // more code...
        }
        Best,
        Vince.
        Last edited by vibarco; 03-12-2012, 01:15 PM.

        Comment


          #5
          Hello Vince,
          You can use the “Export to Excel” feature if you are trying to save the data.
          • Right click on the data grid
          • In the context menu click on Grid>Exprot to Excel.


          If you want to programmatically write the backtest data then you can do it in the OnTermination event.
          Code:
          protected override void OnTermination()
          {
          	foreach (Trade trade in Performance.AllTrades)
          	{
          		//Print the trades.
          	}
          }
          Please let me know if I can be of further assistance.
          JoydeepNinjaTrader Customer Service

          Comment


            #6
            Hi NinjaTrader_Joydeep,

            Thank you for the OnTermination method, really useful when i want to export trades to a file from a backtest (i know how to export data to excel by hand). What about live/demo?i guess delends on where the trades come from we need to use one technique or another, isn't it?.

            In a live/demo strategy, wich approach is the way to go?, if i want to export data to a .csv in real time (each time a position is closed writes to a .csv file), can i use my first approach? logTradeToCVS()?, i think doing that is not so easy in a live/demo. Could you give some peace of advise please?.

            Best,
            Vince.

            Comment


              #7
              Hello Vince,
              Its difficult to comment in absence of the full code, but you can append the trade details in the OnExecution event.

              Code:
              protected override void OnExecution(IExecution execution)
              {
              	if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
              	{
              		//write to file
              	}
              }


              Also please refer to this sample code which demonstrates how to write to a file http://ninjatrader.com/support/forum...ead.php?t=3475

              Please let me know if I can assist you any further.
              JoydeepNinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Joydeep View Post
                Hello Vince,
                You can use the “Export to Excel” feature if you are trying to save the data.
                • Right click on the data grid
                • In the context menu click on Grid>Exprot to Excel.


                If you want to programmatically write the backtest data then you can do it in the OnTermination event.
                Code:
                protected override void OnTermination()
                {
                	foreach (Trade trade in Performance.AllTrades)
                	{
                		//Print the trades.
                	}
                }
                Please let me know if I can be of further assistance.
                Hi NinjaTrader_Joydeep,

                Could you please concretize
                your code:

                protected override void OnTermination()
                {
                foreach (Trade trade in Performance.AllTrades)
                {
                //Print the trades.
                }
                }

                I am interessted in how to get and write the data from the tabs e.g. "Orders", "Strategies", "Executions", "Positions", "Accounts", "Account Performance". Could you please write a more spezific short sample for one of the mentioned tabs above in order to have a clue how to applicate
                the code for the data from the other tabs.

                Many thanks.

                Best,

                Robert

                Comment


                  #9
                  Hello Robert,
                  Welcome to the forum and I am happy to assist you.

                  The Performance class can get details for the current strategy only.


                  Please let me know if I can assist you any further.
                  JoydeepNinjaTrader Customer Service

                  Comment


                    #10
                    Performance.AllTrades.TradesPerformance.Currency.P rofitPerMonth;

                    Hi NinjaTrader_Joydeep,

                    Many thanks for your prompt reply! Why do I get values of ZERO using this variables at the "protected override void OnBarUpdate()" section?
                    MaxConWin = Performance.AllTrades.TradesPerformance.MaxConsecW inner;
                    MaxConLos = Performance.AllTrades.TradesPerformance.MaxConsecL oser;
                    LargWinTrade = Performance.AllTrades.TradesPerformance.Currency.L argestWinner;
                    LargLosTrade = Performance.AllTrades.TradesPerformance.Currency.L argestLoser;
                    NrOfTradespDay = Performance.AllTrades.TradesPerformance.TradesPerD ay;
                    AvgTimeInMarket = Performance.AllTrades.TradesPerformance.AvgTradeDu ration;
                    AvGBarsInTrade = Performance.AllTrades.TradesPerformance.AvgBarsInT rade;
                    ProfitperMonth =Performance.AllTrades.TradesPerformance.Currency. ProfitPerMonth;

                    Some trades were executed yesterday end even the variable ProfitperMonth returns a value of ZERO today! Why?

                    What I have to do programmatically to receive the same results as when I use the "Account Performance" tab, adjust time from 20.03.2012 to 21.03.2012 and push the "Generate" button?

                    Many thanks.

                    Best regards,

                    Robert

                    Comment


                      #11
                      Hello Robert,
                      If you restart the strategy then the strategy will start fresh and will return 0.

                      Unfortunately there are no supported way of retrieving the performance reports from the "Accounts Performance" tab.
                      JoydeepNinjaTrader Customer Service

                      Comment


                        #12
                        What about writing walkforward trades in a cvs?

                        When i run a walkforward i need to create and excel with all the walkforward trade results of every walkforward period manually (exporting each period, cut and paste in my excel). Is there any way to do this from code?.

                        For instance:

                        Now i have manually to create a cvs file called ContinuousWalkforwardSerie.cvs

                        Walkforward period 1 - export trades to cvs - copy this trades to my ContinuousWalkforwardSerie.cvs
                        Walkforward period 2 - export trades to cvs - copy this trades to my ContinuousWalkforwardSerie.cvs
                        Walkforward period 3 - export trades to cvs - copy this trades to my ContinuousWalkforwardSerie.cvs
                        Walkforward period 4 - export trades to cvs- copy this trades to my ContinuousWalkforwardSerie.cvs

                        Can i do this from code automatically?

                        Best,
                        Vince.
                        Last edited by vibarco; 04-25-2012, 12:07 PM.

                        Comment


                          #13
                          Hello Vince,
                          Yes, you can automate the process.

                          Please refer to these reference samples which demonstrates how to write to a txt file from a NinjaScript code.




                          Please let me know if I can assist you any further.
                          JoydeepNinjaTrader Customer Service

                          Comment


                            #14
                            Thanks Joydeep,

                            How can i pass Performance.AllTrades[1].ProfitCurrency to a method?

                            myMethod(Performance.AllTrades[1].ProfitCurrency)??

                            Best,
                            Vince.

                            Comment


                              #15
                              Hello Vince,
                              Please refer to the below code to pass values to your methods

                              Code:
                              private void myMethod(double value)
                              {
                                 //codes
                              }
                              
                              //call the method from say OnBarUpdate
                              private override void OnBarUpdate()
                              {
                                 myMethod(Performance.AllTradeds[1].ProfitCurrency);
                                  //rest of the codes
                              }
                              This is more of a C# issue. Please refer here to know more.


                              Please let me know if I can assist you any further.
                              JoydeepNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by traderqz, Yesterday, 09:06 AM
                              3 responses
                              21 views
                              0 likes
                              Last Post NinjaTrader_ThomasC  
                              Started by f.saeidi, Today, 10:19 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by kujista, Today, 06:23 AM
                              5 responses
                              17 views
                              0 likes
                              Last Post kujista
                              by kujista
                               
                              Started by traderqz, Today, 12:06 AM
                              3 responses
                              6 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by RideMe, 04-07-2024, 04:54 PM
                              5 responses
                              29 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X