Number of Trades Per Day and "X" Events

One of my first backtesting tips featured a piece of code to limit a trading system only to one trade per day.

My colleague Doug came up with a smarter way to do this and even added the possibility to define how many trades he would allow per day.

Here we'll explain the piece of code step by step.

This line calculates how many closed trades we have at the new signal for the next trade.

CEC:=ClosedEntryCount(@,ThisTradeOnly);

This is the same information, but using the WHEN function to figure out the number of closed trades at the first bar of the day.

STA:=(ClosedEntryCount(@,ThisTradeOnly) WHEN BarIx(@,StartOfDay)=0);

Now we subtract the STA value from the number of closed trades at the new potential entry point (CEC). For example, we had ten trades at the start of the day and now we have twelve. That means we traded two times today. We add +1 because we want to get to the number of "allowed trades," so the result is three.

DAY:=CEC-STA+1;

TRADES is a variable to be set to the number of trades per day allowed. If it is set to three, the system would allow one more trade.

DAY = TRADES

The rest is just the original entry signal.

AND

MA(@,Sim,fast)[-1] XBELOW MA(@,Sim,slow)[-1]

Was that too easy? OK, here is another useful function from the Formula Toolbox: EventOffset.

The need to reference a certain value in a chart, for example, the high, if a special condition is met is quite common. This is simple:

High(@) WHEN MA(@,Sim,7) XABOVE MA(@,Sim,21)

This references the high of the bar when the seven-period MA crossed above the twenty-one-period MA. Now we want to do exactly the same thing, but we want to know the high, not from the most recent event, but from three events ago. Here EventOffset comes into play. This function can be accessed only in the Formula Toolbox.

The Event Offset function counts the number of bars since a user-specified event occurred and reports the negative of that number or zero if the event occurred on the selected bar. The function has two parameters and we need parameter number one to accomplish our goal.

Setup parameters:

  • NEvents: Allows the user to specify an occurrence number other than one before the count begins.
  • Max Bars To Search: Selects the maximum number of bars to cover.

The study "before3" tells us how many bars ago the third event (counting backwards) happened.

EventOffset(MA(@,Sim,7) XABOVE MA(@,Sim,21),3,400)

The simplest way to get to the desired result (i.e., the high of that bar), is to put the number of bars into a variable and use High(@)[-offset].

This is how the code looks:

X:=EventOffset(MA(@,Sim,7)[-1] XABOVE MA(@,Sim,21),3,400);

High(@)[X]

We do not need a "-X" because the result from EventOffset is already a negative number. In this example it would be High(@)[-149] at the current bar.

Disclaimer

Trading and investment carry a high level of risk, and CQG, Inc. does not make any recommendations for buying or selling any financial instruments. We offer educational information on ways to use our sophisticated CQG trading tools, but it is up to our customers and other readers to make their own trading and investment decisions or to consult with a registered investment advisor. The opinions expressed here are solely those of the author and do not reflect the opinions of CQG, Inc. or its affiliates.