Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing plot data from custom indicator in custom strategy

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

    Accessing plot data from custom indicator in custom strategy

    Hi everyone.

    I recently developed a support/resistance indicator that calculates daily support/resistance on a 5 minute bar chart and I am trying to develop a countertrend strategy that will buy when price hits support and sells when price his resistance.

    The values are stored in the custom indicator as SLine and RLine as follows;
    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> RLine
    { get {return Values[1];} }

    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> SLine
    { get {return Values[0];} }




    My goal is to store these values as private doubles within the strategy as follows;


    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class SRstrategy : Strategy
    {
    private double Sl, Rl;
    private double contracts = 1;
    private SRstrategy _SRIndicator;


    ....


    else if (State == State.Configure)
    {
    _SRIndicator = SRstrategy(_ExtraneousInput1,_ExtraneousInput2, _ExtraneousInput3, _ExtraneousInput4);
    base.AddChartIndicator(_SRIndicator);

    .............


    protected override void OnBarUpdate()
    {

    _SRIndicator.Update();
    Sl = _SRIndicator.SLine[0]; // OR

    Rl = _SRIndicator.RLine[0]; // OR


    if(Time[0].TimeOfDay >= _StartTime && Time[0].TimeOfDay <= _FinishTime)
    {
    if(this.IsFlat && Low[0] <= Sl)
    {
    base.EnterLong(contracts);
    }
    else if(this.IsFlat && High[0] >= Rl)
    {
    base.EnterShort(contracts);
    }

    }

    if(Time[0].TimeOfDay == _FinishTime)
    {
    if(this.IsLong)
    {
    base.ExitLong();
    }
    else if(this.IsShort)
    {
    base.ExitShort();
    }
    }
    }
    .......

    Does anyone have insight as to why this might not be working?

    Thanks in advance.

    #2
    Hello AuroraAustralis,

    Thank you for the post.

    I cant see anything that really sticks out that is incorrect, but also I am unsure what your current output is if any to provide a more detailed assessment.

    If you are not seeing this work historically, I had noted that you are only using the subplots in OnBarUpdate. I don't see that you are actually calling the base indicator's plot which is needed to have it run OnBarUpdate historically for the indicator. Can you check if this is the cause of the problematic outcome? I see that you have used .Update() from the indicator but I am unsure this would be the same as calling the plot.

    Instead of calling _SRIndicator.Update();, could you instead try calling the default plot:

    Code:
    double tempDoubleValue = _SRIndicator[0];
    There is an example that tries to explain this in the help guide here: https://ninjatrader.com/support/help...chartindicator

    Does this produce any change in outcome?

    If not, could you provide more detail on what specifically is happening with the current script in regard to the values being returned from the plots?

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Thanks so much for the speedy reply. Yes you are right it's not working historically.


      I'm having some trouble understanding however. Since its two private double series I'm trying to access how does "double tempDoubleValue = _SRIndicator[0];" pass these values to Sl and Rl? I'm not too sure I know how to 100% integrate this change into the existing code using the link you provided.

      Apologies if I'm misunderstanding here.\
      Thanks again.

      Comment


        #4
        Hello AuroraAustralis,

        In this case, my suggestion has nothing to do with your existing variables, only that you are not calling the indicator its self and only calling its subplots by their property name.

        Calling the indicator its self should make it calculate historically. In the case of calling just a subplot, that is likely not calling OnBarUpdate historically.

        In the prior post, I noted that you could try replacing the syntax _SRIndicator.Update(); with the following:

        Code:
        double tempDoubleValue = _SRIndicator[0];
        That's literally the change I would suggest and the location of the change. Just remove the _SRIndicator.Update(); line and replace it with the setting of the double. You don't need to use that variable anywhere, it's just a placeholder to make sure the indicator is called from OnBarUpdate. This should be added near the top of OnBarUpdate or where you have the call to Update() before you use the subplots is fine.

        In the help guide example, this is the same concept shown with the SMA, except the SMA is not a great example because it has no subplots. The Bollinger would be a better example, calling the Bollinger(0.5,12)[0] would be needed before you call Bollinger(0.5,12).Upper[0] historically.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi Jesse,

          Thanks for the response, I understand what you mean now and I amended the code to reflect the change however it still seems to fail in the backtest.

          protected override void OnBarUpdate()
          {
          //Add your custom strategy logic here.
          // _SRIndicator.Update();
          tempDoubleValue = _SRIndicator[0];
          Sl = _SRIndicator.SLine[0]; //
          Rl = _SRIndicator.RLine[0]; //



          if(Time[0].TimeOfDay >= _StartTime && Time[0].TimeOfDay <= _FinishTime)
          {
          if(this.IsFlat && Low[0] <= Sl)
          {
          base.EnterLong(contracts);
          }
          else if(this.IsFlat && High[0] >= Rl)
          {
          base.EnterShort(contracts);
          }

          }

          if(Time[0].TimeOfDay == _FinishTime)
          {
          if(this.IsLong)
          {
          base.ExitLong();
          }
          else if(this.IsShort)
          {
          base.ExitShort();
          }
          }

          }

          Are there any further recommendations possible?

          Thanks so much.

          Comment


            #6
            Hello,

            Thank you for trying that.

            Can you tell me, what are the values if you Print the plots being used?

            Could you append a print to see if the values are changing during the backtest?


            Code:
            Rl = _SRIndicator.RLine[0]; // 
            Print(Rl);
            If the value is not changing, you may need to look further into the logic of the indicator that sets the value to see if the indicator is being processed historically at all. You could move the print into the indicators OnBarUpdate near where the plot is set to see if this is called.

            If you are still unable to determine a cause, I would likely need to see a exported example of the indicator being called from a strategy that we could work with. If you are able to export the item and upload it, that would be helpful.

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by jonfan45, Today, 06:06 PM
            0 responses
            6 views
            0 likes
            Last Post jonfan45  
            Started by rustyspring, Today, 05:15 PM
            0 responses
            8 views
            0 likes
            Last Post rustyspring  
            Started by xulu13, Today, 05:01 PM
            0 responses
            12 views
            0 likes
            Last Post xulu13
            by xulu13
             
            Started by Pbarrionuevo, Today, 04:30 PM
            0 responses
            9 views
            0 likes
            Last Post Pbarrionuevo  
            Started by vantojo, Today, 01:39 PM
            0 responses
            20 views
            0 likes
            Last Post vantojo
            by vantojo
             
            Working...
            X