Welcome to NexusFi: the best trading community on the planet, with over 200,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 -- discounts are available after registering.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
After positions are taken, I am monitoring maximum unrealized P/L in a variable called HighestUPnL. When that amount tops $40 (5 ticks in a $10/tick instrument, less transaction fee), I am TRYING to reset the stop to breakeven-1 tick.
The code intended to do so is as follows (I am just showing for short, though have another condition for long):
Per NinjaTrader support, the code to move/set the stoploss is correct. However, when I run the strategy, I get errors, upon trying to take a position, that I can't place orders above (or below, depending whether short or long) the market.
If I comment out the line SetStopLoss("", CalculationMode.Price, Position.AvgPrice - 1 * TickSize, false); I get no errors. This, despite the fact that that code SHOULD only run when the position is open and unrealized P/L is above 40.
When the line is not commented out, the strategy will try to place an order (don't know if for entry or for the stop) MANY points away from the current price.
NT support is telling me it's due to a fast moving market, but no market consistently has transient orders at prices dozens of points away from current market value. They recommended I add, above the line to move the stop to breakeven+1, a line so I have:
When I do so, my strategy never places an order. (I did try to incorporate the GetCurrentBid() check incorporated into the one 'if', but never got that to even compile correctly.)
Despite all my emailing, the tech I am working with insists that the way I am trying to do this screws up because the market is moving too fast and my strategy is trying to place orders above/below inappropriately. However, when telling an ATM strategy to move my stop to breakeven+1, there's never any issue, so I think he's full of it. (I'm a neophyte, so I may be wrong.)
So, I have two issues:
1) My initial order fails if I have the code line below NOT commented out:
Even leaving THAT not commented out makes my initial order fail.
I'm even considering forgetting the stoploss and simply placing limit orders where I want them, but writing the code for that looks even more daunting.
Does anyone have a clue what is going on with this, or have suggestions where to look or what to do? I am fairly desperate, right about now.
Can you help answer these questions from other members on NexusFi?
This is a snippet of code form a robot i wrote, this is actually code that updates both
target and stop to new (higher levels). Use are you own risk
But it might help you forward...
In order to understand what you are trying to do and when
please create a logging :
current price
long or short
previous stop limit
stop limit you are trying to establish
As a sugestion
you might change your code into something :
if (long)
stop < current
SetStopLoss..
else
ExitLong..
if (shot)
stop > current
SetStopLoss
else
ExitShort
(be carefull with exitposition, close to stop's, you might have an OverFill ;-)
which is another headacht
Thank you. The problem I seem to be having is with the syntax of SetStopLoss..., so while your suggestion is good, in terms of cleaning up my code for testing, I'm left with the same syntax that appears to be causing me problems.
Solid diagnostic thinking. Logging those four values is exactly how you catch what's actually happening vs. what you think is happening.
Here's the root cause that logging will almost certainly expose: SetStopLoss() fires on every OnBarUpdate() -- including when you're flat. When Position.MarketPosition is Flat, Position.AvgPrice returns 0. So your stop calculation is computing an offset from 0, which is why orders end up dozens of points from the market. Once you log it, you'll see the exact moment it goes wrong.
Your pseudocode guard is the right instinct:
If long: verify stop price is below current price before calling SetStopLoss
If short: verify stop price is above current price before calling SetStopLoss
If flat: skip SetStopLoss entirely
One alternative worth knowing: ExitLongStopMarket() / ExitShortStopMarket() with isLiveUntilCancelled=true called from OnExecutionUpdate() gives you cleaner order lifecycle control than SetStopLoss() for lively stops. Better suited when you're adjusting stops as the trade develops.
Critical caveat though -- don't mix them. SetStopLoss() and the explicit exit methods are treated as separate orders for the same signal, which violates NT's Internal Order Handling Rules. Pick one approach and stick with it throughout your strategy.
The overfill warning is real, especially on ES during news. When stop and market price converge fast, slippage can exceed your expected exit price by a tick or two.
-- Fi
"The stop that fires at the wrong price isn't a logic problem -- it's a state problem. Know where you are before you tell the market where to exit."
Please leave feedback here. You can disable my ability to reply to your posts by placing me on your ignore list.
Fi provides educational information on a best-effort basis only. You are responsible for your own trading decisions and for verification of all data. This message is not trading advice.