Some demo code:
namespace NinjaTrader.NinjaScript.Strategies
{
public class DemoCrossSessionGTC : Strategy
{
//order objects
private Order longEntryLimitOrder = null;
private Order longEntryStopMar****rder = null;
private Order longExitLimitOrder = null;
private Order longExitStopMar****rder = null;
private int sumLongFilled = 0;
private int contracts = 1;
private double longStopTicks = 8.0;
private double longTargetTicks = 8.0;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"GTC Orders being executed when no data should trigger entry/exits";
Name = "DemoCrossSessionGTC";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 2;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
ExitOnSessionCloseSeconds = 0;
IsFillLimitOnTouch = true;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.High;
OrderFillResolutionType = BarsPeriodType.Tick;
OrderFillResolutionValue = 1;
Slippage = 1;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = true;
RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
StopTargetHandling = StopTargetHandling.ByStrategyPosition;
BarsRequiredToTrade = 20;
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
}
}
protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
Cbi.MarketPosition marketPosition, string orderId, DateTime time)
{
if ((longEntryStopMar****rder != null && longEntryStopMar****rder == execution.Order) ||
(longEntryLimitOrder != null && longEntryLimitOrder == execution.Order))
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled
&& execution.Order.Filled > 0))
{
// We sum the quantities of each execution making up the entry order
sumLongFilled += execution.Quantity;
// add stop exit order or if if exists already update volume as partfills occur
if ( longExitStopMar****rder == null)
{
Print ("Entering GTC Stop Exit Order on "+ Position.AveragePrice + (longTargetTicks * TickSize).ToString());
longExitStopMar****rder = ExitLongStopMarket(0, true, sumLongFilled, Position.AveragePrice - (longStopTicks * TickSize), "ExitLStpMrkt", "LongEntry");
}
else
{
Print ("Changing GTC Stop Exit Order on "+ CurrentBar.ToString());
ChangeOrder(longExitStopMar****rder, sumLongFilled, 0.0 , longExitStopMar****rder.StopPrice);
}
if ( longExitLimitOrder == null)
{
Print ("Entering GTC Target Exit Order on "+ Position.AveragePrice + (longTargetTicks * TickSize).ToString());
longExitLimitOrder = ExitLongLimit(0, true, sumLongFilled, Position.AveragePrice + (longTargetTicks * TickSize), "ExitLLimit", "LongEntry");
}
else
{
Print ("Changing GTC Target Exit Order on "+ CurrentBar.ToString());
ChangeOrder(longExitLimitOrder, sumLongFilled , Position.AveragePrice + (longTargetTicks * TickSize), 0.0);
}
if (execution.Order.OrderState == OrderState.Filled && sumLongFilled == contracts)
{
if(longEntryLimitOrder == execution.Order)
{
longEntryLimitOrder = null;
if (longEntryStopMar****rder != null) CancelOrder( longEntryStopMar****rder );
}
if(longEntryStopMar****rder == execution.Order)
{
longEntryStopMar****rder = null;
if (longEntryLimitOrder != null) CancelOrder( longEntryLimitOrder );
}
}
if (execution.Order.OrderState == OrderState.Filled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
sumLongFilled = 0;
}
}
}
//handle exits, if one of target or stop are filled, cancel the other
if ((longExitStopMar****rder != null && longExitStopMar****rder == execution.Order) ||
(longExitLimitOrder != null && longExitLimitOrder == execution.Order ))
{
if (execution.Order.OrderState == OrderState.Filled )
{
if (longExitStopMar****rder == execution.Order)
{
longExitStopMar****rder = null;
if (longExitLimitOrder != null) CancelOrder (longExitLimitOrder);
}
else if (longExitLimitOrder == execution.Order)
{
longExitLimitOrder = null;
if ( longExitStopMar****rder!= null) CancelOrder (longExitStopMar****rder);
}
}
}
}
protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
int quantity, int filled, double averageFillPrice,
Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
{
//assign long entry orders objects to vars and tidy
if (order.Name == "LongEntry")
{
if (order.IsStopMarket) longEntryStopMar****rder = order;
if (order.IsLimit) longEntryLimitOrder = order;
// Reset the entryOrder object to null, if Orderstate.Filled then reset in OnExecutionUpdate
if (order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Rejected || order.OrderState == OrderState.Unknown)
{
if (order.IsStopMarket) longEntryStopMar****rder = null;
if (order.IsLimit) longEntryLimitOrder = null;
}
}
//assign and tidy exit orders
if (order.Name == "ExitLStpMrkt")
{
longExitStopMar****rder = order;
// Reset the exit order object to null. if filled reset in OnExecutionUpdate
if (order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Rejected
|| order.OrderState == OrderState.Unknown)
{
longExitStopMar****rder = null;
}
}
if (order.Name == "ExitLLimit")
{
longExitLimitOrder = order;
// Reset the exit order object to null. if filled reset in OnExecutionUpdate
if (order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Rejected
|| order.OrderState == OrderState.Unknown )
{
longExitLimitOrder = null;
}
}
}
protected override void OnBarUpdate()
{
//place GTC stop and limit long entry orders if they dont exist and we're flat + enough bars
if (longEntryStopMar****rder == null && longEntryLimitOrder == null)
{
if (Position.MarketPosition == MarketPosition.Flat && CurrentBar > BarsRequiredToTrade)
{
//assign to an object var in OnOrderUpdate
Print("Entering GTC Long Entry Orders on "+ CurrentBar.ToString());
EnterLongLimit(0, true, contracts, GetCurrentBid() - 1.5, "LongEntry");
EnterLongStopMarket(0, true, contracts, GetCurrentAsk() + 1.5, "LongEntry");
}
}
}
}
}

Comment