Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Exit all positions at Loss or Profit
Collapse
X
-
Exit all positions at Loss or Profit
Does anybody have a single strategy that can be enabled in the Control Center daily, will monitor the account during the regular session, and will sell all positions in that account should the total of realized plus unrealized losses be less than a designated amount, or should the the total of realized plus unrealized gains be more than a designated amount?Tags: None
-
Hello MathWiz,
Thank you for your post.
You could create a strategy that subscribes to the account-based event AccountItemUpdate to monitor the total account PnL. You would then create a condition in your script that checks if the Unrealized account PnL plus the Realized account PnL is less than a certain price value and call Flatten() to flatten all positions. Another condition would be created in the script that checks if the Unrealized PnL plus the Realized PnL is greater than a certain price value and call Flatten().
Public documentation for AccountItemUpdate can be referenced here - https://ninjatrader.com/support/help...itemupdate.htm
AccountItems that can be used are documented here - https://ninjatrader.com/support/help...ccountitem.htm
See this documentation for information about Flatten() — https://ninjatrader.com/support/help...ghtsub=flatten
Let us know if we may further assist.
<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
-
Originally posted by NinjaTrader_BrandonH View PostHello MathWiz,
Thank you for your post.
You could create a strategy that subscribes to the account-based event AccountItemUpdate to monitor the total account PnL. You would then create a condition in your script that checks if the Unrealized account PnL plus the Realized account PnL is less than a certain price value and call Flatten() to flatten all positions. Another condition would be created in the script that checks if the Unrealized PnL plus the Realized PnL is greater than a certain price value and call Flatten().
Public documentation for AccountItemUpdate can be referenced here - https://ninjatrader.com/support/help...itemupdate.htm
AccountItems that can be used are documented here - https://ninjatrader.com/support/help...ccountitem.htm
See this documentation for information about Flatten() — https://ninjatrader.com/support/help...ghtsub=flatten
Let us know if we may further assist.
Thanks
Comment
-
Hello George1455,
Thank you for your note.
NinjaTrader 7 does not have the ability to subscribe to the account-based event AccountItemUpdate. Instead, you would need to create a condition that checks if GetAccountValue(AccountItem.RealizedProfitLoss) is less than or greater than a certain value followed by calling Exit methods such as ExitLong/ExitShort to close out of your positions.
GetAccountValue() - https://ninjatrader.com/support/help...countvalue.htm
Order methods - https://ninjatrader.com/support/help...d_approach.htm
If you have further questions regarding this, please open a new forum thread in the NinjaTrader 7 Strategy Development section of the forums.
Let us know if we may assist further.<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>
Comment
-
George,
Are you saying that code like this:
CurrentPNL = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit;
if (CurrentPNL + Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0])) < myDailyStoplevel)
{
Flatten()
}
running as its own strategy will monitor my entire account and flatten all positions if it reaches its trigger?
Comment
-
Hello MathWiz,
Thank you for your note.
CurrentPNL = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit; would save the cumulative profit of all trades to the variable CurrentPNL.
CumProfit — https://ninjatrader.com/support/help.../cumprofit.htm
Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) will return the unrealized PnL for the strategy position. (Strategy positions are different from Account positions)
Position.GetUnrealizedProfitLoss — https://ninjatrader.com/support/helpGuides/nt8/position_getunrealizedprofitloss.htm
This means that the code you shared would check if the cumulative profit of all trades + the unrealized PnL of the strategy position is less than myDailyStoplevel and calls Flatten(). This would not be checking the unrealized PnL of your account position.
To have the strategy monitor the unrealized PnL of your account, you would need to use PositionAccount.GetUnrealizedProfitLoss() instead of Position.GetUnrealizedProfitLoss().
PositionAccount.GetUnrealizedProfitLoss — https://ninjatrader.com/support/help...profitloss.htm
Please note that instead of Flatten() you would need to call Account.Flatten() as seen in the help guide example in post number 2.
See the attached example script that demonstrates how this would be accomplished.
Let us know if we may assist further.
<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>
Comment
-
Hello,
I am hoping you can help me with this script? I downloaded it and changed the account name to my "Playback101" so it matched and I updated the instrument to "ES 06-22" and changed the properties setting to allow a negative number to allow for max loss.
When I start my strategy and this one on the playback101, they are both in "Sync" until it opens a trade and then the CumProfit script "Sync" errors and shows "False" and doesn't work??
I did also try it on a live feed Sim101 account and it did the same?
Please help, thanks.
Comment
-
Hello Neilpacc76,
Thanks for your note.
This script is meant to be an example of how to call the Account.Flatten() method when the cumulative profit of all trades + the unrealized PnL of the account position is less than MyDailyStoplevel. It is not meant to be a fully working tool.
That said, since the script does not place trades and trades are placed manually, the Strategy Position and Account Position would go out of sync. Strategies are not able to detect trades placed manually or placed by other strategies. For example, if the Strategy Position is 0 and the Account Position is 0, then a trade is placed manually, the Account Position would be 1 and the Strategy Position would be 0 since the strategy did not place the trade. This would cause the Strategy Position and Account Position to be out of sync.
See this help guide page for more information: https://ninjatrader.com/support/help..._account_p.htm
Let us know if we may assist further.<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>
Comment
-
Originally posted by NinjaTrader_BrandonH View PostHello MathWiz,
Thank you for your note.
CurrentPNL = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit; would save the cumulative profit of all trades to the variable CurrentPNL.
CumProfit — https://ninjatrader.com/support/help.../cumprofit.htm
Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) will return the unrealized PnL for the strategy position. (Strategy positions are different from Account positions)
Position.GetUnrealizedProfitLoss — https://ninjatrader.com/support/helpGuides/nt8/position_getunrealizedprofitloss.htm
This means that the code you shared would check if the cumulative profit of all trades + the unrealized PnL of the strategy position is less than myDailyStoplevel and calls Flatten(). This would not be checking the unrealized PnL of your account position.
To have the strategy monitor the unrealized PnL of your account, you would need to use PositionAccount.GetUnrealizedProfitLoss() instead of Position.GetUnrealizedProfitLoss().
PositionAccount.GetUnrealizedProfitLoss — https://ninjatrader.com/support/help...profitloss.htm
Please note that instead of Flatten() you would need to call Account.Flatten() as seen in the help guide example in post number 2.
See the attached example script that demonstrates how this would be accomplished.
Let us know if we may assist further.
I stumbled across your strategy and tried to change it into an account daily profit target as well. I'm currently using 154 strategies over 6 accounts(no SIM). Flattening each account with a different daily target and stop is very important. I changed Currency to Ticks and added the Profit Target to go along with the stop you had there. When I tried it out, it would stop the strategy on SIM101 account but seemingly at very sporadic times. I ran it on 5 second chart just so it would update often and wouldn't have to wait until the the bars close on the multiple strategies I'm running. Here is the code, do you see anything obvious why it wouldn't work properly? Thanks
Comment
-
Hi Steve4616, thanks for posting. The SystemPerformance class will only provide the performance metrics of the single strategy it is being called from, not the entire account. Each strategy needs to check its own PnL value and close its position based on that calculation. You can get Account based metrics by using the Get method:
Kind regards,
-ChrisLChris L.NinjaTrader Customer Service
- Likes 1
Comment
-
Originally posted by NinjaTrader_ChrisL View PostHi Steve4616, thanks for posting. The SystemPerformance class will only provide the performance metrics of the single strategy it is being called from, not the entire account. Each strategy needs to check its own PnL value and close its position based on that calculation. You can get Account based metrics by using the Get method:
Kind regards,
-ChrisL
Comment
-
Originally posted by NinjaTrader_ChrisL View PostHi Steve4616, thanks for posting. The SystemPerformance class will only provide the performance metrics of the single strategy it is being called from, not the entire account. Each strategy needs to check its own PnL value and close its position based on that calculation. You can get Account based metrics by using the Get method:
Kind regards,
-ChrisL
I tried it out and it compiles without errors, but immediately disables. Also, if i have any other strategies enabled on the account, it immediately flattens also. I had the profit target and stop set at 200 and -200 and the sim101 account was down $25 and no open positions and it still disabled immediately. Does anything jump out to you that would cause this? Thanks again
Comment
-
Originally posted by NinjaTrader_ChrisL View PostHi, thanks for the follow up. If a strategy is disabled immediately, check the Log tab of the Control Center for any errors. There is likely an exception being thrown.
Kind regards,
-ChrisL
It only shows this, no errors. Also there were no trades open and not trades in the Sim101 account today so the PL is 0, thanks
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by LeanDenUK, Today, 05:41 AM
|
0 responses
3 views
0 likes
|
Last Post
by LeanDenUK
Today, 05:41 AM
|
||
Started by LeanDenUK, Today, 05:40 AM
|
0 responses
2 views
0 likes
|
Last Post
by LeanDenUK
Today, 05:40 AM
|
||
Started by Ludwik, Today, 03:28 AM
|
0 responses
13 views
0 likes
|
Last Post
by Ludwik
Today, 03:28 AM
|
||
Started by mitchdavo, 01-17-2025, 05:30 PM
|
3 responses
26 views
0 likes
|
Last Post
by mitchdavo
Today, 03:17 AM
|
||
Started by LeanDenUK, Today, 03:17 AM
|
0 responses
7 views
0 likes
|
Last Post
by LeanDenUK
Today, 03:17 AM
|
Comment