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
The following user says Thank You to Zondor for this post:
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.
The following user says Thank You to Tasker_182 for this post:
When I look at the output window as the code works I get this:
ShortCurrentAsk: 92.94 (Offsetneg+Tick): -0.05 SMA: 92.8932
Calculated value: 92.89
2013-06-04 3:37:38 AM Entered internal PlaceOrder() method at 2013-06-04 3:37:38 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
ShortCurrentAsk: 92.94 (Offsetneg+Tick): -0.05 SMA: 92.8932
Calculated value: 92.89
2013-06-04 3:37:39 AM Entered internal PlaceOrder() method at 2013-06-04 3:37:39 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
2013-06-04 3:37:39 AM Ignored PlaceOrder() method at 2013-06-04 3:37:39 AM: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Buy' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
ShortCurrentAsk: 92.94 (Offsetneg+Tick): -0.05 SMA: 92.8932
Calculated value: 92.89
2013-06-04 3:37:39 AM Entered internal PlaceOrder() method at 2013-06-04 3:37:39 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
2013-06-04 3:37:39 AM Ignored PlaceOrder() method at 2013-06-04 3:37:39 AM: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Buy' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
ShortCurrentAsk: 92.94 (Offsetneg+Tick): -0.05 SMA: 92.8932
So in that instance the price was above the SMA, at 5 ticks above it went long(which is good!) the output window keeps coming up with this over and over again
2013-06-04 3:37:39 AM Entered internal PlaceOrder() method at 2013-06-04 3:37:39 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
2013-06-04 3:37:39 AM Ignored PlaceOrder() method at 2013-06-04 3:37:39 AM: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Buy' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
Which would mean to me that it keeps getting an order spammed at it that it cannot fill. When in reality all I would like is for it to do nothing until the price moved to 5 ticks below the moving average and have it reverse and go short then.
So basically right now it will do the first entry correctly but it will not reverse for me! :/
Anyways I will keep at it. seems like its a fairly decent sized learning curve but hopefully worth it once I figure it out!
You will see that the first parameter needs to be a DataSeries object, not a double. The expression
GetCurrentAsk() + (Offset * TickSize)
evaluates to a double, not a DataSeries. I'm surprised your code even compiles! NT's syntactical leniency is the cause of your headaches.
Personally, I've had problems getting these cross methods to work for me sometimes, maybe I was using them incorrectly, but a long time ago I decided to never use them. Now I do something like "if x > y and x wasn't > y in the last bar" logic to determine crosses above.
So if I was to use the Close instead of the ask (not sure why you're using the asking price) it could be