NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 04-26-2011, 01:59 PM   #1
wingman
Junior Member
 
Join Date: Apr 2011
Location: Paris
Posts: 12
Thanks: 0
Thanked 0 times in 0 posts
Default Live execution and weird stop/target prices

Hi,

I have a problem in my strategy live execution. It uses a simple EnterLong/Short order in OnBarUpdate and puts a stop loss and profit target in OnExecution when entry is executed.

When position is flat I reset the stop and target. After entry and stop/target of one or two traders, when a new entry is executed the stop and target are rejected with prices I have not set.

Please advise.
W.

Some code :

OnBarUpdate
Code:
if ( Position.MarketPosition == MarketPosition.Flat ) 
{
    // reset stoploss and profittarget.
    SetStopLoss(CalculationMode.Ticks, 2*BarsPeriod.Value + 2);
    SetProfitTarget(CalculationMode.Ticks, 2*BarsPeriod.Value + 2);
    
    if ( entry condition.... )
    {
        entryLongOrder = EnterLong(OrderQuantity, "LONG");
    }
    else if ( ... )
    {
        entryShortOrder = EnterShort(OrderQuantity, "SHORT");
    }
}
OnExecution
Code:
if ( execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || 
     (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0) )
{
    double stopPrice = 0;
    double targetPrice = 0;
    
    if ( Position.MarketPosition == MarketPosition.Long )                    
    {
        targetPrice = execution.Order.AvgFillPrice + (2 * BarsPeriod.Value - 2) * TickSize;
        stopPrice = execution.Order.AvgFillPrice - (2 * BarsPeriod.Value + 2) * TickSize;                    

        SetStopLoss("LONG", CalculationMode.Price, stopPrice, false);
        SetProfitTarget("LONG", CalculationMode.Price, targetPrice);
        ECILogger("Stop at " + stopPrice + " and Target at " + targetPrice + " submitted");                    
    }
wingman is offline  
Reply With Quote
Old 04-26-2011, 02:29 PM   #2
NinjaTrader_Dexter
NinjaTrader Customer Service
 
NinjaTrader_Dexter's Avatar
 
Join Date: Mar 2011
Location: Denver, CO
Posts: 103
Thanks: 0
Thanked 0 times in 0 posts
Default

Hello wingman,

Thank you for your post.

In your OnExecution code, you set stopPrice and targetPrice to 0, you can use Print() to make sure your values are as expected when you use SetStopLoss() and SetProfitTarget() on these values to make sure they are still not 0 or an invalid price.

If you don't have any luck with debugging your values with Print(), the following sample is a little more involved but it covers how to monitor stop loss and profit targets. Please see the following forum post: http://www.ninjatrader.com/support/f...ead.php?t=5790

Please give the above a try and let me know if you still experience issues.
NinjaTrader_Dexter is offline  
Reply With Quote
Old 04-26-2011, 02:53 PM   #3
wingman
Junior Member
 
Join Date: Apr 2011
Location: Paris
Posts: 12
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank you Dexter,

The logs indicate the price is not 0. Here is a small log below.
Strategy is launched and crashed after rejecting a stop order at 112,14.
Logs indicate price submission at 111,82 (Stop) 111,98 (target).

I need to understand what works wrong before going to more complicated solutions using arrays. I use only one contract.

Please advise again.

W.

Quote:
26/04/2011 16:41:53 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Ticks Value=10 Currency=0 Simulated=False
26/04/2011 16:41:53 Entered internal SetStopTarget() method: Type=Target FromEntrySignal='' Mode=Ticks Value=10 Currency=0 Simulated=False
26/04/2011 16:41:55 CL 06-11 Green bar detected, in Flat strategy. Submit Long entry
26/04/2011 16:41:53 Entered internal PlaceOrder() method at 26/04/2011 16:41:53: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='LONG' FromEntrySignal=''
26/04/2011 16:41:55 CL 06-11 Long entry submitted
26/04/2011 16:41:55 CL 06-11 LONG executed at 111,92
26/04/2011 16:41:53 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='LONG' Mode=Price Value=111,82 Currency=0 Simulated=False
26/04/2011 16:41:53 Entered internal SetStopTarget() method: Type=Target FromEntrySignal='LONG' Mode=Price Value=111,98 Currency=0 Simulated=False
26/04/2011 16:41:53 Amended target order: Order='cc6ad7abf93c48c2ae882b25504d5b84/Sim101' Name='Profit target' State=PendingSubmit Instrument='CL 06-11' Action=Sell Limit price=111,98 Stop price=0 Quantity=1 Strategy='ECIRenkoTest2' Type=Limit Tif=Gtc Oco='68db35aa0e444efea592f4a7f3ff24c6-1281' Filled=0 Fill price=0 Token='cc6ad7abf93c48c2ae882b25504d5b84' Gtd='01/12/2099 00:00:00'
26/04/2011 16:41:55 CL 06-11 Stop at 111,82 and Target at 111,98 submitted
**NT** Strategy 'ECIRenkoTest2/a336e14538274fa995dafbbabaafa909' submitted an order that generated the following error 'OrderRejected'. Strategy has sent cancel requests, attempted to close the position and terminated itself.
26/04/2011 16:41:53 CancelAllOrders: BarsInProgress=0
26/04/2011 16:41:55 CL 06-11 Profit target cancelled
26/04/2011 16:41:54 Entered internal PlaceOrder() method at 26/04/2011 16:41:54: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Close' FromEntrySignal=''
26/04/2011 16:41:55 CL 06-11 Close executed at 111,9
26/04/2011 16:41:54 Disable() called: strategy disabled
26/04/2011 16:41:54 CancelAllOrders: BarsInProgress=0
**NT** Disabling NinjaScript strategy 'ECIRenkoTest2/a336e14538274fa995dafbbabaafa909'
wingman is offline  
Reply With Quote
Old 04-26-2011, 04:08 PM   #4
NinjaTrader_Dexter
NinjaTrader Customer Service
 
NinjaTrader_Dexter's Avatar
 
Join Date: Mar 2011
Location: Denver, CO
Posts: 103
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi wingman,

Thank you for clarifying,

OrderRejected can occur for a handful of reasons. This is most commonly caused by submitting buy stop orders below the current price, stop orders above the current price (backwards stoplosses), not having enough available account balance, etc.

So I am able to assist you further, please email your log and trace files to support at ninjatrader.com . This can be done from within NinjaTrader's control center, Help->Mail to Support, check "log and trace files" and put "ATTN: Dexter" in the subject.

You can also manually email support at ninjatrader.com with your mail client and attach the files manually:

You will find the log file in the Documents > NinjaTrader 7 > Log folder.
The log file will be named ‘log.YYYYMMDD.txt’

You will find the trace file in the Documents > NinjaTrader 7 > Trace folder.
The trace file will be named ‘trace.YYYYMMDD.txt’

Reference this post and I will follow up with you once I have this additional information.
NinjaTrader_Dexter is offline  
Reply With Quote
Old 04-27-2011, 04:49 AM   #5
wingman
Junior Member
 
Join Date: Apr 2011
Location: Paris
Posts: 12
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi Dexter,

I am putting my answer here so everybody can share/comment.

Again thank you for you help.

I hardcoded the bars value but... no change.

If of any use : my yesterday message sent on 10:53 shows the prices were set ok :

OnExecution confirms a LONG execution at 16:41 at 111,92 .
>26/04/2011 16:41:55 CL 06-11 LONG executed at 111,92
Immediately in OnExecution we set Stop and Profit at 8 and -10 ticks : 111,82 and 111,98 : the print and calculation is ok
>26/04/2011 16:41:53 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='LONG' Mode=Price Value=111,82 Currency=0 Simulated=False
>26/04/2011 16:41:53 Entered internal SetStopTarget() method: Type=Target FromEntrySignal='LONG' Mode=Price Value=111,98 Currency=0 Simulated=False
>26/04/2011 16:41:53 Amended target order: Order='cc6ad7abf93c48c2ae882b25504d5b84/Sim101' Name='Profit target' State=PendingSubmit Instrument='CL 06-11' Action=Sell Limit price=111,98 Stop price=0 Quantity=1 Strategy='ECIRenkoTest2' Type=Limit Tif=Gtc Oco='68db35aa0e444efea592f4a7f3ff24c6-1281' Filled=0 Fill price=0 Token='cc6ad7abf93c48c2ae882b25504d5b84' Gtd='01/12/2099 00:00:00'
>26/04/2011 16:41:55 CL 06-11 Stop at 111,82 and Target at 111,98 submitted
Order is then rejected with *112,14* : very far from the 111,82 and 111,98 stop/profit set earlier.

Where did this 112,14 came from ?

Note : I use only one simulation account. I reset it completely before use, then flatten everything, then check there is no position open.

W.
wingman is offline  
Reply With Quote
Old 04-27-2011, 08:25 AM   #6
NinjaTrader_Dexter
NinjaTrader Customer Service
 
NinjaTrader_Dexter's Avatar
 
Join Date: Mar 2011
Location: Denver, CO
Posts: 103
Thanks: 0
Thanked 0 times in 0 posts
Default

Hello wingman,

I just followed up with you in the email you sent in, look for my response there. Thank you
NinjaTrader_Dexter is offline  
Reply With Quote
Old 04-28-2011, 08:44 AM   #7
wingman
Junior Member
 
Join Date: Apr 2011
Location: Paris
Posts: 12
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank you Dexter. I will look at that this WE.

Meanwhile, I have reprogrammed exit orders (nothing else) to use Exit instead of the Set stop and profit orders. And it works fine.

W.
wingman is offline  
Reply With Quote
Old 04-28-2011, 08:52 AM   #8
NinjaTrader_Dexter
NinjaTrader Customer Service
 
NinjaTrader_Dexter's Avatar
 
Join Date: Mar 2011
Location: Denver, CO
Posts: 103
Thanks: 0
Thanked 0 times in 0 posts
Default

Hello wingman,

Sounds good, just let us know if we can assist any further.
NinjaTrader_Dexter is offline  
Reply With Quote
Old 04-29-2011, 11:13 AM   #9
KBJ
Senior Member
 
Join Date: Mar 2007
Location: , Florida, USA
Posts: 663
Thanks: 36
Thanked 7 times in 6 posts
Default

Quote:
Originally Posted by NinjaTrader_Dexter View Post
OrderRejected can occur for a handful of reasons. This is most commonly caused by submitting buy stop orders below the current price, stop orders above the current price (backwards stoplosses), not having enough available account balance, etc.
Dexter,

Is there any way to get the detailed reason why a particular order has been rejected? Is there a Status field or Secondary Status field that's maintained anywhere that would contain this data, or is this information not reported by the exchange or just thrown away?

If not currently available, could this be added in future releases of NinjaTrader?

Thanks.
KBJ is offline  
Reply With Quote
Old 04-29-2011, 11:18 AM   #10
NinjaTrader_Dexter
NinjaTrader Customer Service
 
NinjaTrader_Dexter's Avatar
 
Join Date: Mar 2011
Location: Denver, CO
Posts: 103
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi KBJ,

If order rejection information is available, it will be entered into the order's trace file entry.

For example, it is populated in Native error here:

2011-04-26 02:25:45:914 (ZenFire) Cbi.Connection.ProcessEventArgs.OrderStatusEventAr gs: Order='6539d5c2fc10426fae9194019f34a9e1/CLRenkoSimulation' Name='Stop loss' New state=Rejected Instrument='CL 06-11' Action=Sell Limit price=0 Stop price=112,08 Quantity=1 Type=Stop Filled=0 Fill price=0 Error=OrderRejected Native error='Sell stop or sell stop limit orders can't be placed above the market.'
NinjaTrader_Dexter 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
Target and Stop Execution in Strategy AutoTraderDev Strategy Development 5 04-22-2011 06:10 AM
Plotting Profit Target & Stop Loss Prices SystemTrading Strategy Development 1 02-19-2009 11:46 AM
Inputting stop loss, profit target and limit prices. Scooter1 Miscellaneous Support 11 10-09-2008 10:37 AM
Weird execution error ct Strategy Development 1 07-16-2008 11:07 AM
ATM stop/target prices monpere ATM Strategies (Discretionary Trading) 3 06-18-2008 01:54 PM


All times are GMT -6. The time now is 02:50 AM.