I am trying to execute a strategy where each entry is handled independently. TO do this I instantiate a new object when the entry criteria is met and the new object manages itself till the order is closed. The problems I have are -
1. The code inside the new class does not execute.
2. Even though I call the strategy on bar update it does not seem to be doing so. It seems to be getting called only on bar close. I used the visual studio debugging process to fnd this out.
Can someone help me with what I am doing wrong.
The code is a pretty simple one and is below..
#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("Enter the description of your strategy here")]
public class DIBS : Strategy
{
#region Variables
// Wizard generated variables
private int validationPips = 1; // Default setting for ValidationPips
// User defined variables (add any user defined variables below)
private IOrder entryOrder = null;
private bool orderPlaced;
#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()
{
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar<2)return;
if (FirstTickOfBar)
{
orderPlaced=false;
}
if (orderPlaced==false && DIBSIndicator().DIBSLong.ContainsValue(1) && Close[0]>High[1])
{
new SOrder(Action.Buy, OrderType.Market, Close[0], 100000, Low[1], High[1]-Low[1], "", Time[0]);
orderPlaced=true;
}
}
protected override void OnOrderUpdate(IOrder order)
{
if (entryOrder != null && entryOrder.Token == order.Token)
{
Print(order.ToString());
if (order.OrderState == OrderState.Filled)
entryOrder = null;
}
}
public class SOrder : Strategy
{
public readonly OrderType orderType;
public readonly Action action;
public readonly double orderPrice, stopLoss, takeProfit;
public readonly int quantity;
public readonly DateTime expiration;
public readonly string trailingProfit;
private bool orderPlaced=false;
private IOrder order;
public SOrder(Action act, OrderType ordType, double entPrc, int qty, double stpLss, double tkePrft, string trlPrft, DateTime exp)
{
action=act;
orderType=ordType;
orderPrice=entPrc;
quantity=qty;
stopLoss=stpLss;
takeProfit=tkePrft;
trailingProfit=trlPrft;
expiration=exp;
}
protected override void OnBarUpdate()
{
if (action==Action.Buy)
{
switch (orderType)
{
case OrderType.Market:
order=EnterLong(quantity, "Order");
orderPlaced=true;
break;
case OrderType.Stop:
order=EnterLongStop(quantity, orderPrice, "Order");
orderPlaced=true;
break;
}
}
else if (action==Action.Sell)
{
switch (orderType)
{
case OrderType.Market:
order=EnterShort(quantity, "Order");
orderPlaced=true;
break;
case OrderType.Stop:
order=EnterShortStop(quantity, orderPrice, "Order");
orderPlaced=true;
break;
}
}
if (order.OrderState==OrderState.Filled)
{
SetStopLoss("Order", CalculationMode.Price, stopLoss, true);
SetProfitTarget("Order", CalculationMode.Price, takeProfit);
}
if (Time[0]>expiration && order.OrderType==OrderType.Stop && order.OrderState != OrderState.Filled)
{
CancelOrder(order);
}
}
}
}
}
Comment