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

Retrieving historical trades of Strategy

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

    Retrieving historical trades of Strategy

    What is the correct method to retrieve historical trades (closed) by a Strategy. I want to retrieve all the trades generated by my strategy this week.
    For this, I am using Performance.AllTrades - but it returns only the trades (closed) during the current execution of the strategy.It does not return trades closed during previous executions of the strategy.

    For example, if I run my strategy live for a few hours and lets assume it generates 4 trades. The Performance.AllTrades correctly returns 4 trades.

    Now, I get disconnected (or restart NinjaTrader).
    When I run my strategy again using Performance.AllTrades does not return any trades because no new trades were placed yet.

    #2
    Hello firstlanetech,

    Thank you for your note.

    If you set the following to false under Initialize,
    Code:
     ExcludeTradeHistoryInBacktest = false;
    Do you see historical trades in addition to live trades?

    I provided a sample strategy which will buy/sell over and over when you enable it. It will print how many trades the strategy has taken both live and historically to the output window.

    I look forward to your reply.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hi Alan,

      Thank you for the reply. I tried ExcludeTradeHistoryInBacktest = false; as suggested, but it does not seem to work.

      The strategy runs on each tick - if the position is flat, open a BUY trade at market with SL and TP of 20 ticks. It does not use any indicators - only price action. We stop the strategy after 10 trades today - (just to test the AllTrades part).

      Here is what I have in Initialize - I am running it on each tick.


      Code:
      protected override void Initialize()
      {		
         CalculateOnBarClose	= false;
      [B]   ExcludeTradeHistoryInBacktest = false;
      [/B]}
      And here is the OnBarUpdate() code. I ignore if Historical is true - because my strategy is price-based and not indicator based. Does that matter ?
      It prints the number of trades today correctly. But if I stop the strategy and start it again (without changing any inputs), it prints the number of trades today as 0.


      Code:
      protected override void OnBarUpdate()
      {
      [B]	if (Historical ) return;	
      [/B]	
      	// Place a buy order at market if position is flat with Profit of 20 ticks and 
      	// Stop-loss of 20 ticks.
      	if( Position.MarketPosition == MarketPosition.Flat )
      	{
      [B]		TradeCollection trades = Performance.AllTrades;
      [/B]		int todaysTradeCount = 0;
      		foreach(Trade trade in trades)
      		{
      			if( trade.Exit.Time.ToString("dd-MMM-yyyy") == Time[0].ToString("dd-MMM-yyyy") )   // Check if historical trade was closed today
      				todaysTradeCount++;
      		}
      		Print("Number of trades Today = " + todaysTradeCount.ToString());
      		if( todaysTradeCount >= 10 )
      		{
      			Print("Strategy trade 10 times today. Stopped.");
      			this.Disable();
      		}
                      else
                      {
      		        EnterLong(10000);  // BUY @ market
      		        SetStopLoss(CalculationMode.Ticks, 20);   // Stoploss is 20 ticks
      		        SetProfitTarget(CalculationMode.Ticks, 20); // Profit is 20 ticks.
                      }
      	}				
      }

      What I am trying to achieve is to disable the strategy for the rest of the day if the daily loss (or profit) exceeds the user input amount. The strategy can be disconnected and reconnected (network issues etc) - so I need to look into historical trades to calculate the day's P/L.

      See the attached strategy -
      Works accurately on 1min EURUSD. But if you stop and restart, the Trades history is incorrect.
      Attached Files
      Last edited by firstlanetech; 08-10-2018, 01:02 AM.

      Comment


        #4
        Hello firstlanetech,

        Thank you for your response.

        Performance.AllTrades will only include trades of the current run of the strategy. These include trades calculated over historical data (which your strategy ignores) and those that are taking by the strategy now in realtime.

        Performance.RealtimeTrades will only include trades from the current run of the strategy and only those it takes in realtime.

        If you want to track previous runs of the strategy inside the strategy itself you would need to write to and read from an external file. We have an example of reading and writing to an external file at the following link: https://ninjatrader.com/support/help..._propertie.htm

        Please let me know if you have any questions.

        Comment


          #5
          Thank you for the details. I will save the trade history to a file when the OnPositionUpdate() is called.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by jclose, Today, 09:37 PM
          0 responses
          4 views
          0 likes
          Last Post jclose
          by jclose
           
          Started by WeyldFalcon, 08-07-2020, 06:13 AM
          10 responses
          1,413 views
          0 likes
          Last Post Traderontheroad  
          Started by firefoxforum12, Today, 08:53 PM
          0 responses
          10 views
          0 likes
          Last Post firefoxforum12  
          Started by stafe, Today, 08:34 PM
          0 responses
          10 views
          0 likes
          Last Post stafe
          by stafe
           
          Started by sastrades, 01-31-2024, 10:19 PM
          11 responses
          169 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Working...
          X