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

2 Indicators in Strategy overwrite each other

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

    2 Indicators in Strategy overwrite each other

    Hi
    I have created a simple strategy to test the instansiatin of a custom Indicator I have created.
    But there seems to be something wrong. Both indicators are added to the chart but with the same values eventhough they are different in the code. Also as the plot color of one of the indicators are changed it also changes the other. I need some help to figure out what is wrong here.

    Code:
    namespace NinjaTrader.NinjaScript.Strategies._own
    {
    public class VWAPBounce : Strategy
    {
    
    private VWAP_Attach vwapDay;
    private VWAP_Attach vwapWeek;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "VWAPBounce";
    Calculate = Calculate.OnPriceChange;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    }
    else if(State == State.DataLoaded)
    {
    vwapDay = VWAP_Attach(Close);
    vwapDay.AttachDate = DateTime.Now;
    vwapDay.AttachTime = new TimeSpan(00,00,00);
    
    
    vwapWeek = VWAP_Attach(Close);
    vwapWeek.AttachDate = StartOfWeek(DateTime.Now, DayOfWeek.Monday);
    vwapWeek.AttachTime = new TimeSpan(00,00,00);
    
    vwapDay.Plots[0].Brush = Brushes.AliceBlue;
    
    AddChartIndicator(vwapDay);
    AddChartIndicator(vwapWeek);
    }
    }
    
    protected override void OnBarUpdate()
    {
    //Add your custom strategy logic here.
    }
    
    public DateTime StartOfWeek(DateTime dt, DayOfWeek startOfWeek)
    {
    int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7;
    return dt.AddDays(-1 * diff).Date;
    }
    }
    }
    Best Regards,
    Sune


    #2
    Hello SuneSorgenfrei,

    Thank you for your post.

    i would suggest printing the date and times before they are used in the method calls, and then print vwapDay/vwapWeek.AttachDate in OnBarUpdate() for each indicator, which would let you know if that property is being set properly. From there that should direct you to how that property is being used in the logic of the hosted indicator and why it seems to be showing up the same for both.

    I'm not seeing anything glaring in your code example that would cause two indicators with different properties to display the same, however.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi Kate
      If I uncomment one of the 2 vwap's then everything works as expected the day attaches to the current day and the week attaches to the first day of the week.
      But if I have both in at the same time the one created last overwrites the other. I did print out the values in the OnBarChange() and the value is the same, which it should of course not be.
      Any ideas what could be wrong here ? Can it be something in the indicator I instansiate ? It almost acts as if it is the same object instansiated into a new variable.

      Best Regards,
      Sune

      Comment


        #4
        Hello SuneSorgenfrei,

        Thank you for your note.

        Is the VWAP_Attach indicator you're using calling the Order Flow VWAP? There's a known issue with using multiple copies of the Order Flow VWAP that are the same except for the reset interval. A workaround for this can be found on this post:

        How to use week or month VWAP order flow inside a strategy? This line seems to grab the values for the session VWAP. VWAPs[0] = OrderFlowVWAP(VWAPResolution.Standard, TradingHours.String2TradingHours("US Equities ETH"), VWAPStandardDeviations.Three, 1, 2, 3).VWAP[0]; The indicator itself has settings for Month, Week


        If not using the Order Flow VWAP and the VWAP_Attach indicator is not a compiled third party assembly, could you provide an export of the strategy that includes the indicator?

        Thanks in advance; I look forward to assisting you further.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hi Kate
          No it is my own indicator a simple VWAP indicator that can be attched.
          I have attached an C# file containing both classes.

          Best Regards,
          Sune
          Attached Files

          Comment


            #6
            Hello SuneSorgenfrei,

            Thank you for your reply.

            I think you're running into a similar issue with indicator caching. When an indicator is called with the same parameters it would be cached and the cached version used. I'd recommend adding a parameter that you don't actually use anywhere to the indicator and basically use that so there's a difference between the two calls. I'm seeing the following give different values between the two plots. Basically I just added a user input for Instance Number to the indicator and then called the indicators by calling VWAP_Attach(Close, 1) and VWAP_Attach(Close, 2) so the inputs aren't the same between the two instances.

            Please let us know if we may be of further assistance to you.
            Attached Files
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Hi Kate
              Thanks for your help this solved the issue and I have learned something also

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by alifarahani, Today, 09:40 AM
              6 responses
              30 views
              0 likes
              Last Post alifarahani  
              Started by Waxavi, Today, 02:10 AM
              1 response
              17 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Started by Kaledus, Today, 01:29 PM
              5 responses
              13 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by Waxavi, Today, 02:00 AM
              1 response
              12 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Started by gentlebenthebear, Today, 01:30 AM
              3 responses
              17 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X