PDA

View Full Version : Script is running wild: buy - sell - buy - sell ...


BillCh
02-06-2009, 10:47 AM
Hi,

new to NT. Tried strategy which buys if price goes below Bollinger band, target is an SMA, and StopLoss is a price which is calculated at entry (and vice versa for shorts).

Seemd to work okay, but had problems twice today: All of a sudden NT sendet buy - short - buy - short - ... orders for about two minutes.

The order log from the output window showed dozends of these:

2/6/2009 12:15:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='HoboST' Mode=Ticks Value=12 Currency=0 Simulated=False
2/6/2009 12:15:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='HoboLG' Mode=Ticks Value=12 Currency=0 Simulated=False
2/6/2009 12:15:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='HoboST' Mode=Ticks Value=12 Currency=0 Simulated=False

The last records before that repetition were:

2/6/2009 12:14:59 PM Entered internal PlaceOrder() method at 2/6/2009 12:14:59 PM: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='xlg' FromEntrySignal='HoboLG'
2/6/2009 12:14:59 PM Ignored PlaceOrder() method: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='xlg' FromEntrySignal='HoboLG' Reason='This was an exit order but no position exists to exit'
2/6/2009 12:15:00 PM Cancelled pending exit order, since associated position is closed: Order='00567cf06dd14798b0bc2175b6f380f8/Sim101' Name='Stop loss' State=Accepted Instrument='ES 03-09' Action=Sell Limit price=0 Stop price=855.5 Quantity=2 Strategy='HoboESv01' Type=Stop Tif=Gtc Oco='40d1b309d3604db18d5bb98c0f32eccb' Filled=0 Fill price=0 Token='00567cf06dd14798b0bc2175b6f380f8' Gtd='12/1/2099 12:00:00 AM'

What my script is doing at the entry (long):


// HOBO LONG ENTRY
if (CrossBelow(Low, HoboSigLong(BBStdDev, Period), 1)
&& ToTime(Time[0]) >= ToTime(9, 30, 0)
&& ToTime(Time[0]) < ToTime(15, 0, 0))
{
EnterLong(InitCtr, "HoboLG");
stpLG = HoboStopLo(BBStdDev, Period)[0];
SetStopLoss( "HoboST", CalculationMode.Price, stpLG, false );
}

BillCh
02-06-2009, 10:48 AM
And the (long) exit:


// LONG EXIT on SMA TARGET
if (CrossAbove(High, SMA(Median, Period), 1))
{
ExitLong("xlg", "HoboLG");
}


And aboe that all, first after OnBarUpdate() I have:


// Reset Stop Loss Levels if flat
if (Position.MarketPosition == MarketPosition.Flat)
{
stpLG = SMA(Median, Period)[0]*20;
stpST = 0;
SetStopLoss("HoboST", CalculationMode.Ticks, 12, false);
SetStopLoss("HoboLG", CalculationMode.Ticks, 12, false);
}


At Initialize():


SetStopLoss("", CalculationMode.Ticks, 12, false);
CalculateOnBarClose = false;

EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;


Sorry for the long post!

NinjaTrader_Bertrand
02-06-2009, 11:09 AM
Hi BillCh, thanks for the post, please check out this reference sample and code here - http://www.ninjatrader-support2.com/vb/showthread.php?t=3222

Then work those checks in to modify your StopLoss price only when a corresponding long / short position is open.

You also want to make sure you reference the correct signal names for entries and exits. For example you have a long entry named 'HoboLG' and set your stop loss to exit from a 'HoboSt' position that is never entered.

BillCh
02-06-2009, 12:33 PM
Thanks for your reply!

I had an obvious typo in the code with referencing SetStopLoss to 'HoboST' while it should have been 'HoboLG'. Thank you for the hint!

I am a little concernd about resetting SetStopLoss with if(Pos... == MarketPositon.Flat). Does .Flat mean when in my case the TWS (from Interactive Brokers) returns a flat, or after NT has sent out the position close signals? In other words, I could have sent an LMT order and not being flat yet, but my SetStoppLoss would be reset even before it should be?

Is there an advantage using SetStopLoss over just using a simple ExitLong/ShortStop() order?

And, is it a problem to have a combination of more than one ExitLong/ShortStop() orders with different levels thru one OneBarUpdate() cycle?

Sorry for that many questions!

NinjaTrader_Bertrand
02-06-2009, 01:01 PM
You are welcome, the flat condition is registered when NinjaTrader receives the fill confirmation of the order taking your position flat.

The advantage of SetStopLoss and SetProfitTarget would be that they are automatically a OCO bracket order - http://www.ninjatrader-support.com/HelpGuideV6/SetStopLoss.html

Other orders such as ExitLongStop also allow you to work with the IOrder object for more finetuning - http://www.ninjatrader-support.com/HelpGuideV6/ExitLongStop.html

Sure you can submit ExitLongStop twice, the later call will modify the former one unless you use unique signal names. Not sure though why you would like to do that, just submit the order you need.