This free NinjaScript strategy will close any open positions and stop trading for the current day if your daily P/L reaches a set negative or positive value. 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 download is a single zip file that you import directly into NinjaTrader. It’s 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 – this way 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”.
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 code 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 code will not work with releases prior to NinjaTrader 8. Click here if you don’t know how to install NT strategies. Use at your own risk, and backtest backtest backtest before you go live with any strategy.