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

Enter short/long above/below custom indicator's dataseries

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

    Enter short/long above/below custom indicator's dataseries

    Hi everyone, I'm new to Ninjatrader and I'm trying to develop a strategy in which exit short when the dataseries cross above my CustomVWAP's upper standard deviation and enter long when the dataseries cross below myCustomVWAP's lower standard deviation (see the attached image for an example).
    Below my code.

    Code:
     
     private acVWAP vwap;
     private DataSeries myDataSeries;
    
     protected override void Initialize()
     {
            CalculateOnBarClose = false;
    	myDataSeries = new DataSeries(this,MaximumBarsLookBack.Infinite);
    	vwap = acVWAP(DateTime.MinValue, pStdDev);
            Add(vwap);
     }
    
    protected override void OnBarUpdate()
    {
    	if(CurrentBar < 0) return;
    			
    	// Condition set 1
    	if(myDataSeries[0] >= vwap.Upper[0])
    		EnterShort("");
    	else if (myDataSeries[0] <= vwap.Lower[0])
    		EnterLong("Long entry");
    }
    Wherein myDataSeries[0] should be the current value of the dataseries (is it right?) and vwap.Upper and vwap.Lower return the Upper and Lower standard deviation, respectively.

    Note: I didn't use the methods CrossAbove(myDataSeries, vwap.Upper, 1) and CrossBelow(myDataSeries, vwap.Lower, 1) because they seems to not work for me.
    However, another issue is that myDataSeries[0] is always 0, so the only action taken is always "EnterLong".

    Can someone explain me how to reach my goal, that is: enter short when crossAbove vwap.Upper and enter long when crossBelow vwap.Lower?

    Regards
    Stefano
    Attached Files
    Last edited by stefanodp; 03-13-2018, 05:15 AM.

    #2
    Hello stefanodp,

    Thanks for your post and welcome to the NinjaTrader forums!

    You are creating a new data series but the data series is not populated with anything. The data series can be used an array of containers that would match the number of bars in data that is loaded with chart so that you could store anything related to a particular bar/time. For example if you wanted to store the range value of a bar you could use myDataSeries.Set(High[0] - Low[0]). Then if you needed to know the range from say 5 bars ago you can pull that with double myRange = myDataSeries[5];

    In this case however, you do not need to add a new data series as you can use any of the predefined data series: Close, Open, High, Low, Volume, Time, as they are all available for your script. Reference: https://ninjatrader.com/support/help...rice_data2.htm

    So your conditions would become:

    if (Close[0] >= vwap.Upper[0])
    EnterLong();
    etc.etc.

    To avoid multiple entries I would recommend using the CrossAbove and CrossBelow methods that would look like:

    if (CrossAbove(Close, vwap.Upper[0]))
    EnterLong();
    etc.etc.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul, thank you for replying.
      I tried to do the follow, as you suggested:

      Code:
       protected override void OnBarUpdate()
       {		
      	double upper = vwap.Upper[0];
      	double lower = vwap.Lower[0];
      		
      	if(CrossBelow(Close, lower, 1))
                      EnterLong();
      	else if(CrossAbove(Close, upper, 1))
      		ExitShort();
       }
      but it doesn't work, the code never satisfies the if/else if condictions.
      If I change CrossBelow(Close, lower, 1) with Close[0] <= lower and CrossAbove(Close, upper, 1) with Close[0] >= upper it seems to work. However, Close[0] always equals lower which in turn equals upper when doing BackTest in StrategyAnalizer.

      However, is there a way to know the current value of the Bar in real-time in the OnBarUpdate() method? Or is it Close[0] itself? Note I set CalculateOnBarClose = false.

      Thank you!

      Comment


        #4
        Hello stefanodp,

        Thanks for your reply.

        Yes, Close[0] will be the current price when using CalculateOnBarClose = false.

        You may want to do some debugging by using Print statements to see what is happening although you will generate quite a few with CalculateOnBarClose = false. Here is a link to our debugging tips: https://ninjatrader.com/support/foru...ead.php?t=3418

        Note: Even though you are using CalculateOnBarClose = false, on historical data your code can only execute as CalculateOnBarClose = true. the false mode only works on real time data or (or market replay data).
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hi Paul,
          Thank you for the explaination.
          I did some debug: Close dataseries change in time and it's ok. However I noticed that the dataseries Upper and Lower in my custom vwap indicator always equal each other at each bar when used in the strategy, even though in that indicator they give correct results.

          Is there something wrong when calling the vwap indicator?

          Comment


            #6
            Hello Stefano,

            Thanks for your reply.

            Please try:

            protected override void OnBarUpdate()
            {
            vwap.Update();
            double upper = vwap.Upper[0];
            double lower = vwap.Lower[0];
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Unfortunately it doesn't work.
              In the image you can see what happens.
              Attached Files

              Comment


                #8
                Hello stefanodp,

                Thanks for your post.

                Can you post the code for your custom indicator so I can better understand what you are working with?

                Alternatively, if you prefer to not post publically, please feel free to write into PlatformSupport[at]NinjaTrader[dot]Com. Mark the e-mail atten: Paul, include a link to this thread and of course the source code file (.CS) of the indicator for review.
                Paul H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by algospoke, Yesterday, 06:40 PM
                2 responses
                19 views
                0 likes
                Last Post algospoke  
                Started by ghoul, Today, 06:02 PM
                3 responses
                14 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by jeronymite, 04-12-2024, 04:26 PM
                3 responses
                45 views
                0 likes
                Last Post jeronymite  
                Started by Barry Milan, Yesterday, 10:35 PM
                7 responses
                20 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by AttiM, 02-14-2024, 05:20 PM
                10 responses
                181 views
                0 likes
                Last Post jeronymite  
                Working...
                X