NexusFi: Find Your Edge


Home Menu

 





SampleOnOrderUpdate strategy


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one zeller4 with 2 posts (0 thanks)
    2. looks_two him1 with 1 posts (0 thanks)
    3. looks_3 gregid with 1 posts (2 thanks)
    4. looks_4 manev31 with 1 posts (0 thanks)
    1. trending_up 2,600 views
    2. thumb_up 2 thanks given
    3. group 3 followers
    1. forum 3 posts
    2. attach_file 2 attachments




 
Search this Thread

SampleOnOrderUpdate strategy

  #1 (permalink)
 zeller4 
Orlando Florida
 
Experience: Intermediate
Platform: NT8
Trading: CL, NQ, ES, RTY
Posts: 477 since Jun 2009
Thanks Given: 1,416
Thanks Received: 404

Hello,

I'm trying to work with this sample code to add a second entry order. I'm stuck at this point of adding another parameter :

 
Code

#region Variables

private IOrder entryOrder1 = null; // This variable holds an object representing our entry order
private IOrder entryOrder2 = null; // This variable holds an object representing our entry order
private IOrder stopOrder1 = null; // This variable holds an object representing our stop loss order
private IOrder stopOrder2 = null; // This variable holds an object representing our stop loss order
private IOrder targetOrder1 = null; // This variable holds an object representing our profit target order
private IOrder targetOrder2 = null; // This variable holds an object representing our profit target order



 
Code

protectedoverridevoid 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 (entryOrder1 != null && 
entryOrder2 != null && 
entryOrder1.Token == order1.Token &&
entryOrder2.Token == order2.Token
)
{ 
// Reset the entryOrder object to null if order was cancelled without any fill
if (order1.OrderState == OrderState.Cancelled && 
order2.OrderState == OrderState.Cancelled && 
order1.Filled == 0 &&
order2.Filled == 0
)
{
entryOrder1 = null;
entryOrder2 = null;
 
}
}
}//end of OnOrderUpdate

so, the only thing (if i understand correctly) needing modifying is:
 
Code

protectedoverridevoid OnOrderUpdate(IOrder order)
{
thanks for your help.
Kirk

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Exit Strategy
NinjaTrader
Better Renko Gaps
The Elite Circle
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
MC PL editor upgrade
MultiCharts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Diary of a simple price action trader
26 thanks
Just another trading journal: PA, Wyckoff & Trends
24 thanks
Tao te Trade: way of the WLD
22 thanks
My NQ Trading Journal
16 thanks
HumbleTraders next chapter
9 thanks
  #2 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 since Aug 2009
Thanks Given: 320
Thanks Received: 623

Hi Kirk,

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

it should be just:
order.Token

Attached Files
Elite Membership required to download: IOrdersMulti_Zeller.zip
Reply With Quote
Thanked by:
  #3 (permalink)
 zeller4 
Orlando Florida
 
Experience: Intermediate
Platform: NT8
Trading: CL, NQ, ES, RTY
Posts: 477 since Jun 2009
Thanks Given: 1,416
Thanks Received: 404


Awesome, gregid,

thanks so much for your help.

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.

thanks again,
Kirk

Attached Files
Elite Membership required to download: IOrdersMulti_Zeller_v6_5.cs
Started this thread Reply With Quote
  #4 (permalink)
 manev31 
Switzerland
 
Experience: Intermediate
Platform: Tradestation, Python
Trading: Forex
Posts: 12 since Aug 2014
Thanks Given: 2
Thanks Received: 1

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");

// Target order 8 ticks above our entry price
targetOrder_1a = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 168 * TickSize, "Target 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);
}

#region Properties
#endregion
}
}

Reply With Quote




Last Updated on September 3, 2014


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts