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 04-20-2011, 09:47 PM   #1
olingerc
Member
 
Join Date: Jan 2011
Posts: 58
Thanks: 0
Thanked 0 times in 0 posts
Default code for close position?

Even tho I sync my auto trader strategy to my live account I still get overfill errors because the auto trader thinks a poaition is open. For example I have the strategy to trade one contract per trade but if I start the strategy mid day with no open orders it will see that it should be in a long position one contract but in reality I have no open position. So when it goes to sell the long position it puts a sell order in to cover the long position then another sell order for the new trade. So I now have two contracts in a short position when I should only have one. This happens when I start the strategy mid day or manually edit orders myself. If I made the strategy close the position before it made a buy or sell order would this solve my issue? I wand the strategy to trade let's say one contract but I want to be able to add or subtract from my position and when it comes order trigger time I would like the strategy to close all contract positions not just one contract how can I do this? Thanks
olingerc is offline  
Reply With Quote
Old 04-21-2011, 03:49 AM   #2
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

olingerc, please first of all ensure to read those two links below on how the strategy vs account position sync is handled in NinjaTrader, if you just would like to take the first freshly generated entry after starting the strategy up using 'WaitUntilFlat' would be what you seek :

http://www.ninjatrader.com/support/h..._account_p.htm

http://www.ninjatrader.com/support/h..._positions.htm

Thanks,
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 04-21-2011, 05:56 AM   #3
olingerc
Member
 
Join Date: Jan 2011
Posts: 58
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks those links did a lot to help clear up my confusion If I understood them correctly. Basically if Im running a strategy and I get disconnected for some reason and I have the check box "Wait until flat" checked with my account synced then on start up it it will cancel any open orders that were previously made and start fresh from a flat position. To me that's a horrible scenario.

Immediately submit live working orders with account position synced will NOT cancel the orders that the strategy made before lets say a power outage or disconnection. It will basically pick up right where it left off assuming the orders have not changed.


And If I want to place manual orders "outside" of the strategy I can't have the accounts synced because the strategy will always try to correct the position if its synced.

If I understood this correctly I want the 2nd option. Thanks


I hope I have this all right. I will have to play test it out over the next week or so just to make 100% sure it's the way I want.

Thanks.
olingerc is offline  
Reply With Quote
Old 04-21-2011, 06:24 AM   #4
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're welcome, correct those are the two options to start a strategy up for realtime trading use - you can have manual orders executed to the same account as long as the currently held strategy position is present in the account as well, as otherwise resulting position changes executed from the strategy would not resulting in trades taken on your account as you intend to. Should you wish to manually change strategy created orders such as stoplosses or profit targets then using ATM templates in your NinjaScript strategy would be the way to go.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 04-21-2011, 01:10 PM   #5
olingerc
Member
 
Join Date: Jan 2011
Posts: 58
Thanks: 0
Thanked 0 times in 0 posts
Default

Im having a huge huge issue. I was trading with my strategy live today. I have the Immediately submit live working historical orders box checked and My account is not synced. I started the strategy with the live account while my position was flat in the beginning of the day. A buy signal came along and it fired off buying 2 contracts 1331.50. At this point everything looks normal. It came to a sell signal and the strategy did this It sold 1 contract at 1331.75 and then 1 more at 1331.75 and then sold 2 at 1331.75 to "close position" and then sold 2 more at 1331.75 as the normal sell position.

My strategy is set up to only submit orders of 2. Why did I get two orders of 1 and then my normal order?

Here is the simple code:

// Condition set 1
if (CrossAbove(EMA(57), SMA(226), 1))
{
EnterLong(2, "");
ExitShort("", "");
}

// Condition set 2
if (CrossBelow(EMA(67), SMA(222), 1))
{
ExitLong("", "");
EnterShort(2, "");
}
}
}



Clearly I have it set to buy and sell in orders of 2. Why did it sell two single orders of 1 contract, THEN close the position and then enter a short position of 2 like the strategy says. I am so confused here.
olingerc is offline  
Reply With Quote
Old 04-21-2011, 02:01 PM   #6
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

olinger,

Please see my reply in the new thread you created for this. If you have any further questions, reply back to the new one.
NinjaTrader_RyanM 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
Automatically enter position on Market Open, Close position on Market Close kevendickenson SuperDOM and other Order Entry Windows 1 04-02-2010 04:30 AM
stop position from code ATI user Version 7 Beta General Questions & Bug Reports 2 03-15-2010 04:28 PM
position close on close of bar mballagan Miscellaneous Support 3 01-14-2010 10:11 AM
Position Sizing Custom Code Anagoge Strategy Development 7 05-19-2009 06:55 PM
close position Jesse Automated Trading 1 12-15-2008 10:52 AM


All times are GMT -6. The time now is 09:06 AM.