NexusFi: Find Your Edge


Home Menu

 





How to access the previous value of an indicator? (MC.NET)


Discussion in MultiCharts

Updated
    1. trending_up 2,787 views
    2. thumb_up 3 thanks given
    3. group 4 followers
    1. forum 8 posts
    2. attach_file 0 attachments




 
Search this Thread

How to access the previous value of an indicator? (MC.NET)

  #1 (permalink)
 Jeffrey 
Hong Kong
 
Experience: Beginner
Platform: MultiCharts, NinjaTrader
Trading: HSIF, ES
Posts: 10 since Jul 2010
Thanks Given: 2
Thanks Received: 1

 
Code
public class New_ATR : IndicatorObject {
      
      [Input]
      public int period { get; set; }
      
      private double trueRange;
      private double averageTrueRange;
      
      private IPlotObject plot1;
      
      public New_ATR(object _ctx):
      base(_ctx){
      
         // default 14 days
         period = 14;         
      }
      
      protected override void Create() {
         // create variable objects, function objects, plot objects etc.
         plot1 = AddPlot(new PlotAttributes("ATR", EPlotShapes.Line, Color.Cyan, Color.Empty, 0, 0, true));
      }
      
      protected override void StartCalc() {
         // assign inputs
      }
      protected override void CalcBar(){
         
         // calculate the value of first bar
         if (Bars.CurrentBar == 0) {            
             trueRange = Bars.High[0] - Bars.Low[0];
            plot1.Set(0, trueRange);
         }
         else {
            // logic from NT7
            //double trueRange = High[0] - Low[0];
            //trueRange = Math.Max(Math.Abs(Low[0] - Close[1]), Math.Max(trueRange, Math.Abs(High[0] - Close[1])));
            //Value.Set(((Math.Min(CurrentBar + 1, Period) - 1 ) * Value[1] + trueRange) / Math.Min(CurrentBar + 1, Period));
            
            trueRange = Bars.High[0] - Bars.Low[0];            
            trueRange = Math.Max(Bars.Low[0] - Bars.Close[1], Math.Max(trueRange, Math.Abs(Bars.High[0] - Bars.Close[1])));
            
            averageTrueRange = ((Math.Min(Bars.CurrentBar + 1, period) - 1 ) * ?????? + trueRange) / Math.Min(Bars.CurrentBar + 1, period);
            
            plot1.Set(0, averageTrueRange);
         }
      }
   }
Say i am on the 2nd bar of the daily series, I want to access the variable of trueRange of first bar so that I can calculate the average of two days. How can I do this?

Is it possible to store the indicator value of current bar into a array so that I have a series of values that I can read anytime? Cheers.

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
How to apply profiles
Traders Hideout
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
ZombieSqueeze
Platforms and Indicators
About a successful futures trader who didnt know anythin …
Psychology and Money Management
 
  #2 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

Hi Jeffrey,

either compute the truerange for the particular bar again or store the result into a VariableSeries variable. Then you can reference previous values.

4.5 Functions and Special Variables - MultiCharts

Regards,
ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #3 (permalink)
 Jeffrey 
Hong Kong
 
Experience: Beginner
Platform: MultiCharts, NinjaTrader
Trading: HSIF, ES
Posts: 10 since Jul 2010
Thanks Given: 2
Thanks Received: 1



ABCTG View Post
Hi Jeffrey,

either compute the truerange for the particular bar again or store the result into a VariableSeries variable. Then you can reference previous values.

4.5 Functions and Special Variables - MultiCharts

Regards,
ABCTG

Thanks for the swirl response. Let's say I follow this to set the value into the array

 
Code
using System;
 
namespace PowerLanguage.Indicator
{
    public class ITest : IndicatorObject
    {
        private VariableSeries<Double> m_plotVal;
        private IPlotObject Plot1;
 
        public ITest(object ctx) : base(ctx)
        {
            length = 10;
        }
 
        [Input]
        public int length { get; set; }
 
        protected override void Create()
        {
            Plot1 = AddPlot(new PlotAttributes("Test"));
            m_plotVal = new VariableSeries<Double>(this);
        }
 
        protected override void CalcBar() {
            m_plotVal.Value = Bars.Close[length] - Bars.CloseValue;
            Plot1.Set(0, m_plotVal.Value);
        }
    }
}
How can I read the value of previous bar from the from the VariableSeries? I can't find a good example to show me this...Appreciated if you can shed some light!

Started this thread Reply With Quote
  #4 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

Jeffrey,

the same as you do it for the Close. In the code you posted m_plotVal[X], where X is the bar you want to go back (0 = Currentbar).

Regards,
ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #5 (permalink)
 
Jasonnator's Avatar
 Jasonnator 
Denver, Colorado United States
 
Experience: Intermediate
Platform: NT8 + Custom
Broker: NT Brokerage, Kinetick, IQFeed, Interactive Brokers
Trading: ES
Posts: 159 since Dec 2014
Thanks Given: 40
Thanks Received: 166

I have noticed that if I try to go pretty far back in the data series, I need to use Bars.FullSymbolData.Close[x]. I'm less than 1 week on MC so I'm not exactly sure why this is. I think it may have something to do with maximum number of bars (ExecInfo.MaxBarsBack) an indicator can see by default.

Reply With Quote
  #6 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

Jasonnator,

you are correct. By default the first bar on the chart for a study is the first bar after the maximum number of bars you set for your study to reference. This is done in order to ensure all values are probably computed.

Regards,
ABCTG


Jasonnator View Post
I have noticed that if I try to go pretty far back in the data series, I need to use Bars.FullSymbolData.Close[x]. I'm less than 1 week on MC so I'm not exactly sure why this is. I think it may have something to do with maximum number of bars (ExecInfo.MaxBarsBack) an indicator can see by default.


Follow me on Twitter Reply With Quote
  #7 (permalink)
 
Jasonnator's Avatar
 Jasonnator 
Denver, Colorado United States
 
Experience: Intermediate
Platform: NT8 + Custom
Broker: NT Brokerage, Kinetick, IQFeed, Interactive Brokers
Trading: ES
Posts: 159 since Dec 2014
Thanks Given: 40
Thanks Received: 166

@ABCTG
Just to clarify, if MaxBarsBack = 300, then Bars.Close[0] is still the current bar but it's the same as Bars.FullSymbolData.Close[299] in terms of all bars on the chart? That would mean the the far right bar is always MaxBarsBack...

Reply With Quote
  #8 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

Jasonnator,

bar 0 will always be the current bar. Bars.Close[1] and Bars.FullSymbolData.Close[1] both return the bar to the left of the current bar. The only difference is that Bars.Close[x] is limited by the Max bars back setting and Bars.FullSymbolData is not.

Regards,
ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #9 (permalink)
 
Jasonnator's Avatar
 Jasonnator 
Denver, Colorado United States
 
Experience: Intermediate
Platform: NT8 + Custom
Broker: NT Brokerage, Kinetick, IQFeed, Interactive Brokers
Trading: ES
Posts: 159 since Dec 2014
Thanks Given: 40
Thanks Received: 166

Gotcha, thanks

Reply With Quote




Last Updated on April 22, 2015


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts