NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


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-16-2008, 01:27 AM   #1
bboyle1234
Junior Member
 
Join Date: Jun 2007
Posts: 29
Thanks: 1
Thanked 0 times in 0 posts
Default Finding out what the current stops / targets

Hi,

Let's imagine I have used the SetStopLoss() method at some stage to enter a stop loss level.

Later, I want to retrieve the price of that stop loss for some task such as the idea illustrated in the following pseudo code:

if ( market position is long )

if ( current market price is greater than (current stop loss level + 50 ticks) )

then set stop loss 25 ticks higher than it is now.

Performing this would require knowledge of the value of the currently active stop loss order. How can I get it?

cheers / Ben
bboyle1234 is offline  
Reply With Quote
Old 04-16-2008, 01:40 AM   #2
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

http://www.ninjatrader-support.com/v...ead.php?t=5790
NinjaTrader_Dierk is offline  
Reply With Quote
Old 04-16-2008, 03:47 AM   #3
bboyle1234
Junior Member
 
Join Date: Jun 2007
Posts: 29
Thanks: 1
Thanked 0 times in 0 posts
Default

Thanks Dierk,
That's perfect for storing the the Order Tokens.
How can I retrieve the price of the order at any time? eg, I would expect to be able to code something like:

string token = profitTargetOrders[0].ToString();
double currentOrderPrice = this.Orders[token].LimitPrice;

Or were you expecting me to take some initiative and store the extra information from within the OrderUpdate() function?

cheers / Ben
bboyle1234 is offline  
Reply With Quote
Old 04-16-2008, 03:55 AM   #4
bboyle1234
Junior Member
 
Join Date: Jun 2007
Posts: 29
Thanks: 1
Thanked 0 times in 0 posts
Default

Anyway, I came up with something like this in order to store the actual order prices as well. Is the idea ok?

Code:
    public abstract class MyStrategyBase : Strategy
    {
        public event OrderUpdateEventHandler OrderUpdate;
        
        protected override void OnOrderUpdate(IOrder order)
        {                    
            if (OrderUpdate != null)
                OrderUpdate(this, new OrderUpdateEventArgs(order));
        }
        
        public delegate void OrderUpdateEventHandler(object sender, OrderUpdateEventArgs e);
        public class OrderUpdateEventArgs : EventArgs
        {
            IOrder order;
            public OrderUpdateEventArgs(IOrder order)
            {
                this.order = order;
            }
            public IOrder Order
            {
                get { return order; }
            }
        }
    }
    
    public class StopAndTargetWatcher
    {
        public Dictionary<string, IOrder> stopLossOrders = new Dictionary<string, IOrder>();
        public Dictionary<string, IOrder> profitTargetOrders = new Dictionary<string, IOrder>();
        
        public StopAndTargetWatcher(MyStrategyBase s)
        {
            s.OrderUpdate += new MyStrategyBase.OrderUpdateEventHandler(OnOrderUpdate);
        }
        protected virtual void OnOrderUpdate(object sender, MyStrategyBase.OrderUpdateEventArgs e)
        {
            if (e.Order.OrderState == OrderState.PendingSubmit)
                if (e.Order.Name == "Stop Loss")
                    stopLossOrders.Add(e.Order.Token, e.Order);
                else if (e.Order.Name == "Profit Target")
                    profitTargetOrders.Add(e.Order.Token, e.Order);
        
            if ((e.Order.OrderState == OrderState.Cancelled)
                || (e.Order.OrderState == OrderState.Filled)
                || (e.Order.OrderState == OrderState.Rejected))
            {
                if (stopLossOrders.ContainsKey(e.Order.Token))
                    stopLossOrders.Remove(e.Order.Token);
                if (profitTargetOrders.ContainsKey(e.Order.Token))
                    profitTargetOrders.Remove(e.Order.Token);
            }
        }
    }
bboyle1234 is offline  
Reply With Quote
Old 04-16-2008, 04:13 AM   #5
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

>> Or were you expecting me to take some initiative and store the extra information from within the OrderUpdate() function?
correct
NinjaTrader_Dierk is offline  
Reply With Quote
Old 04-16-2008, 07:17 AM   #6
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

I would advise that your class replace any IOrder object held in the dictionary with each call for to OnOrderUpdate(). IOrder objects passed in are static and based on how you have coded it, any order price changes can occur but the reference you hold and check against will not necessarily represent the latest data on this order.
NinjaTrader_Ray is offline  
Reply With Quote
Old 04-16-2008, 08:43 AM   #7
bboyle1234
Junior Member
 
Join Date: Jun 2007
Posts: 29
Thanks: 1
Thanked 0 times in 0 posts
Default

Oh yes! So glad you pointed it out.
bboyle1234 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
Simulated stops and profit targets. dtf139 ATM Strategies (Discretionary Trading) 3 03-11-2008 06:57 AM
Strategy Analyzer- stops and targets dtf139 Strategy Analyzer 3 12-14-2007 01:28 PM
Printing Current StopLoss & Profit Targets Jim-Boulder Strategy Analyzer 1 06-25-2007 11:16 AM
Adjusting targets & stops Zardoz Automated Trading 5 04-24-2006 12:06 PM
Stops/Targets calculated by % copodon Suggestions And Feedback 2 05-13-2005 10:50 AM


All times are GMT -6. The time now is 02:13 PM.