December 21


Disabling Trading At Profit Target or Max Stoploss

By Jeff Swanson

December 21, 2020

Jeff Swanson, money management

When building trading systems, particularly intraday trading systems, you often run into the special conditions where you might want to stop trading. Those two conditions are:

1) When a particular daily profit target is reached. In this case you have determined that when a particular dollar amount is reached in profit then no more trading should be done for the remainder of the day.

2) When a specific dollar value is reached. In this case, you have determined that after you have lost X amount of money, it’s best to stop trading for the rest of the day.

In both of these cases it’s possible to write code that will disable the opening of new trades when either of these functions is reached. In this article I’m going to use EasyLanguage to create a function that will perform these two tasks of disabling an intraday trading system. The final code will be freely available at the bottom of this article where you can easily apply it to your own intraday strategy.

Tracking Daily P&L

The first thing to code is a mechanism for tracking the daily profit or loss for the current day. It will track the net profit of a trading system, but we want to know the profit or loss just for a single day. So, we’re going to write some code to manually track this. The following code will do just that.

First, we’ll define two variables to hold our profit. The first is the profit or loss accumulated for our strategy through yesterday. The second variable is the accumulated profit or loss for our strategy just for today.

PLB4Today(0),   // P&L total before today
PLToday(0),    // P&L total for today (NetProfit + Open Position Profit – PLB4Today)

Next, we’ll add the following code to determine what our current profit or loss is just for today. We do this by using TradeStaton’s built-in reserved word, NetProfit. But NetProfit gives the value for all closed trades. We don’t want that. We don’t want to include the P&L from trades closed today. Thus, we must subtract the net profit up through yesterday. Hence, our PLB4Today variable comes into play.

PLToday = NetProfit – PLB4Today;

Now we have PLToday holding just the value of the profit or loss from today. But what if we have an open position? Maybe it’s a big loss or maybe a big win. Either way, we should include it. We can easily do this by using TradeStation’s built-in reserved word, OpenPositionProfit.

PLToday = NetProfit – PLB4Today + OpenPositionProfit;

Now PLToday contains the closed profit from today plus any open position profit. The only other task for properly tracking the P&L is to update our PLB4Today variable with today’s P&L at the close of the day. The following line of code handles that.

if ( Date <> Date[1] ) then PLB4Today = NetProfit;

Disabling Trading

Now that we have the means to track today’s profit or loss we can make decisions based upon this information. First, let’s tackle the idea of disabling our trading based upon reaching a profit target level. Initially, we’ll create flag which will act as our indicator when trading should be stopped. Let’s create a Boolean variable called OK_To_Trade.


At this time it’s just a mater of setting this variable based upon our daily P&L. Let’s turn our trading system off if we reach $500 in losses today. In other words, we will not open any new positions after we lost $500 within one day.

if ( PLToday <= -500 ) then OK_To_Trade = False;

Let’s say our trading system did just lose $500 and the code set our OK_To_Trade variable to false, now what? Well, when this variable is set to false we presumably want to close all trades and take no more new trades for the remainder of the day. To accomplish this we write the following code:

if ( OK_To_Trade = False ) Then
Sell next bar at market;
Buy To Cover next bar at market;
if ( OK_To_Trade = True ) Then
{ Our trade entry logic goes here}

At this point we have the code that will disable taking new trades if our daily loss limit of $500 is reached. Of course you can create another input variable to hold your daily loss limit. That way you can easily test different values.

Another important aspect to note about this code is our PLToday is computed by including any open position profit. That means your trading system would stop trading if your close P&L plus any open position profit was at $500 or greater. This may or may not be what you intend. For example, maybe you only wish to have closed profit counted. If that’s the case then you don’t want to include the open position profit in your calculation. It’s a small change in the code but, something to keep in mind.

Now let’s turn our attention to disable trading when our profit target is reached. We can do this by creating another condition where we set our trading flag to False. In this case, let’s stop trading when we reach a $600 profit for the day.

if ( PLToday >= 600 ) then OK_To_Trade = False;

Now our OK_To_Trade is set to False when we have a profit of $600 or more. At this point setting our flag to false will result in the same behavior as when we set this flag to false when we reached our stop loss limit. That is, all trades will be closed and no new trades will be taken today.

Again, keep in mind as soon as all closed P&L plus the open position profit reaches $600 the current trade will be closed. This might not be what you want. You may prefer to move your stop or simply allow the trade to unfold naturally. So, keep this in mind.

Creating A Function

Putting this code into a function is a great idea because it makes reusing this code very easy. I’m not going to go through the steps used to convert this to a function, instead I’ll simply explain the inputs and how it can be used.

First, the function is called _CE_Limit_Profits_And_Losses

The function takes four parameters:

  1. Daily profit target in dollars – enter zero to disable
  2. Daily stop loss in dollars – enter zero to disable
  3. Stop limit flag – True indicates stop limit hit
  4. Profit Max Flag – True indicates profit max hit

The first two inputs are rather straight forward. You can disable either of these first two inputs by passing in a zero. For example, if you don’t want to use the daily profit target simply pass in a value of zero.

Both inputs #3 and #4 are set by the function. They are referenced inputs which means information is passed back from the function to the calling strategy.

Input #3 is a Boolean flag which is set to true if the stop limit was hit. Likewise with input #4, this Boolean flag will be set to true if the profit max was hit. Setting these variables allows your calling program to know which of the two conditions was met. This can be helpful in tailoring different behaviors depending if the daily profit target was hit or the daily loss limit was hit.

Below is a simple SMA strategy that buys when price closes above the moving average and sells short when price moves below SMA. The _CE_Limit_Profits_And_Losses function is used to enforce both a daily profit target and daily max stop loss. The DailyLossLimit flag and StopLimitFlag are used to create unique exit messages on the sell and buy-to-cover orders.

TradeFlag = _CE_Limit_Profits_And_Losses( DailyProfitTarget, DailyLossLimit, StopLimitFlag, ProfitMaxFlag );

If ( TradeFlag ) Then
If ( Close > Average( Close,100 ) ) then buy next bar at market;
If ( Close <= Average( Close, 100 ) ) then sell next bar at market;
Else begin
If ( StopLimitFlag ) Then
Sell(“Loss Limit”) next bar at market;
Buytocover(“Loss Limit “) next bar at market;
Else If ( ProfitMaxFlag ) Then
Sell(“Profit Max”) next bar at market;
Buytocover(“Profit Max “) next bar at market;
Else Begin
Sell(“LE”) next bar at market;
Buytocover(“SE”) next bar at market;


Jeff Swanson

About the author

Jeff has built and traded automated trading systems for the futures markets since 2008. He is the creator of the online courses System Development Master Class and Alpha Compass. Jeff is also the founder of EasyLanguage Mastery - a website and mission to empower the EasyLanguage trader with the proper knowledge and tools to become a profitable trader.

  • This is excellent Jeff – thank you so much, I had one of those days I exceeded the max loss I’m comfortable with at this point and just kept emotionally trading jumping into everything – it was a disaster. Thank goodness I’m trading very small size at the moment to learn from days like this. I’ll be coding this up this week to protect me from myself!

  • hey Jeff, will this work for me if im not using an automated system. itll just read my realized pnl and if it hits say 1K it’ll close down the account?

    • Hi Willie. I don’t think this code will be of much help to you. It disables (turns off) a strategy if the strategy PNL falls below a specific level, not your account (realized PNL). It does not do anything with your account. Sorry.

  • hey Jeff,
    what about when I run few strategies, then the NetProfit is for all of them together?
    thus if just one strategy hit the stop/ take profit it disable all the rest of them?
    if it hit 1,000$ take profit at one strategy then the NetProfit is now +1,000$ and the system is disable although the rest of the strategies with a small p@l?

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Learn To Code & Build Strategies
    Using EasyLanguage.