![]() |
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
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Junior Member
Join Date: Jun 2007
Posts: 29
Thanks: 1
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#2 |
|
Administrator
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
|
Dierk
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Jun 2007
Posts: 29
Thanks: 1
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#4 |
|
Junior Member
Join Date: Jun 2007
Posts: 29
Thanks: 1
Thanked 0 times in 0 posts
|
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);
}
}
}
|
|
|
|
|
|
#5 |
|
Administrator
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
|
>> Or were you expecting me to take some initiative and store the extra information from within the OrderUpdate() function?
correct
Dierk
NinjaTrader Customer Service |
|
|
|
|
|
#6 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
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.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#7 |
|
Junior Member
Join Date: Jun 2007
Posts: 29
Thanks: 1
Thanked 0 times in 0 posts
|
Oh yes! So glad you pointed it out.
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |