![]() |
This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com
|
|||||||
| General Programming General NinjaScript programming questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Junior Member
Join Date: Jan 2009
Posts: 5
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
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
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Jan 2009
Posts: 584
Thanks: 2
Thanked 21 times in 12 posts
|
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]; |
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Jan 2009
Posts: 584
Thanks: 2
Thanked 21 times in 12 posts
|
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 |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Next day open price lover than stop loss. | Czarek | Strategy Development | 7 | 06-13-2011 05:39 AM |
| 9.30 open price, 4.30 close | diffused | Strategy Development | 3 | 01-20-2009 01:18 PM |
| how to sendmail() with current symbol and close price | tbtrades | Miscellaneous Support | 3 | 10-17-2008 12:28 PM |
| How to not let strategy close open order on end of day? | bonnyshi | Strategy Development | 2 | 10-08-2008 08:26 AM |
| Virtual execution price is not the same as the current price | Xtrooper | ATM Strategies (Discretionary Trading) | 4 | 10-06-2008 09:12 AM |