PDA

View Full Version : Limit Orders still behaving badly...


BigDog008
06-09-2009, 07:00 AM
I'm still wrestling with limit orders, and am still having issues in getting the model to send orders when I have my cancellation logic in place... I've tried doing TraceOrders = true; along with Print() statements but am stuck... this is the follwing info I get at the Output screen....


Order='NT-00000/Replay101' Name='Short' State=Working Instrument='ES 06-09' Action=SellShort Limit price=944.25 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='14bc74ceed314544bcba8a89a9afba29' Gtd='12/1/2099 12:00:00 AM'
Order='NT-00000/Replay101' Name='Short' State=Filled Instrument='ES 06-09' Action=SellShort Limit price=944.25 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=1 Fill price=944.25 Token='14bc74ceed314544bcba8a89a9afba29' Gtd='12/1/2099 12:00:00 AM'
Order='NT-00003/Replay101' Name='Short' State=Working Instrument='ES 06-09' Action=SellShort Limit price=941 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='864e51fabf144f35aa368b4d695a5354' Gtd='12/1/2099 12:00:00 AM'
Order='NT-00003/Replay101' Name='Short' State=Working Instrument='ES 06-09' Action=SellShort Limit price=941 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='864e51fabf144f35aa368b4d695a5354' Gtd='12/1/2099 12:00:00 AM'
Order='NT-00003/Replay101' Name='Short' State=Filled Instrument='ES 06-09' Action=SellShort Limit price=941 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=1 Fill price=941 Token='864e51fabf144f35aa368b4d695a5354' Gtd='12/1/2099 12:00:00 AM'
Order='NT-00006/Replay101' Name='Short' State=Working Instrument='ES 06-09' Action=SellShort Limit price=941.75 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='1d848f0a5f394e4a9b955774d2d5e076' Gtd='12/1/2099 12:00:00 AM'
Order='NT-00006/Replay101' Name='Short' State=Filled Instrument='ES 06-09' Action=SellShort Limit price=941.75 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=1 Fill price=941.75 Token='1d848f0a5f394e4a9b955774d2d5e076' Gtd='12/1/2099 12:00:00 AM'
Order='NT-00009/Replay101' Name='Short' State=Working Instrument='ES 06-09' Action=SellShort Limit price=939 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='17f8b78fc6db4c34a4d90f2ddfc8007a' Gtd='12/1/2099 12:00:00 AM'
Order='NT-00009/Replay101' Name='Short' State=Filled Instrument='ES 06-09' Action=SellShort Limit price=939 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=1 Fill price=939 Token='17f8b78fc6db4c34a4d90f2ddfc8007a' Gtd='12/1/2099 12:00:00 AM'
Order='NT-00012/Replay101' Name='Short' State=Working Instrument='ES 06-09' Action=SellShort Limit price=938.75 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='51cc9f4bc34b4859ad9ca1796d016764' Gtd='12/1/2099 12:00:00 AM'
Order='NT-00012/Replay101' Name='Short' State=Working Instrument='ES 06-09' Action=SellShort Limit price=938.75 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='51cc9f4bc34b4859ad9ca1796d016764' Gtd='12/1/2099 12:00:00 AM'
Now what's odd about this is that I want to run my model only from 8:30 am to 10:00 am Chicago time.... these orders are going out the moment I push Start on the strategies tab, even when its before 8:30 am on the Market Replay bar...

This is the logic I am using... Orders Per Direction is only 1 for now....


{
// Condition set 1
if (High[0] > EMA(Period)[0]
&& entryOrder == null)
{
entryOrder = EnterLongLimit(0, true, DefaultQuantity, GetCurrentBid() + -2 * TickSize, "Long");
}

// Condition set 2
if (Low[0] < EMA(Period)[0]
&& entryOrder == null)
{
entryOrder = EnterShortLimit(0, true, DefaultQuantity, GetCurrentAsk() + 2 * TickSize, "Short");
}

if (entryOrder != null)
{
Print(entryOrder.ToString());

if (entryOrder.OrderState == OrderState.Working
&& entryOrder.Action == Action.Buy
&& GetCurrentBid() >= entryOrder.LimitPrice + 4 * TickSize)

{
CancelOrder(entryOrder);
entryOrder = null;
}

if (entryOrder.OrderState == OrderState.Working
&& entryOrder.Action == Action.SellShort
&& GetCurrentAsk() <= entryOrder.LimitPrice - 4 * TickSize)

{
CancelOrder(entryOrder);
entryOrder = null;
}

if (entryOrder.OrderState == OrderState.Filled
&& entryOrder.Action == Action.Buy)
{
SetProfitTarget("Long", CalculationMode.Ticks, 8);
SetStopLoss("Long", CalculationMode.Ticks, 6, false);
entryOrder = null;
}

if (entryOrder.OrderState == OrderState.Filled
&& entryOrder.Action == Action.SellShort)

{
SetProfitTarget("Short", CalculationMode.Ticks, 8);
SetStopLoss("Short", CalculationMode.Ticks, 6, false);
entryOrder = null;
}


}
}

NinjaTrader_Josh
06-09-2009, 07:22 AM
No where in your code do you have a time filter preventing trades to that time range. Please see this reference sample: http://www.ninjatrader-support2.com/vb/showthread.php?t=3226

BigDog008
06-09-2009, 07:27 AM
No where in your code do you have a time filter preventing trades to that time range. Please see this reference sample: http://www.ninjatrader-support2.com/vb/showthread.php?t=3226


Would a time filter be needed even when say trading hours are from time XX:XX to YY:YY in the strategy setup?

NinjaTrader_Josh
06-09-2009, 07:37 AM
If you want to limit trading you should use a time filter.

BigDog008
06-09-2009, 07:48 AM
Josh,

I put in the time filter as shown below, and am still having the same issue where the model is sending orders even though its before 8:30... the time filter is the first line


protected override void OnBarUpdate()
{
if ((ToTime(Time[0]) >= 83000 && ToTime(Time[0]) < 100000))
{
// Condition set 1
if (High[0] > EMA(Period)[0]
&& entryOrder == null)
{
entryOrder = EnterLongLimit(0, true, DefaultQuantity, GetCurrentBid() + -2 * TickSize, "Long");
}

// Condition set 2
if (Low[0] < EMA(Period)[0]
&& entryOrder == null)
{
entryOrder = EnterShortLimit(0, true, DefaultQuantity, GetCurrentAsk() + 2 * TickSize, "Short");
}

if (entryOrder != null)
{
Print(entryOrder.ToString());

if (entryOrder.OrderState == OrderState.Working
&& entryOrder.Action == Action.Buy
&& GetCurrentBid() >= entryOrder.LimitPrice + 4 * TickSize)

{
CancelOrder(entryOrder);
entryOrder = null;
}

if (entryOrder.OrderState == OrderState.Working
&& entryOrder.Action == Action.SellShort
&& GetCurrentAsk() <= entryOrder.LimitPrice - 4 * TickSize)

{
CancelOrder(entryOrder);
entryOrder = null;
}

if (entryOrder.OrderState == OrderState.Filled
&& entryOrder.Action == Action.Buy)
{
SetProfitTarget("Long", CalculationMode.Ticks, 8);
SetStopLoss("Long", CalculationMode.Ticks, 6, false);
entryOrder = null;
}

if (entryOrder.OrderState == OrderState.Filled
&& entryOrder.Action == Action.SellShort)

{
SetProfitTarget("Short", CalculationMode.Ticks, 8);
SetStopLoss("Short", CalculationMode.Ticks, 6, false);
entryOrder = null;
}

}
}
}

NinjaTrader_Josh
06-09-2009, 07:52 AM
Please add Print()s and figure out where it is entering your code segments.

BigDog008
06-09-2009, 07:57 AM
Josh that's the thing...
It's putting out orders at say 85216 even though the replay bar says it is only 81023... i've used these replay files for many strategies without any issue at all (about 100 trading days worth) so I know they are good...

see the output below...


85216
Order='NT-00000/Replay101' Name='Short' State=Working Instrument='ES 06-09' Action=SellShort Limit price=944.25 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='c46c5bc513734c3ab9a990f58ae10685' Gtd='12/1/2099 12:00:00 AM'
85502
Order='NT-00000/Replay101' Name='Short' State=Filled Instrument='ES 06-09' Action=SellShort Limit price=944.25 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=1 Fill price=944.25 Token='c46c5bc513734c3ab9a990f58ae10685' Gtd='12/1/2099 12:00:00 AM'
85752
85805
Order='NT-00003/Replay101' Name='Short' State=Working Instrument='ES 06-09' Action=SellShort Limit price=941 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='ce43dfd7dabf4e2e8b11cf842c0f3469' Gtd='12/1/2099 12:00:00 AM'
85925
Order='NT-00003/Replay101' Name='Short' State=Working Instrument='ES 06-09' Action=SellShort Limit price=941 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='ce43dfd7dabf4e2e8b11cf842c0f3469' Gtd='12/1/2099 12:00:00 AM'
85940
Order='NT-00003/Replay101' Name='Short' State=Filled Instrument='ES 06-09' Action=SellShort Limit price=941 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=1 Fill price=941 Token='ce43dfd7dabf4e2e8b11cf842c0f3469' Gtd='12/1/2099 12:00:00 AM'
90123
Order='NT-00006/Replay101' Name='Short' State=Working Instrument='ES 06-09' Action=SellShort Limit price=941.75 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='492a014d6146421a9641125b0ab586ed' Gtd='12/1/2099 12:00:00 AM'
90307
Order='NT-00006/Replay101' Name='Short' State=Filled Instrument='ES 06-09' Action=SellShort Limit price=941.75 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=1 Fill price=941.75 Token='492a014d6146421a9641125b0ab586ed' Gtd='12/1/2099 12:00:00 AM'
90515
90604
90623
Order='NT-00009/Replay101' Name='Short' State=Working Instrument='ES 06-09' Action=SellShort Limit price=939 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='9e7784aecbc546deb6cbfba6a1702f42' Gtd='12/1/2099 12:00:00 AM'
90724
Order='NT-00009/Replay101' Name='Short' State=Filled Instrument='ES 06-09' Action=SellShort Limit price=939 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=1 Fill price=939 Token='9e7784aecbc546deb6cbfba6a1702f42' Gtd='12/1/2099 12:00:00 AM'
90808
Order='NT-00012/Replay101' Name='Short' State=Working Instrument='ES 06-09' Action=SellShort Limit price=938.75 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='b997b440a8d04525b32bf0cf9e242532' Gtd='12/1/2099 12:00:00 AM'
90933
Order='NT-00012/Replay101' Name='Short' State=Working Instrument='ES 06-09' Action=SellShort Limit price=938.75 Stop price=0 Quantity=1 Strategy='LimitOrders' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='b997b440a8d04525b32bf0cf9e242532' Gtd='12/1/2099 12:00:00 AM'

NinjaTrader_Josh
06-09-2009, 08:05 AM
Not following you. A timestamp of 85216 means 8:52:16 which means you are in the tradeable range.

BigDog008
06-09-2009, 08:07 AM
Josh
I've added this screenshot to illustrate what I mean...

As you can see... replay is only at 8:10:23... but ninja is placing orders at future time stamps....

NinjaTrader_Josh
06-09-2009, 08:12 AM
The timestamp being printed from the strategy is the timestamp of the replay. It is not processing bars in the future. If you print out every bar's timestamp you will see it has reached there properly.

BigDog008
06-09-2009, 08:16 AM
but the replay has only reached 8:10am... how could it process timestamps for 9:00 am?

NinjaTrader_Josh
06-09-2009, 08:29 AM
Please just print the timestamp of every bar and see what time the replay really is at.

BigDog008
06-09-2009, 08:39 AM
Still having the same issues Josh :(

would you happen to have any code snippet with LimitOrders being cancelled if the price deviates after certain number of ticks rather than bars?

or is there a way to cancel out limit orders using something other than IOrder?

really appreciate your help and patience

NinjaTrader_Josh
06-09-2009, 08:48 AM
if (Close[0] > entryPrice + 10 * TickSize)
CancelOrder(limitOrder);

BigDog008
06-09-2009, 09:21 AM
if (Close[0] > entryPrice + 10 * TickSize)
CancelOrder(limitOrder);


Just to be sure... where you have CancelOrder(limitOrder) the limitOrder would be the name of the order correct?

ie

CancelOrder("Long")
?

NinjaTrader_Josh
06-09-2009, 09:42 AM
No. limitOrder would be the name of my IOrder object for the limit order.

limitOrder = EnterLongLimit(...);
CancelOrder(limitOrder);

BigDog008
06-09-2009, 10:18 AM
another question, how do I reference the entryPrice? i know i could use avg position price, but i want to reference the exact entry price of a given order in case i expand to more than one contract

NinjaTrader_Josh
06-09-2009, 11:08 AM
IOrder.AvgFillPrice