PDA

View Full Version : DLL position limitations


Oli
03-26-2007, 08:56 PM
The way functions such as NTMarketPosition fail to work when NT is closed and reopened (overnight) means that I cannot trade from eSignal using NT.

I must therefore consider a move to internal NT scripting.

Do the NT internal functions such as Position.MarketPosition persist if NT is closed and reopened?

NinjaTrader_Dierk
03-26-2007, 09:36 PM
No they do not. Please read more here: http://ninjatrader.mywowbb.com/view_topic.php?id=1641&forum_id=3&highlight=ntmark etposition

Oli
03-26-2007, 10:29 PM
That's actually one of my posts, so I have read it and it still provides no solution.

I have complex automated strategies that must be able to determine if a position exist, what its size is.

In restart/reboot situations I can be in a position which I want to stay in, not exit. The program needs to know that (obviously). So when initialising the Stratgy needs to check long/short/flat and it must persist across all time scales and system situations. The idea that this information is not available means that NTs capabailities to handle Automated Trading are pure fantasy.

Please clarify that what you are saying is that current portfolio positions are only available so long as NT is continuously running.

Thanks

NinjaTrader_Dierk
03-26-2007, 10:31 PM
Your understanding is correct.

Oli
03-26-2007, 10:37 PM
LMAO

I suggest you remove all references to Automated Systems Trading from your documentation and website.

The only thing NinjaTrader can to is put trades on or off. It cannot, even to the slightest degree, manage open positions because it has no idea what those open positions are. Highly disappointing.

NinjaTrader_Dierk
03-26-2007, 10:42 PM
You may consider using the file based ATI. Due to the nature if information trasnfer through files it may do what you are looking for.

NinjaTrader_Ray
03-27-2007, 01:17 AM
Oli,

As you now know, the DLL interface has a limitation regarding positions between sessions. What others do that require overnight handling is use the File interface information update files:

http://www.ninjatrader-support.com/HelpGuide/helpguide.html

to retreive position information.

Ray

Oli
03-28-2007, 02:29 AM
was my reply removed for a reason?

shawnj
03-28-2007, 02:48 AM
Surely with NinjaScript, Interactive Brokers or PATSystems can be querried for current position status which can then be accessed by code. Right?

This whole discussion is just concening the DLL interface. Right?

Oli
03-28-2007, 03:02 AM
No it is not.

Oli
03-28-2007, 03:03 AM
see the reply from Herr Droth below - we are talking NinjaScript functionality

NinjaTrader_Ray
03-28-2007, 03:08 AM
For complete clarity:

We are only referring to the function call NTMarketPosition() in the DLL interface. The limitation is that the position size returned between NT sessions is not persisted. As a workaround, one can access the File based interface to retrieve current position size. This is when using external application to feed orders through NinjaTrader's Automated Trading Interface.

Shawnj, you are correct. NinjaScript provides access to the current position held by your strategy or your account. NinjaTrader maintains position information from all brokers we connect to.

Ray

Oli
03-28-2007, 03:15 AM
Ray,

I wish you had said this earlier - Dierk Droth has made a grave error here. Please read my first post:

"I must therefore consider a move to internal NT scripting.

Do the NT internal functions such as Position.MarketPosition persist if NT is closed and reopened?"

I mean "intenal NT scripting" is clear and "Position.MarketPosition" is a NinjaScript function!

You are now saying unequivacbly "yes" to this, while the first response was no (admittedly not from yourself).

I believe there is some terrible miscommunication here. I thought I was clearly understanding what was being said, for any part of mine in the misunderstaning I apologize but in my defense I think that my original post has been badly misunderstood.

NinjaTrader_Ray
03-28-2007, 03:24 AM
Oli,

I will post what I sent you in private message.

NinjaScript strategies hold the strategy position size. If you disconnect/shutdown NT, the strategy will terminate (since you are no longer connected) and upon reconnect you will have to re-apply your strategy to the chart. NT will then historically calculate the current virtual strategy position size and optionally submit orders to protect this position. Your live account position may or may not match the reported strategy position. Because of this, you must manually synchronize the two if this makes sense.

It may not make sense since you may have multiple strategies that you are running and/or you may be manually trading in which case the strategy position may never need to sync with the real account position since there is not a one to one relationship.

Most products, such as TradeStation will also get out of sync but our approach is by design since we provide complete flexibility on trading both in automation and manually on the same market in the same account simultaneously.

Ray

Oli
03-28-2007, 03:50 AM
Ray,

so Position.MarketPosition from NinjaScript will always return at least a correct value for long (>0), short (<0), flat(0)? This is regardless of if I shut NT down, have the power go out mid session or anything else that interupts the running of the program?

NinjaTrader_Ray
03-28-2007, 04:20 AM
Oli,

It will return the correct virtual position of the strategy, not your netaccount position. These are two completely different things.

Correct account position is returned by:

Cbi.Position myPosition = Account.Positions.FindByInstrument(Instrument instrument);
if (myPosition != null)
// There is a broker position

Ray

zoltran
03-28-2007, 06:41 AM
Sorry to chime in ....
Ray

Does Account.Position refer to only the instrument the strategy is running against, or the account as a whole?

Reason I'm asking, is that there are some functions, such as 'Flatten All' that refer to the whole account.

NinjaTrader_Ray
03-28-2007, 06:58 AM
No problem.

My mistake actually. What we have in the layer under NinjaScript (which is not officially supported via documentation nor tech support) is

Account.Positions.FindByInstrument(Instrument instrument)

so, if you wanted to determine if an accountposition existed for the given instrument you would:

Cbi.Position myPosition = Account.Positions.FindByInstrument(Instrument instrument);
if (myPosition != null)
// There is a position

For clarification, account level propertiesare not supported in the NinjaScript layer although it is exposed for programmer who want to access it. We are just not in a position to provide the support you would expect with these lower level methods and properties.

The above references the account as a whole.

Ray

shawnj
03-29-2007, 02:24 AM
Ray, perhaps you could give some example generic code, sort of a code temple, for writing a robust/fault tolerent automated strategy. Weneed to handle, in an unambiguous way, the following senerios:

Senerio 1) Loose broker order connection and/or data connection.

I'm guessing two events need to be handled:

a) Lost connection event

I'm guessing here the best we can do is just display a dialog box or maybe email telling us we lost the connection.

b) Connection Reestablished event

Here it seems like we needour code to resond to this event which will querry the broker and resync our code with what the broker says is our working positions and the orders pending in the exchanges/brokers computes.



Senerio 2) NinjaTrader shutdown/restart.

a) Shutdown event.

Seems like not much we can do here. Assuming a crash or power failure.

b) NinjaTrader restart event.

Perhaps in the initalization code we could check the broker and do basically 1b above. I guess there are complicating factors such as if multiple stategies are running we need to parse out the brokers reply to each particular running strategy.

Oli
03-29-2007, 04:53 AM
Cbi.Position myPosition = Account.Positions.FindByInstrument(Instrument instrument);
if (myPosition != null)
// There is a broker position

I am missing the syntax to make this statement work!

NinjaTrader.Cbi is loaded with using. Cbi.Position is?

myPosition - user defined variable, type? bool?

Account.Positions.FindByInstrument("requires input here")

(Instrument instrument) to me means that there is a type "Instrument"?

I have no idea how to get this to work.

Needs something like inst = get.Instrument.

No idea...

NinjaTrader_Ray
03-29-2007, 05:03 AM
Oli,

Create a strategy and insert the following code:

Cbi.Position myPosition = Account.Positions.FindByInstrument(Instrument);
if (myPosition != null)
Print(myPosition.Quantity);
else
Print("There is no position");



Run on a chart and view the output window.

Ray

Oli
03-29-2007, 09:48 PM
trying to get this to work today... ignore previous post here...

Oli
03-29-2007, 10:14 PM
OK - this code works, but Quantity alone is not sufficient. I need something like: Side, ie 1S or 1L. It is not enough to know that the position is open.

Are there extensions to Account.Positions other than Quantity?

NinjaTrader_Dierk
03-29-2007, 10:29 PM
Please check out the myPosition.MarketPosition property which indicates the market side.

Oli
03-29-2007, 11:16 PM
I seem to get weird results when using MarketPosition as a property of myPosition:





Cbi.Position myPosition = Account.Positions.FindByInstrument(Instrument);

if (myPosition != null){

Print(myPosition.Quantity);

Print(myPosition.MarketPosition);

}

else{

Print("There is no position");

Print(myPosition);

}

NinjaTrader_Dierk
03-29-2007, 11:25 PM
Please consult the docs: Print() accepts string, double, int and bool values but not type NinjaTrader.Data.MarketPosition nor NinjaTrader.Cbi.Position. You need to convert the information you want to print out to string or double or int or bool values.

Please try e.g.
Print ((int)myPosition.MarketPosition);
Print(myPosition.ToString());

Oli
03-29-2007, 11:39 PM
Thanks Dierk,

the solution for those in need:

XX = myPosition.MarketPosition.ToString();

Dierk, is there any way you could post here what other Properties would be available to myPosition in the current context?

NinjaTrader_Dierk
03-29-2007, 11:44 PM
Real simple: Just start typing "myPosition." in NinjaScript editor and IntelliSense window pops up and shows you what's available (including documentation). This is true for any NinjaScript object.

Also: please make sure you are on latest NT6B10.

Oli
03-30-2007, 12:01 AM
Got it - thanks