string myPosition = Position.MarketPosition == MarketPosition[3] == "Long" ? "Long" : (Whatever);
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
how to know x bars ago market position ?
Collapse
X
-
Hello fercho,
Thanks for your post.
There are no documented/supported methods for checking if you were in a long position 3 bars ago. This would require custom calculations.
You could consider setting creating a Series<bool> variable named something like 'longPos', setting it to true when Position.MarketPosition == MarketPosition.Long, and setting the Series<bool> variable to false when you are not in a long position. Then you could check if longPos[3] is true to see if you are in a long position 3 bars ago.
See the help guide documentation below for more information.
Series<T>: https://ninjatrader.com/support/help...t8/seriest.htm
Position.MarketPosition: https://ninjatrader.com/support/help...etposition.htm<span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>
-
Hi Brandom, thanks for your reply, asked also to ChatGPT and answered:
// Define a variable to store the market position three bars ago
private MarketPosition positionThreeBarsAgo;
protected override void OnBarUpdate()
{
// Check if there are enough bars to access the position three bars ago
if (CurrentBar > 2)
{
// Retrieve the market position three bars ago
positionThreeBarsAgo = Position.GetAt(CurrentBar - 3).MarketPosition;
// Check if the position three bars ago was "Long"
if (positionThreeBarsAgo == MarketPosition.Long)
{
// Your custom logic for when the position was "Long" three bars ago
Print("Position was Long three bars ago");
}
}
}
proble is that GetAt is not recognized by NinjaTrader,
by the way your answer is sort of what I'm currently using thanks
Comment
-
Hello fercho,
Thanks for your post.
GetAt() is a non-existent NinjaScript method.
From our experience at this time, ChatGPT is not quite adequate to generate valid compilable NinjaScripts that function as the user has intended. We often find that the generated code will call non-existent properties and methods, use improper classes or inheritance, and may have incorrect logic. We highly encourage that you create a new NinjaScript yourself using the NinjaScript Editor, and use the code generated by ChatGPT as more as suggestions and guides when coding the script yourself, than using the actual code generated.
While It would not be within our support model to correct these scripts at user request, we would be happy to provide insight for any direct specific inquiries you may have if you would like to create this script yourself. Our support is able to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior.
You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.<span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>
- Likes 1
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by gyilaoliver, Today, 08:28 AM
|
3 responses
12 views
0 likes
|
Last Post
|
||
Started by Darkslide_Tom, 03-23-2025, 11:08 PM
|
3 responses
17 views
0 likes
|
Last Post
|
||
Started by rtwave, 03-13-2025, 04:09 PM
|
4 responses
29 views
0 likes
|
Last Post
|
||
Started by MiCe1999, 12-01-2024, 09:01 PM
|
4 responses
45 views
0 likes
|
Last Post
![]() |
||
Started by coopgrafik, Today, 07:57 AM
|
1 response
7 views
0 likes
|
Last Post
|
Comment