• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Sell Script and Auto Chart

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

    Sell Script and Auto Chart

    I have a strategy below where my sell order is not picking up. I want a trailing stop. Also, is my statement about adding bars correct. I'd like to have a defaulted amount of time as well as a trailing stop to kick in. For the time, do I need to enclose it in BarsInProgress==1 as seen here:

    http://www.ninjatrader.com/support/h...inprogress.htm

    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Code for first strategy
        /// </summary>
        [Description("Code for first strategy")]
        public class VWAP : Strategy
        {
            #region Variables
            // Wizard generated variables
            private int myInput0 = 1; // Default setting for MyInput0
            // User defined variables (add any user defined variables below)
            #endregion
            double[] typical_price = new double[3];
            double[] vwap = new double[3];
            private int vol_price = 0;
            private int total_volume = 0;
            private int total_vol_price = 0;
            private int i = 0;
            private bool increasing_vwap = true;
            private bool increasing_close = true;
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                // Trailing Stop
                SetTrailStop("", CalculationMode.Percent, 2, false);
                
                // Add a 5 minute Bars object: BarsInProgress index = 1
                Add(PeriodType.Minute, 5);
                
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // VWAP    
                total_volume = 0;
                total_vol_price = 0;
                increasing_vwap = true;
                increasing_close = true;
                for (i=0;i<=2;i++){
                    typical_price[i] = Math.Round((High[i]+Low[i]+Close[i])/3,2);
                    total_volume += System.Convert.ToInt32(Math.Floor((Volume[i])));
                    vol_price = System.Convert.ToInt32(Math.Floor((Volume[i]*typical_price[i])));
                    total_vol_price += vol_price;
                    vwap[i] = total_vol_price/total_volume;
                    Print("vwap["+i+"] = "+vwap[i]+" with tick size "+Volume[i]);
                    
                    // check for increasing indicators in a cumulative fashion
                    if (i>0 && vwap[i]<vwap[i-1]) {increasing_vwap = false;}
                    if (i==2 && NBarsUp(i,false,true,true)[0] == 0) {increasing_close = false;}    
                }
                
                // check for average volume as well?
                
                // Buy conditions met
                if (increasing_vwap && increasing_close){
                    
                    EnterLong(this.StrategyData.Strategy.Name);
                }
                
                // Sell Conditions met (optional since we have trailing stop???)
                
            }
    
            #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public int MyInput0
            {
                get { return myInput0; }
                set { myInput0 = Math.Max(1, value); }
            }
            #endregion
        }
    }
    Last edited by math_celebrity; 04-17-2014, 10:12 AM.

    #2
    Hello math_celebrity,

    Welcome to the NinjaTrader Support Forums!

    Using SetTrailStop() and Percentage mode, it is going to be a double value so 2 = 200% and 0.2 = 20% so with 200% so this is going to put your Stop Loss order very far away.

    As for your added time series, you added a 5 Minute series to your script but I am not sure what you are trying to do with it.

    Are you trying to get to the typical values of the two time frames inside of your for loop?
    JCNinjaTrader Customer Service

    Comment


      #3
      Thanks for the trailing stop, I fixed that and it looks good.

      For the time series, is it possible to add this to the strategy so that it is the default when we load a strategy?

      Comment


        #4
        Hello math_celebrity,

        It will not load the added series as a default when you apply the strategy. It will only be used if you want to have your strategy set to 1 time series and use another time series for some calculations.

        You may look at our SampleMultiTimeFrame strategy that comes loaded inside of NinjaTrader for an example of this. Tools -> Edit NinjaScript -> Strategy.
        JCNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by vpzdcv, Today, 02:31 AM
        0 responses
        3 views
        0 likes
        Last Post vpzdcv
        by vpzdcv
         
        Started by YevhenShynkarenko, Today, 01:22 AM
        0 responses
        7 views
        0 likes
        Last Post YevhenShynkarenko  
        Started by ttodua, Today, 12:52 AM
        0 responses
        4 views
        0 likes
        Last Post ttodua
        by ttodua
         
        Started by ttodua, Today, 12:50 AM
        0 responses
        2 views
        0 likes
        Last Post ttodua
        by ttodua
         
        Started by mulshah, Yesterday, 11:12 PM
        0 responses
        3 views
        0 likes
        Last Post mulshah
        by mulshah
         
        Working...
        X