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

Goal: MAE & MFE tracker given a stop value; seeking programming ideas

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

    Goal: MAE & MFE tracker given a stop value; seeking programming ideas

    Howdy all--

    I have built a custom indicator that produces entry signals given some conditions that I was interested in watching. The indicator works great, and it does the thinking for me so that I can focus on context.

    I would like to do a bit of back testing on it, and I am seeking some advice and counsel on how I might go about accomplishing the following high level objectives:

    1. I want to assign each indicator entry with an entry price, Maximum Adverse Excursion ("MAE") and Maximum Favorable Excursion ("MFE") for each incoming tick.

    2. Then, for each incoming tick, I want each entry's MAE and MFE to be updated.

    3. If the incoming tick pulls price through a user defined stop value (in ticks) for one of the entries, I would like the "stopped" entries to set the MAE to the stop loss value, and then stop incrementing the MFE because the stop has been hit and therefore that trade would be finished.

    4. Therefore, with each incoming tick, entries that had a MAE equal to the stop value would be skipped over.

    5. I then want to export the entries' MAE and MFE to a csv file--or if that is too difficult, to a output window where I could copy the results and then paste them into excel, where I could massage the data to extract the entry number, MAE and MFE from the copied data.

    Given the above, it seems that some sort of an array or DataSeries would be the best vehicle to use, but I'm not quite sure how to go about this. So I'm looking for some high level structure advice and recommendations that I can use to get going. I would hope that the initial high level advice would then allow me to get going, at which point I would ask progressively more detailed questions as necessary.

    So to start off, I'm looking for initial direction (ie, use an array or use a DataSeries or use an ArrayList or whatever), at which point I can google my google off to get going.

    Please do not hesitate to shoot me any questions.

    Thanks in advance!

    Aventeren

    #2
    The native Performance methods will only give you the Average MAE/MFE from the collection of trades. If you want to calculate these for each trade and update tick per tick, you'd need to develop a custom method for calculating these values and then updating them with each incoming tick.

    For backtesting purposes, you would need to Add() the 1-tick series in order for you to recalculate these values.

    As far as storing them, you should can just store them in an array using the DataSeries class.

    Information on how to Sync a custom data series can be found below:

    Note: In NinjaTrader 8 It is no longer needed to use an indicator to sync a secondary series. This can be done directly from the Series<T> (https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?seriest.htm) constructor. This post is left for historical purposes. Series objects are useful for


    Please let me know if you have any questions.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Originally posted by NinjaTrader_Matthew View Post
      The native Performance methods will only give you the Average MAE/MFE from the collection of trades. If you want to calculate these for each trade and update tick per tick, you'd need to develop a custom method for calculating these values and then updating them with each incoming tick.

      For backtesting purposes, you would need to Add() the 1-tick series in order for you to recalculate these values.

      As far as storing them, you should can just store them in an array using the DataSeries class.

      Information on how to Sync a custom data series can be found below:

      Note: In NinjaTrader 8 It is no longer needed to use an indicator to sync a secondary series. This can be done directly from the Series<T> (https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?seriest.htm) constructor. This post is left for historical purposes. Series objects are useful for


      Please let me know if you have any questions.
      Thanks, Matthew; so you believe that I should use a DataSeries as the mechanism to store, assess and report each trade's calculated MAE and MFE? Do you think I should use two separate DateSeries--one for the MAE and one for the MFE values? For instance, let's say we're talking about trade 15, then if I created two separate DataSeries for the MAE and MFE, then [14] element in each DataSeries would correspond to the 15th trade?

      Also, in terms of incrementing through the DataSeries on each BarUpdate, would I use a "foreach" command to increment through the DataSeries?

      Thanks for your help and advice--I REALLY appreciate it.

      Comment


        #4
        Hi,

        Yes, that should work as you'd be able to assign the series a value with each bar and access it when needed. It would likely be 'cleaner' to use two different data series for each metric..

        If i'm following correctly, you shouldn't need to to use foreach as OnBarUpdate will run through each series and can be accessed using myMAE[0]. This should give you the current mae for every tick if you've synced to a 1-tick series.
        MatthewNinjaTrader Product Management

        Comment


          #5
          Originally posted by NinjaTrader_Matthew View Post
          Hi,

          Yes, that should work as you'd be able to assign the series a value with each bar and access it when needed. It would likely be 'cleaner' to use two different data series for each metric..

          If i'm following correctly, you shouldn't need to to use foreach as OnBarUpdate will run through each series and can be accessed using myMAE[0]. This should give you the current mae for every tick if you've synced to a 1-tick series.
          That makes sense. Thanks.

          Okay, how to I go about printing the DataSeries elements (ie, [0], [1], etc) to a CSV file?

          Comment


            #6
            We do not have an example on this topic, but perhaps this link will help:



            You may find it easier to just write to a text file using StreamWriter:

            MatthewNinjaTrader Product Management

            Comment


              #7
              Originally posted by NinjaTrader_Matthew View Post
              We do not have an example on this topic, but perhaps this link will help:



              You may find it easier to just write to a text file using StreamWriter:

              http://www.ninjatrader.com/support/f...ead.php?t=3475
              Thanks! I'm off to learn about StreamWriter...

              Comment


                #8
                Originally posted by NinjaTrader_Matthew View Post
                Hi,

                Yes, that should work as you'd be able to assign the series a value with each bar and access it when needed. It would likely be 'cleaner' to use two different data series for each metric..

                If i'm following correctly, you shouldn't need to to use foreach as OnBarUpdate will run through each series and can be accessed using myMAE[0]. This should give you the current mae for every tick if you've synced to a 1-tick series.
                Matthew, I've been thinking about your OnBarUpdate comment, and I think that I need to run some sort of a loop to cycle back through all of the myMFE and myMAE DataSeries elements to see whether or not the incoming tick will increment all of the open myMFE items, skip the stopped myMAE items, decrement the open myMAE items, correct? Wouldn't this necessitate that I run some sort of loop to cycle through the DataSeries elements (ie, [0], [1], etc) to see how the incoming tick will update the DataSeries?

                Comment


                  #9
                  Oh I see, yes for point #3 of your original point, you would need to loop through the array to see if these values would change. Sorry for any confusion.
                  MatthewNinjaTrader Product Management

                  Comment


                    #10
                    Originally posted by NinjaTrader_Matthew View Post
                    Oh I see, yes for point #3 of your original point, you would need to loop through the array to see if these values would change. Sorry for any confusion.
                    Cool; so a foreach would be best for looping?

                    Comment


                      #11
                      That would be the safest route as it will stop the iteration once it reaches the end of the array. If you used a for loop, you need to specify when you want the loop to end.
                      MatthewNinjaTrader Product Management

                      Comment


                        #12
                        Matthew, is a DataSeries limited in how large the array can be? For instance, I'm looking to backtest each CL contract, and my indicator will create somewhere in the neighborhood of 800 possible entries per 30 days. Will a DataSeries be able to hold 800 elements?

                        Thanks,

                        Aventeren

                        Comment


                          #13
                          The data series will only hold as many bars you have on the chart. There are no limits i'm aware of beyond.
                          MatthewNinjaTrader Product Management

                          Comment


                            #14
                            DataSeries vs Array

                            Matthew--

                            Another question on the DataSeries versus Array approach. From what I understand about DataSeries, the syntax is myMFE[1] is equivalent to the MFE from 1 bar ago. However, my trades will not be keyed to bars ago, but rather a uniquely assigned trade number that will increment each time a trade is made. Therefore I was thinking that myMFE[14] would refer to the MFE for trade #15, which is not the MFE from 14 bars ago. Do you see the distinction?

                            Given the above, do you think that a DataSeries is still the best structure or should I instead consider using an Array or an ArrayList?

                            Thanks for your help--I REALLY appreciate it.

                            All best,

                            Aventeren

                            Comment


                              #15
                              Any data series you create is going to contain the same number of elements as there are bars on the chart.

                              If you wish to ONLY store your mae/mfe and would like to access these as per the # of trades, then you'd need to use a list.

                              Otherwise using a data series, it will refer to the value at the X barsAgo
                              MatthewNinjaTrader Product Management

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Max238, Today, 01:28 AM
                              1 response
                              21 views
                              0 likes
                              Last Post CactusMan  
                              Started by giulyko00, Yesterday, 12:03 PM
                              2 responses
                              10 views
                              0 likes
                              Last Post giulyko00  
                              Started by r68cervera, Today, 05:29 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post r68cervera  
                              Started by geddyisodin, Today, 05:20 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post geddyisodin  
                              Started by JonesJoker, 04-22-2024, 12:23 PM
                              6 responses
                              37 views
                              0 likes
                              Last Post JonesJoker  
                              Working...
                              X