Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Backtesting and getting all the trades

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

    Backtesting and getting all the trades

    Hi,

    I have a question regarding SA functionalities I may not be aware of.

    What I'm trying to do
    I want to backtest a strategy on a list containing several hundred names and once it's done I want to get all the trades that resulted out of that backtest, for all the names. It's perfectly possible to do so for one name at a time.

    The question
    Is there a way of getting all the trades for all the names at once that resulted from the above backtest?

    Thanks for your help,
    Dan M.

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

    Yes, it is possible to write the trades to a file via further custom coding your NinjaScript strategy. For example you can append the trades which the strategy took in the OnTermination event.

    Code:
    protected override void OnTermination()
    {
    	foreach (Trade t in Performance.AllTrades)
    	{
                  //append the trades to a file
    	}
    }



    Please refer to the below sample codes which demonstrates how to write to a text file from a NinjaScript code.



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

    Comment


      #3
      Hi Joydeep,

      That's even better than what I was hoping for.

      Thanks for your help,
      Dan M.

      Comment


        #4
        Hello Dan,
        Thanks for your note.
        Please let me know if I can assist you any further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          How to get trades for ALL symbols in backtest?

          I am using following code in OnTermination method of the strategy to write trades data to a file. Now when I run the strategy backtest on a watchlist, only the trades data for the last symbol are getting captured in the file. How can I capture trades data for ALL symbols and write to a file. Any suggestions?
          ------------------------------------------------------------
          Code:
          protected override void OnTermination()
          		{
          			if(header)
          			{
          				string col_names = "Trade, Instrument, Market pos, Quantity, Entry price, Exit price, Entry time, Exit time, Entry name, Exit name, Profit";
          				header=false;
          				string fileFQN = dirName + fileName;
          				File.Delete(fileFQN);
          				sw = File.AppendText(fileFQN);
          				sw.WriteLine(col_names);
          			}
          			foreach(Trade trade in Performance.AllTrades)
          			{
          				System.Text.StringBuilder sb = new System.Text.StringBuilder();
          				sb.Append(trade.TradeNumber);
          				sb.Append(",").Append(trade.Entry.Instrument.FullName);
          				sb.Append(",").Append(trade.Entry.MarketPosition.ToString());
          				sb.Append(",").Append(trade.Quantity);
          				sb.Append(",").Append(trade.Entry.Price);
          				sb.Append(",").Append(trade.Exit.Price);
          				sb.Append(",").Append(trade.Entry.Time.ToShortDateString());
          				sb.Append(",").Append(trade.Exit.Time.ToShortDateString());
          				sb.Append(",").Append(trade.Entry.Name);
          				sb.Append(",").Append(trade.Exit.Name);
          				sb.Append(",").Append(Math.Round(trade.ProfitPercent,4));
          				sw.WriteLine(sb.ToString());
          				//sb.AppendLine();
          			}
          			if (sw != null)
          			{
          				sw.Dispose();
          				sw = null;
          			}
          		}
          Last edited by atrader123; 09-23-2012, 06:06 PM. Reason: Added CODE tag for formatting

          Comment


            #6
            I think now I know the problem. Looks like OnTermination() method is called per symbol during strategy backtest. I was under the impression, it will be called only once for the entire backtest irrespective of the number of symbols in watchlist. Are there any methods that can be invoked only once by the strategy during backtest irrespective of the number of symbols in the watchlist? Regards.

            Comment


              #7
              Originally posted by atrader123 View Post
              I think now I know the problem. Looks like OnTermination() method is called per symbol during strategy backtest. I was under the impression, it will be called only once for the entire backtest irrespective of the number of symbols in watchlist. Are there any methods that can be invoked only once by the strategy during backtest irrespective of the number of symbols in the watchlist? Regards.

              Removethe delete , then the append will work better, no?

              And change the header code to only do the first instrument so you don't repeat the header information

              Does that help?


              File.Delete(fileFQN);

              Comment


                #8
                Hi sledge - Thanks. I am not sure how to do only for 1st instrument and maintain the state when strategy gets executed on the next symbol. It seems for each instrument a new instance of strategy will be used. Regards

                Comment


                  #9
                  Hello atrader123,
                  Yes, a new instance of the strategy will be created for each instrument. As sledge said you need to remove the File.Delete section of the code or use unique file name for each instrument.
                  JoydeepNinjaTrader Customer Service

                  Comment


                    #10
                    Removing File.Delete helps in getting trades from all symbols into one file. But I couldn't figure out how to have column names printed only once in the file instead of once per each symbol. For now, I am removing these extra column names manually after the file is created. If any one knows, how to modify the code so that column names will be written only once then please let me know.

                    Comment


                      #11
                      Try this out....

                      Originally posted by atrader123 View Post
                      ------------------------------------------------------------
                      Code:
                      protected override void OnTermination()
                      		{
                      			if(header)
                      			{
                      				string col_names = "Trade, Instrument, Market pos, Quantity, Entry price, Exit price, Entry time, Exit time, Entry name, Exit name, Profit";
                      				header=false;
                      				string fileFQN = dirName + fileName;
                      			//	File.Delete(fileFQN);
                       if(File.Exists(fileFQN)){
                      				sw = File.AppendText(fileFQN);
                      }
                       else {  sw.File.AppendText(fileFQN);
                      				sw.WriteLine(col_names);
                      			}
                      }
                      			foreach(Trade trade in Performance.AllTrades)
                      			{
                      				System.Text.StringBuilder sb = new System.Text.StringBuilder();
                      				sb.Append(trade.TradeNumber);
                      				sb.Append(",").Append(trade.Entry.Instrument.FullName);
                      				sb.Append(",").Append(trade.Entry.MarketPosition.ToString());
                      				sb.Append(",").Append(trade.Quantity);
                      				sb.Append(",").Append(trade.Entry.Price);
                      				sb.Append(",").Append(trade.Exit.Price);
                      				sb.Append(",").Append(trade.Entry.Time.ToShortDateString());
                      				sb.Append(",").Append(trade.Exit.Time.ToShortDateString());
                      				sb.Append(",").Append(trade.Entry.Name);
                      				sb.Append(",").Append(trade.Exit.Name);
                      				sb.Append(",").Append(Math.Round(trade.ProfitPercent,4));
                      				sw.WriteLine(sb.ToString());
                      				//sb.AppendLine();
                      			}
                      			if (sw != null)
                      			{
                      				sw.Dispose();
                      				sw = null;
                      			}
                      		}

                      Comment


                        #12
                        Hi sledge - Thanks very much. That solved my problem. I appreciate your help. Regards.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by GussJ, 03-04-2020, 03:11 PM
                        15 responses
                        3,268 views
                        0 likes
                        Last Post xiinteractive  
                        Started by Tim-c, Today, 02:10 PM
                        1 response
                        7 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by Taddypole, Today, 02:47 PM
                        0 responses
                        2 views
                        0 likes
                        Last Post Taddypole  
                        Started by chbruno, 04-24-2024, 04:10 PM
                        4 responses
                        50 views
                        0 likes
                        Last Post chbruno
                        by chbruno
                         
                        Started by TraderG23, 12-08-2023, 07:56 AM
                        10 responses
                        401 views
                        1 like
                        Last Post beobast
                        by beobast
                         
                        Working...
                        X