Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Just getting into programming so I thought I would set myself a challenge of building what I thought would be a fairly simple strategy. I am attempting to create a strategy that does the following: When the price reaches a point that is + or - 5 ticks of it either goes long or short.
Example: my indicator (lets use a SMA just for example) is at 95.05 the price is currently at 94.8. What I would like to have happen is that as the price moves up, at 95.00 it will enter a short position. If the price continues to go up at 95.10 it would reverse and enter a long position. (or vice versa ofc if the price was coming down to my SMA.
I started out using the wizard, and then looking at the view code button of the different sample strategies figured I would need to use an if, then else if statement to effect the reverse.
Here is my code currently.
Anyways, currently it does not seem anything is working. Not sure what I am doing wrong - when I hit the compile button it doesn't come up with any logic errors. If someone could help me with this that would be great!
I added in the brackets so the code now looks like this:
It is sort of working. For whatever reason it was going long when I meant it to go short and short when I meant it to go long so I switched the short/ long orders and the directions are atleast good.
However it doesnt seem to reverse, it only does the one trade?
You are not using predefined reusable instances of the external class, which in this case is the SMA class..
Not doing so exacts a huge performance penalty.
Refer to the "Anyone have any hints for Optimizing C# code" thread for a further explanation.
The strategy wizard produces code in a simplistic manner without regard for its operational efficiency.
The examples in the Ninjatrader Help coding tutorials likewise ignore such considerations.
"If we don't loosen up some money, this sucker is going down." -GW Bush, 2008
“Lack of proof that something is true does not prove that it is not true - when you want to believe.” -Humpty Dumpty, 2014
“The greatest shortcoming of the human race is our inability to understand the exponential function.” Prof. Albert Bartlett
So what I am reading in that other thread is basically the way the SMA is calculated is every tick throughout the bar it is recalculated which obviously takes up computing power.
Looking at the code for the stochastic that you optimized you put in a If statement
if(FirstTickOfBar)
Whereas in Richard Todd's article that I found on the ninjatrader link he uses
a void OnBarUpdate.
im thinking that because I do want every tick to check if it has gotten close to the SMA for my entry conditions I will use the void OnBarUpdate?
If you haven't already done so, read the NinjaTrader help file and in particular look for the statements you are using and what their requirements are. It takes time to go through the help file but there really is quite a bit of relevant info and examples in there.
In general:
1) When something does not seem to execute check the Ninja Log for any error statements. Also bring up the output window to see if anything is printed there.
2)use Print statements (that output to the Output window) to identify that your code is executing through the logical area in question. For example use a Print statement in your "else if" section to show that the program is going through the "else if" section. If it does then use further print statements perhaps to advise the status of the variables being used to help understand the logic decisions.
It can be frustrating yet rewarding when you make breakthroughs in coding, just keep plugging away.