Setting a Stop Loss to Activate before Interval Close
I’m fairly new to developing using NinjaTrader and would really appreciate some help with this.
I’m running 10 minute interval bars, but in cases of rare volatility the unrealized P/L will exceed my stop loss I’ve set (-4%). I need the rest of the variables for entries/exits to calculate on bar close, but the stop loss needs to be calculated at (what I assume to be) a lower interval rate so the close of the 10 minute bar doesn’t have a massive loss exceeding the -4%.
Is it best to set a lower interval bar specifically for this stop loss, and if so, how do I create a custom interval for this specific exit. Alternatively, if there’s an easier/better way of doing this (where the exit will occur before the end of the 10 minute interval if the unrealized P/L is met) I’d appreciate the advice.
//Stop Loss -0.04
if (Position.GetProfitLoss(Close, PerformanceUnit.Percent) < -0.04)
I assume you have it in the "OnBarUpdate" ?
This means the check will only be done when the bar closes
There are a few solutions :
1. you can subscribe another event, that gives you more granularity
you could for example put this logic in the OnMarketUpdate,
but be carefull as this code will run for every tick coming in,
including the bid/ask changing
2. you can place the order with a ATM strategy
which would automatically put the stop
the stop could your fixed 4% or a more intelligent trailing stop
It looks like if I create a stop loss at -0.04 then it will take loss 'mid-bar' (in the middle of the 10 minute interval), but when I add any Stop Losses ie: "SetStopLoss("S1", CalculationMode.Percent, 0.02, false); it automatically prevents any of my other exits I have created from working.
It seems like creating simple stop losses is the easiest method, but can you help me figure out why none of my other exits I've created are working as soon as I add the 'SetStopLoss' to the code (since many of those exits would have occurred prior to the stop loss or exit at close occurring). I've tried putting the stop loss code both in 'Initialize' and 'OnBarUpdate'.
Thank you again for you help I really appreciate it.
Look at the BOT I have published in the past, it has all kinds of bits of code you might be able to find usefull
you will for example see a mechanism to update the target/stop upon certain conditions (i called it turbo
To be honest I'm trying to learn as much as possible but given my inexperience I want to understand how to code using an automated strategy first before moving to ATM, though I will definitely check out the link you sent (thank you!).
Do you know the potential reason for the problem of none of the other exits I've created working as soon as I add a SetStopLoss into the code. I've tried placing the SetStopLoss in a number of different places but it doesn't seem to make a difference. It seems like I'm making a fairly simple error since it wouldn't make sense for a stop loss to replace any other exit. Again, thank you for your help!
• The SetStopLoss() method can NOT be used concurrently with the SetTrailStop() method for the same position, if both methods are called for the same position (fromEntrySignal) the SetStopLoss() will always take precedence. You can however, use both methods in the same strategy if they reference different signal names.
• Stop loss orders are submitted in real-time on incoming executions from entry orders
• A strategy will either generate a stop loss order for each partial fill of an entry order or one order for all fills. See additional information under the Strategies tab of the Options dialog window.
• If a profit target order is generated in addition to a stop loss order, they are submitted as OCO (one cancels other)
• Stop loss orders are submitted as stop market orders
• A stop loss order is automatically cancelled if the managing position is closed by another strategy generated exit order
Looking to the log file, might explain what happens in your case
(log tab in NT)
I had read this, but since I don't have any trailing stop orders included, I don't think that's the issue, nor the other bullets listed.
From what I understand, a stop loss will initiate a market order to close a position if a specific loss is reached (ie: if the stop loss is -4%, then if you have bought stock, when the current price is -4% below the avg. entry price, it will create a sell using market).
My issue still remains that prior to adding a Stop Loss to my code, all of my other Exits work fine (taking appropriate profits or losses when the criteria is met). But as soon as I add a 'SetStopLoss', the strategy will ignore any other exits and only exit when the stop loss is met or 'Exit on Close' occurs........even though an exit that I had created that would have been profitable occurred before the stop loss or exit on close.
I've placed my entries and exits (excluding stop loss) within 'OnBarUpdate()', and my stop loss within 'Initialize()'. But I still can't figure out why SetStopLoss will supersede any other exits that would have occurred normally (if the criteria was met to exit before the stop loss). Very frustrating, but I feel like this is a common error.
If it helps, when I create a strategy using the builder, any stop losses will also supersede any exits that are made as a condition, even if the 'condition exit' occurs before the stop loss. Please let me know if you see a potential error in what I've done. As always, your help is really appreciated.
Not sure i understand what you are trying to do, but i think i see the mistake
let me articulate it differently.
If you are in a position, you can have two orders attached to that position.
that is typically called your TARGET and your STOP.
The Target means, when the price is reached and someone is candidate buyer
and it is your turn in the queue, your position is sold.
The Stop means you will sell at any price, once the price is reached (another
sale ticks the price). at that moment your position is sold at a market order,
this means the next buyer in the market gets filled with your position, hence
the world slippage, if that buyer is 1, 2, 3 ticks lower, you will be unloaded
at that lower price and even lower, if you sell multiple contracts
Now you can only have 1 STOP, when that stop is reached, you are unloaded
I understand what you're saying and appreciate your explaining the 2 orders (Profit/Stop), but if this is the case I misunderstood the function of the stop. I was under the impression that I could have multiple conditions for exit that could still occur if their criteria was met prior to the Stop(s).
Since this isn't the case, my problem unfortunately remains. I'm running 10 minute intervals, and have a condition that leads to an exit if a loss exceeding -0.04 occurs (if (Position.GetProfitLoss(OnMarketUpdate, PerformanceUnit.Percent) < -0.04). This is current a part of OnBarUpdate (which I realize means that it'll only look at the end of each interval).
I need this exit to occur at any time a loss of -4% is reached, instead of only checking at the end of each bar. if I can achievethis by using OnMarketBar or OnMarketUpdate, I'm not sure how to program/code or where to place it. I'm completely ok with it using the 'Last', 'Bid' or 'Ask' to constantly check to see if the Unrealized P/L of -0.04% has been reached.