![]() |
This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com
|
|||||||
| 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. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Junior Member
Join Date: Aug 2011
Posts: 9
Thanks: 2
Thanked 0 times in 0 posts
|
Im trying to write a code that says, if a current market position is open, do not create a contract. So the code has to wait and check till the old contract finishes, then enters a new one.
I tried the following..."doesnt work) I also tried using just if (Position.MarketPosition == MarketPosition.Long) Still doesnt work.,....on a 1min chart, the code keeps buying multiple contracts...and you can see it increase....2,3.....4... I want to make sure it enters only one at a time... if (Position.MarketPosition == MarketPosition.Long || Position.MarketPosition) { if (MACD(12, 26, 9)[0] > MACD(12, 26, 9)[1] && MACD(12, 26, 9).Avg[0] > MACD(12, 26, 9).Avg[1]) { AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, GetAtmStrategyUniqueId(), "myfill", GetAtmStrategyUniqueId()); } } regards, |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,421
Thanks: 252
Thanked 982 times in 964 posts
|
Hi Dave, since you're working with the AtmStrategy methods here you would need to use the related GetAtmStrategyMarketPosition() as well for those checks -
http://www.ninjatrader.com/support/h...etposition.htm A complete workking example of those methods combined can be reviewed via the 'SampleAtmStrategy' script installed with NT per default.
Bertrand
NinjaTrader Customer Service |
|
|
|
|
The following user says thank you to NinjaTrader_Bertrand for this post: |
|
|
|
#3 |
|
Junior Member
Join Date: Aug 2011
Posts: 9
Thanks: 2
Thanked 0 times in 0 posts
|
Ive tried many ways.....strategy still keeps entering orders even when there is an existing open position.
if (GetAtmStrategyMarketPosition("myfill") == MarketPosition.Long || GetAtmStrategyMarketPosition("myfill") == MarketPosition.Short) {dont enter} if (GetAtmStrategyMarketPosition("myfill") != (MarketPosition.Long | MarketPosition.Short)) {enter} no matter what i do.... != == ||....doesnt work....strategy still enters market multiple times. I even tried MarketPosition.Flat (assuming Flat means not long or short) Full Code: if (GetAtmStrategyMarketPosition("myfill") == MarketPosition.Long || GetAtmStrategyMarketPosition("myfill") == MarketPosition.Short) { } else { if (MACD(12, 26, 9)[0] > MACD(12, 26, 9)[1] && MACD(12, 26, 9).Avg[0] > MACD(12, 26, 9).Avg[1]) { AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, GetAtmStrategyUniqueId(), "myfill", GetAtmStrategyUniqueId()); } // Condition set 2 if (MACD(12, 26, 9)[0] < MACD(12, 26, 9)[1] && MACD(12, 26, 9).Avg[0] < MACD(12, 26, 9).Avg[1]) { AtmStrategyCreate(OrderAction.Sell, OrderType.Market, 0, 0, TimeInForce.Day, GetAtmStrategyUniqueId(), "myfill", GetAtmStrategyUniqueId()); } } |
|
|
|
|
|
#4 |
|
Junior Member
Join Date: Aug 2011
Posts: 9
Thanks: 2
Thanked 0 times in 0 posts
|
This seemed to work....messy code...but works
if (MACD(12, 26, 9)[0] > MACD(12, 26, 9)[1] && MACD(12, 26, 9).Avg[0] > MACD(12, 26, 9).Avg[1] && GetAtmStrategyMarketPosition("myfill") == MarketPosition.Flat) { AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, GetAtmStrategyUniqueId(), "myfill", GetAtmStrategyUniqueId()); } // Condition set 2 if (MACD(12, 26, 9)[0] < MACD(12, 26, 9)[1] && MACD(12, 26, 9).Avg[0] < MACD(12, 26, 9).Avg[1] && GetAtmStrategyMarketPosition("myfill") == MarketPosition.Flat) { AtmStrategyCreate(OrderAction.Sell, OrderType.Market, 0, 0, TimeInForce.Day, GetAtmStrategyUniqueId(), "myfill", GetAtmStrategyUniqueId()); } |
|
|
|
|
|
#5 | |
|
Junior Member
Join Date: Aug 2011
Posts: 9
Thanks: 2
Thanked 0 times in 0 posts
|
Quote:
No does not work....just looked at it....entered 2 contracts after a couple of minutes
|
|
|
|
|
|
|
#6 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,421
Thanks: 252
Thanked 982 times in 964 posts
|
Dave, please check into the example we ship with NT, namely the 'SampleAtmStrategy' - it would show how to setup the general structure, which would include checks and reset for the order and atmStrategyId.
Bertrand
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Position.MarketPosition and subtraction | figos | General Programming | 5 | 12-30-2010 09:15 AM |
| Position.MarketPosition | AnotherTrader | Automated Trading | 1 | 03-17-2010 01:45 PM |
| Position.MarketPosition | ATI user | Version 7 Beta General Questions & Bug Reports | 5 | 03-09-2010 04:16 PM |
| position.marketposition | mas_61 | General Programming | 1 | 10-26-2008 07:48 AM |
| Position.MarketPosition | Oli | Suggestions And Feedback | 5 | 04-03-2007 01:15 AM |