Go Back   NinjaTrader Support Forum > NinjaTrader 8 > Add-On Development

Add-On Development General NinjaScript development questions.

NinjaTrader
Reply
 
Thread Tools Display Modes
Old 06-01-2018, 11:40 AM   #1
ajsdnyyrr1234DDf
Member
 
Join Date: Jan 2018
Posts: 53
Thanks: 2
Thanked 1 time in 1 post
Default ExitStopMarket() Not working as expected

Hello:

I am using ExitLongStopMarket() to move the StopLoss in a trade. I am facing an issue when I move the StopLoss. The original StopLoss order stays put and doesn't cancel. I am modifying the order object. This happens only If I modify the Stop Signal name. If I keep the same Signal name in the order it works fine. I need to use a different signal name to make tracking my orders better. Is there any way to fix the issue.

When I change the signal name, Is it not modifying the same order object? It appears If I change the Quantity/Price and keep the Signal name and FromEntrySignal the order object is modified but If I change the StopLoss signal name then the original order object stays the same and a new StopLoss order is created.

See below the log:



===========
Now the interesting thing is If I do not change the name Name but keep it the same and update the StopLoss price.
Then it is working as expected. The StopLoss is moved correctly
===========



===========
Now the interesting thing is If I do not change the name Name but keep it the same and update the StopLoss price.
Then it is working as expected. The StopLoss is moved correctly
===========


Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Working' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.7013 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Accepted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.7013 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Change submitted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.7013 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Working' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.7021 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Accepted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.7021 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Change submitted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.7021 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Working' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70228 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Accepted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70228 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Change submitted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70228 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Working' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70259 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Accepted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70259 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636364144/ACCOUNT' Name='StopShort STB1' New state='Submitted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70259 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

=====
I modified the StopLoss to give the trade little bit of breathing room
Here I changed the StopLoss name to "StopShort STB1"

Now the effect is , The Original StopLoss as well as the New StopLoss are there in Working State" I am expecting the Original Stop Loss to be replaced with the New StopLoss Order

==========

NinjaScript strategy 'Ninja8STRX/131611074' submitting order
Order='636340689/ACCOUNT' Name='TargetShort' New state='Working' Instrument='NZDUSD' Action='Buy to cover' Limit price=0.67365 Stop price=0 Quantity=14,000 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636340689/ACCOUNT' Name='TargetShort' New state='Accepted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0.67365 Stop price=0 Quantity=14,000 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636340689/ACCOUNT' Name='TargetShort' New state='Submitted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0.67365 Stop price=0 Quantity=14,000 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636340685/ACCOUNT' Name='StopShort Init' New state='Working' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70265 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636340685/ACCOUNT' Name='StopShort Init' New state='Accepted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70265 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636340685/ACCOUNT' Name='StopShort Init' New state='Submitted' Instrument='NZDUSD' Action='Buy to cover' Limit price=0 Stop price=0.70265 Quantity=14,000 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

=====
My Initial StopLoss Order is called = "StopShort Init"
====

Instrument='NZDUSD' Account='ACCOUNT' Average price=0.69865 Quantity=14,000 Market position=Short Operation=Operation_Add
NinjaScript strategy 'Ninja8STRX/131611074' submitting order
Execution='636340684' Instrument='NZDUSD' Account='ACCOUNT' Exchange=Default Price=0.69865 Quantity=14,000 Market position=Short Operation=Operation_Add Order='636340684' Time='6/1/2018 01:30'
NinjaScript strategy 'Ninja8STRX/131611074' submitting order
Order='636340684/ACCOUNT' Name='ShortSTRXBig' New state='Filled' Instrument='NZDUSD' Action='Sell short' Limit price=0 Stop price=0 Quantity=14,000 Type='Market' Time in force=GTC Oco='' Filled=14000 Fill price=0.69865 Error='No error' Native error=''
Order='636340684/ACCOUNT' Name='ShortSTRXBig' New state='Working' Instrument='NZDUSD' Action='Sell short' Limit price=0 Stop price=0 Quantity=14,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636340684/ACCOUNT' Name='ShortSTRXBig' New state='Submitted' Instrument='NZDUSD' Action='Sell short' Limit price=0 Stop price=0 Quantity=14,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Order='636340684/ACCOUNT' Name='ShortSTRXBig' New state='Accepted' Instrument='NZDUSD' Action='Sell short' Limit price=0 Stop price=0 Quantity=14,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
NinjaScript strategy 'Ninja8STRX/131611074' submitting order
Enabling NinjaScript strategy 'Ninja8STRX/131611074' : On starting a real-time strategy - StartBehavior=AdoptAccountPosition AccountPosition=NZDUSD 14000L, NZDUSD 14000L EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=False SetOrderQuantityBy=Strategy ConnectionLossHandling=Keep running DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes


=============

Character limit doesn't allow me to input the code snippet into this message.
I will add it as the replay to this thread.
ajsdnyyrr1234DDf is offline  
Reply With Quote
Old 06-01-2018, 11:42 AM   #2
ajsdnyyrr1234DDf
Member
 
Join Date: Jan 2018
Posts: 53
Thanks: 2
Thanked 1 time in 1 post
Default

Code Snippet
============


Here are the relevant code details:

Entry Order:

EnterLong(Convert.ToInt32(QuantityBig), @"LongBig");

StopLoss Order in OnExecutionUpdate:

protected override void OnExecutionUpdate(......))

StopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - StopLossInitBig, "StopLong Init", @"LongBig");

TargetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + TakeProfitBig, "TargetLong", @"LongBig");

Trail Stop and Update Quantity and some time StopLoss Name change in OnBarUpdate():

protected override void OnBarUpdate()

if ((Stop Loss Condition))
{
StopOrder = ExitLongStopMarket(0, true, StopOrderQuantity, (Close[0] - STB1), "StopLong STB1", @"LongBig");
StopLongPrice = (Close[0] - STB1);
}
else if ((Stop Loss Condition & Profit Condition))
{
ExitLong(0, BigTL2, "TakeProfit STB2", @"LongBig");
TargetOrder = ExitLongLimit(0, true, StopOrderQuantity - BigTL2, TargetLongPrice, "TargetLong", @"LongBig");
StopOrder = ExitLongStopMarket(0, true, StopOrderQuantity - BigTL2, (Close[0] - STB2), "StopLong STB2", @"LongBig");
StopLongPrice = (Close[0] - STB2);
StopOrderQuantity = (StopOrderQuantity - BigTL2);
}
else
{
StopOrder = ExitLongStopMarket(0, true, StopOrderQuantity, (Close[0] - STB2), "StopLong STB2", @"LongBig");
StopLongPrice = (Close[0] - STB2);
}

As you can see from the above, I change the name inside the StopLoss Order object when the trade passes thru certain targets levels which I want to track. The problem is when I change the StopLoss name in the same order object, it does not seem to take it instead, it is keeping the old and creating a new order and what I end is with two duplicate working orders.

Can you suggest a method to avoid the issue and keep the same StopLoss order and to avoid the duplications?
ajsdnyyrr1234DDf is offline  
Reply With Quote
Old 06-01-2018, 12:21 PM   #3
NinjaTrader_PatrickH
NinjaTrader Customer Service
 
NinjaTrader_PatrickH's Avatar
 
Join Date: Jul 2011
Location: Denver, CO
Posts: 11,662
Thanks: 783
Thanked 2,014 times in 1,940 posts
Default

Hello ajsdnyyrr1234DDf,

Thank you for your post.

You would either need to avoid using two signal names for essentially the same order or use two Order objects and cancel the one you are switching from.

Please let me know if you have any questions.
NinjaTrader_PatrickH is online now  
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
SetZOrder not working as expected overflowing Version 8 Beta 4 02-22-2016 11:41 AM
AND operator not working as expected kdw2493 Strategy Development 2 11-18-2015 04:33 PM
reset not working as expected onnb1 General Development 1 03-28-2012 02:20 AM
AtmStrategyTemplate not working as expected Taddypole Platform Technical Support 1 02-07-2012 10:56 PM
SetStopLoss() not working as expected suedeuno Strategy Development 3 01-03-2009 10:16 AM


All times are GMT -6. The time now is 01:32 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.