PDA

View Full Version : Exiting Position Impossible?


Oli
04-02-2007, 07:20 PM
I have spent a lot of time using

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

To get some semblance of real (ie actual) portfolio level tracking into NT. Now I am at another barrier.

I believe that Position.MarketPosition is used internally by NinjaScript to decide if there is or is not a position to "Exit" when using ExitLong, for example.

This mean that although my code is now much more successful at traking the portfolio, all of the Exit commands are not working because internally NinjaScript thinks there is no position when in fact there is.

How can I use "Exit" commands when

Position.MarketPosition == MarketPosition.Flat

But in fact a position exists?

NinjaTrader_Dierk
04-02-2007, 09:05 PM
You likely are confusing strategy position and account position.

Strategy position = position managed by a strategy.
Account position = actual position on an account.

The strategy only processes exit orders as the strategy position is not flat, meaning a strategy will not process exit orders as the account position is not flat but the strategy position is flat.

Please consult the docs on how to sync your account position with your strategy position: Options->Strategies tab

Oli
04-02-2007, 09:30 PM
I cannot understand your reply Dierk...

I believe that the account is not flat, but the strategy is flat.

So my original question still stands, when the Strategy IS flat, ie Position.MarketPosition == MarketPosition.Flat

But the Account is NOT flat

How can I get Exit commands to actually execute? <-That is the question.

NinjaTrader_Dierk
04-02-2007, 09:49 PM
So strategy position is flat but account position is not flat? This is the scenario described by my post, where the strategy will not process exit orders.

-> You can not exit the (non-flat) account position if the strategy position is flat.

Oli
04-02-2007, 10:02 PM
Dierk Droth wrote: -> You can not exit the (non-flat) account position if the strategy position is flat.


I hope you realise what this means for automated trading systems...

So I have gone to the trouble to code a whole work around to the fact that Strategy.Position is blind with respect to Account.Position, and now it turns out that this is useless because no EXIT commands work... highly ironic.

Is there anyway to set the value of Position.MarketPosition? Can I change its value? <- question

NinjaTrader_Dierk
04-02-2007, 10:09 PM
Sure exit commands work. Just enter a position with your strategy and then exit it with your strategy, no problem.

I again recommend you check out the docs to understand the concept of syncing to your just started strategy to a live account position.

Here is another sample: You can running multiple strategies on the same account. All strategies manage their own (strategy) position. However, all strategy positions result in one (!) real account position.

-> The scenario where real account position does not match (virtual) strategy position is very likely. That's why we provided the options to sync both.

Oli
04-02-2007, 10:17 PM
Dierk, I don't think you are getting the point.

1) For what ever reason a Strategy is interuptted and Restarted. The Strategy is now flat but the account is NOT flat. This means that the last thing that the Strategy did BEFORE the interuption was anENTER command. That is why the Account position is not flat.

2) This restarted Strategy CANNOT enter because it already has an account position. The next thing it will have to do is EXIT. There is no need or posibility of ENTER here.

3) However it cannot exit because Strategy is FLAT and ALL EXIT COMMANDS ARE IGNORED.

4) I need to be able to use NinjaScript to set the Value of Position.MarketPosition. I want to tell the Strategy = you are not FLAT. How can I do this? <- question (again)

Oli
04-02-2007, 10:25 PM
Dierk Droth wrote:
-> The scenario where real account position does not match (virtual) strategy position is very likely. That's why we provided the options to sync both.

I am unable to locate documentation relating to this, can you please tell me where I can find it? Thanks...

NinjaTrader_Dierk
04-02-2007, 10:26 PM
Dierk Droth wrote:Please consult the docs on how to sync your account position with your strategy position: Options->Strategies tab

Oli
04-02-2007, 10:28 PM
Screen 1

Oli
04-02-2007, 10:29 PM
Screen 2

Oli
04-02-2007, 10:30 PM
I can see no options that have anything to do with "syncing" Account.Position with Strategy.Position...

Please answer the following question: Can I manipulate the value of Position.MarketPosition?

NinjaTrader_Dierk
04-02-2007, 10:40 PM
No you can not, since there is no need to.

Here is what happens as you start a strategy e.g. on chart:
- strategy is executed on the historical part of the data series (data in the past)
- let's say it ends up with a historical strategy position
- as the strategy now runs on realtime data, it continues on executing orders based on the strategy position which was build as the strategy ran on historical data.

-> strategy works consistent to it's historical position. No need to "fake" a strategy position. More so it could even throw off the strategy logic as you try to "fake" a position.

Of course, there is an issue where the strategy historical position does not match the real account position (multiple strategies running on the same account, or strategy logic just "moved on" and no longer reflects real account position as you restart the strategy while later). In that event options below - which you posted for our convenience - are available.

Hope this explains the concept.

Oli
04-02-2007, 10:51 PM
Unfortunately it explains nothing. And thanks for the sarcasm: just what a user needs from support...

What I have written here explains my situation, nothing you have replied provides a solution.

Oli wrote:

1) For what ever reason a Strategy is interuptted and Restarted. The Strategy is now flat but the account is NOT flat. This means that the last thing that the Strategy did BEFORE the interuption was anENTER command. That is why the Account position is not flat.

2) This restarted Strategy CANNOT enter because it already has an account position. The next thing it will have to do is EXIT. There is no need or posibility of ENTER here.

3) However it cannot exit because Strategy is FLAT and ALL EXIT COMMANDS ARE IGNORED.

4) I need to be able to use NinjaScript to set the Value of Position.MarketPosition. I want to tell the Strategy = you are not FLAT. How can I do this? <- question (again)

NinjaTrader_Ray
04-03-2007, 01:07 AM
Oli,

We currently do not provide support for exiting an account position.Somewhere in a previous post I had mentioned that you had to manualy enter orders to synca strategy position to an account position. Some users may want to do this, others may not since there is no one to one relationship between a strategy and account position.

Ray

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

After further investigation, it seems that there has been a misunderstanding. I will post here shortly a method that will allow you to sync your account to your strategy position.

Ray

Oli
04-03-2007, 01:29 AM
Ray,

surely there is a way to override the mechanism which prevents Exit commands from functioning when the Strategy is flat? I can't believe that there is not... that's all I need, a way to use the Exit commands when Position.MarketPosition is flat.


Just saw your below.... will wait

NinjaTrader_Ray
04-03-2007, 01:48 AM
Oli,

I have decided to provide a new methodthat will execute orders independant of the NinjaScript Enter() and Exit() internal logic. This will be availabe in the next release likely sometime next week.

The intent of this is -

- To sync an Account.Position --> Strategy. Position (you should never try to go the other way, syncing Strategy to Account position)
- Will only work in real-time processing, not Historical

Ray

Oli
04-03-2007, 02:10 AM
Ray that sounds good.

I have coded my scripts so that as soon as the position goes flat, they revert back to using the internal Strategy controls. What this means is that

1) a strategy is restarted and Account is not flat but Strategy is flat.

2) This is fine because the script knows that the account is not flat and ignores the Strategy positions at first.

3) then an Exit condition occurs and it needs to exit, even though the Strategy still thinks it is flat (THIS is where I hope your new method comes to the rescue (and I'll happily beta-test it for you :P)

4) Because the account now really is flat, Strategy==Account and Strategy level Position logic works fine and everything is back in order...

Please keep me updated,

Thanks,

NinjaTrader_Ray
04-03-2007, 02:21 AM
Oli,

So basically, your idea is:

- If the account is not flat but the strategy is flat, wait until the strategy generates an exit condition and at that time, close out the account position, then both account and strategy are flat?

What do want to do here:

- Account flat, strategy long

And here:

- Account long 1, strategy long 2

We are thinking about a Synchronization() method that can be called and would provide internal handling so the sync order generated is processed to ensure that its filled or if rejected, provide additional handling etc...

Oli
04-03-2007, 02:44 AM
Ray,

I do not want something that issues buy/sell orders outside of my Strategy logic.

This situation where Strategy != Account is ok for me, as long as I can EXIT when when ever I want (ie even when Strategy is flat).

I have found the Strategy working on Historical Data feature to be a very large hassle and so have all Enter/Exit commands inside a {if (!Historical)}. I'll breifly explain here why the Strategy working off Historical data is a not such a good idea. Let's say Event X happens and the Strategy is not working. The Strategy is restarted, it goes back through the Historical data and decides that it is Long. Problem is Strategy wasn't working when this signal came along and so Account is Flat. Then the "Strategy" Position is just some fantasy, more like a back test. The only thing that matters is that Account is Flat, not that the Strategy "should" be long.

I suggest you dump the Historical Strategy stuff and simply have the Strategy align itself to the Account every time the Strategy is started (ie as part of Initialize()). Then Strategy.Position == Account.Position right from the start and alwaysand all these problems disappear. More than this it is a great feature in that all you have to do to re-align Strategy and Account is refresh the Strategy. In fact if you would allow me to programatically alter the logic state of Position.MarketPosition I could reset it already myself...

I also strongly suggest adding Order Status logic as well. A long is only a long when order=filled. But between submitting -> filled Account should not report itself as Flat. It is Pending. So really there are FOUR, not THREE, Position states (long, short, flat, pending)

NinjaTrader_Ray
04-03-2007, 02:58 AM
Oli,

With all due respect, if you find our model a joke, then why try and squeeze a square peg into a round hole? Although I am happy to work with you and support you on our model, it sounds like you need a lower level programming environment?

Syncing a strategy to an account position at start up is just not valid. One can have many strategiesrunning concurrently including manual trades. You can not always assume that an account position should always equal a strategy position.

Ray

Oli
04-03-2007, 03:03 AM
Ray,

absolutely no disrespect intended there. I have written large chuncks of highly intense systems using yourapplication and am on here every working day working through my technical issues with you guys so that speaks a lot :dude:

I don't follow your logic at all. Even if I have 10000000 strategies all running at the same time a pointing at the same instrument they all need to know only one thing: what is the current account position. I have not idea why you think this it istrue that Strategies need their own Position layer. Can you give a simple example?

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

- I have a SuperDOM and I have taken amanualposition long in the ES for 2 contracts, my account position is 2 long.

- I run a strategy on the ES that says I am flat.

In the above situation, if you were to assume that the strategy position should match the account position, the strategy would then sell the 2 long to put the account flat.


---> Problem. My manual trade was just closed and I did not want this to happen.

If this example is not clear enough then we will neveragree so lets just agree to disagree and move on.

Ray

Oli
04-03-2007, 03:35 AM
I agree with you - I just would never enter trades that go against or outside my system. What you are saying is that this person has entered a trade that possibly conflicts with a running active strategy which no sane person would ever do. In that situation you would have to simply turn the strategy off. The alternative is the current set up which I think creates way more problems than it solves.

But I also agree - lets move on.

What I need is very simple. I need something, a switch or simply on option that allows me to submit Exit orders when Strategy.Position is Flat. It is that simple. Something like FlattenLimit(). This submits the order to the Account regardless of Position.MarketPosition.

Oli
04-03-2007, 05:16 AM
Ray,

do you think that some form of workaround for my problem is possible?

Thanks for your time...

NinjaTrader_Ray
04-03-2007, 05:48 AM
Yes, next release.

funk101
04-05-2007, 03:57 AM
Ok, I've read this thread. I'm wondering if my problem is the same. I'm working on my strategy, and everytime I F5 I get back trades. I see that the unrealized PnL is still calculating, yet I can't see an open order, nor is the position tab showing any trades. Yet, I'm seeing TraceOrders spit out data as if I was in a trade. How can I stop this? Is there a setting, or do I have to hack something, or wait for next realease?

Thanks.

NinjaTrader_Ray
04-05-2007, 04:08 AM
Not the same.

When you apply a strategy, it processes on your historical data set and you are either flat, long or short.

If the strategy is not flat, then it holds a virtual position. You can then manually adjusts your account if you want to have the account in sycn with your strategy position.

There are options that control how the strategy transitions from a historical state to a live state. See the Help Guide Options > Strategies section. In the application, see Tools > Options > Strategies > NinjaScript.

Ray