class MyStrategy : Strategy {
private MarketPosition priorMarketPosition;
private MarketPosition currentMarketPosition;
protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
int quantity, MarketPosition marketPosition)
{
//
// If we just opened a position, take appropriate action
//
this.priorMarketPosition = this.currentMarketPosition;
this.currentMarketPosition = position.MarketPosition;
if (this.currentMarketPosition != MarketPosition.Flat && this.priorMarketPosition == MarketPosition.Flat) {
takeAppropriateAction();
}
}
private void takeAppropriateAction() {
// do something here
}
}
Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
How to determine when a position was just opened
Collapse
X
-
How to determine when a position was just opened
I need to know when we go from MarketPosition.Flat to some other MarketPosition (Long or Short), ie I need to know the moment when we open a position.To do that I use this code:
PHP Code:
Its best practice to only work with the passed by value parameters and not reference parameters. This insures that you process each change of the underlying state.Tags: None
-
Hello westofpluto,
Thanks for your question.
The strategy Position object is updated in Execution events and will be updated as early as OnExecutionUpdate. (Account positions like PositionAccount would be updated on PositionUpdate events and would be updated as early as OnPositionUpdate.) It is not necessary to manage your own Position object and the Position object can be referenced anywhere in your script.
Position - https://ninjatrader.com/de/support/h.../?position.htm
OnExecutionUpdate - https://ninjatrader.com/support/help...tionupdate.htm
We look forward to assisting.
JimNinjaTrader Customer Service
-
Jim, you totally missed the point of my question. I know how to get access to my current market position. What I need to know is the actual event, the moment when we enter a position. At the time at which my strategy enters a position, I need to set some variables in my strategy. Please re-read my question carefully, and please take another stab at answering. Thanks.
Comment
-
Hello westofpluto,
I had informed you that the strategy position is updated in OnExecutionUpdate.
Use OnExecutionUpdate to identify when the strategy position has changed, at the time at which your strategy enters or exits a position.
Please clarify on withstanding issues if checking for a strategy position change here is not doing what you expect.
JimNinjaTrader Customer Service
Comment
-
Ok, so in OnExecutionUpdate, you are saying it is possible to determine that the strategy just opened a new position. I assume that we can see this by checking the marketPosition value passed in?
But the documentation says that marketPosition in this context can only be MarketPosition.Long or MarketPosition.Short. If I also wantto know when we havce closed a position, how will I know that in OnExecutionUpdate?
Please explain.
Comment
-
Hello westofpluto,
In OnExecutionUpdate, you can check the strategy's Position object and check the MarketPosition here to see if you have entered a position I.E. if (Position.MarketPosition == MarketPosition.Long) or if (Position.MarketPosition == MarketPosition.Short)
To see if you have exited a position, you can check if the Strategy's Position object has a MarketPosition of flat. (Position.MarketPosition == MarketPosition.Flat)
The marketPosition argument in OnExecutionUpdate lets us know if the the execution was a buy (Long operation) or a sell (Short operation)
Please note that if you are scaling into an existing position, checking if (Position.MarketPosition == MarketPosition.Long) in OnExecutionUpdate would return as true and would not identify that you have entered a new position. You could create a class level Position object for the "LastSeenPosition" and check if (LastSeenPosition.MarketPosition == MarketPosition.Flat && (Position.MarketPosition == MarketPosition.Long || Position.MarketPosition == MarketPosition.Short)) to identify when a new position is entered for a strategy that scales into an existing position.
For example:
Code:private Position LastSeenPosition; protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity, Cbi.MarketPosition marketPosition, string orderId, DateTime time) { if (LastSeenPosition != null && LastSeenPosition.MarketPosition == MarketPosition.Flat && (Position.MarketPosition == MarketPosition.Long || Position.MarketPosition == MarketPosition.Short)) Print("New Position Entered."); else if (LastSeenPosition == null) Print("New Position Entered."); LastSeenPosition = Position; }
JimNinjaTrader Customer Service
Comment
-
Thanks, Jim, this makes sense except for this part:
PHP Code:else if (LastSeenPosition == null)
Print("New Position Entered.");
Comment
-
Hello westofpluto,
LastSeenPosition is our own property and would be initialized as null. We assign LastSeenPosition at the end of OnExecutionUpdate where it then is no longer null, so the first time that it is checked in the beginning of OnExecutionUpdate, it would be null. This would be the first execution made by the strategy which would open a strategy position.
The strategy's Position object itself should never be null.
Let us know if there is anything else we can do to help.JimNinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by algospoke, Yesterday, 06:40 PM
|
2 responses
24 views
0 likes
|
Last Post
by algospoke
Today, 07:04 PM
|
||
Started by ghoul, Today, 06:02 PM
|
3 responses
15 views
0 likes
|
Last Post Today, 06:43 PM | ||
Started by jeronymite, 04-12-2024, 04:26 PM
|
3 responses
46 views
0 likes
|
Last Post
by jeronymite
Yesterday, 10:10 PM
|
||
Started by Barry Milan, Yesterday, 10:35 PM
|
7 responses
23 views
0 likes
|
Last Post Today, 06:16 PM | ||
Started by AttiM, 02-14-2024, 05:20 PM
|
10 responses
181 views
0 likes
|
Last Post
by jeronymite
Today, 06:11 PM
|
Comment