Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
I modified the SampleOnOrderUpdate strategy to allow multiple entries. Take a look at the attached example, but bear in mind it was written and exported in NT7. To make it work in NT6.5, the main change you will have to make is to compare Tokens instead of Orders directly (as it is advised for NT7)
So for example instead of (NT7)
entryOrder_1a == order
you need to compare (NT6.5):
entryOrder_1a.Token == order.Token
(just refer to original sample)
BTW: here is where the problem is in your code:
order1.Token
I didn't know that two orders would both use the order.Token and i also didn't know it would be similar for execution.Order.Token.
BTW, what would be a sample of using IPosition now in this code?
I asked the question on another thread if programmers are going fully into v7 strategies or sticking with v6.5 for the moment. What are your thoughts (I haven't checked the other thread yet if you already answered there - will check.
I am submitting the following condition on the strategy attached above but it is not responding at all. It is entering
the market immediately and it is ignoring the time and if the current open is above yesterday highs.
Can someone help me how to make this conditions active. Bellow is the whole code.
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Submit an entry limit order if we currently don't have an entry order open
[B] if (entryOrder_1a == null && entryOrder_1b == null
&& CurrentDayOHL().CurrentOpen[0] >= PriorDayOHLC().PriorHigh[0]
&& ToTime(Time[0]) > ToTime(8, 30, 0)
&& ToTime(Time[0]) < ToTime(8, 46, 0))
{
/* The entryOrder object will take on a unique ID from our EnterLong()
that we can use later for order identification purposes in the OnOrderUpdate() method. */
entryOrder_1a = EnterLong(1, "Enter Long - Scout");
entryOrder_1b = EnterLong(1, "Enter Long - Runner");
Print("Entered A and B");
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Sample demonstrating the use of the OnOrderUpdate() method.
/// </summary>
[Description("Sample strategy demonstrating a use case involving the OnOrderUpdate() method")]
public class SampleOnOrderUpdate : Strategy
{
#region Variables
private IOrder entryOrder_1a = null; // This variable holds an object representing our entry order
private IOrder entryOrder_1b = null; // This variable holds an object representing our entry order
private IOrder stopOrder_1a = null; // This variable holds an object representing our stop loss order
private IOrder stopOrder_1b = null; // This variable holds an object representing our stop loss order
private IOrder targetOrder_1a = null; // This variable holds an object representing our profit target order
private IOrder targetOrder_1b = null; // This variable holds an object representing our profit target order
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.UniqueEntries;
ClearOutputWindow();
TraceOrders = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Submit an entry limit order if we currently don't have an entry order open
if (entryOrder_1a == null && entryOrder_1b == null && CurrentDayOHL().CurrentOpen[0] >= PriorDayOHLC().PriorHigh[0] && ToTime(Time[0]) > ToTime(8, 30, 0) && ToTime(Time[0]) < ToTime(8, 46, 0))
{
/* The entryOrder object will take on a unique ID from our EnterLong()
that we can use later for order identification purposes in the OnOrderUpdate() method. */
entryOrder_1a = EnterLong(1, "Enter Long - Scout");
entryOrder_1b = EnterLong(1, "Enter Long - Runner");
Print("Entered A and B");
}
/* If we have a long position and the current price is 4 ticks in profit, raise the stop-loss order to breakeven.
We use (7 * (TickSize / 2)) to denote 4 ticks because of potential precision issues with doubles. Under certain
conditions (4 * TickSize) could end up being 3.9999 instead of 4 if the TickSize was 1. Using our method of determining
4 ticks helps cope with the precision issue if it does arise. */
if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AvgPrice + (80 * (TickSize / 2)))
{
// Print("Stop order 1a: "+(stopOrder_1a.StopPrice == null? "null" : stopOrder_1a.StopPrice.ToString()) );
// Checks to see if our Stop Order has been submitted already
if (stopOrder_1a != null && stopOrder_1a.StopPrice < Position.AvgPrice)
{
// Modifies stop-loss to breakeven
stopOrder_1a = ExitLongStop(0, true, stopOrder_1a.Quantity, Position.AvgPrice, "Stop Scout", "Enter Long - Scout");
}
if (stopOrder_1b != null && stopOrder_1b.StopPrice < Position.AvgPrice)
{
// Modifies stop-loss to breakeven
stopOrder_1b = ExitLongStop(0, true, stopOrder_1b.Quantity, Position.AvgPrice, "Stop Runner", "Enter Long - Runner");
}
}
}
/// <summary>
/// Called on each incoming order event
/// </summary>
protected override void OnOrderUpdate(IOrder order)
{
// Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
if (entryOrder_1a != null && entryOrder_1a == order)
{
// Reset the entryOrder object to null if order was cancelled without any fill
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
entryOrder_1a = null;
}
}
if (entryOrder_1b != null && entryOrder_1b == order)
{
// Reset the entryOrder object to null if order was cancelled without any fill
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
entryOrder_1b = null;
}
}
}
/// <summary>
/// Called on each incoming execution
/// </summary>
protected override void OnExecution(IExecution execution)
{
/* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
which ensures your strategy has received the execution which is used for internal signal tracking. */
if (entryOrder_1a != null && entryOrder_1a == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
// Stop-Loss order 4 ticks below our entry price
stopOrder_1a = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 48 * TickSize, "Stop Scout", "Enter Long - Scout");
// Resets the entryOrder object to null after the order has been filled or partially filled
if (execution.Order.OrderState != OrderState.PartFilled)
{
entryOrder_1a = null;
}
}
}
if (entryOrder_1b != null && entryOrder_1b == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
// Stop-Loss order 4 ticks below our entry price
stopOrder_1b = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 48 * TickSize, "Stop Runner", "Enter Long - Runner");
// Target order 8 ticks above our entry price
// for this strategy targetOrder_1b will be disabled
targetOrder_1b = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 168 * TickSize, "Target Runner", "Enter Long - Runner");
// Resets the entryOrder object to null after the order has been filled or partially filled
if (execution.Order.OrderState != OrderState.PartFilled)
{
entryOrder_1b = null;
}
}
}
// Reset our stop order and target orders' IOrder objects after our position is closed.
if ((stopOrder_1a != null && stopOrder_1a == execution.Order) || (targetOrder_1a != null && targetOrder_1a == execution.Order))
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
{
stopOrder_1a = null;
targetOrder_1a = null;
}
}
if ((stopOrder_1b != null && stopOrder_1b == execution.Order) || (targetOrder_1b != null && targetOrder_1b == execution.Order))
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
{
stopOrder_1b = null;
targetOrder_1b = null;
}
}
}
/// <summary>
/// Called on each incoming position event
/// </summary>
protected override void OnPositionUpdate(IPosition position)
{
// Print our current position to the lower right hand corner of the chart
DrawTextFixed("MyTag", position.ToString(), TextPosition.BottomRight);
}