Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

AutoTrading from Indicator ??

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

    AutoTrading from Indicator ??

    I have an indicator that does exactly what I want. Now I want to add autotrading. Can this be done from an indicator or does it only work from a strategy? Assuming it can work from an indicator....

    As is typical for the way my brain works, I see a line of code in various examples or in threads or on NT help screens and think it should work in my code. So I study it and then place it in my code. But the following line produces an error which I do not understand.

    EnterShortLimit(2, downTarget);

    downTarget has already been defined as double and is working just fine. Since this is a limit order, downtarget the"worst" price I am willing to pay, but hope to be filled better. This line is only activated when trade conditions exist-- the same code that produces an audio alert that is also working just fine.

    The error I receive is: The name "EnterShortLimit" does not exist in the current context.

    I thought this was a command line, like DrawDiamond... I have received this error when something is not spelled correctly, but EnterShortLimit is spelled correctly.

    I know I still have to set the profit target and stop but first things first. Can anyone tell me why this line is producing an error? Thanks.

    #2
    If you are trying to add that line of code to an indicator, that won't work, as orders can only be coded within a strategy. So, you'll need to create a new strategy, then access your indicator values from the code within your strategy.

    Comment


      #3
      Well, at least I know I am not crazy. As you have pointed me in the right direction I'll take a stab at it. I am sure I will return for more guidance...but I don't mind learning by trail and error. Much thanks...

      Comment


        #4
        Originally posted by sarasotavince View Post
        Well, at least I know I am not crazy. As you have pointed me in the right direction I'll take a stab at it. I am sure I will return for more guidance...but I don't mind learning by trail and error. Much thanks...
        Check out the sample strategies.

        Also read the NT support guide/documentation under the Help and work through examples.

        Scott Daggett has an Ebook That might help your understanding.

        Comment


          #5
          Hello sarasotavince,

          Thank you for your post and thank you to coolmoss and sledge for their assistance.

          As mentioned, the Strategy methods for orders cannot be used in indicators as they are a different class. You can find the tutorials on strategies at the following link: http://www.ninjatrader.com/support/h...strategies.htm

          Comment


            #6
            This is good information but it does not address how to reference an indicator value within a strategy. For example, I have a variable called ok2Trade. I have it coded perfectly within the indicator. Now I want to build a strategy that references that variable (the value of that variable)... so how do I code:

            if ( from indicator: Beta003 - variable: ok2Trade ==1)
            {EnterLongLimit.....etc}

            I tried Beta003.ok2Trade==1, but that was too simple.

            I am eager to learn but can't find clear enough examples that address my problem. Seems simple enough, I did not find an NT help under strategies that references indicators. It's like everything is a secret.

            Comment


              #7
              I just had a thought. Is it easier to transfer the indicator code to to a strategy and have it all in the same place? Will the speed of processing all the plots and whatnot slow the strategy significantly?

              Comment


                #8
                Hello sarasotavince,

                Thank you for your response.

                It would be best to familiarize yourself with how to call indicator methods and values from within another indicator or strategy. You can find information on indicator methods at the following link: http://www.ninjatrader.com/support/h..._indicator.htm

                Please let me know if you have any questions.

                Comment


                  #9
                  Yes, I have questions. Thank you. Which part of this answers my question. The link you provided makes no sense to me.

                  Indicator method can accept the following valid forms of input data.

                  Default Input
                  Default input of the custom indicator, Market Analyzer column or strategy is used as default input if input is not specified.

                  // Printing the current value of the 10 period SMA of closing prices
                  // using the default input.
                  double value = SMA(10)[0];
                  Print("The current SMA value is " + value.ToString());

                  Price Series
                  Open, High, Low, Close and Volume can all be used as input for an indicator method.

                  // Passing in the a price series of High prices and printing out the current value of the
                  // 14 period simple moving average
                  double value = SMA(High, 14)[0];
                  Print("The current SMA value is " + value.ToString());

                  Indicator
                  Indicators can be used as input for indicators.

                  // Printing the current value of the 20 period simple moving average of a 14 period RSI
                  // using a data series of closing prices
                  double value = SMA(RSI(Close, 14), 20)[0];
                  Print("The current SMA value is " + value.ToString());

                  DataSeries
                  DataSeries can be used as input for indicators.

                  // Instantiating a new DataSeries object and passing it in as input to calculate
                  // a simple moving average
                  DataSeries myDataSeries = new DataSeries(this);
                  double value = SMA(myDataSeries, 20)[0];

                  Bars Object
                  A Bars object (holds a series of bar object that contain OHLC data) can be used as input for indicators.

                  // Passing in the second Bars object held in a multi-instrument and timeframe strategy
                  // The default value used for the SMA calculation is the close price
                  double value = SMA(BarsArray[1], 20)[0];
                  Print("The current SMA value is " + value.ToString());


                  Tips
                  1. The input series of an indicator cannot be the hosting indicator as it will cause recursive loops.

                  // Using the hosting indicator like below will cause errors with recursive loops
                  double value = SMA(this, 20)[0];

                  How do I reference a value from an indicator within a strategy? None of the above seems to answer that question. Can you provide an example?

                  Comment


                    #10
                    Let me give you an example. Let's say you have an indicator called myIndicator(), which if added to a chart plots a single value as a line, histogram, or whatever.

                    If I want to reference the value of that indicator, then the syntax for a simple if/then logic would be:

                    if( myIndicator().Plot0[0] > someValue)

                    Notice what I did. I took the syntax of the indicator itself and appended '.Plot0' which refers to the first plot of the indicator and also appended [0] which is simply the reference to the number of bars ago to reference, just like a regular data series.

                    This syntax should work with any simple indicator which only produces a single plot.

                    HTH

                    Comment


                      #11
                      Originally posted by sarasotavince View Post
                      Yes, I have questions. Thank you. Which part of this answers my question. The link you provided makes no sense to me.



                      How do I reference a value from an indicator within a strategy? None of the above seems to answer that question. Can you provide an example?
                      In your indicator, hopefully you can make it look like this:

                      Code:
                          public class beta003 : Indicator
                          {
                              //#region Variables
                              // Wizard generated variables
                              // User defined variables (add any user defined variables below)
                              public int ok2Trade;
                      Then in the Strategy...

                      Code:
                      public class my1stStrat: Strategy
                      {
                      
                      private beta003 BETA;
                      
                      ...
                      
                      
                      protected override void OnStartUp() 
                      {
                      
                      BETA = beta003 ( BarsArray[0], ..... );  //whatever your constructor requires in the indicator
                      }
                      
                      protected override void OnBarUpdate()
                      {
                         if ( BETA.ok2Trade ==1 )
                         {
                                   EnterLong ( );
                      Well, without your indicator code, I can not be more specific.

                      I hope this helps.

                      p.s. This is generally considered poor form, as variables shouldn't be exposed public, and you should have procedures/functions/methods to alter them.. but I'm not about to code that up at this point.

                      Comment


                        #12
                        Thank you coolmoss, that will be helpful in the future...

                        Thank you sledge,

                        I believe you understand exactly what I am asking. Your example should give me enough room to experiment. One question for now...I may be back for more later! So, thanks again so much, this is how I learn best...

                        Is there a way to be sure the indicator updates before the strategy starts? For example, this entire strategy for entering trades hinges on if it is "ok2Trade" as determined by that variable in the indicator beta003. If ok2Trade is YES and then turns to NO after on bar update, I don't want the strategy to fire off a trade order. Do all active indicators update before all active strategies by default? Does it happen in the order I add items to a chart?...in that case, I would add the indicator first, then the strategy.

                        Comment


                          #13
                          Hello sarasotavince,

                          Thank you for your response.

                          If you are checking the indicator's method in your if condition before entry then it will ensure the indicator's value is the desired value before the action is taken and there would be no need to ensure the indicator updates before the strategy. If you are using a bool you can look into using a BoolSeries in your indicator and call that from the strategy with the indicator's method. You can find an example of using a BoolSeries in an indicator at the following link: http://www.ninjatrader.com/support/f...ead.php?t=4991

                          Comment


                            #14
                            For ok2Trade, No BoolSeries, just an INT that gets updated (1 =yes, 2=maybe, 3=no) after certain other variables are updated...so, let me summarize to make sure I understand completely.

                            My indicator, which I call Beta003 runs ON BAR UPDATE and determines a value of 1, 2, or 3 for the varibale ok2Trade. My strategy, now called teststrategy, only fires off a trade if ok2Trade =1, which will always be updated before the IF statement in the strategy that reads:

                            If (BETA.ok2Trade==1)
                            { code for order here }

                            Thanks for your help.

                            Comment


                              #15
                              Hello sarasotavince,

                              Thank you for your response.

                              Not necessarily "before the IF statement", however if I use the index of barsAgo [0] then it will call the most recent update of that indicator. So if I use CalculateOnBarClose = true in the strategy, then [0] would be the most recent closed bar of the indicator. And if I used CalculateOnBarClose = false, then [0] would be the most recent tick of the indicator. If they both process on the same instrument and time frame then you are forcing the strategy to check the indicator's current value with [0].

                              So the condition below in a strategy would check that the current bar's close price is greater than the SMA's last close or bar, the SMA being the indicator we are calling in this case:
                              Code:
                              if(Close[0] > SMA(period)[0])
                              With CalculateOnBarClose = true the above would check the last closed bars, and with CalculateOnBarClose = false this would be the most recent tick of the price and the SMA. For information on CalculateOnBarClose please visit the following link: http://www.ninjatrader.com/support/h...onbarclose.htm

                              Keep in mind that the strategy is on the last bar or tick as well so they are both processing on the same data, unless of course you set the CalculateOnBarClose exclusively in the Indicator which is generally not recommended or if you use a different time frame or instrument for the strategy or instrument than the other.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              72 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              43 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              25 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              28 views
                              0 likes
                              Last Post TheRealMorford  
                              Started by Mindset, 02-28-2026, 06:16 AM
                              0 responses
                              59 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Working...
                              X