• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Optimizer last bar

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

    Optimizer last bar

    Hi

    Is there a way to access the very last bar of an optimizer? I want to run an optimizer and optimize a variable

    Let’s say variable is .1;1;.1; at every single iteration once the optimizer has tested ... for example... “.1” I want to print out values from my strategy.

    I’m thinking if I run the optimizer from from 1/1/2017 to 1/1/2018... I can add a condition in my strategy to print those values if the day is 1/1/2018.

    But is there a way to access the last bar of optimizers iteration for each individual iteration of a variable? Also, using a genetic optimizer, does the optimizer run each variable from 1/1/2017 to 1/1/2018 data for each iteration of a variable or does it randomly test these values within the date range in small chunks?

    Basically, I want to see the FINAL results of my strategy from different possible values of a variable and then do further analysis.

    #2
    Thank you for your question, staycool3_a


    It sounds like what you are after is processing during State.Terminated if State.Configure was reached. I have attached a NinjaScript example which accomplishes this goal, and produces the following output :


    Code:
    Last close was 2910.75 on bar 247449 for optimization over fast period 7
    Last close was 2910.75 on bar 247449 for optimization over fast period 4
    Last close was 2910.75 on bar 247449 for optimization over fast period 1
    Last close was 2910.75 on bar 247449 for optimization over fast period 2
    Last close was 2910.75 on bar 247449 for optimization over fast period 8
    Last close was 2910.75 on bar 247449 for optimization over fast period 3
    Last close was 2910.75 on bar 247449 for optimization over fast period 6
    Last close was 2910.75 on bar 247449 for optimization over fast period 5
    Last close was 2910.75 on bar 247449 for optimization over fast period 10
    Last close was 2910.75 on bar 247449 for optimization over fast period 9
    Last close was 2910.75 on bar 247449 for optimization over fast period 7

    I will address the genetic optimizer question in another post.
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      With respect to the genetic optimizer, I modified the above code so it actually places trades. Code samples we provide are for educational purposes, and are not intended for live trading, and are not guaranteed to accomplish any user goal or to be maintained.


      When I run this over all the values 1 through 100 (setting Fast to 1;100;1) we see


      • A single input value is used for each individual strategy run; the value of Fast does not change over the lifecycle of the strategy and stays the same through the entire date range
      • A random sample of starting values is selected
      • Many high scoring values are repeated. For example, look for values near the optimal value of 5, such as 6.
      • Many values are ignored


      Code:
      Last close was 2910.75 on bar 247449 for optimization over fast period 57
      Last close was 2910.75 on bar 247449 for optimization over fast period 40
      Last close was 2910.75 on bar 247449 for optimization over fast period 26
      Last close was 2910.75 on bar 247449 for optimization over fast period 98
      Last close was 2910.75 on bar 247449 for optimization over fast period 82
      Last close was 2910.75 on bar 247449 for optimization over fast period 25
      Last close was 2910.75 on bar 247449 for optimization over fast period 97
      Last close was 2910.75 on bar 247449 for optimization over fast period 69
      Last close was 2910.75 on bar 247449 for optimization over fast period 55
      Last close was 2910.75 on bar 247449 for optimization over fast period 39
      Last close was 2910.75 on bar 247449 for optimization over fast period 87
      Last close was 2910.75 on bar 247449 for optimization over fast period 32
      Last close was 2910.75 on bar 247449 for optimization over fast period 6
      Last close was 2910.75 on bar 247449 for optimization over fast period 5
      Last close was 2910.75 on bar 247449 for optimization over fast period 7
      Last close was 2910.75 on bar 247449 for optimization over fast period 91
      Last close was 2910.75 on bar 247449 for optimization over fast period 78
      Last close was 2910.75 on bar 247449 for optimization over fast period 19
      Last close was 2910.75 on bar 247449 for optimization over fast period 24
      Last close was 2910.75 on bar 247449 for optimization over fast period 28
      Last close was 2910.75 on bar 247449 for optimization over fast period 80
      Last close was 2910.75 on bar 247449 for optimization over fast period 27
      Last close was 2910.75 on bar 247449 for optimization over fast period 60
      Last close was 2910.75 on bar 247449 for optimization over fast period 68
      Last close was 2910.75 on bar 247449 for optimization over fast period 51
      Last close was 2910.75 on bar 247449 for optimization over fast period 25
      Last close was 2910.75 on bar 247449 for optimization over fast period 5
      Last close was 2910.75 on bar 247449 for optimization over fast period 57
      Last close was 2910.75 on bar 247449 for optimization over fast period 6
      Last close was 2910.75 on bar 247449 for optimization over fast period 7
      Last close was 2910.75 on bar 247449 for optimization over fast period 69
      Last close was 2910.75 on bar 247449 for optimization over fast period 26
      Last close was 2910.75 on bar 247449 for optimization over fast period 55
      Last close was 2910.75 on bar 247449 for optimization over fast period 62
      Last close was 2910.75 on bar 247449 for optimization over fast period 19
      Last close was 2910.75 on bar 247449 for optimization over fast period 63
      Last close was 2910.75 on bar 247449 for optimization over fast period 32
      Last close was 2910.75 on bar 247449 for optimization over fast period 93
      Last close was 2910.75 on bar 247449 for optimization over fast period 10
      Last close was 2910.75 on bar 247449 for optimization over fast period 74
      Last close was 2910.75 on bar 247449 for optimization over fast period 76
      Last close was 2910.75 on bar 247449 for optimization over fast period 94
      Last close was 2910.75 on bar 247449 for optimization over fast period 89
      Last close was 2910.75 on bar 247449 for optimization over fast period 49
      Last close was 2910.75 on bar 247449 for optimization over fast period 43
      Last close was 2910.75 on bar 247449 for optimization over fast period 36
      Last close was 2910.75 on bar 247449 for optimization over fast period 47
      Last close was 2910.75 on bar 247449 for optimization over fast period 95
      Last close was 2910.75 on bar 247449 for optimization over fast period 80
      Last close was 2910.75 on bar 247449 for optimization over fast period 88
      Last close was 2910.75 on bar 247449 for optimization over fast period 35
      Last close was 2910.75 on bar 247449 for optimization over fast period 2
      Last close was 2910.75 on bar 247449 for optimization over fast period 5
      Last close was 2910.75 on bar 247449 for optimization over fast period 74
      Last close was 2910.75 on bar 247449 for optimization over fast period 29
      Last close was 2910.75 on bar 247449 for optimization over fast period 69
      Last close was 2910.75 on bar 247449 for optimization over fast period 3
      Last close was 2910.75 on bar 247449 for optimization over fast period 36
      Last close was 2910.75 on bar 247449 for optimization over fast period 57
      Last close was 2910.75 on bar 247449 for optimization over fast period 26
      Last close was 2910.75 on bar 247449 for optimization over fast period 71
      Last close was 2910.75 on bar 247449 for optimization over fast period 10
      Last close was 2910.75 on bar 247449 for optimization over fast period 100
      Last close was 2910.75 on bar 247449 for optimization over fast period 84
      Last close was 2910.75 on bar 247449 for optimization over fast period 20
      Last close was 2910.75 on bar 247449 for optimization over fast period 41
      Last close was 2910.75 on bar 247449 for optimization over fast period 13
      Last close was 2910.75 on bar 247449 for optimization over fast period 91
      Last close was 2910.75 on bar 247449 for optimization over fast period 54
      Last close was 2910.75 on bar 247449 for optimization over fast period 56
      Last close was 2910.75 on bar 247449 for optimization over fast period 17
      Last close was 2910.75 on bar 247449 for optimization over fast period 7
      Last close was 2910.75 on bar 247449 for optimization over fast period 65
      Last close was 2910.75 on bar 247449 for optimization over fast period 72
      Last close was 2910.75 on bar 247449 for optimization over fast period 70
      Last close was 2910.75 on bar 247449 for optimization over fast period 2
      Last close was 2910.75 on bar 247449 for optimization over fast period 29
      Last close was 2910.75 on bar 247449 for optimization over fast period 69
      Last close was 2910.75 on bar 247449 for optimization over fast period 5
      Last close was 2910.75 on bar 247449 for optimization over fast period 57
      Last close was 2910.75 on bar 247449 for optimization over fast period 7
      Last close was 2910.75 on bar 247449 for optimization over fast period 26
      Last close was 2910.75 on bar 247449 for optimization over fast period 3
      Last close was 2910.75 on bar 247449 for optimization over fast period 10
      Last close was 2910.75 on bar 247449 for optimization over fast period 90
      Last close was 2910.75 on bar 247449 for optimization over fast period 74
      Last close was 2910.75 on bar 247449 for optimization over fast period 71
      Last close was 2910.75 on bar 247449 for optimization over fast period 6
      Last close was 2910.75 on bar 247449 for optimization over fast period 18
      Last close was 2910.75 on bar 247449 for optimization over fast period 97
      Last close was 2910.75 on bar 247449 for optimization over fast period 8
      Last close was 2910.75 on bar 247449 for optimization over fast period 91
      Last close was 2910.75 on bar 247449 for optimization over fast period 61
      Last close was 2910.75 on bar 247449 for optimization over fast period 11
      Last close was 2910.75 on bar 247449 for optimization over fast period 85
      Last close was 2910.75 on bar 247449 for optimization over fast period 99
      Last close was 2910.75 on bar 247449 for optimization over fast period 1
      Last close was 2910.75 on bar 247449 for optimization over fast period 66
      Last close was 2910.75 on bar 247449 for optimization over fast period 21
      Last close was 2910.75 on bar 247449 for optimization over fast period 13
      Last close was 2910.75 on bar 247449 for optimization over fast period 5
      Please let us know if there are any other ways we can help.
      Attached Files
      Jessica P.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_JessicaP View Post
        With respect to the genetic optimizer, I modified the above code so it actually places trades. Code samples we provide are for educational purposes, and are not intended for live trading, and are not guaranteed to accomplish any user goal or to be maintained.


        When I run this over all the values 1 through 100 (setting Fast to 1;100;1) we see


        • A single input value is used for each individual strategy run; the value of Fast does not change over the lifecycle of the strategy and stays the same through the entire date range
        • A random sample of starting values is selected
        • Many high scoring values are repeated. For example, look for values near the optimal value of 5, such as 6.
        • Many values are ignored


        Code:
        Last close was 2910.75 on bar 247449 for optimization over fast period 57
        Last close was 2910.75 on bar 247449 for optimization over fast period 40
        Last close was 2910.75 on bar 247449 for optimization over fast period 26
        Last close was 2910.75 on bar 247449 for optimization over fast period 98
        Last close was 2910.75 on bar 247449 for optimization over fast period 82
        Last close was 2910.75 on bar 247449 for optimization over fast period 25
        Last close was 2910.75 on bar 247449 for optimization over fast period 97
        Last close was 2910.75 on bar 247449 for optimization over fast period 69
        Last close was 2910.75 on bar 247449 for optimization over fast period 55
        Last close was 2910.75 on bar 247449 for optimization over fast period 39
        Last close was 2910.75 on bar 247449 for optimization over fast period 87
        Last close was 2910.75 on bar 247449 for optimization over fast period 32
        Last close was 2910.75 on bar 247449 for optimization over fast period 6
        Last close was 2910.75 on bar 247449 for optimization over fast period 5
        Last close was 2910.75 on bar 247449 for optimization over fast period 7
        Last close was 2910.75 on bar 247449 for optimization over fast period 91
        Last close was 2910.75 on bar 247449 for optimization over fast period 78
        Last close was 2910.75 on bar 247449 for optimization over fast period 19
        Last close was 2910.75 on bar 247449 for optimization over fast period 24
        Last close was 2910.75 on bar 247449 for optimization over fast period 28
        Last close was 2910.75 on bar 247449 for optimization over fast period 80
        Last close was 2910.75 on bar 247449 for optimization over fast period 27
        Last close was 2910.75 on bar 247449 for optimization over fast period 60
        Last close was 2910.75 on bar 247449 for optimization over fast period 68
        Last close was 2910.75 on bar 247449 for optimization over fast period 51
        Last close was 2910.75 on bar 247449 for optimization over fast period 25
        Last close was 2910.75 on bar 247449 for optimization over fast period 5
        Last close was 2910.75 on bar 247449 for optimization over fast period 57
        Last close was 2910.75 on bar 247449 for optimization over fast period 6
        Last close was 2910.75 on bar 247449 for optimization over fast period 7
        Last close was 2910.75 on bar 247449 for optimization over fast period 69
        Last close was 2910.75 on bar 247449 for optimization over fast period 26
        Last close was 2910.75 on bar 247449 for optimization over fast period 55
        Last close was 2910.75 on bar 247449 for optimization over fast period 62
        Last close was 2910.75 on bar 247449 for optimization over fast period 19
        Last close was 2910.75 on bar 247449 for optimization over fast period 63
        Last close was 2910.75 on bar 247449 for optimization over fast period 32
        Last close was 2910.75 on bar 247449 for optimization over fast period 93
        Last close was 2910.75 on bar 247449 for optimization over fast period 10
        Last close was 2910.75 on bar 247449 for optimization over fast period 74
        Last close was 2910.75 on bar 247449 for optimization over fast period 76
        Last close was 2910.75 on bar 247449 for optimization over fast period 94
        Last close was 2910.75 on bar 247449 for optimization over fast period 89
        Last close was 2910.75 on bar 247449 for optimization over fast period 49
        Last close was 2910.75 on bar 247449 for optimization over fast period 43
        Last close was 2910.75 on bar 247449 for optimization over fast period 36
        Last close was 2910.75 on bar 247449 for optimization over fast period 47
        Last close was 2910.75 on bar 247449 for optimization over fast period 95
        Last close was 2910.75 on bar 247449 for optimization over fast period 80
        Last close was 2910.75 on bar 247449 for optimization over fast period 88
        Last close was 2910.75 on bar 247449 for optimization over fast period 35
        Last close was 2910.75 on bar 247449 for optimization over fast period 2
        Last close was 2910.75 on bar 247449 for optimization over fast period 5
        Last close was 2910.75 on bar 247449 for optimization over fast period 74
        Last close was 2910.75 on bar 247449 for optimization over fast period 29
        Last close was 2910.75 on bar 247449 for optimization over fast period 69
        Last close was 2910.75 on bar 247449 for optimization over fast period 3
        Last close was 2910.75 on bar 247449 for optimization over fast period 36
        Last close was 2910.75 on bar 247449 for optimization over fast period 57
        Last close was 2910.75 on bar 247449 for optimization over fast period 26
        Last close was 2910.75 on bar 247449 for optimization over fast period 71
        Last close was 2910.75 on bar 247449 for optimization over fast period 10
        Last close was 2910.75 on bar 247449 for optimization over fast period 100
        Last close was 2910.75 on bar 247449 for optimization over fast period 84
        Last close was 2910.75 on bar 247449 for optimization over fast period 20
        Last close was 2910.75 on bar 247449 for optimization over fast period 41
        Last close was 2910.75 on bar 247449 for optimization over fast period 13
        Last close was 2910.75 on bar 247449 for optimization over fast period 91
        Last close was 2910.75 on bar 247449 for optimization over fast period 54
        Last close was 2910.75 on bar 247449 for optimization over fast period 56
        Last close was 2910.75 on bar 247449 for optimization over fast period 17
        Last close was 2910.75 on bar 247449 for optimization over fast period 7
        Last close was 2910.75 on bar 247449 for optimization over fast period 65
        Last close was 2910.75 on bar 247449 for optimization over fast period 72
        Last close was 2910.75 on bar 247449 for optimization over fast period 70
        Last close was 2910.75 on bar 247449 for optimization over fast period 2
        Last close was 2910.75 on bar 247449 for optimization over fast period 29
        Last close was 2910.75 on bar 247449 for optimization over fast period 69
        Last close was 2910.75 on bar 247449 for optimization over fast period 5
        Last close was 2910.75 on bar 247449 for optimization over fast period 57
        Last close was 2910.75 on bar 247449 for optimization over fast period 7
        Last close was 2910.75 on bar 247449 for optimization over fast period 26
        Last close was 2910.75 on bar 247449 for optimization over fast period 3
        Last close was 2910.75 on bar 247449 for optimization over fast period 10
        Last close was 2910.75 on bar 247449 for optimization over fast period 90
        Last close was 2910.75 on bar 247449 for optimization over fast period 74
        Last close was 2910.75 on bar 247449 for optimization over fast period 71
        Last close was 2910.75 on bar 247449 for optimization over fast period 6
        Last close was 2910.75 on bar 247449 for optimization over fast period 18
        Last close was 2910.75 on bar 247449 for optimization over fast period 97
        Last close was 2910.75 on bar 247449 for optimization over fast period 8
        Last close was 2910.75 on bar 247449 for optimization over fast period 91
        Last close was 2910.75 on bar 247449 for optimization over fast period 61
        Last close was 2910.75 on bar 247449 for optimization over fast period 11
        Last close was 2910.75 on bar 247449 for optimization over fast period 85
        Last close was 2910.75 on bar 247449 for optimization over fast period 99
        Last close was 2910.75 on bar 247449 for optimization over fast period 1
        Last close was 2910.75 on bar 247449 for optimization over fast period 66
        Last close was 2910.75 on bar 247449 for optimization over fast period 21
        Last close was 2910.75 on bar 247449 for optimization over fast period 13
        Last close was 2910.75 on bar 247449 for optimization over fast period 5
        Please let us know if there are any other ways we can help.
        Thank you, Jessica.

        This is perfect.

        Another aspect of my question is:

        When the genetic optimizer is running, when it picks 5 for fast period... does it run this for the entire date range or does it randomly select random dates within the date range.

        Thank you

        Comment


          #5
          BTW:

          This is what I ended up doing to get the values and it seems to be printing the values being optimized at the "end" of each optimization itteration:

          Code:
          protected override void OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition)
          {
                	
              if (position.MarketPosition == MarketPosition.Flat)
              {
          		++strategy_trade_counts;
          		Trade a = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];		
          		if(a.ProfitCurrency>=0)
          			++winning_count;
          		
          		if(a.ProfitCurrency<0)
          			++losing_count;
          		
          		winning_average = SystemPerformance.AllTrades.WinningTrades.TradesPerformance.NetProfit / winning_count;
          		losing_average = Math.Abs(SystemPerformance.AllTrades.LosingTrades.TradesPerformance.NetProfit) / losing_count;
          				
          		cum_profit =SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit;
          	
              }
          	
          			if(Times[0][0].Date == new DateTime(2014, 12, 31))
          			{			
          			Print(string.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};{10}",Time[0],strategy_trade_counts,winning_count,losing_count,winning_average,losing_average,PL_Ratio,Target,break_even,Tapper_increments,cum_profit));
          			}
          	
          	
          	
            
          }
          I needed to calculate the expectancy for my strategy so I needed to calculate my correct winning, losing trade count etc and due to the issue I talked about in the other thread you're working on with me I had to manually count my trades. I added the print date statement to print the values at the end of my optimization iteration. The date has to be the last day of the date range for the optimization. I'm just not sure if the optimizer uses those values and runs them for the entire date range.

          -ps; I did a test; the optimizer iterations were 20; and the output printed that I was looking for is also 20 and then I tested the output individually in the strategy analyzer and the cum_profit matches the output I printed during the optimzition so the answer to my question is, "YES, each iteration runs through the entire period for a given variable the optimizer is testing."
          Last edited by staycool3_a; 09-17-2018, 08:47 PM.

          Comment


            #6
            You are correct. As mentioned previously, a single input value is used for each individual strategy run; the value of Fast does not change over the lifecycle of the strategy and stays the same through the entire date range.


            If you would like these values to change within a date range I would recommend reviewing the walk forward optimizer, https://ninjatrader.com/support/help...e_a_strate.htm


            Please let us know if there are any other ways we can help.
            Jessica P.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by dr0832, 01-04-2019, 10:34 AM
            7 responses
            141 views
            0 likes
            Last Post Lancer
            by Lancer
             
            Started by cbentrikin, Today, 07:46 PM
            0 responses
            4 views
            0 likes
            Last Post cbentrikin  
            Started by RedDuke, Today, 06:14 PM
            0 responses
            4 views
            0 likes
            Last Post RedDuke
            by RedDuke
             
            Started by DariusDeck, Today, 04:22 PM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_Marco  
            Started by pstrusi, Today, 05:05 PM
            1 response
            3 views
            0 likes
            Last Post pstrusi
            by pstrusi
             
            Working...
            X