Free Daily Loss Limit NinjaScript
This Free Daily Loss Limit NinjaScript will close any open positions and stop trading for the current day if your daily P/L reaches a set positive or negative value. This code will only close positions that were entered by a strategy while that strategy is still running – it does not work with manually entered orders.
The Free Daily Loss Limit NinjaScript will close any open positions and stop trading for the current day if your daily P/L reaches a set negative or positive value. This code is meant to be pasted into an existing automated strategy such as our Free Automated Trading NinjaScript and will only close orders that were created by your strategy while it is still running - it does not work with manually entered orders. If you have a strategy that you run uninterrupted over multiple days, this indicator will reset the daily P/L back to zero at the beginning of each day for a fresh start. In addition to limiting trading based on P/L, you can limit the daily timeframe in which you’d like to trade, i.e. trade only from 9am to 11am.
This NinjaScript daily loss limit add-on is not assembled which means you have full access to view and change the source code either in NinjaScript Editor or your preferred way to edit .cs files.
Details / Instructions
There are simpler ways to accomplish a daily PL limit if you stop and restart your strategy every trading day, but those ways don’t always work when back testing over multiple days like the Free Daily Loss Limit NinjaScript does.
Calculate must be set to on each tick.
If you paste this exit logic into your code, make sure your entries have a signal name that the exits reference. In this case they’re “long” and “short”.
Adjustable Daily Loss Limit Variables
The dailyLossLimit and dailyProfitLimit variables located in the Dashboard region of the code determine the negative or positive daily P/L in currency at which you want the strategy to stop taking trades for the day.
The dailyOpenTime and dailyCloseTime are the beginning and end times of your trading day (in hhmmss format).
This Free Daily Loss Limit NinjaScript will work with any time series because it’s checking the time within the OnMarketData method. If you’re running a 1-tick time series you could move this code from OnMarketData to OnBarUpdate and check the time with ToTime(Time). Note: NT doesn’t currently offer a way to check the session start/stop time which is why we have to set variables for this.
This NinjaScript daily loss limit download is a single zip file that you import directly into NinjaTrader.
This daily loss limit code will not work with releases prior to NinjaTrader 8.
Got something to discuss?
Hi. This is very cool. Thank you so much.
I installed it and I don’t know if I set it up correctly but it doesn’t work in my SIM account.
Do I need a strategy to make it work? If you trade by yourself should it work too?
Hello. This is meant to stop an automated strategy from trading, so if you’re trying to prevent manual entries, this won’t do it.
I’m trying to use this with a SampleMACrossOver strategy. Is this possible?
Yes. Just replace the stuff in the “entry logic” region with your own entry logic that uses an SMA crossover. You can copy and paste from the SampleMACrossOver strategy.
its not working for me. can You give an exaple maybe?
Igor, Please explain. If you run this as is on market replay you will see it working with generic entry criteria. Then it’s up to you to manually code different entry logic.
I am actually need this indicator that will work with NT-7, but for MANUAL ENTRY.
Is it possible to find someone that can help to create one for me ?
Your response will be appreciated.
Sorry Sam, We don’t do any NT7 programming.
Greatly needed functionality for NinjaTrader! Been trying to accomplish this on my own and NO GO.
But my strategies are being created with Strategy Builder. And although I assume I can take the logic and force fit it into this strategy it would be a greater benefit if it was an indicator that I could call from Strategy Builder.
Is there any chance this can be modified for that?
We’re planning on creating an indicator section to the site very soon so keep checking back. I’ll add this request to the shortlist.
Very useful. Has the indicator been built for this yet?
This will look at the PnL on a single strategy only? Or will it look at the entire account’s PnL?
It’s not a standalone strategy, but rather example code meant to be incorporated into your own strategy, so if you’re running multiple strategies, you could add the logic to each one and it will work.
I down loaded it. Now what? Is there a video I can watch? I have no Idea how to use code. I’m barely good with strategy builder.
Hi Brett, All the code on this site is for advanced users who manually code their strategies and have some knowledge of C#. I’m currently working on an indicator version of this that will be accessible in strategy builder. That should be uploaded to ninjatrader-indicators.com this week. If you want to learn more about coding ninjascript I’d start by watching some C# intro videos on youtube and poking around in some of the indicators/strategies that come with ninjatrader in the ninjascript editor.
Hi! Thanks for the great content in general!
Is it possible to make a small video how I am able to implement the code in a existing strategy using the editor!? I have built several startegies up to date only with the builder and I would like to integrate this function. Thanks a lot. Do you have a youtube channel? Thanks Oli
I installed it, but now I can’t find it in my list of indicators?? Is it named something else in the list?
Hello. Does this script use the Total PnL of the day or just the realized PnL of the day? In other words, is it adding unrealized PnL to the Realized PnL as it functions during a trade?
Well, I downloaded this as I have lots of other things…and I can’t find it anywhere in NT. I must be an idiot (at lease I feel like it). I hate to ask for help on such a simple thing…..but….can someone help?
Hi- from around NOV 2020 , it was posted ” I’m currently working on an indicator version of this that will be accessible in strategy builder. That should be uploaded to ninjatrader-indicators.com this week.” I do not see that indicator in the offerings in DEC 2021. Is it currently offered? If so, request link. Many thanks
Do I have to ncoptroste this into an existing strategy because my strategy is calculated per close of bar not tick.