Setting a Stop Loss to Activate before Interval Close - NinjaTrader Programming | futures io social day trading
futures io futures trading


Setting a Stop Loss to Activate before Interval Close
Updated: Views / Replies:655 / 11
Created: by stealthtrading Attachments:0

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you dont need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 
Thread Tools Search this Thread
 

Setting a Stop Loss to Activate before Interval Close

  #1 (permalink)
Elite Member
Toronto
 
Futures Experience: None
Platform: NinjaTrader
Favorite Futures: Gold
 
Posts: 10 since Feb 2016
Thanks: 0 given, 0 received

Setting a Stop Loss to Activate before Interval Close

Im fairly new to developing using NinjaTrader and would really appreciate some help with this.
Im running 10 minute interval bars, but in cases of rare volatility the unrealized P/L will exceed my stop loss Ive 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 doesnt 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 theres 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) Id 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");
}

Reply With Quote
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Elite Member
Omer עומר / Israel י
 
Futures Experience: Master
Platform: NinjaTrader, Proprietary,
Broker/Data: Ninjabrokerage/IQfeed + Synthetic datafeed
Favorite Futures: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
 
rleplae's Avatar
 
Posts: 2,501 since Sep 2013
Thanks: 1,706 given, 3,706 received
Forum Reputation: Legendary


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

Reply With Quote
 
  #4 (permalink)
Elite Member
Toronto
 
Futures Experience: None
Platform: NinjaTrader
Favorite Futures: Gold
 
Posts: 10 since Feb 2016
Thanks: 0 given, 0 received


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.

Reply With Quote
 
  #5 (permalink)
Elite Member
Omer עומר / Israel י
 
Futures Experience: Master
Platform: NinjaTrader, Proprietary,
Broker/Data: Ninjabrokerage/IQfeed + Synthetic datafeed
Favorite Futures: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
 
rleplae's Avatar
 
Posts: 2,501 since Sep 2013
Thanks: 1,706 given, 3,706 received
Forum Reputation: Legendary


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

https://futures.io/elite-automated-trading/32559-ron-s-alpha-trade-method-ninjatrader-advanced-trade-management-bot.html

Reply With Quote
 
  #6 (permalink)
Elite Member
Toronto
 
Futures Experience: None
Platform: NinjaTrader
Favorite Futures: Gold
 
Posts: 10 since Feb 2016
Thanks: 0 given, 0 received


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

https://futures.io/elite-automated-trading/32559-ron-s-alpha-trade-method-ninjatrader-advanced-trade-management-bot.html

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!

Reply With Quote
 
  #7 (permalink)
Elite Member
Omer עומר / Israel י
 
Futures Experience: Master
Platform: NinjaTrader, Proprietary,
Broker/Data: Ninjabrokerage/IQfeed + Synthetic datafeed
Favorite Futures: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
 
rleplae's Avatar
 
Posts: 2,501 since Sep 2013
Thanks: 1,706 given, 3,706 received
Forum Reputation: Legendary


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)

Reply With Quote
 
  #8 (permalink)
Elite Member
Toronto
 
Futures Experience: None
Platform: NinjaTrader
Favorite Futures: Gold
 
Posts: 10 since Feb 2016
Thanks: 0 given, 0 received


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.

Reply With Quote
 
  #9 (permalink)
Elite Member
Omer עומר / Israel י
 
Futures Experience: Master
Platform: NinjaTrader, Proprietary,
Broker/Data: Ninjabrokerage/IQfeed + Synthetic datafeed
Favorite Futures: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
 
rleplae's Avatar
 
Posts: 2,501 since Sep 2013
Thanks: 1,706 given, 3,706 received
Forum Reputation: Legendary


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 ?

Reply With Quote
 
  #10 (permalink)
Elite Member
Toronto
 
Futures Experience: None
Platform: NinjaTrader
Favorite Futures: Gold
 
Posts: 10 since Feb 2016
Thanks: 0 given, 0 received



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.

Reply With Quote

Reply



futures io > > > > > Setting a Stop Loss to Activate before Interval Close

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
anasupertrend activate automatically stop curleta NinjaTrader Programming 4 December 16th, 2014 08:20 PM
Ninjascript -- Setting stop loss based on previous bars bstzgr The Elite Circle 6 August 25th, 2014 08:40 AM
Stop loss triggered at CLOSE of bar launch NinjaTrader Programming 2 August 1st, 2014 04:00 PM
Setting a Stop loss for all Position arjfca Elite EasyLanguage Automated Trading 3 November 28th, 2012 09:16 AM
Reading Interval setting in Strategy leduc NinjaTrader Programming 4 May 26th, 2010 07:48 PM


All times are GMT -4. The time now is 12:47 AM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts
Page generated 2017-12-14 in 0.20 seconds with 19 queries on phoenix via your IP 54.221.93.187