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
How to convert a LessThan to a CrossBelow for an Exit Long
Collapse
X
-
How to convert a LessThan to a CrossBelow for an Exit Long
Hello - I have a strategy that compares an SMA[0] to SMA[1] and then either makes a trade or exits a trade depending on whether it is greater or lesser than. Is it possible to do this with CrossAbove and CrossBelow? Attached is the code. Thank you for any help.Tags: None
-
Hello JohnS52,
Thanks for your post.
Here are links to the CrossAbove and CrossBelow methods:
In both methods there are two ways to use them, one data series cross another or one data series cross a double value. In your code, you were trying to have a double value cross another double value.
Again, referencing your code, here is a specific example of one crossing the other:
if (CrossAbove(SMA1, SMA2), 1) // fast cross above the slow
{
// do something
}
Paul H.NinjaTrader Customer Service
-
Hello Paul, thanks for the reply.
What I am trying to do is (CrossAbove(SMA1, SMA2[2]), 1) in which I find the point where the SMA1 crosses above the value of itself from 2 bars ago (similar to using a displaced indicator value).
This page seems to suggest that I can do that but it doesn't compile: https://ninjatrader.com/support/help...__brackets.htm
from the example for Using Brackets: if (CrossAbove(SMA(14), SMA(28)[0], 1)) // would work fine with a ISeries<double> as first argument and a double as the second argument
Comment
-
Hello JohnS52,
Thanks for your reply.
I agree that this line:
if (CrossAbove(SMA1, SMA2[2], 1))
{
}
will compile, so please review the specific error message and ensure that it relates to that script that you have open (as indicated on the left column "Ninjascript file" ) and check the line/column number to ensure that it is that line that is in error.
Note: the compiler will compile all scripts at the same time so any error in any file will show below the open Ninjascript file in the editor.
reference: https://ninjatrader.com/support/help...ile_errors.htmPaul H.NinjaTrader Customer Service
Comment
-
Hello Paul -
Thanks for your help. I have incorporated the changes and am attaching the revised strategy. It compiles fine and runs in Strategy Analyzer but the exits are not working properly. Oddly, I can get [1] to work but not [2]. I am attaching an image. What am I doing wrong: Any suggestions?
Comment
-
Hello JohnS52,
Thanks for your reply.
The reason it would or would not enter or exit would be based on the logic and the values the logic is using.
To figure out why your strategy is performing as it does, the best approach would be to print out the variables used on a bar by bar basis as this will show you what values were at each point.
This debugging process is done with simple Print statements that send their output to the New>Ninjascript output window.
An example for you would be:
Print (Time[0]+" SMA1: "+SMA1[0]+" SMA2: "+SMA2[0]+" SMA3: "+SMA3[0]+" SMA4-1: "+SMA4[1]);
In addition or another approach would be to have your exit conditions draw on the chart when they are true.
Here is a link to our debugging tips: https://ninjatrader.com/support/help...script_cod.htm
Paul H.NinjaTrader Customer Service
Comment
-
Hello Paul - thanks for the tips. CrossAbove(SMA1, SMA2, 1) works great - CrossAbove(SMA1, SMA2[1], 1) does not work at all: it compiles but the code does not produce results. There must be some way to access the value from the prior bar. I can use the Values syntax, as I was doing previously, but this doesn't transfer to a CrossOver. The problem with > and < is they are continuous events, whereas a CrossAbove/CrossBelow is a one-time event. I suppose Slope would achieve the same effect. Any other suggestions?
Comment
-
Hello JohnS52,
Thanks for your reply.
I took your strategy and commented out all of the code and tested this code:
if (CrossAbove(SMA1, SMA2[1], 1))
{
BackBrush = Brushes.LightGreen;
}
In the attached screenshot (below) you can see that it does indeed work. You can always test a condition in such a manner.
Regarding, "The problem with > and < is they are continuous events, ..." Correct, you would have to use additional logic to control processing. For example, once the condition has been found you could set a bool variable so that the condition is not checked again until you have reset the bool. For example
if (SMA1[1] < SMA2[1] && SMA1[0] > SMA2[0] && myBool = true) // was the 1 sma below the 2 sma on the previous bar and is now (current bar) the 1 sma above the 2 sma?
{
// a cross over has occurred
myBool = false; // prevent checking again
}
if (some condition you determine)
{
// reset bool(s) here
myBool = true; // ready for next crossover
}
Paul H.NinjaTrader Customer Service
Comment
-
Thank you, Paul, for the suggestions.
(CrossAbove(SMA1, SMA2[1], 1) is working today: not sure what my coding error was yesterday. (CrossAbove(SMA1, SMA2[2], 1) doesn't work but that may not matter here.
Just so I understand the coding: (CrossAbove(SMA1, SMA2[1], 1) means that the SMA1[calculated from the current bar] crossed above the SMA2[calculated from one bar previous], 1 bar ago - is that correct?Last edited by JohnS52; 06-18-2020, 12:28 PM.
- Likes 1
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by mly916, Today, 09:48 PM
|
0 responses
6 views
0 likes
|
Last Post
by mly916
Today, 09:48 PM
|
||
Started by Mikey_, Today, 09:21 PM
|
0 responses
10 views
0 likes
|
Last Post
by Mikey_
Today, 09:21 PM
|
||
Started by cre8able, Today, 05:53 PM
|
3 responses
13 views
0 likes
|
Last Post
by cre8able
Today, 09:20 PM
|
||
Started by Aryan Kumar, Today, 09:04 PM
|
0 responses
4 views
0 likes
|
Last Post
by Aryan Kumar
Today, 09:04 PM
|
||
Started by yertle, Yesterday, 05:49 PM
|
3 responses
16 views
0 likes
|
Last Post
by Mindset
Today, 08:38 PM
|
Comment