Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
bars since cross
Collapse
X
-
bars since cross
I would like to set a condition for a strategy, an action to take place within the first 15 bars of a MACD cross, how would I do this?Tags: None
-
Hello jackegtrader, and thank you for your question.
I have prepared a series of images showing how to generate a template for the kind of condition you would like through the New Strategy wizard. Step by step,
1. Control Center -> Tools -> New NinjaScript -> Strategy
2. Next -> Give your script a name -> Next
3. Let's skip the parameter screen. Next
4. On the Condition Builder screen, click "Add"
5. Select Indicator -> MACD and set the Bars ago to 15 on the left. Select == as the operator. Select Misc -> true on the right hand side. Select OK
6 (Not pictured) Select "Unlock Code"
Please let us know if there is anything else we can do to help.Last edited by NinjaTrader_JessicaP; 05-02-2016, 10:00 AM.Jessica P.NinjaTrader Customer Service
-
Jessica, how does this help with bars 1-14? And it only refers to the MACD, not the MACD cross. And where does one go once the code is unlocked?
Comment
-
Hello birdjaguar,
I am attaching an image with a more complete example. Note that I set the 2 MACD data series in the Plot section. Note also that I set 15 for the look back period. Finally note that I set crossabove as the comparator.
I am not sure I understand what is meant with your second question, but all the code generated here will go into the OnBarUpdate section. If that does not quite answer your second question, could you elaborate?Jessica P.NinjaTrader Customer Service
Comment
-
Thank you. Your attachment is very helpful. My question now is how can a lookback period be applied to an operator? In the wizard the lookback period is only available for crossabove or crossbelow. How can the lookback period be used with > or < or >= etc.?
Comment
-
Thank you for the clarification. You are correct that you would have to unlock code for this. Applying a lookback period with an operator can be accomplished with a for loop. For instance, this for loop over a 15 bar lookback period will alert you if two MACD lines were equal at any point in the last 15 bars.
Code:[FONT=Courier New]int lookback = 15, fast = 3, slow = 15, smooth = 7; bool conditionOccurred = false; MACD macd = new MACD(fast, slow, smooth); for(int i = 0; i < lookback && ! conditionOccurred; i++) { if (macd.Avg[i] == macd.Diff[i]) { conditionOccurred = true; } }[/FONT]
The above style of for loop tests to see if the comparator was valid at any point in the lookback period. If you would prefer to see if the comparator was valid at every point in the lookback period, we can just invert our boolean, like so :
Code:[FONT=Courier New]int lookback = 15, fast = 3, slow = 15, smooth = 7; bool conditionOccurred = [B]true[/B]; MACD macd = new MACD(fast, slow, smooth); for(int i = 0; i < lookback && ! conditionOccurred; i++) { if (macd.Avg[i] [B]!=[/B] macd.Diff[i]) { conditionOccurred = [B]false[/B]; } }[/FONT]
Jessica P.NinjaTrader Customer Service
Comment
-
Thank you. That is thorough and very useful. I actually did go down that path, but was not savvy enough in C# programming to be able to pull it off. I was trying to combine signals between a MACD and Heiken Ashi within 5 bars. The MACD became too complicated for me at this point; so I used the "lookback"concept from your previous post to solve the problem on the Heiken Ahsi end. Given a MACD signal, I used the following:
CrossAbove(HeikenAshi().HAClose, HeikenAshi().HAOpen, 5) for the long side and
CrossBelow(HeikenAshi().HAClose, HeikenAshi().HAOpen, 5) for the short side.
Thanks again. I am good to go.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by algospoke, Yesterday, 06:40 PM
|
2 responses
20 views
0 likes
|
Last Post
by algospoke
Today, 07:04 PM
|
||
Started by ghoul, Today, 06:02 PM
|
3 responses
14 views
0 likes
|
Last Post Today, 06:43 PM | ||
Started by jeronymite, 04-12-2024, 04:26 PM
|
3 responses
45 views
0 likes
|
Last Post
by jeronymite
Yesterday, 10:10 PM
|
||
Started by Barry Milan, Yesterday, 10:35 PM
|
7 responses
21 views
0 likes
|
Last Post Today, 06:16 PM | ||
Started by AttiM, 02-14-2024, 05:20 PM
|
10 responses
181 views
0 likes
|
Last Post
by jeronymite
Today, 06:11 PM
|
Comment