Hello, I have a strategy working. One of the parameters is that the strategy is deactivated once it reaches an amount of loss or profit is automatically deactivated using the code: if ((Account.GetAccountItem (AccountItem.RealizedProfitLoss, NinjaTrader.Cbi.Currency.UsDollar) .Value + Account.GetAccountItem (AccountItem.UnrealizedProfitLoss, NinjaTrader.Cbi.Currency.UsDollar) .Value) > = (MaxProfit)) { Print ("Daily Profit reached" + "\ n Current ->" + (Account.GetAccountItem (AccountItem.RealizedProfitLoss, NinjaTrader.Cbi.Currency.UsDollar) .Value + Account.GetAccountItem (AccountItem.UnrealizedProfitLoss, NinjaTrader.Cbi.Currency.VsDollar). Print ("Closing"); CloseStrategy ("Daily Goal Achieved"); return; } The point is that the next day I can't activate it again. Could you help me so that it can be activated the next day? Thank you
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
Reset Strategy every day PNL
Collapse
X
-
Reset Strategy every day PNL
Hello, I have a strategy working. One of the parameters is that the strategy is deactivated once it reaches an amount of loss or profit is automatically deactivated using the code: if ((Account.GetAccountItem (AccountItem.RealizedProfitLoss, NinjaTrader.Cbi.Currency.UsDollar) .Value + Account.GetAccountItem (AccountItem.UnrealizedProfitLoss, NinjaTrader.Cbi.Currency.UsDollar) .Value) > = (MaxProfit)) { Print ("Daily Profit reached" + "\ n Current ->" + (Account.GetAccountItem (AccountItem.RealizedProfitLoss, NinjaTrader.Cbi.Currency.UsDollar) .Value + Account.GetAccountItem (AccountItem.UnrealizedProfitLoss, NinjaTrader.Cbi.Currency.VsDollar). Print ("Closing"); CloseStrategy ("Daily Goal Achieved"); return; } The point is that the next day I can't activate it again. Could you help me so that it can be activated the next day? Thank youTags: None
-
Hello manueldecastro,
Thanks for your post.
When a strategy is applied to a chart, with several days of data, the strategy will perform historical trades and the strategy will continue to accumulate the PNL over the duration of that period so if you are comparing the strategy PNL to a daily limit value then it is likely that the strategy will have hit that daily limit before the current trading day.
There are a couple approaches depending on how you wish to use the strategy.
If you want to enable and disable the strategy each day, then you would want to prevent accumulatedPNL and the easiest way to do this is to prevent historical orders when you apply the strategy. To prevent historical orders, in the OnBarUpdate() you can add: if(State != State.Realtime) return; ( which means If it is not real time data, return and do not process any code below that line, when it is real time data then the code below that line would be executed.).
If you want to turn your strategy on and let it run and/or do want to see historical trades then the other approach would be to, once per day save the accumulatedPNL into a double variable. A typical way to do this would be to trigger saving the value on IsFirstBarOfSession (reference: https://ninjatrader.com/support/help...rofsession.htm). After saving the accumulated PNL, as your new days trades progress, the dailyPNL will then be the difference between the acumulatedPNL and the saved double variable and it is this difference you would test against your daily limit value.Paul H.NinjaTrader Customer Service
-
Hello
I tried to enter the code:
protected override void protected override void OnBarUpdate ()
{
if (State == State.Historical) return;
if (State! = State.Realtime) return;
if (BarsInProgress! = 0)
return;
if (CurrentBars [0] <5)
return;
In this way, the strategy cannot be activated every day if it has reached the specified pnl during the previous session.
For example. if my pnl target is 500 and reaches it on day 1, the strategy closes when it reaches 500. On day 2, I activate the strategy again but it is deactivated at the moment the totalpnl is 500.
Could you clarify the method of saving the pnl in the double variable?
Thank you.
Comment
-
Hi manuel decastro,
Add a double, e.g. "atopenPNL" and update it each day at the start of the trading session [if (Bars.IsFirstBarOfSession)], see above.
If you wanted to capture accumulatedPNL of the SYSTEM (not the account) at that point in time, you could set it as follows:
atopenPNL = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit + (Position.GetUnrealizedProfitLoss(PerformanceUnit. Currency, Close[0])).
Then, throughout the day, compare the variable atopenPNL against current accumulatedPNL and do whatever you want based on the result of the comparison.
You will need to finetune your code to ensure you only deactivate it once the dailyPNL (difference between accumulatedPNL and atopenPNL) exceeds a certain value.
NT-Roland
- Likes 1
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by CortexZenUSA, Today, 12:53 AM
|
0 responses
2 views
0 likes
|
Last Post
by CortexZenUSA
Today, 12:53 AM
|
||
Started by CortexZenUSA, Today, 12:46 AM
|
0 responses
1 view
0 likes
|
Last Post
by CortexZenUSA
Today, 12:46 AM
|
||
Started by usazencortex, Today, 12:43 AM
|
0 responses
5 views
0 likes
|
Last Post
by usazencortex
Today, 12:43 AM
|
||
Started by sidlercom80, 10-28-2023, 08:49 AM
|
168 responses
2,266 views
0 likes
|
Last Post
by sidlercom80
Today, 12:05 AM
|
||
Started by Barry Milan, Yesterday, 10:35 PM
|
3 responses
13 views
0 likes
|
Last Post Yesterday, 11:58 PM |
Comment