NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > Application Technical Support > Automated Trading

Automated Trading Support for automated trading systems using NinjaScript. Support for our ATI (Automated Trading Interface) used to link an external application such as TradeStation and eSignal to NinjaTrader.

Reply
 
Thread Tools Display Modes
Old 02-06-2009, 10:47 AM   #1
BillCh
Member
 
Join Date: Feb 2009
Posts: 68
Thanks: 0
Thanked 0 times in 0 posts
Default Script is running wild: buy - sell - buy - sell ...

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):

Code:
 
// 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 is offline  
Reply With Quote
Old 02-06-2009, 10:48 AM   #2
BillCh
Member
 
Join Date: Feb 2009
Posts: 68
Thanks: 0
Thanked 0 times in 0 posts
Default

And the (long) exit:

Code:
// LONG EXIT on SMA TARGET
if (CrossAbove(High, SMA(Median, Period), 1))
{
ExitLong("xlg", "HoboLG");
}
And aboe that all, first after OnBarUpdate() I have:

Code:
// 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():

Code:
SetStopLoss("", CalculationMode.Ticks, 12, false);
CalculateOnBarClose = false;
 
EntriesPerDirection = 1; 
EntryHandling = EntryHandling.AllEntries; 
Sorry for the long post!
BillCh is offline  
Reply With Quote
Old 02-06-2009, 11:09 AM   #3
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
Default

Hi BillCh, thanks for the post, please check out this reference sample and code here - http://www.ninjatrader-support2.com/...ead.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.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 02-06-2009, 12:33 PM   #4
BillCh
Member
 
Join Date: Feb 2009
Posts: 68
Thanks: 0
Thanked 0 times in 0 posts
Default

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!
BillCh is offline  
Reply With Quote
Old 02-06-2009, 01:01 PM   #5
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
Default

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/H...tStopLoss.html

Other orders such as ExitLongStop also allow you to work with the IOrder object for more finetuning - http://www.ninjatrader-support.com/H...tLongStop.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.
NinjaTrader_Bertrand is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
script: buy at next day open and sell at close randomcz General Programming 1 02-04-2009 12:21 PM
Buy / Sell Stop bsamani General Programming 5 12-11-2008 01:58 PM
buy sell centered ? T2020 Charting 3 10-16-2008 11:20 AM
Sound on Buy and Sell laserjet General Programming 3 09-30-2008 07:00 PM
Buy Sell Indicator infotrader Charting 2 01-28-2008 02:12 PM


All times are GMT -6. The time now is 11:25 PM.