protected override void Initialize() { Add(PeriodType.Day,1); } protected override void OnBarUpdate() { double ATRDaily = (ATR(BarsArray[1],14)[0]); if (Position.MarketPosition == MarketPosition.Flat) { if (ConditionA && ATRDaily ) { EnterLong(); } } }
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!
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
NinjaTrader
PeriodType.Day
Collapse
X
-
PeriodType.Day
I'm in a lower time frame and would like to have the daily ATR as a condition in the strategy with the following code, but when executing the strategy, the whole strategy stops working. Can I call a PeriodType.Day in a strategy or is the syntax wrong?
Code:Tags: None
-
Hello 2Look4me,
Thanks for your post.
If the strategy stops working you may want to check the "log" tab of the control center for any error messages related to the strategy.
I suspect you will need to check for the correct number of bars to be loaded before processing your code. If you are looking at a 14 day ATR, then to have the correct value for the ATR you would need a minimum of 14 days loaded before trying to execute the code starting at the line double ATRDaily = (ATR(BarsArray[1],14)[0]); This would be done by using a CurrentBars[1] check. For example, as the first statement in the OnBarUpdate() method you would have: if (CurrentBars[1] < 14) return; // do not process further until 14 daily bars loaded. Reference: http://ninjatrader.com/support/helpG...urrentbars.htm
Also in the conditions statement: if (ConditionA && ATRDaily ) you would need to have the ATRDaily resolve to a logical condition as currently it is a numeric value. An example would be if (ConditionA && ATRDaily > 30.2)
When using add dataseries please note that when the daily bar closes it will also call OnBarUpdate() and you may experience unintended actions as all of the references would change from your lower time frame to the higher timeframe. In this case, you may want to add a statement at the beginning, after the CurrentBars check, to check for the "BarsInProgress". A BarsInProgress value of 0 (zero) will be the chart bars BarsInProgress and 1 would be in this case the daily bars, something like: if (BarsInProgress != 0) return; // only execute when lower time frame calls OBU. Reference: http://ninjatrader.com/support/helpG...inprogress.htm
To tie all these together we recommend reviewing this section of the helpguide: http://ninjatrader.com/support/helpG...nstruments.htmPaul H.NinjaTrader Customer Service
-
Paul, thanks for the detailed explanation and for the links. I'm still processing the info and made the following changes with the same results as before and the log tab shows no errors either.
Code:protected override void Initialize() { Add(PeriodType.Day,1); } protected override void OnBarUpdate() { if(CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired) return; bool ATRDaily = (ATR(BarsArray[1],14)[0])>=30; if (BarsInProgress != 0) return; if (Position.MarketPosition == MarketPosition.Flat) { if (ConditionA && ATRDaily) { EnterLong(); } } }
Comment
-
Hello 2Look4me,
Thanks for your post.
At this point you would need to use print statements in your code to help you assess the actual values of ATR(BarsArray[1],14)[0] and of the state of the bool ATRDaily.
I copied your code and removed the section:
if (ConditionA && ATRDaily)
{
EnterLong();
}
and in its placed added this print line:
Print ("Daily ATR value: "+ATR(BarsArray[1],14)[0]+" ATRDaily state: "+ATRDaily);
I used the ES 12-16 with 60 minute bars and added the strategy to it, which resulted in an output like:
Daily ATR value: 22.5002226248533 ATRDaily state: False
Daily ATR value: 22.5002226248533 ATRDaily state: False
Daily ATR value: 22.5002226248533 ATRDaily state: False
Daily ATR value: 22.5002226248533 ATRDaily state: False
Daily ATR value: 30.7680638659352 ATRDaily state: True
Daily ATR value: 30.7680638659352 ATRDaily state: True
Daily ATR value: 30.7680638659352 ATRDaily state: True
Daily ATR value: 30.7680638659352 ATRDaily state: True
Note: I suspect you may not have enough bars loaded. In my test it required 40 days of data to produce resultsPaul H.NinjaTrader Customer Service
Comment
-
As soon asCode:Add(PeriodType.Day,1)
Then adding the bars check:
Code:if(CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired ) return;
Therefore as I followed what you suggested, there is no Print in the Output Window and there aren't any errors in the log tab.
Thanks for the continuous help.
Comment
-
Hello 2Look4me,
Thanks for your reply.
Please download the attached sample strategy to your Ninjatrader.
Open a chart of ES 12-16, 60 minute bars with 50 days of data.
Open the output window (Tools>output window)
Apply the strategy to the 60 minute chart and then observe the output window.
You might then review the differences between your code and the sample code as the sample code is based on what you have advised in this thread.Attached FilesPaul H.NinjaTrader Customer Service
Comment
-
Paul, thanks for the sample code. I followed your instructions:
HTML Code:Please download the attached sample strategy to your Ninjatrader. Open a chart of ES 12-16, 60 minute bars with 50 days of data. Open the output window (Tools>output window) Apply the strategy to the 60 minute chart and then observe the output window.
Comment
-
With your sample code I isolated the problem, but I'm getting an error from the following code:
Code:if(Position.MarketPosition == MarketPosition.Flat) { if(BarsSinceExit()!=0 && ConditionA && ConditionB) { EnterLong(); } }
HTML Code:You must use the overload that has a BarsInProgress parameter when calling the BarsSinceExit method in the context of a multi time frame and instrument strategy
Comment
-
Hello 2Look4me,
Thanks for your reply.
The error message is advising that because you have a multitimeframe strategy that you will need to use the overload of the method that includes the BarsInProgress index so that it then would know which Bars object you want Ninjascript to look for the last exit on.
Please see the overload syntax of the method in the helpguide: http://ninjatrader.com/support/helpG...ssinceexit.htm
In addition, note that the example in the helpguide shows two checks of the BarsSinceExit(), one is to see if the specified number of bars have passed since exit and the other is to see if there has been an exit (-1). You will need to add that check for -1 or your condition may not become true.Paul H.NinjaTrader Customer Service
Comment
-
Paul, thanks for the link and the explanation. Upon adding:
Code:if((BarsSinceExit()!=0 || BarsSinceExit()==-1) && ConditionA && ConditionB) { EnterLong(); }
Comment
-
I restarted NT. Still the strategy cannot be enabled as by the previous post. After failing to enable the strategy, there are no errors in the log tab, but now I noticed that the output window gives the same error:
HTML Code:You must use the overload that has a BarsInProgress parameter when calling......
Comment
-
Hello 2Look4me,
Thanks for your reply.
The issue here is that you did not use the overload method as shown below from the helpguide:
Syntax: BarsSinceExit(int barsInProgressIndex, string signalName, int exitsAgo)
where:
barsInProgressIndex - The index of the Bars object the entry order was submitted against.
signalName - The signal name of an entry order specified in an order entry method. Pass in empty string "" for default signal.
exitsAgo - Number of exits ago. Pass in 0 for the number of bars since the last exit.
Taking a guess on what you have shared, , I suspect you want to use 0 for the BarsInProgress. If you haven't given your orders a name then you would use "" for the signal name and I would assume you want the last exit so a 0 would work there. Putting it together:
BarsSinceExit(0,"",0)!=0 || BarsSinceExit(0,"",0)==-1)Paul H.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by mattbsea, Today, 05:44 PM
|
0 responses
4 views
0 likes
|
Last Post
by mattbsea
Today, 05:44 PM
|
||
Started by RideMe, 04-07-2024, 04:54 PM
|
6 responses
31 views
0 likes
|
Last Post
by RideMe
Today, 05:31 PM
|
||
Started by tkaboris, Today, 05:13 PM
|
0 responses
2 views
0 likes
|
Last Post
by tkaboris
Today, 05:13 PM
|
||
Started by GussJ, 03-04-2020, 03:11 PM
|
16 responses
3,282 views
0 likes
|
Last Post
by Leafcutter
Today, 04:14 PM
|
||
Started by WHICKED, Today, 12:45 PM
|
2 responses
20 views
0 likes
|
Last Post
by WHICKED
Today, 03:36 PM
|
Comment