System Trader Success
Share It!

Using EasyLanguage To Limit Trades

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 MP. MP 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 }
end;

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.


Input:
ResetCondition(date <> date[1]),
MaxTrades(5);
Variables:
OKtoTrade(FALSE);
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;
End;
If ( Barssinceentry(0) >= Random(5) + 2 ) then Begin
sell next bar at open;
buytocover next bar at open;
End;

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

Downloads

Limit Trades Function ( TradeStation ELD or text file )

About the Author Jeff Swanson

Jeff is the founder of System Trader Success - a website and mission to empowering the retail trader with the proper knowledge and tools to become a profitable trader the world of quantitative/automated trading.

follow me on:

Leave a Comment:

4 comments
Add Your Reply