How to exit all position at the given time?
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
Exit all position on the given time.
Collapse
X
-
Hello Czarek,
Are you attempting to close all positions for all accounts on all instruments? (all positions)
Or are you wanting to close the singular position of the strategy? (you have posted in the strategy development section of the forums)
To close the position of the strategy at a specific time in OnBarUpdate():
if (ToTime(Time[0]) == 90000)
{
if (Position.MarketPosition == MarketPosition.Long)
ExitLong();
if (Position.MarketPosition == MarketPosition.Short)
ExitShort();
}
This would close an open position at 9:00 AM .
Below are public links to the help guide on Time[0], ToTime(), Position, MarketPosition, ExitLong(), and ExitShort().
Also, below is a public link to a forum post with helpful information about getting started with NinjaScript.
Chelsea B.NinjaTrader Customer Service
-
Hello,
If I want to close position in Strategy Analyzer 1 hour before session closes - how to accomplish it programmatically?
I tried updating Property "ExitOnSessionCloseSeconds" to 3600 - but it doesn't seem to work in Strategy Analyzer..
Note, sometimes there are holidays (so sessions are shorter than usual) - I want to accomplish these as well (ideally I want to know programmatically whether that current session has different trading hours than regular one...)
Thank you
Comment
-
Hello music_p13,
You are correct, ExitOnSessionCloseSeconds is a real-time only property, it will not have any effect on your ExitOnSessionClose time in backtesting processing historical data.
You can use a session iterator to get the time of the session close. The PreventEntryAfterExitOnCloseExample is a good example of getting the time of the session close. (It prevents new entries after the session close until the new session)
NinjaTrader Community, A common inquiry is that the Exit on close didn't work in a NinjaScript Strategy because there is a position after the exit on close should have occurred. When viewing the log we often find that the Exit on close does indeed exit the position shortly before the end of the session (based on the Exit on
The current session's close time can be compared to the <tradingHours>.Sessions collection.
You would likely need to loop through each day and compare that to this sessions close time .DayOfWeek to know that its the session for today.Chelsea B.NinjaTrader Customer Service
- Likes 1
Comment
-
Hi there,
I am trying to close orders at exactly 23:55:00. code:
Code:if (ExitOnDayClose && Time[0].Hour == 23 && Time[0].Minute == 55) { CloseAllTrades(); }
- M1 timeframe - bot close order at 23:54.
- M5 timeframe - bot close order at 23:50.
- M30 timeframe - bot close order at 23:30.
- H1 timeframe - bot close order at 23:00.
- etc..
Waiting for your reply. Thanks
Comment
-
Hello Revazi123,
For OnBarUpdate() to run before the bar closes, Calculate must be OnPriceChange or OnEachTick.
If you want the time of the current tick, in either real-time or historical, add a 1 tick series.
Or get the latest market update in real-time (and with TickReplay in historical) in OnMarketData().
Chelsea B.NinjaTrader Customer Service
Comment
-
Hello ChelseaB,
I am using Prints to check what is happening and this new version what I have coded(already tried that but as you suggested I tried it again as maybe I missed something) with OnMarketData() also with 1 tick series but strategy doing same thing its close orders at close of previous candle.
Condition is that I want to close trades at exactly 23:55 and this function shouldn't be depend on timeframe.
I really don't understand why its doing it like this again.
Comment
-
Hello Revazi123,
Thank you for providing the condition. The code of the print and the output from the output window was not provided.
The print outside the condition should appear as:
Print(string.Format("{0} | ExitOnDayClose: {1} && marketDataUpdate.Time.Hour: {2} == 23 && marketDataUpdate.Time.Minute: {3} == 55", marketDataUpdate.Time, ExitOnDayClose, marketDataUpdate.Time.Minute));
With the action block, above CloseAllTrades(); should be a print:
Print(string.Format("{0} | CloseAllTrades()", marketDataUpdate.Time));
May we have the output from these prints?Chelsea B.NinjaTrader Customer Service
- Likes 1
Comment
-
Hi Chelsea,
Revisiting another of your older posts. I was also trying to implement Exit at a given time within Strategy Builder framework and for now my attempts were not terribly successfull.
I am trying to implement a couple of build-in safeguards to prevent my Perpetual Strategy staying beyond trading hours and Exit at Given time is one of those safeguards.
1. I defined Overall Trading Session hours from 17:00-15:40 CET.
2. I limited my own trading hours from 17:10-14:50
3. I instructed my strategy to exit (close) position at exactly 15:00 using Strategy Builder Time Series equal Time Value
However , if I did not explicitly specify to Exit on Session Close within given time period (60-300 seconds), my strategy is still enabled and position being held from 15:40-17.00 ( as seen on chart and from LOG), which is obviously not welcome scenario. Having looked at the post I made sure that Calculations are set on Price Change. I guess somewhere there is a fault in my implementation, but I could not identify it yet.
Cheers
Comment
-
Hello kazbek966,
Is the issue with the conditions not evaluating as true?
Have you added a print to the actions for that condition set to see at what times the condition is true, or have you printed each value used in the condition set?
What bar type and interval is on the chart?
Is there a bar that closes at exactly 15:00?Chelsea B.NinjaTrader Customer Service
Comment
-
Hi Chelsea, I run two trials: first on Renko , that obviously not time dependent and could have potential issues with time, other on Standard bar 1 Minute chart that obviously should not have any issue to have a candle closed exactly at 15:00. May be better way is to specify close as time period : say from 15:00 to 15:01? I dont really mind. There are no issues per se with having three conditional tabs, correct ? I am enclosing main part of the strategy that had run on both LONG and SHORT side to account for both eventualities. For some reason CS file upload did not work. Where should I put those PRINT Command as this something I still not very much comfortable with.
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if ((Position.MarketPosition == MarketPosition.Flat)
&& (IsFalling(AuLLMA1.LLMA) == true)
// TIME
&& ((Times[0][0].TimeOfDay >= new TimeSpan(17, 10, 0))
|| (Times[0][0].TimeOfDay < new TimeSpan(14, 50, 0))))
{
EnterShort(Convert.ToInt32(DefaultQuantity), "");
}
// Set 2
if ((Position.MarketPosition == MarketPosition.Short)
&& (IsRising(AuLLMA1.LLMA) == true))
{
ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
}
// Set 3
if ((Position.MarketPosition == MarketPosition.Short)
&& (Times[0][1].TimeOfDay == new TimeSpan(15, 0, 0)))
{
ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
}
}
CheersLast edited by kazbek966; 02-23-2022, 08:50 AM.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by sidlercom80, 10-28-2023, 08:49 AM
|
167 responses
2,260 views
0 likes
|
Last Post
by jeronymite
Today, 09:54 PM
|
||
Started by warreng86, 11-10-2020, 02:04 PM
|
7 responses
1,361 views
0 likes
|
Last Post Today, 09:41 PM | ||
Started by Perr0Grande, Today, 08:16 PM
|
0 responses
5 views
0 likes
|
Last Post
by Perr0Grande
Today, 08:16 PM
|
||
Started by elderan, Today, 08:03 PM
|
0 responses
9 views
0 likes
|
Last Post
by elderan
Today, 08:03 PM
|
||
Started by algospoke, Today, 06:40 PM
|
0 responses
10 views
0 likes
|
Last Post
by algospoke
Today, 06:40 PM
|
Comment