I have an idea regarding trading the YM. I am having a difficult time with the
coding of the stops. I think I know how to take the stop loss stop and turn
it into a breakeven stop. Unfortunately I don't know how to move the breakeven
up with each upward (downward) tick of the YM.
If the YM is above the breakeven by 10 points that is fine.
If the YM goes up one point I would need the breakeven to turn into a trailing
stop, with the YM still above the stop (in this case trailing) by 10 points.
Someone at another forum gave me some code, and some of that is here:
First the coding might be wrong, but besides that there would have to
be a few hundred lines of code (just for a bull move) as the YM moves
up. A little block something like the above for every tick up. What if
the YM moves up 50 points?!
Anyway I was hoping that someone already had this problem and had it
answered, or at least maybe someone has an idea to create a loop
(or equivalent) with NinjaScript to avoid writing such a long program.
This code was written by a consultant for another project of mine. I have parsed out specifics for stops: perhaps it might be useful as you develop your code. Sorry, I cant support it as I am barely at the point of understanding what parts of the strategy go where, but I am trying!
The following user says Thank You to Trader.Jon for this post:
I also tried for weeks to have a trail code "functioning" in my strategies. Can certainly understand the stress and frustration when days and nights were spent and nothing seemed working.
Just got the enclosed sample and hope it may help you. Good Luck!
Beth
The following 2 users say Thank You to wgreenie for this post:
Sorry to throw in another idea here which is opposite to yours. I learned the following from a more experienced member:
Quote
Due to its inherent design, Ninja forces limitations upon your strategy. As an example, Ninja requires that you set OnBarClose to true in order to have any chance at success. That alone limits your design tremendously.
Unquote
Based on my experiences, when I had CalculateOnBarClose @false, sim-trade and Market Replay results had looping trades, ie 1 smooth short trade became choppy trades in-and-out within a second. Not able to identify the cause for sure though with CalculateOnBarClose @true there're no looping trades.
Sorry, I don't mean to burst your bubble, but this is just not true.
There are two models that you have to choose between. You can either process data at the end of each bar (which is really the beginning of the next bar depending on the bar type) or on each incoming tick. The former is when CalcOnBarClose=true and the latter is when CalcOnBarClose=false.
With either model, you need to understand what Ninja is actually doing behind the scenes when you are calling the enter/exit/setStop/etc routines. Sadly, the doc almost helps (all of the little warnings that this is advanced coding should be a red flag).
Unfortunately, it is pretty easy to write code that works well in backtests, but fails when you run live. This is usually a problem in the strategy coding, not some limitation in Ninja. If you want to be accurate, you need to use COBC=false, but that means you need to dig in and really understand what is going on.
The following 3 users say Thank You to aslan for this post:
Thanks aslan. That's good news that CalculateOnBarClose @ false will work IF one knows what's going on! Unfortunately not me as for some unknown reasons seemed it might have triggered looping trades PLUS for weeks now I still have a super-mega big head trying to make a simple strategy with a trail code to work!!
* limit your data set (i.e. specific day or less) to help cut down on the amount of info to sift thru when debugging
* use set TraceOrders = true in Initialize() in order to see all order actions in Ninja in the output window, this combined with your own prints should help you figure out where your looping is coming from.
* make sure you are doing compare of price values correctly, Close[0] > lastPrice does not cut it when comparing double values. Just comparing values works most of the time, but you really need to use the following: Instrument.MasterInstrument.Compare() - see help. I hate needing this, and I usually leave it out until I get to the final code and then add it in. If you have a comparison that you think should trigger this could be it.
Hope this gives you some ideas.
The following 2 users say Thank You to aslan for this post:
Wow, this script right here is exactly why I joined the elite futures.io (formerly BMT) section. Super thanks, I know its 4 years later but NT is still the leader!
Yes Stephen. NT Support Forum is extremely important/helpful for my learning. You can also email the NT support team directly for any issues you may have.