Announcement

Collapse
No announcement yet.

Using EasyLanguage (Easy Language) To Limit Trades

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using EasyLanguage (Easy Language) 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”.

Disclaimer: There is a risk of loss in trading futures, forex and options. Futures, forex and options trading are not appropriate for all investors. Only risk capital should be used when trading futures. All information is for educational use only and is not investment advice. Past performance is not indicative of future results.

This website is hosted and operated by AMP Global Clearing, LLC ("AMP"), which provides brokerage services to traders of futures and foreign exchange products. This website is intended for customer support, educational and informational purposes only and should NOT be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by AMP and the information made available on this Web site is NOT an offer or solicitation of any kind. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of AMP.

All Users and Contributors along with their websites, products and services, are independent persons or companies that are in no manner affiliated with AMP. AMP is not responsible for, do not approve, recommend or endorse any User and/or Contributor Content referenced on this website and it’s your sole responsibility to evaluate All Content. Please be aware that any performance information provided by a user and/or contributor should be considered hypothetical.

HYPOTHETICAL PERFORMANCE RESULTS HAVE MANY INHERENT LIMITATIONS, SOME OF WHICH ARE DESCRIBED BELOW. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL OR IS LIKELY TO ACHIEVE PROFITS OR LOSSES SIMILAR TO THOSE SHOWN. IN FACT, THERE ARE FREQUENTLY SHARP DIFFERENCES BETWEEN HYPOTHETICAL PERFORMANCE RESULTS AND THE ACTUAL RESULTS SUBSEQUENTLY ACHIEVED BY ANY PARTICULAR TRADING PROGRAM.

ONE OF THE LIMITATIONS OF HYPOTHETICAL PERFORMANCE RESULTS IS THAT THEY ARE GENERALLY PREPARED WITH THE BENEFIT OF HINDSIGHT. IN ADDITION, HYPOTHETICAL TRADING DOES NOT INVOLVE FINANCIAL RISK, AND NO HYPOTHETICAL TRADING RECORD CAN COMPLETELY ACCOUNT FOR THE IMPACT OF FINANCIAL RISK IN ACTUAL TRADING. FOR EXAMPLE, THE ABILITY TO WITHSTAND LOSSES OR TO ADHERE TO A PARTICULAR TRADING PROGRAM IN SPITE OF TRADING LOSSES ARE MATERIAL POINTS WHICH CAN ALSO ADVERSELY AFFECT ACTUAL TRADING RESULTS. THERE ARE NUMEROUS OTHER FACTORS RELATED TO THE MARKETS IN GENERAL OR TO THE IMPLEMENTATION OF ANY SPECIFIC TRADING PROGRAM WHICH CANNOT BE FULLY ACCOUNTED FOR IN THE PREPARATION OF HYPOTHETICAL PERFORMANCE RESULTS AND ALL OF WHICH CAN ADVERSELY AFFECT ACTUAL TRADING RESULTS.

AMP has NOT endorsed or recommended the use any product, services offered by any 3rd party User or Contributor on this forum. No person employed by, or associated with, AMP is authorized to provide any information about any 3rd party User or Contributor content. All specific brokerage related questions should be directed towards AMP.

Copyright © 2017. All rights reserved.

Working...
X