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

Conditional orders based on "past backtesting results"

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

    Conditional orders based on "past backtesting results"

    I'd like to place entry positions based on the success of previous backtest results.
    The Buy condition is "If past 80 trades yield a Profit Factor > 1.2", place a Buy order.

    How do i code something like this (or configure this if it's a feature in NT)?
    Sample code appreciated ..

    thanks
    Kiran

    #2
    Hello kbellare,

    Thank you for your note.

    Each time the strategy is enabled it runs over the historical data. This means it backtests and the performance objects hold information.

    Please take a look at the information available on the TradesCollection: http://ninjatrader.com/support/helpG...collection.htm

    Comment


      #3
      Originally posted by NinjaTrader_PatrickH View Post
      Hello kbellare,

      Thank you for your note.

      Each time the strategy is enabled it runs over the historical data. This means it backtests and the performance objects hold information.

      Please take a look at the information available on the TradesCollection: http://ninjatrader.com/support/helpG...collection.htm
      Thanks Patrick,

      This is very helpful. Couple of questions -
      1) How can i create a TradeCollection object with the last 20 trades? I can then evaluate the cumulative Profit Factor of these last 20 trades ..
      I tried the following code below but it doesn't seem to work -
      ////Code below ..//////////////
      TradeCollection last20Trades = Performance.AllTrades.GetTrades("*", "*", 20);
      if (last20Trades.TradesPerformance.ProfitFactor>1.2) {
      // Continue to open new positions
      }

      2) In a Walk-forward optimization, can i force a re-optimization based on this condition instead of fixed Optimize/Test days? e.g. Re-optimize if Profit Factor of last 20 trades drop, start a new optimization run.
      Not sure if there's a custom Type .cs file i can modify for this logic.

      thanks
      Kiran
      Last edited by kbellare; 08-24-2015, 01:24 PM.

      Comment


        #4
        Hello Kiran,

        Thank you for your patience.

        1. You would use the code below. I would recommend reviewing our statistics definitions and the TradeCollections:
        Code:
        			double last20P = 0;
        			double last20L = 0;
        			double last20Q = 0;
        			double last20GP = 0;
        			double last20GL = 0;
        			double last20PF = 0;
        			if(Performance.AllTrades.Count == 20)
        			{
        				for(int i = 19; i >= 0; i--)
        				{
        					Trade myTrade = Performance.AllTrades[Performance.AllTrades.Count - (i + 1)];
        					if(myTrade.ProfitCurrency < 0)
        					{
        						last20L = myTrade.ProfitCurrency;
        						last20Q = myTrade.Quantity;
        						last20GL += last20L * last20Q;
        					}
        					else if(myTrade.ProfitCurrency >= 0)
        					{
        						last20P = myTrade.ProfitCurrency;
        						last20Q = myTrade.Quantity;
        						last20GP += last20P * last20Q;
        					}
        				}
        				last20PF = last20GP / last20GL;
        				Print(Time[0]);
        				Print(last20PF);
        				Print("");
        			}
        2. This is unsupported in NinjaTrader 7 but it is possible. You can find an example under (My) Documents\NinjaTrader 7\bin\Custom\Type in your PC. It will be named @DefaultOptimizationMethod.cs.

        Comment


          #5
          Self-optimizing strategy run

          Originally posted by NinjaTrader_PatrickH View Post
          Hello Kiran,

          2. This is unsupported in NinjaTrader 7 but it is possible. You can find an example under (My) Documents\NinjaTrader 7\bin\Custom\Type in your PC. It will be named @DefaultOptimizationMethod.cs.
          Thanks Patrick,
          Would like to take this further and have a "self-optimizing strategy" which optimizes when performance suffers and adjusts with new optimal parameters. Following is the flow -

          //1) Check for performance after every 20 trades - can use the code you provided below in 1.
          //2) Call Optimize with the set of optimization parameters
          Optimize();
          3) Check optimization results - Net Profit, Profit Factor, and reset the strategy parameters if these need to be updated

          Questions
          a) how do we set the optimization parameters from the code (instead of GUI)?
          b) how do we get the optimization results - metrics such as Net Profit, Profit Factor?
          Appreciate if you could show some sample code for this. Didn't find this in the @DefaultOptimizationMethod.cs

          thanks
          Kiran

          Comment


            #6
            Hello Kiran,

            Thank you for your response.

            I do not believe the Strategy Analyzer is exposed even with unsupported code, but I could be wrong. So we would not entirely be able to do what you are attempting.

            Results would have to manually calculated as I did if not found in the Performance collection: http://ninjatrader.com/support/helpG...erformance.htm

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by rbeckmann05, Yesterday, 06:48 PM
            1 response
            12 views
            0 likes
            Last Post bltdavid  
            Started by llanqui, Today, 03:53 AM
            0 responses
            6 views
            0 likes
            Last Post llanqui
            by llanqui
             
            Started by burtoninlondon, Today, 12:38 AM
            0 responses
            10 views
            0 likes
            Last Post burtoninlondon  
            Started by AaronKoRn, Yesterday, 09:49 PM
            0 responses
            15 views
            0 likes
            Last Post AaronKoRn  
            Started by carnitron, Yesterday, 08:42 PM
            0 responses
            11 views
            0 likes
            Last Post carnitron  
            Working...
            X