longshort intraday.calc on barclose true.
go long after an upbar high+3 ticks.go short after downbar low - 3ticks
the startegy works fine.but no short or long trade is taken on the same bar after a long or short is exited on this same bar.
pls advise...
here is the code
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("longshort intraday.go long after upbar high+3 ticks.go short after downbar low - 3ticks")]
public class projectminisessiondaytrading1longshort : Strategy
{
#region Variables
private IOrder longentryOrder = null; // This variable holds an object representing our long entry order
private IOrder shortentryOrder = null; // This variable holds an object representing our short entry order
private IOrder stopOrder = null; // This variable holds an object representing our stop loss order
private IOrder targetOrder = null; // This variable holds an object representing our profit target order
private int barNumberOfOrder = 0;
private int cancelBars = 2; // number of bars to cancel order after
#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()
{
TraceOrders = false;
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Submit an longentry limit order if we currently don't have an entry order open
if (longentryOrder == null && Close[0] > Open[0] && (ToTime(Time[0]) < 135900))
{
longentryOrder = EnterLongStop(0, true,1,High[0] + 6 * TickSize,"LONGEntry");
barNumberOfOrder = CurrentBar;
}
// cancel the entry order
else if (longentryOrder != null
&& ( (Close[0] < Open[0]))
)
CancelOrder(longentryOrder);
if (Position.MarketPosition == MarketPosition.Long
&& (Close[0] < Open[0])
&& (ToTime(Time[0]) < 135900)
)
{
// Checks to see if our Stop Order has been submitted already
if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice)
{
// Modifies stop-loss
stopOrder = ExitLongStop(0, true, stopOrder.Quantity, Low[0] - 6 * TickSize, "LONGStopmodified", "LONGEntry");
}
}
/*********************now look for short trades*****************************/
// Submit an short entry order if we currently don't have an entry order open
if (shortentryOrder == null && Close[0] < Open[0] && (ToTime(Time[0]) < 135900))
{
shortentryOrder = EnterShortStop(0, true,1,Low[0] - 6 * TickSize,"SHORTEntry");
barNumberOfOrder = CurrentBar;
}
// cancel the shortentry order
else if (shortentryOrder != null
&& ((Close[0] > Open[0]))
)
CancelOrder(shortentryOrder);
if (Position.MarketPosition == MarketPosition.Short
&& (Close[0] > Open[0])
&& (ToTime(Time[0]) < 135900)
)
{
// Checks to see if our Stop Order has been submitted already
if (stopOrder != null && stopOrder.StopPrice > Position.AvgPrice)
{
// Modifies stop-loss
stopOrder = ExitShortStop(0, true, stopOrder.Quantity, High[0] + 6 * TickSize, "SHORTStopmodified", "SHORTEntry");
}
}
}
/// <summary>
/// Called on each incoming order event
/// </summary>
protected override void OnOrderUpdate(IOrder order)
{
// The longentryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
if (longentryOrder != null && longentryOrder == order)
{
// Reset the longentryOrder object to null if order was cancelled without any fill
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
longentryOrder = null;
}
}
// The shortentryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
if (shortentryOrder != null && shortentryOrder == order)
{
// Reset the shortentryOrder object to null if order was cancelled without any fill
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
shortentryOrder = null;
}
}
}
/// <summary>
/// Called on each incoming execution
/// </summary>
protected override void OnExecution(IExecution execution)
{
if (longentryOrder != null && longentryOrder == 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 below our entry price
stopOrder = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 22 * TickSize, "LONGStop", "LONGEntry");
// Target order above our entry price
targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 22 * TickSize, "LONGTarget", "LONGEntry");
// Resets the longentryOrder object to null after the order has been filled
if (execution.Order.OrderState != OrderState.PartFilled)
{
longentryOrder = null;
}
}
}
if (shortentryOrder != null && shortentryOrder == 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 above our entry price
stopOrder = ExitShortStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 22 * TickSize, "SHORTStop", "SHORTEntry");
// Target order below our entry price
targetOrder = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 22 * TickSize, "SHORTTarget", "SHORTEntry");
// Resets the shortentryOrder object to null after the order has been filled
if (execution.Order.OrderState != OrderState.PartFilled)
{
shortentryOrder = null;
}
}
}
// Reset our stop order and target orders' IOrder objects after our position is closed.
if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order))
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled )
{
stopOrder = null;
targetOrder = 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("Tag", position.ToString(), TextPosition.BottomRight);
}
#region Properties
#region Properties
[Description("")]
[GridCategory("Parameters")]
public int BarNumberOfOrder
{
get { return barNumberOfOrder; }
set { barNumberOfOrder = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int CancelBars
{
get { return cancelBars; }
set { cancelBars = Math.Max(1, value); }
}
#endregion
#endregion
}
}

Comment