NexusFi: Find Your Edge


Home Menu

 





Setting a Stop Loss to Activate before Interval Close


Discussion in NinjaTrader

Updated
    1. trending_up 3,124 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 11 posts
    2. attach_file 0 attachments




 
Search this Thread

Setting a Stop Loss to Activate before Interval Close

  #1 (permalink)
 stealthtrading 
Toronto
 
Experience: None
Platform: NinjaTrader
Trading: Gold
Posts: 10 since Feb 2016
Thanks Given: 0
Thanks Received: 0

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.
Thanks


//Stop Loss -0.04
if (Position.GetProfitLoss(Close[0], PerformanceUnit.Percent) < -0.04)
{

ExitShortLimit(GetCurrentAsk(), "S1L", "S1");
ExitLongLimit(GetCurrentBid(), "L1L", "L1");
}

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NexusFi Journal Challenge - April 2024
Feedback and Announcements
Request for MACD with option to use different MAs for fa …
NinjaTrader
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
ZombieSqueeze
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Retail Trading As An Industry
58 thanks
Battlestations: Show us your trading desks!
52 thanks
NexusFi site changelog and issues/problem reporting
48 thanks
What percentage per day is possible? [Poll]
31 thanks
GFIs1 1 DAX trade per day journal
29 thanks

  #3 (permalink)
 
rleplae's Avatar
 rleplae 
Gits (Hooglede) Belgium
Legendary Market Wizard
 
Experience: Master
Platform: NinjaTrader, Proprietary,
Broker: Ninjabrokerage/IQfeed + Synthetic datafeed
Trading: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
Posts: 3,003 since Sep 2013
Thanks Given: 2,442
Thanks Received: 5,862


Where did you put that code ?

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

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #4 (permalink)
 stealthtrading 
Toronto
 
Experience: None
Platform: NinjaTrader
Trading: Gold
Posts: 10 since Feb 2016
Thanks Given: 0
Thanks Received: 0


rleplae View Post
Where did you put that code ?

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

Thanks for your reply. I originally put the code in OnBarUpdate - thanks for clarification that it would calculate on the close of the interval.

I tried to place the exit in OnMarketUpdate (see code below) but I keep getting an error saying 'No Suitable Method Found to Override'. Here is the code:

protected override void OnMarketUpdate()
{


// Take Loss -0.04
if (Position.GetProfitLoss(Close[0], PerformanceUnit.Percent) < -0.04)
{

ExitShortLimit(GetCurrentAsk(), "S1L", "S1");
ExitLongLimit(GetCurrentBid(), "L1L", "L1");
}

CalculateOnBarClose = false;
}

-------

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.

Started this thread Reply With Quote
  #5 (permalink)
 
rleplae's Avatar
 rleplae 
Gits (Hooglede) Belgium
Legendary Market Wizard
 
Experience: Master
Platform: NinjaTrader, Proprietary,
Broker: Ninjabrokerage/IQfeed + Synthetic datafeed
Trading: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
Posts: 3,003 since Sep 2013
Thanks Given: 2,442
Thanks Received: 5,862


stealthtrading View Post
Thanks for your reply. I originally put the code in OnBarUpdate - thanks for clarification that it would calculate on the close of the interval.

I tried to place the exit in OnMarketUpdate (see code below) but I keep getting an error saying 'No Suitable Method Found to Override'. Here is the code:

protected override void OnMarketUpdate()
{


// Take Loss -0.04
if (Position.GetProfitLoss(Close[0], PerformanceUnit.Percent) < -0.04)
{

ExitShortLimit(GetCurrentAsk(), "S1L", "S1");
ExitLongLimit(GetCurrentBid(), "L1L", "L1");
}

CalculateOnBarClose = false;
}

-------

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.

Why not use an ATM ?

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
on acceleration).


Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #6 (permalink)
 stealthtrading 
Toronto
 
Experience: None
Platform: NinjaTrader
Trading: Gold
Posts: 10 since Feb 2016
Thanks Given: 0
Thanks Received: 0


rleplae View Post
Why not use an ATM ?

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
on acceleration).


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!

Started this thread Reply With Quote
  #7 (permalink)
 
rleplae's Avatar
 rleplae 
Gits (Hooglede) Belgium
Legendary Market Wizard
 
Experience: Master
Platform: NinjaTrader, Proprietary,
Broker: Ninjabrokerage/IQfeed + Synthetic datafeed
Trading: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
Posts: 3,003 since Sep 2013
Thanks Given: 2,442
Thanks Received: 5,862


stealthtrading View Post

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.

Did you study below ?

from NT online doc

• 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)

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #8 (permalink)
 stealthtrading 
Toronto
 
Experience: None
Platform: NinjaTrader
Trading: Gold
Posts: 10 since Feb 2016
Thanks Given: 0
Thanks Received: 0


rleplae View Post
Did you study below ?

from NT online doc

• 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.

Started this thread Reply With Quote
  #9 (permalink)
 
rleplae's Avatar
 rleplae 
Gits (Hooglede) Belgium
Legendary Market Wizard
 
Experience: Master
Platform: NinjaTrader, Proprietary,
Broker: Ninjabrokerage/IQfeed + Synthetic datafeed
Trading: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
Posts: 3,003 since Sep 2013
Thanks Given: 2,442
Thanks Received: 5,862


stealthtrading View Post
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

Does that explain it ?

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #10 (permalink)
 stealthtrading 
Toronto
 
Experience: None
Platform: NinjaTrader
Trading: Gold
Posts: 10 since Feb 2016
Thanks Given: 0
Thanks Received: 0



rleplae View Post
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

Does that explain it ?

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[0], 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.

Started this thread Reply With Quote





Last Updated on April 10, 2016


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts