NinjaTrader Support Forum  
X

Attention!

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


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 09-04-2011, 11:30 PM   #1
davedxb
Junior Member
 
Join Date: Aug 2011
Posts: 9
Thanks: 2
Thanked 0 times in 0 posts
Default Position.MarketPosition == MarketPosition.Long (doesnt work)

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,
davedxb is offline  
Reply With Quote
Old 09-05-2011, 04:33 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,421
Thanks: 252
Thanked 982 times in 964 posts
Default

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.
NinjaTrader_Bertrand is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Bertrand for this post:
Old 09-06-2011, 02:03 AM   #3
davedxb
Junior Member
 
Join Date: Aug 2011
Posts: 9
Thanks: 2
Thanked 0 times in 0 posts
Default

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());
}
}
davedxb is offline  
Reply With Quote
Old 09-06-2011, 02:17 AM   #4
davedxb
Junior Member
 
Join Date: Aug 2011
Posts: 9
Thanks: 2
Thanked 0 times in 0 posts
Default

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());
}
davedxb is offline  
Reply With Quote
Old 09-06-2011, 03:06 AM   #5
davedxb
Junior Member
 
Join Date: Aug 2011
Posts: 9
Thanks: 2
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by davedxb View Post
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());
}

No does not work....just looked at it....entered 2 contracts after a couple of minutes
davedxb is offline  
Reply With Quote
Old 09-06-2011, 08:59 AM   #6
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,421
Thanks: 252
Thanked 982 times in 964 posts
Default

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.
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
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


All times are GMT -6. The time now is 06:36 PM.