September 19


Using EasyLanguage To Limit Trades

By Jeff Swanson

September 19, 2016

EasyLanguage, free easylanguage, function, limiting trades, money management

In this article I’m going to demonstrate an EasyLanguage technique to limit the number of trades your trading system will take within a given period. Most often this is done to limit the number of trades a strategy will open in a single day. For example, you may want your day trading strategy to only take a maximum of 20 trades per day. Once it reaches that number, you wish the strategy to not open any more trades until the next trading day.

The code I’m going to write will be a more flexible method than the built-in TradeStation reserved word, EnteriesToday. This works fine in many cases, but what if you wish to limit the number of trades in the overnight session? EnteriesToday assumes the regular session and will reset the trade counter when the day changes – right in the middle of your trading. What if you wish to limit the number of trades over a given week? Nope, can’t do it with EnteriesToday.

Tracking Each Trade

The solution is rather straightforward. We simply wish to track each new position. This can be done by incrementing a variable every time your strategy opens a trade.

Variable: TradesCounter(0);

How do we know when a trade has opened? One way to do this is to monitor the market position state of your strategy. This is done by checking the status of the TradeStation’s built-in reserved word MPMP will have a value of 0 if your strategy is flat. It will have a value of -1 if your strategy is short, and it will have a value of 1 if your strategy is long. Thus, we simply look for a switch between these states.

If ( MP <> 0 ) and (MP[1] <> MP ) then TradesCounter = TradesCounter+ 1;

In the above code we see the first test is to check that we’re not currently flat. The second test then checks that on the previous bar the market position status is not the same as the current status. Put another way, something changed! Again, the first check assures we have a position, either long or short. The second check is looking for a position changed from the previous bar. If this happens, a new position was taken. When a new position is taken, we increase our counter.

Now we can test our counter to see if we’re at our limit. Let’s say we don’t want more than 15 trades, then we can code something like this…

If TradesCounter < 15 then begin
{ Trade entry logic }

The trading entry logic will only be executed if our counter is below our threshold of 15 trades. Once our counter rises above 15, the trade entry logic will no longer execute. Our trade exit logic will continue to function to handle any open positions.

Resetting The Trade Counter

The next step is to reset our TradesCounter counter to zero. This is done when the particular time horizon has passed. That is, do we wish to limit our trades upon a new day? If this is the case then we can write this line of code:

If date <> date[1] then TradesCounter = 0;

The above line of code will set our counter to zero when the date on the previous bar is different than the current bar. Keep in mind, this code we’re writing is for intra-day trading! Thus, when the previous bar has a different date than the current bar we must have entered a new day.

But what if we want to reset our counter at a different time? For example, if we wish to reset our counter at the close of the U.S. regular session? Or, maybe at 2:00 a.m. Central when the regular session for the Euro currency futures opens? Well, this can be accomplished by entering the appropriate time when to reset the counter.

If ( time = 200 ) then TradesCounter = 0;

In the above line, the counter will be reset at 2:00 a.m. based upon your local machine time. You can create an input called ResetTime which will allow you to easily change the time when the counter is reset. The code is below.

If ( time = ResetTime ) then TradesCounter = 0;

Maybe you wish to limit the number of trades in a given week. How would you code that? Well, we simply look for a a particular day of the week to reset our counter. Clearing-out or zeroing our counter at the start of the U.S. Regular Session on Monday will allow us to track the number of trades throughout the entire week.

We can do this by using TradeStation’s built-in Reserved Word called, DayOfWeek. This returns an integer value zero through six. The meaning of this value is straightforward with zero being Sunday and Friday being five. In our example we want to reset our counter on a Monday morning at 8:30 a.m. Central – the regular session open within the U.S.

If ( DayofWeek(Date) = 1 ) and ( Time = 830 ) then TradesCounter = 0;

Limit Trades Function

With this code you can now limit the number of trades on an intra-day strategy. You can easily take the code snippets contained within this article and add them to your strategy. In the downloads section of this article below, you’ll find a function that I created which can be used in your code. The function is called, _CE_Limit_Trades. An example of calling this function is below.

OKtoTrade = _CE_Limit_Trades( ResetCondition, MaxTrades );

The first parameter is your reset condition. This is the condition on which to reset your trade counter. I provided several ideas within this article. The second parameter is the maximum number of trades you wish your strategy to take.

Here is an example strategy using the Limit Trades Function.

ResetCondition(date <> date[1]),
OKtoTrade = _CE_Limit_Trades( ResetCondition, MaxTrades );
If ( OKtoTrade ) Then Begin
If ( RSI( Close, 3 ) < 10 ) Then buy next bar at open;
If ( RSI( Close, 3 ) > 90 ) Then sellshort next bar at open;
If ( Barssinceentry(0) >= Random(5) + 2 ) then Begin
sell next bar at open;
buytocover next bar at open;

In the example below you can see the ResetCondition is set to reset the counter upon a new date. However, the ResetCondition can be any valid EasyLanguage code which evaluate to true/false. For example you could use the text “date <> date[1]” or “time = 200”.

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.

  • Hi Jeff,
    I always get concern with a statement of the sort; if time = 800. My programing days go back to Frotran and we were always cautioned about such a statement because of floating point considerations and that time may actually be 799.9999 and not exactly equal to 800 causing the statement to be false rather than true.

    Your thoughts?


    • This is a good point. Evaluations, such as the one you pointed out, are always potential areas for coding errors. In this case, time is not a floating point number but an integer. Furthermore, when dealing with bar charts the time stamp on any bar is always a known block size. For example, on a 5-minute chart the time stamp on a sequence of bars will be 745, 750, 755, 800, 805. Of course if you use a four minute chart this would not be true. If you were using a specific time to exit a trade you might use “>=800”. However, in this application using “=800” is just fine.

  • Hi Jeff,

    it might also be useful to limit the number of consecutive trades in any one direction, lets say no more than 3 consecutive longs or shorts for example. An easylanguage example would be most appreciated!!

    • Hello Vaclav. You can use the Marketposition reserved word when a trade is open. Long trades will have a value of 1. Short trades a value of -1. This will allow you to track long trades only or short trades only.

  • Sorry but when i use exits the code not work! for example if i use stop loss and take profit when my position si close the strategy entry still in same session.

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

    Learn To Code & Build Strategies
    Using EasyLanguage.