Intraday trading example

This example tries to show one possible solution for intraday trading. The purpose of this chapter is not to develop a profitable strategy here. It is up to you. This is just an example of one possible intraday setup.

Say we want to implement this assignment:

  • Use 5-minutes bars.

  • Look at the opening price of the AAPL at 9:35.

  • In relation to the previous day's close, buy or short it.

  • Then, place a limit order in relation to the opening price.

  • If not closed, liquidate the position at the close at 16:00.

Example 11. Strategy: Trading signals

// ========= A strategy setup ============

// Your Collective2 trading system ID
SetOption("C2TradingSystemId", 1111111);   

// An used symbol

// Commissions setup

// 5-minutes time bars used

// A time range - last 30 days
SetOption("ApplytoRecentDays", 30);

// Set initial capital 
// Round a number of shares to 100. 

// Use Open as prices for the backtest. 
// We will delay trades on the next bar and use its Open. 
BuyPrice = Open;
ShortPrice = Open;
SellPrice = Open;
CoverPrice = Open;

// ========= A strategy logic setup ============

// Previous day close price time
previousDayEndTime = 160000;

// Today price check time
thisDayStartTime = 093500;

// Trades entry time. 
// To be on the safe side in the backtest, trade on the next bar after the moment stored in "thisDayStartTime". 
// In reality, we are not able to send a signal at that moment. Only AFTER it!     
entryTime = 094000; 
// Our unconditional exit time
thisDayExitTime = 155500;  

// ========= Prepare strategy data ============

// Get date and time to local arrays to speed things up
aDay = Day();
tn = TimeNum();

// Store previous day closing prices to the array.
previousClose = ValueWhen(aDay == Ref( aDay, -1 ) AND tn == previousDayEndTime, Close) ;

// Store each day opening price to the array.
openingPrice = ValueWhen(tn == thisDayStartTime, Open) ;

          Your strategy logic here 
Following code just SIMULATES your logic. 
We are not attempting to develop a profitable strategy in this example!  
Say we Buy when "openingPrice" is somewhere under previous day close
and Short if "openingPrice" is somewhere above previous day close

// Buy logic
myBuyCondition = openingPrice < previousClose * 0.999;
// Sell logic
mySellCondition = Close > openingPrice * 1.001;

// Short logic
myShortCondition = openingPrice > previousClose * 1.001;
// Cover logic
myCoverCondition = Close < openingPrice * 0.999;

// ========= Generate signals =============

// If our conditions was filled on the previous bar, generate a signal: 

Buy = Ref(myBuyCondition,-1) AND tn == entryTime;
Sell = Ref(mySellCondition,-1) OR (tn == thisDayExitTime);

Short = Ref(myShortCondition,-1) AND tn == entryTime;
Cover = Ref(myCoverCondition,-1) OR (tn == thisDayExitTime);

// ======== Send signals to your Collective2 system =======
// Market entry prices with profitTarget. Adjust profit target prices as you like. 
// All generated signals visible in Exploration.     

C2TradingSignal(sigBTO, profitTarget = openingPrice * 1.001, SigFilter = Buy);  
C2TradingSignal(sigSTO, profitTarget = openingPrice * 0.999, SigFilter = Short); 

// =========== Investigate LONG trades data, conditions and prices ============
Filter = 1;

Let's look what happens when we run Exploration and Backtest for long trades only.

We do not have any previousClose at the very first day in the range:

Figure 8. First exploration rows

First exploration rows

We have the first previousClose on the second day. The value is repeated throughout the all day, so we can compare it with the other data of that day.

We can also see that our buy condition myBuyCondition is TRUE at the very first bar on the second day. But we don't care. We have no BUY signal yet, because we have exact time entryTime for entering to positions: Buy = Ref(myBuyCondition,-1) AND tn == entryTime;

Figure 9. The second day data

The second day data

The first Buy signal is at 2016-07-01 09:40:00. Its price is the Open price, which is dictated by the BuyPrice = Open; command.

Our buying condition was filled on the previous bar and the Buy signal is generated one bar later. You can play with that if you do not like that.

Figure 10. The first BUY signal

The first BUY signal

We can see that trade in the Backtest trades list. The trade is closed three bars later and its price in the backtest is the open price of its closing bar. It mimics a market price.

Figure 11. The first trade

The first trade