Go Back   NinjaTrader Support Forum > NinjaTrader 7 > Platform Technical Support

Platform Technical Support General platform technical support for NinjaTrader 7.

Reply
 
Thread Tools Display Modes
Old 02-08-2009, 03:28 PM   #1
unSane
Junior Member
 
Join Date: Sep 2008
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default Using EnterLongStop() with existing short position

Forgive me if I'm being dense, but how do you do this elegantly?

Let's say I have a short position with a stop loss and profit target.

I get a buy signal on the current bar and want to go long if price rises above a certain trigger price.

However if I use EnterLongStop() the order is ignored because of the internal rules (since I have existing buy orders -- the stop loss and profit target -- in the same direction).

It works if I buy at market but I don't want to buy at market.

Clearly, I can submit an order to close the short position, and then submit my EnterLongStop() order on the next bar, but this means there's a delay in submitting it.

What is the cleanest way of exiting a short position with a stoploss and entering a buy stop order on the same bar?
unSane is offline  
Reply With Quote
Old 02-09-2009, 02:18 AM   #2
NinjaTrader_Ben
NinjaTrader Customer Service
 
NinjaTrader_Ben's Avatar
 
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 6 times in 6 posts
Default

Hello,

Try submitting an opposing order, like you are doing already, then enter a new order immediately.

If you post your code I can help you more.
NinjaTrader_Ben is offline  
Reply With Quote
Old 02-09-2009, 06:57 AM   #3
unSane
Junior Member
 
Join Date: Sep 2008
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default

OK, here's a very simplified version of the code which exhibits the behavior.

Let's say the strategy is short, with a stop loss and profit target and I am working on 15m bars.

On bar N I get a long signal.

The strategy closes the short position via ExitShort().

However, EnterLongStop() is ignored because of the existing pending stop/profit target orders to close the short position and which will not be cancelled until the next bar. Of course EnterLong() works fine because it is a market order.

In order to EnterLongStop() I have to set a flag and wait until the next bar to enter, which delays entry by 15 mins to bar N+1.

I coded a workaround using ExitShortStop() and ExitShortLimit() orders to manually run the profit target and stop loss routines, but as you guys are no doubt aware it gets complicated as you have to make sure they cancel each other and so on. I would much rather use the built-in code for this.

What is the cleanest way to submit the EnterLongStop() order and close the existing short position on the same bar?

Code:
        protected override void Initialize()
        {
            CalculateOnBarClose = true;
			
			Add( AcmeSwing( SwingPercent ));
			
			SetProfitTarget( CalculationMode.Percent, profitTarget );
			SetStopLoss( CalculationMode.Percent, stopLoss );
			
			TraceOrders = true;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
			thisPivotType  = (int) AcmeSwing( SwingPercent ).AcmePivotType[0];
			thisPivotPrice = AcmeSwing( SwingPercent ).AcmePivotPrice[0];
						
			// Entries
			if( thisPivotType == pivotTypeHigherLow || thisPivotType == pivotTypeLowerLow )
			{
				ExitShort();
				EnterLongStop( High[0], "Bullish reversal" );
			}
			
			if( thisPivotType == pivotTypeLowerHigh || thisPivotType == pivotTypeHigherHigh )
			{
				ExitLong();
				EnterShortStop( Low[0], "Bearish reversal" );
			}			
        }
unSane is offline  
Reply With Quote
Old 02-09-2009, 08:15 AM   #4
NinjaTrader_JoshP
NinjaTrader Product Manager
 
NinjaTrader_JoshP's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,459
Thanks: 1
Thanked 293 times in 98 posts
Default

The flexibility you are seeking is done through manually controlling stop/target orders. This is done through the use of IOrders and monitoring ExitShortStop() and ExitShortLimit() not through Set() methods.

In terms of the delay, run with CalculateOnBarClose = false. In backtesting, this will always be the case. You need to wait for the bar close before you can do anything.
NinjaTrader_JoshP is offline  
Reply With Quote
Old 02-10-2009, 12:18 AM   #5
Newshues
Member
 
Join Date: Jan 2009
Posts: 40
Thanks: 0
Thanked 0 times in 0 posts
Default

I think the cleanest way to do what you want is to move the SetProfitTarget() and SetStopLoss() from the Initialize section to the OnBarUpdate section.

This will allow you to tie the same signal name to all the orders - your position and the corresponding stop and profit orders.

SetProfitTarget(string fromEntrySignal, CalculationMode mode, double value)

fromEntrySignal
The entry signal name. This ties the profit target exit to the entry and exits the position quantity represented by the actual entry.



Basically you need to tie the all the orders together so they are identified as a group rather than individual orders. You do that in ninja with the signal names.

As far as I understand things that should do what you want it to do.
Newshues is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if last position was long or short!? whitegun Platform Technical Support 3 08-12-2015 11:46 AM
..."And close any open short/long position" behealed Platform Technical Support 11 02-23-2009 11:52 AM
Long ? short when flat vs. position ju1234 Strategy Development 1 11-21-2008 08:16 AM
Strange Behaviour with EnterLongLimit and Short Position simone Strategy Development 21 12-01-2007 01:48 AM
Problems adding contracts to existing position/strategy Ouiji Platform Technical Support 1 05-10-2007 02:10 PM


All times are GMT -6. The time now is 07:57 PM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content. Visit the CFTC resources for education regarding the industry and signs of fraud.