Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Entering X days before
Collapse
X
-
Entering X days before
I am looking for a way to make a strategy that will look at today's date and determine if it is "x" amount of days before another date that I have specified. I attempted this with the ToDay(Time[0]) function and was able to enter in the position when i wanted yet, if I wanted the strategy to hold the stock through to the next month It would always exit my position on the first day of the month. Is there any idea on how to get the Ninja script to not exit my position on the first of the month?Tags: None
-
if (ToDay(Time[0]) + xdays == 20090915 )
{
Enter Position
}
20090915 is the date I want to set. Then xdays is the number of days before hand when I would like to enter.
if (ToDay(Time[0]) - ydays > 20090915 )
{
Exit Position
}
The problem occurs here when the number of Y days would take you into another month. It always exits you on the first days of the month.
Comment
-
Thank you. My next question would be if i declare a variable using DateTime will it update everyday the strategy runs. I.E.
DateTime date1 = DateTime.Today;
DateTime date 2 = DateTime.Today.AddDays(3);
ultimately what i am trying to do is have the strategy enter on date1. which will be a certain amount of days before a given date (ex. November 12 20XX). Then Exit a certain amount of days after the given date. So it would enter on the 9th and then exit on the 14th.
Comment
-
Hello cboat,
DateTime.Today will grab your current system date from your PC Clock. The variable is initialized with this date and then it will depend on how you use it later on. If the value is assigned in OnBarUpdate() then it will update each time there is a new bar or tick.Ryan M.NinjaTrader Customer Service
Comment
-
Also I am getting an error message saying that I cannot reference a nonstatic field. I.E. divDate. here is a view of my code
private DateTime divDate = new DateTime (2010, 09, 10);
private DateTime today = DateTime.Today;
private DateTime entryDay = divDate.AddDays(-2);
private DateTime exitDay = divDate.AddDays(2);
Comment
-
Hi cboat,
For you first question, I believe so, have you tried this?
Have you added the appropriate get/set in the properties section of the code?
Something like...
Code:[Description("")] [GridCategory("Parameters")] public DateTime EntryDate { get { return entryDate;} set { entryDate = value;} }TimNinjaTrader Customer Service
Comment
-
Hi cboat,
Since some of your variables include others in their calculation, first, declare the variables...
Then make any adjustment under OnBarUpdate...Code:private DateTime divDate = new DateTime (2010, 09, 10); private DateTime today = DateTime.Today; private DateTime entryDay; private DateTime exitDay;
Code:entryDay = divDate.AddDays(-2); exitDay = divDate.AddDays(2);
TimNinjaTrader Customer Service
Comment
-
Excellent Thank you very the strategy is compiling without errors. My final question. Is when I backtest on the strategy, since I am using DateTime.Today the strategy is always working off my system clock today. This makes it difficult for me to make sure it is running correctly. Is there a small fix I can use to get around this problem?
Comment
-
Hi cboat,
Can you clarify? Are you concerned it will not actually use today's date?
You can print the value using Print()
More info at - http://www.ninjatrader-support.com/H...ide.html?Print
Or you can set this in the code as a specific date.TimNinjaTrader Customer Service
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
580 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
335 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
102 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
554 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
552 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment