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

Can we write to TradeCollection?

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

    Can we write to TradeCollection?

    Hi, I have a module within my strategy that performs a historical test of a cross-over of 2 moving averages and the trades that would have been generated by these cross-over signals. This happens while the strategy is live.

    I am looking for a simple way to log my 'virtual' trades within the strategy. I would also like to track the statistics such as entry/exit times, MAE/MFE, etc.

    To avoid reinventing the wheel, is it possible for me to instantiate and instance of the TradeCollection object and write my own virtual Trades to the collection?

    Thanks in advance.

    -Dave

    #2
    Hello Dave,

    Thank you for your post.

    You could use if(Historical) and process your cross over conditions in historical, then use the Historical Performance Report of the Strategy Performance to see how these trades would have performed with the Performance Statistics available in NinjaTrader.

    There is no supported method to create virtual trades to check their Performance Statistics in NinjaScript. You could write and read to a text file, but then you would also need to create your own calculations for the Performance Statistics as there would not be a method to add these as trades for the Trades Collection.

    For a reference sample on writing to and reading from a text file please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=3477

    Please let me know if I may be of further assistance.

    Comment


      #3
      Thanks Patrick for the quick reply. As I mentioned I need to perform this test while the strategy is live, and if I understand correctly, if(Historical) would always return 'false' in a live enabled strategy. If so I don't think this would work for me.

      The crude code snippet below is something that I wrote to calculate PnL from these virtual trades... it does the job but will start to become unruly as I add statistics tracking. Again, I was hoping not to have to reinvent the wheel, but if this is the case then that's fine, I just need to know either way so I can get started.

      cheers -Dave

      Code:
      for (int idx = 20; idx <= CurrentBar; idx++) //Loop from Bar 20 to current bar
      {
          if (SMA(fast)[idx] > SMA(slow) && SMA(fast)[idx-1] < SMA(slow)[idx-1] ) // go long.. simulating NT 'CrossAbove' function
                  {
                      pnl = (Open[EntryBar] - Open[idx]);//*Instrument.MasterInstrument.PointValue;
                      cumulativePnL += pnl;
                      cumulativeShortPnL += pnl;
                      EntryBar = idx;
                  } // end long logic
                      
          else if (SMA(fast)[idx] < SMA(slow)[idx] && SMA(fast)[idx-1] > SMA(slow)[idx-1])// go short.. simulating NT 'CrossAbove' function
                  {
                      pnl = (Open[idx] - Open[EntryBar]);//*Instrument.MasterInstrument.PointValue;
                      cumulativePnL += pnl;
                      cumulativeLongPnL += pnl;
                      EntryBar = idx;
                  }
      }

      Comment


        #4
        Hello Dave,

        Thank you for your response.

        if(Historical) will work on a real-time strategy as the strategy will calculate on historical values unless you specific if(Hsitorical)return;

        You can check the hsitorical performance of your strategy by right clicking in your chart > select Strategy Performance > your strategy > Historical.

        Please let me know if I may be of further assistance.

        Comment


          #5
          Hi Patrick... i probably wasn't was clear in my original posting... Within my strategy I have 2 modules: 'Module1' executes trades (based on real-time and historical data, but this is irrelevant) and contains my main strategy rules. In a separate module 'Module2' I am essentially performing a back-test/back-fill using 2 moving averages so that my main Module1 can use the results to make trading decisions.

          So my strategy's logic might be something like:

          'If the PnL of the cross-over of SMA(fast) and SMA(slow) from bar 20 to the current bar is greater than 5% && <insert main strategy logic here> then EnterLong()'

          In order to do this test I need to loop through each of the previous bars (from bar 20 to Current Bar) to see if those 2 moving averages crossed, then use those as virtual trade signals to determine the PnL. if(Historical) won't really help in this regard.

          I've since poked around a bit and I think i found something that would work... posting it here for future readers with the same issue as me...:

          variable region:

          Code:
          TradeCollection MyTradeCollection;
                  Trade MyTrade;
                  IExecution MyIExecution;
          during OnBarUpdate:

          Code:
          // create an execution...
                      MyIExecution.Token = SomeToken;
                      MyIExecution.Price = Open(CurrentBar);
                      ....<add other elements of an IExecution such as entry time, etc.
              
                  // create a trade from the execution...
                  
                      MyTrade.TradeNumber = 1;
                      MyTrade.Quantity = 1;
                      ....etc.
                      
                  // add the trade to the trade collection...
                      MyTradeCollection.Add(MyTrade);
          cheers -Dave

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by benmarkal, Yesterday, 12:52 PM
          3 responses
          22 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by helpwanted, Today, 03:06 AM
          1 response
          18 views
          0 likes
          Last Post sarafuenonly123  
          Started by Brevo, Today, 01:45 AM
          0 responses
          11 views
          0 likes
          Last Post Brevo
          by Brevo
           
          Started by aussugardefender, Today, 01:07 AM
          0 responses
          6 views
          0 likes
          Last Post aussugardefender  
          Started by pvincent, 06-23-2022, 12:53 PM
          14 responses
          244 views
          0 likes
          Last Post Nyman
          by Nyman
           
          Working...
          X