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ā.
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?
Red
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!!
Good idea. I’ll add that to the list of ideas for future articles! Thanks.
Print( MP ); results in an EL error, “unknown identifier” in TS v10 update 824. Great idea but what is this called now?
I’m not running V10. I’m still using 9.5. I’m not sure if the Print statement is unknown or if it’s MP. Try changing the MP to “Marketposition”.
In TS 10, you need to do this: MP = Marketposition;
Marketposition is a function and won’t work the same way. Thanks for the article!
Hi Curtis. Thanks for the tip.
You’re welcome. Also, if you use setstoploss then you will not get the flag. George Pruitt shows a method where you can record buys and sells with a counter at entry. That should work for code that uses market order entries with stop loss. However, if you use limit order entry then it probably won’t work. Need to use a different method.
Actually– it might be another bug. It seems the buy was triggered from the last bar of previous day.
Back again, for limit order systems, you can use StrategyHost and track the fill event for Tradestation. Not sure how that would work in MC.
Hi Jeff, thanks for great tip. Do you have an idea how to track long trades only?
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.
Post your code, and I’ll have a look.