please support me with the following question:
I'm trying to rewrite SampleOnOrderUpdate to the short position.
For this I use the following code:
namespace NinjaTrader.NinjaScript.Strategies
{
public class SampleOnOrderUpdate : Strategy
{
private Order entryOrder = null;
private Order stopOrder = null;
private Order targetOrder = null;
private Order entryOrderShort = null;
private Order stopOrderShort = null;
private Order targetOrderShort = null;
bool tradeLong, tradeShort, testBreakEven;
double deltaBreakEven;
protected override void OnStateChange()
{
#region settings
if (State == State.SetDefaults)
{
Description = @"Sample ";
Name = "SampleOnOrderUpdate";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
}
else if (State == State.Realtime)
{
if (entryOrder != null) entryOrder = GetRealtimeOrder(entryOrder);
if (stopOrder != null) stopOrder = GetRealtimeOrder(stopOrder);
if (targetOrder != null) targetOrder = GetRealtimeOrder(targetOrder);
// Short
if (entryOrderShort != null)
entryOrderShort = GetRealtimeOrder(entryOrderShort);
if (stopOrderShort != null)
stopOrderShort = GetRealtimeOrder(stopOrderShort);
if (targetOrderShort!= null)
targetOrderShort = GetRealtimeOrder(targetOrderShort);
}
#endregion
}
protected override void OnBarUpdate()
{
tradeLong = false;
tradeShort = true;
if (State == State.Realtime)
{
#region Long
if (tradeLong) // OCO ==> ++
{
if (entryOrder == null && Close[0] > Open[0]
&& CurrentBar > BarsRequiredToTrade)
{EnterLong(1, "MyEntry");}
// BreakEven
if (Position.MarketPosition == MarketPosition.Long
&& Close[0] >= Position.AveragePrice + 5)
{
if (stopOrder != null
&& stopOrder.StopPrice < Position.AveragePrice)
{
stopOrder = ExitLongStopMarket(0, true, stopOrder.Quantity,
Position.AveragePrice + 3, "MyStop", "MyEntry"); // Trailing ++
}
}
}
#endregion
#region Short
if (tradeShort)
{
if (Position.AveragePrice > 0) deltaBreakEven =
Position.AveragePrice - 10;
testBreakEven = Close[0] < (deltaBreakEven);
if (entryOrderShort == null && Close[0] < Open[0]
&& CurrentBar > BarsRequiredToTrade)
{EnterShort(1, "MyEntryShort");}
// BreakEven
if (Position.MarketPosition == MarketPosition.Short && testBreakEven)
{
if (stopOrderShort != null
&& stopOrderShort.StopPrice > Position.AveragePrice)
{
stopOrderShort = ExitShortStopMarket(0, true,
stopOrderShort.Quantity,
Position.AveragePrice, "TrailStop", "MyEntryShort");
}
}
}
#endregion
}
}
protected override void OnOrderUpdate(Order order,
double limitPrice, double stopPrice, int quantity, int filled,
double averageFillPrice, OrderState orderState,
DateTime time, ErrorCode error, string nativeError)
{
#region Long
if (order.Name == "MyEntry")
{
entryOrder = order;
if (order.OrderState == OrderState.Cancelled
&& order.Filled == 0) entryOrder = null;
}
#endregion
#region Short
if (order.Name == "MyEntryShort")
{
entryOrderShort = order;
if (order.OrderState == OrderState.Cancelled
&& order.Filled == 0) entryOrderShort = null;
}
#endregion
}
protected override void OnExecutionUpdate(Execution execution,
string executionId, double price, int quantity,
MarketPosition marketPosition, string orderId, DateTime time)
{
#region Long
if (entryOrder != null && entryOrder == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled
|| execution.Order.OrderState == OrderState.PartFilled
|| (execution.Order.OrderState == OrderState.Cancelled
&& execution.Order.Filled > 0))
{
stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled,
execution.Order.AverageFillPrice - 6 , "MyStop", "MyEntry");
targetOrder = ExitLongLimit(0, true, execution.Order.Filled,
execution.Order.AverageFillPrice + 15 , "MyTarget", "MyEntry");
if (execution.Order.OrderState != OrderState.PartFilled)
entryOrder = null;
}
}
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;
}
}
#endregion
#region Short
if (entryOrderShort != null && entryOrderShort == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled
|| execution.Order.OrderState == OrderState.PartFilled
|| (execution.Order.OrderState == OrderState.Cancelled
&& execution.Order.Filled > 0))
{
stopOrderShort = ExitShortStopMarket(0, true, execution.Order.Filled,
execution.Order.AverageFillPrice + 10 , "InitStop", "MyEntryShort");
targetOrderShort = ExitShortLimit(0, true, execution.Order.Filled,
execution.Order.AverageFillPrice - 20 , "Target", "MyEntryShort");
if (execution.Order.OrderState != OrderState.PartFilled)
entryOrderShort = null;
}
}
if ((stopOrderShort != null && stopOrderShort == execution.Order)
|| (targetOrderShort != null && targetOrderShort == execution.Order))
{
if (execution.Order.OrderState == OrderState.Filled
|| execution.Order.OrderState == OrderState.PartFilled)
{
stopOrderShort = null; targetOrderShort = null;
}
}
#endregion
}
}
}
For the calculation of the break-even I cannot use PositionAveragePrice + x as on the LongPosition.
PositionAveragePrice - x sends a continuous signal to the broker; the position is opened and immediately closed again.
I therefore calculate the Delta (EntryPrice - x) and use the condition testBreakEven. This enables me to control the trading process correctly.
Is this procedure correct?
Unlike the long position, OCO does not work with this programming.
I get two stop prices. How can I change this?
Thank you very much for your support!
Comment