Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using MA to identify stocks with specific characteristics

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

    Using MA to identify stocks with specific characteristics

    OK, here is what I am thinking about doing - not sure if MA has the ability to do this, but I think it does if one uses NinjaScript.

    I would like to look at all stocks and identify ones that meet certain criteria - for example EOD close above 10, a minimum dollar value of stock traded daily over $1M, and perhaps a few other criteria. This would reduce the stock number to perhaps a few hundred which could be used as an instrument list for next day trading.

    I thought one could use the countif() function to screen for these simultaneous occurrances as follows:

    CountIf(delegate {return CurrentDayOHL().CurrentClose[0] > 10} && delegate {return CurrentDayOHL().CurrentClose[0]*volume > 1000000} && ...(additional conditions),1)

    But from here I am not sure how this would select a stock, and how the selected stocks would form a new instrument list.

    Any help on either the strategy or specific code execution would be greatly appreciated.

    Kevin

    #2
    Kevin,

    The Market Analyzer only returns values in a grid like format, and can have alerts or filters setup. You can have the market analyzer check for particular numeric values, etc.

    It can be used with NinjaScript to scan for stocks if you create an indicator that identifies these conditions and returns some value you expect in your filter. There should only be a need for something like this in an indicator :

    Code:
    if ( my_conditions_are_true )
    {
       Plot0.Set(1);
    }
    else
    {
       Plot0.Set(0);
    }
    Then you just create a "filter" in the market analyzer that only displays the stocks where its Plot0[0] == 1.

    I would not suggest using a strategy in this case.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Ok, so is there a way (either MA or some other piece of NT) that scans ALL stocks against a specific indicator and can take the output and automatically add to or replace the current contents of an instrument list?

      Obviously when thinking about truly automated trading it is important to make sure that the various pieces of your software can interact seamlessly. I want to have the software scan for stocks that meet specific criteria, then have those stocks automatically update a user-defined instrument list and have them ready for my trading strategy to monitor and trade the next day. Is this achievable in NT?

      Comment


        #4
        Originally posted by Kevin_in_GA View Post
        Ok, so is there a way (either MA or some other piece of NT) that scans ALL stocks against a specific indicator and can take the output and automatically add to or replace the current contents of an instrument list?

        Obviously when thinking about truly automated trading it is important to make sure that the various pieces of your software can interact seamlessly. I want to have the software scan for stocks that meet specific criteria, then have those stocks automatically update a user-defined instrument list and have them ready for my trading strategy to monitor and trade the next day. Is this achievable in NT?
        Yes, that is a job for the Market Analyzer.

        Comment


          #5
          Can you or one of the NT support guys explain how this happens? Possibly show the code or provide instructions on how to do this? It would be of great help to those of us just starting out on this journey.

          Kevin

          Comment


            #6
            Kevin,

            The market analyzer basically can be used to visually scan stocks, so for example only the instruments that pass certain criteria show up in that list. You setup a list of stocks, then setup a filter, and then only the stocks that pass the criteria of the filter show up. This is all done manually.

            I am noticing from your post you perhaps want an autotrading strategy that is updated with some list of stocks. There are a few ways you could do this, such as writing to a text file which instruments pass certain criteria and then having your strategy read it, to having a multi-series strategy that scans for these itself.

            What is done with code as far as using the Market Analyzer for scanning for stock swould be setting up your more complex criteria as an indicator. Indicators have plots that can be set to certain values dependent on how you code it.

            I would suggest to get familiar with how NinjaScript code works. Its full C# but the platform is engineered so certain things are expected and really the only way to get through it all with full understanding would be to start at the beginning.

            Essentially its as simple as what I provided in a previous post, setting a "plot" "dataseries" to be 0 if your conditions are not met, or 1 if your conditions are met. Then, you setup a filter in the Market Analyzer to not show anything that doesnt have 1 in that plot manually (no code is needed for the filter setup).

            Here is our NinjaScript educational resources : http://www.ninjatrader.com/support/h..._resources.htm

            Here is a video that may help : http://www.youtube.com/watch?v=JZpo01eSO9c

            Here are reference samples for various things : http://www.ninjatrader.com/support/f...splay.php?f=30

            Please let me know if I may assist further.
            Last edited by NinjaTrader_AdamP; 06-03-2012, 10:51 AM.
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_AdamP View Post
              Kevin,

              The market analyzer basically can be used to visually scan stocks, so for example only the instruments that pass certain criteria show up in that list. You setup a list of stocks, then setup a filter, and then only the stocks that pass the criteria of the filter show up. This is all done manually.

              I am noticing from your post you perhaps want an autotrading strategy that is updated with some list of stocks. There are a few ways you could do this, such as writing to a text file which instruments pass certain criteria and then having your strategy read it, to having a multi-series strategy that scans for these itself.
              OK, but how? I mean, you make this sound straightforward but the links provided really fail to address how this would be implemented. I am thinking that here is a great example of how a mini-tutorial (or even better, a ninjascript template where the user can put in their favorite indicator(s) and it's good to go) would be very helpful.

              This would be run after trading is closed, with the stated goal of simply creating a .txt or .csv file with the selected stocks (in my case, no other data is needed).

              The code in the link you provided is as follows:

              Code:
               
              // Add this to your declarations to use StreamWriter
              using System.IO;
              // This namespace holds all indicators and is required. Do not change it.
              namespace NinjaTrader.Indicator
              {
              /// <summary>
              /// A quick sample demonstrating StreamWriter properties. The techniques used in this indicator are also applicable to strategies.
              /// </summary>
              [Description("A quick sample demonstrating StreamWriter properties.")]
              [Gui.Design.DisplayName("SampleStreamWriter")]
              public class SampleStreamWriter : Indicator
              {
              #region Variables
              private int date = 0;
              private double currentOpen = 0;
              private double currentHigh = 0;
              private double currentLow = 0;
              private double currentClose = 0;
              private bool currentOpenSaved = false;
              private bool currentLowSaved = false;
               
              // This sets the path in which the text file will be created.
              private string path = Cbi.Core.UserDataDir.ToString() + "MyTestFile.txt";
               
              // Creates a StreamWriter and a StreamReader object
              private System.IO.StreamWriter sw;
              #endregion
              /// <summary>
              /// This method is used to configure the indicator and is called once before any bar data is loaded.
              /// </summary>
              protected override void Initialize()
              {
              CalculateOnBarClose = true;
              }
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
              /* The try-catch block is used for error handling.
              In this case it is used to ensure you only have one stream object operating on the same file at any given moment. */
              try
              {
              // If file at 'path' doesn't exist it will create the file. If it does exist it will append the file.
              if (CurrentBar == 0)
              sw = File.AppendText(path);
               
              // This is the output of all lines. The output format is as follows: Date Open High Low Close
              sw.WriteLine(ToDay(Time[0]) + " " + Open[0] + " " + High[0] + " " + Low[0] + " " + Close[0]);
              }
               
              catch (Exception e)
              {
              // Outputs the error to the log
              Log("You cannot write and read from the same file at the same time. Please remove SampleStreamReader.", NinjaTrader.Cbi.LogLevel.Error);
              throw;
              }
              }
              // Necessary to call in order to clean up resources used by the StreamWriter object
              protected override void OnTermination() 
              {
              // Disposes resources used by the StreamWriter
              if (sw != null)
              {
              sw.Dispose();
              sw = null;
              }
              }
              it creates a file with data from an individual stock or real-time data feed, but what I am trying to do is to get the output as a list of the stocks themselves. Not sure how this is modified to accomplish this.


              What is done with code as far as using the Market Analyzer for scanning for stock swould be setting up your more complex criteria as an indicator. Indicators have plots that can be set to certain values dependent on how you code it.

              I would suggest to get familiar with how NinjaScript code works. Its full C# but the platform is engineered so certain things are expected and really the only way to get through it all with full understanding would be to start at the beginning.

              Essentially its as simple as what I provided in a previous post, setting a "plot" "dataseries" to be 0 if your conditions are not met, or 1 if your conditions are met. Then, you setup a filter in the Market Analyzer to not show anything that doesnt have 1 in that plot manually (no code is needed for the filter setup).

              I will do as you suggest here - again my goal is to learn this properly before putting any money at risk.

              This is why pre-defined templates might be very helpful, especially for those of us trying to learn as we go.

              From what I see here the output file (let's call it selectedstocks.txt) is written to my hard drive, and can be accessed by NT for next day use. How is that done, and what degree of user involvement is needed?

              Or is this something that resides within NT and is always available, updated automatically if possible?

              I'm really just trying to put a fully automated system into place, and am grateful for any help toward that end. Happy to share the template at the end since I would guess others would like this as well.

              Kevin

              Comment


                #8
                Please see this example I created for you that is a stock threshold indicator that filters out stock above a certain value that are added to the market analyzer. You add this indicator column with this indicator and daily data and it will calculate and output to a text file the stocks that meet the criteria. You can then use that data where you need it later.

                Comment


                  #9
                  I did not see anything attached.

                  Comment


                    #10
                    Attaching Again. Let me know if issues.
                    Attached Files

                    Comment


                      #11
                      Thanks, Brett. I'm working through it now.

                      I'm assuming the text file is written to a directory defined by

                      privatestring path = Cbi.Core.UserDataDir.ToString() + "FilteredResults.txt"


                      If so, what would be the code snippet used in the strategy that would read this and use it as the instrument list? Is that assumption correct?

                      Man - there is a lot to learn here!

                      Kevin

                      Comment


                        #12
                        Hello,

                        Use this generalized sample here on reading from text file. You would do this in initialize and parse the text file and do the Add() calls for the instruments in the file.

                        Comment


                          #13
                          Working through this as well. I might have some questions (correction - I'm certain I'll have some questions) on the specific code. Thanks for directing me to this.

                          A somewhat unrelated question - can you set up MA so that it automatically runs each night at a set time? Then once it is done, Streamwriter can write the results to an output file and have the results ready for trading the next day.

                          Kevin

                          Comment


                            #14
                            Hello,

                            You can set an indicator that lays dormant until a specified time and then take action at that time.

                            This can be done with the Time sample, the sample has to do with limiting trading hours but you can customize it to you need.

                            Comment


                              #15
                              Originally posted by NinjaTrader_Brett View Post
                              Hello,

                              You can set an indicator that lays dormant until a specified time and then take action at that time.

                              This can be done with the Time sample, the sample has to do with limiting trading hours but you can customize it to you need.

                              http://www.ninjatrader.com/support/f...ead.php?t=3226
                              Not entirely sure what you mean by this - I had expected some code that would be part of a larger strategy, but opened up MA and ran a specific screen for stocks once the time hit a specified value.

                              I can see how you can set up the if/then statement like so:

                              Code:
                               
                              protected override void OnBarUpdate()
                               {
                                 // Checks to see if the current day is a weekday
                                 if (Time[0].DayOfWeek != DayOfWeek.Saturday && Time[0].DayOfWeek != DayOfWeek.Sunday)
                                 {
                                  //Run MA analyzer at 8:00 PM.  The timezone used here is (GMT-05:00) EST. 
                                  if (ToTime(Time[0]) == 200000)
                                  {
                                  tell MA to run a specific set of indicators and write the output to a .txt file
                                  }
                                }
                              }
                              I just don't know how to "turn on" MA. I don't see how this would be done from an indicator. I had expected there to be a set of code that interacts with each part of NT and can be used to set up things like this. Again, I am just starting out, so any help or clarification would be really appreciated.

                              Kevin

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by geddyisodin, Today, 05:20 AM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by Max238, Today, 01:28 AM
                              3 responses
                              32 views
                              0 likes
                              Last Post Max238
                              by Max238
                               
                              Started by timko, Today, 06:45 AM
                              2 responses
                              13 views
                              0 likes
                              Last Post NinjaTrader_ChristopherJ  
                              Started by habeebft, Today, 07:27 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post habeebft  
                              Started by Tim-c, Today, 03:54 AM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X