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

Startegy involving previous day close price and current open price

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

    Startegy involving previous day close price and current open price

    Hi,

    I just want to do a simple startegy where i can get the close price of the previous day(at 4:15PM) and then compare with the opening price of the current day(9:30AM) and if opening price is smaller than previous closing i would be buying. I would be selling if the converse is true. Also, I need to set my target profit and stop loss to be the difference b/w the opening and previous day closing. Could you please help me out?

    Thanks,
    Ron

    #2
    Ron,

    Please take a look at this tutorial for creating strategies with the Strategy Wizard: http://www.ninjatrader-support.com/H...tml?Overview34

    To start off, in the Condition Builder, you want to select an indicator on the left hand side called "Prior Day OHLC" and then on select the indicator "Current Day OHL" on the right.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      I have done something similar during quiet market hours, maybe it helps as a starting point. Just remove the Notify function and the indicator should compile. Please continue by yourself. Look at the OnBarUpdate() function


      Best regards
      Andreas

      -------------------

      #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.Gui.Chart;
      #endregion

      // This namespace holds all indicators and is required. Do not change it.
      namespace NinjaTrader.Indicator
      {
      /// <summary>
      /// voice and visual message
      /// </summary>
      [Description("voice and visual message")]
      public class TouchSessionOpenOrClose : Indicator
      {
      #region Variables
      // Wizard generated variables
      private string openTime = @"8.00"; // Default setting for OpenTime
      private double openTolerance = 6; // Default setting for OpenTolerance
      private string closeTime = @"17.30"; // Default setting for CloseTime
      private double closeTolerance = 6; // Default setting for CloseTolerance
      // User defined variables (add any user defined variables below)
      #endregion
      private DateTime currentDate = Cbi.Globals.MinDate;
      private DateTime LastSessionClose = Cbi.Globals.MinDate;
      public int nBarsWait;
      public int lastOpenNotify;
      public int lastCloseNotify;

      /// <summary>
      /// This method is used to configure the indicator and is called once before any bar data is loaded.
      /// </summary>
      protected override void Initialize()
      {
      Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "_Open"));
      Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "_Close"));
      CalculateOnBarClose = false;
      Overlay = true;
      PriceTypeSupported = false;
      nBarsWait=5;
      Print("TouchSessionOpenOrClose::InitCalled");
      }
      public override void Dispose(){

      CustomThreadPool.QueueUserWorkItem(null, "");
      base.Dispose();
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>

      protected override void OnBarUpdate()
      {


      if (!Data.BarsType.GetInstance(Bars.Period.Id).IsIntr aday){
      //DrawTextFixed("error msg", "TouchSessionOpenOrClose only works on intraday intervals", TextPosition.BottomRight);
      return;
      }
      // If the current data is not the same date as the current bar then its a new session
      if (currentDate != Bars.GetSessionDate(Time[0])){
      LastSessionClose=currentDate.Add(System.Convert.To DateTime(CloseTime).TimeOfDay);
      currentDate = Bars.GetSessionDate(Time[0]);
      }
      DateTime SessionOpen = Time[0].Date.Add(System.Convert.ToDateTime(OpenTime).Time OfDay);
      if(Historical) return;
      try {
      RemoveDrawObjects();
      if(!Bars.FirstBarOfSession && OpenTolerance>0 && System.Convert.ToDateTime(OpenTime).TimeOfDay!=nul l) {
      int nOpen=GetBar(SessionOpen);
      if(nOpen>0 && Math.Abs(Open[nOpen] - Close[0])<=OpenTolerance) {
      DrawLine("tag4",nOpen,Open[nOpen],0,Open[nOpen],Plots[0].Pen.Color,Plots[0].Pen.DashStyle,System.Convert.ToInt32(Plots[0].Pen.Width));
      DrawText("tag3","OPEN",nOpen,Open[nOpen],Color.Black);
      // add NotifyAcoustically max every N minutes
      if(lastOpenNotify+nBarsWait<CurrentBar) {
      //Notify("today's OPEN");
      lastOpenNotify=CurrentBar;
      }
      }

      }
      // GetBars might fail
      if(CloseTolerance>0 && System.Convert.ToDateTime(CloseTime).TimeOfDay!=nu ll ){
      int n=CurrentBar-Bars.GetBar(LastSessionClose);
      double d=Typical[n];
      if(n> 0 && CurrentBar>n && Math.Abs(d - Close[0])<=CloseTolerance) {
      DrawLine("tag1",n,d,0,d,Plots[1].Pen.Color,Plots[1].Pen.DashStyle,System.Convert.ToInt32(Plots[1].Pen.Width));
      DrawText("tag2","CLOSE last session",n,d,Color.Black);
      if(lastCloseNotify+nBarsWait<CurrentBar) {
      //Notify("last session CLOSE");
      lastCloseNotify=CurrentBar;
      }
      }
      }
      }
      // add NotifyAcoustically max every N minutes
      catch {
      }
      }

      void Notify(string str){

      if(Historical) return;
      string s=Instrument.FullName;
      if(Instrument.FullName.StartsWith("FGBL")) {s="Bund future";}
      if(Instrument.FullName.StartsWith("FDAX")) {s="Dax future";}
      if(Instrument.FullName.StartsWith("6E")) {s="Euro dollar future";}
      if(Instrument.FullName.StartsWith("ES ")) {s="Mini S Pee 500 future";}
      s+=", approaching ";
      s+=str;
      //CustomThreadPool.QueueUserWorkItem(Winmm.SpeakText , s);
      }
      #region Properties
      [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
      [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
      public DataSeries _Open
      {
      get { return Values[0]; }
      }

      [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
      [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
      public DataSeries _Close
      {
      get { return Values[1]; }
      }

      [Description("")]
      [Category("Parameters")]
      public string OpenTime
      {
      get {

      return openTime;

      }
      set {
      openTime = value;
      try {
      DateTime dt=System.Convert.ToDateTime(openTime);
      }
      catch {
      openTime= "0.00";
      }

      }
      }

      [Description("")]
      [Category("Parameters")]
      public double OpenTolerance
      {
      get { return openTolerance; }
      set { openTolerance = Math.Max(0, value); }
      }

      [Description("")]
      [Category("Parameters")]
      public string CloseTime
      {
      get { return closeTime; }
      set { closeTime = value;
      try {
      DateTime dt=System.Convert.ToDateTime(closeTime);
      }
      catch {
      closeTime= "0.00";
      }

      }
      }

      [Description("")]
      [Category("Parameters")]
      public double CloseTolerance
      {
      get { return closeTolerance; }
      set { closeTolerance = Math.Max(0, value); }
      }
      #endregion
      }
      }

      #region NinjaScript generated code. Neither change nor remove.
      // This namespace holds all indicators and is required. Do not change it.
      namespace NinjaTrader.Indicator
      {
      public partial class Indicator : IndicatorBase
      {
      private TouchSessionOpenOrClose[] cacheTouchSessionOpenOrClose = null;

      private static TouchSessionOpenOrClose checkTouchSessionOpenOrClose = new TouchSessionOpenOrClose();

      /// <summary>
      /// voice and visual message
      /// </summary>
      /// <returns></returns>
      public TouchSessionOpenOrClose TouchSessionOpenOrClose(string closeTime, double closeTolerance, string openTime, double openTolerance)
      {
      return TouchSessionOpenOrClose(Input, closeTime, closeTolerance, openTime, openTolerance);
      }

      /// <summary>
      /// voice and visual message
      /// </summary>
      /// <returns></returns>
      public TouchSessionOpenOrClose TouchSessionOpenOrClose(Data.IDataSeries input, string closeTime, double closeTolerance, string openTime, double openTolerance)
      {
      checkTouchSessionOpenOrClose.CloseTime = closeTime;
      closeTime = checkTouchSessionOpenOrClose.CloseTime;
      checkTouchSessionOpenOrClose.CloseTolerance = closeTolerance;
      closeTolerance = checkTouchSessionOpenOrClose.CloseTolerance;
      checkTouchSessionOpenOrClose.OpenTime = openTime;
      openTime = checkTouchSessionOpenOrClose.OpenTime;
      checkTouchSessionOpenOrClose.OpenTolerance = openTolerance;
      openTolerance = checkTouchSessionOpenOrClose.OpenTolerance;

      if (cacheTouchSessionOpenOrClose != null)
      for (int idx = 0; idx < cacheTouchSessionOpenOrClose.Length; idx++)
      if (cacheTouchSessionOpenOrClose[idx].CloseTime == closeTime && Math.Abs(cacheTouchSessionOpenOrClose[idx].CloseTolerance - closeTolerance) <= double.Epsilon && cacheTouchSessionOpenOrClose[idx].OpenTime == openTime && Math.Abs(cacheTouchSessionOpenOrClose[idx].OpenTolerance - openTolerance) <= double.Epsilon && cacheTouchSessionOpenOrClose[idx].EqualsInput(input))
      return cacheTouchSessionOpenOrClose[idx];

      TouchSessionOpenOrClose indicator = new TouchSessionOpenOrClose();
      indicator.BarsRequired = BarsRequired;
      indicator.CalculateOnBarClose = CalculateOnBarClose;
      indicator.Input = input;
      indicator.CloseTime = closeTime;
      indicator.CloseTolerance = closeTolerance;
      indicator.OpenTime = openTime;
      indicator.OpenTolerance = openTolerance;
      indicator.SetUp();

      TouchSessionOpenOrClose[] tmp = new TouchSessionOpenOrClose[cacheTouchSessionOpenOrClose == null ? 1 : cacheTouchSessionOpenOrClose.Length + 1];

      Comment


        #4
        if (cacheTouchSessionOpenOrClose != null)
        cacheTouchSessionOpenOrClose.CopyTo(tmp, 0);
        tmp[tmp.Length - 1] = indicator;
        cacheTouchSessionOpenOrClose = tmp;
        Indicators.Add(indicator);

        return indicator;
        }

        }
        }
        // This namespace holds all market analyzer column definitions and is required. Do not change it.
        namespace NinjaTrader.MarketAnalyzer
        {
        public partial class Column : ColumnBase
        {
        /// <summary>
        /// voice and visual message
        /// </summary>
        /// <returns></returns>
        [Gui.Design.WizardCondition("Indicator")]
        public Indicator.TouchSessionOpenOrClose TouchSessionOpenOrClose(string closeTime, double closeTolerance, string openTime, double openTolerance)
        {
        return _indicator.TouchSessionOpenOrClose(Input, closeTime, closeTolerance, openTime, openTolerance);
        }

        /// <summary>
        /// voice and visual message
        /// </summary>
        /// <returns></returns>
        public Indicator.TouchSessionOpenOrClose TouchSessionOpenOrClose(Data.IDataSeries input, string closeTime, double closeTolerance, string openTime, double openTolerance)
        {
        return _indicator.TouchSessionOpenOrClose(input, closeTime, closeTolerance, openTime, openTolerance);
        }

        }
        }


        // This namespace holds all strategies and is required. Do not change it.
        namespace NinjaTrader.Strategy
        {
        public partial class Strategy : StrategyBase
        {
        /// <summary>
        /// voice and visual message
        /// </summary>
        /// <returns></returns>
        [Gui.Design.WizardCondition("Indicator")]
        public Indicator.TouchSessionOpenOrClose TouchSessionOpenOrClose(string closeTime, double closeTolerance, string openTime, double openTolerance)
        {
        return _indicator.TouchSessionOpenOrClose(Input, closeTime, closeTolerance, openTime, openTolerance);
        }

        /// <summary>
        /// voice and visual message
        /// </summary>
        /// <returns></returns>
        public Indicator.TouchSessionOpenOrClose TouchSessionOpenOrClose(Data.IDataSeries input, string closeTime, double closeTolerance, string openTime, double openTolerance)
        {
        if (InInitialize && input == null)
        throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

        return _indicator.TouchSessionOpenOrClose(input, closeTime, closeTolerance, openTime, openTolerance);
        }

        }
        }
        #endregion

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Christopher_R, Today, 12:29 AM
        0 responses
        9 views
        0 likes
        Last Post Christopher_R  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        166 responses
        2,235 views
        0 likes
        Last Post sidlercom80  
        Started by thread, Yesterday, 11:58 PM
        0 responses
        3 views
        0 likes
        Last Post thread
        by thread
         
        Started by jclose, Yesterday, 09:37 PM
        0 responses
        8 views
        0 likes
        Last Post jclose
        by jclose
         
        Started by WeyldFalcon, 08-07-2020, 06:13 AM
        10 responses
        1,415 views
        0 likes
        Last Post Traderontheroad  
        Working...
        X