Free Daily Loss Limit NinjaScript

$0.00

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.

Overview

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).

Advanced

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[0]). 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.

Notes

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?


Guest
alvferna
2 years 25 days ago

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?
Thank you!

Support
Support
2 years 25 days ago

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.

Guest
jtansey
2 years 25 days ago

I’m trying to use this with a SampleMACrossOver strategy. Is this possible?

Support
Support
2 years 25 days ago

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.

Guest
igor
2 years 25 days ago

its not working for me. can You give an exaple maybe?

Support
Support
2 years 25 days ago

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.

Guest
Samuel Kajena
2 years 25 days ago

Good Day,
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.

Support
Support
2 years 25 days ago

Sorry Sam, We don’t do any NT7 programming.

Guest
JimmY MonT
2 years 25 days ago

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?

Support
Support
2 years 25 days ago

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.

Guest
Eric
2 years 25 days ago

Very useful. Has the indicator been built for this yet?
Thank you.

Guest
Eric
2 years 25 days ago

This will look at the PnL on a single strategy only? Or will it look at the entire account’s PnL?
Thanks.

Support
Support
2 years 25 days ago

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.

Guest
Brett Browand
2 years 25 days ago

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.

Support
Support
2 years 25 days ago

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.

Guest
Oli
2 years 25 days ago

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

Guest
Steve Weber
2 years 25 days ago

I installed it, but now I can’t find it in my list of indicators?? Is it named something else in the list?

Customer
test test
1 year 8 months ago

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?

Customer
Rodney Hamon
1 year 1 month ago

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?

Guest
Joe
11 months 16 days ago

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

Guest
John
4 months 14 days ago

Do I have to ncoptroste this into an existing strategy because my strategy is calculated per close of bar not tick.
Thank you