Another style of trading includes using a preset profit target from the entry price and a preset stop order to manage the risk. If one order is filled, then the other order is automatically cancelled. This is an easy setup to incorporate using the XLS Trader functionality.
First, a few additional modifications have been made. Cell B3 is now the location of the symbol. Cell C3 has an RTD call for the tick size using the symbol in cell B3:
=RTD("cqg.rtd", ,"ContractData",B3, "TickSize",, "T")
We need the tick size for setting the target order price and stop order price as they are set based on the entry price plus or minus the number of ticks times the tick size. In addition, cell B4 has an RTD call for the previous 1-minute’s bar close. This price is used for the limit price for the Buy order. This could be any study value you want to use that outputs a tradeable price. If this is a real-time updating price then the limit order will also update. The RTD formula in cell B4:
=RTD("cqg.rtd",,"StudyData",B3,"Bar","","Close","1","-1","all","","","False","T","ExcelInterval")
As a side note, an Excel RTD sample spreadsheet detailing most chart types and studies is located here:
http://news.cqg.com/workspaces/main/2012/11/cqg-to-rtd-excel.html
If you are using RTD study formulas, it is important that you modify the Excel RTD Throttle. The Excel default value is to update every 2,000 milliseconds. You should lower it to zero.
http://news.cqg.com/blogs/2011/04/adjusting-rtd-interval-throttle-microsoft-excel
Also, be sure your Excel is set to update automatically. Go to Formulas/Calculation Options and select “Automatic.”
Cell B6 has the OpenPosition function. The value displayed is the number of contracts long from the Buy order in cell A5.
The OpenPosition function is important because this function declares the size of the filled order and fires the target and stop orders in when there is a filled position from the order in Cell A5. Before discussing the Exit Target and Exit Stop Loss order let us look at how the prices for these two orders are set.
First, we need the filled order price to determine the target and stop orders prices. Cell C6 has the AverageEntryPrice function.
This function uses the same parameters as the OpenPosition function, but returns the average price. We will use the average price as the starting point for the target and stop orders. Cell B7 has the number of ticks used for target price. This is currently a manual entry. The Target Price is simply the number of ticks times the tick size and the product is added to the average price. Cell C8:
=C6+(B7*C3)
The Stop Price is the number of ticks times the tick size and the product is subtracted from the average price. The stop order price is calculated in Cell C10:
=C6-(B7*C3)
Now, that we have the Target Order Price and the Stop Order Price we can write the Exit functions. Cell A8 has the Target Order. Notice that the Order parameter is not a cell reference but is a Limit Order function with the cell references for the symbol (B3), size (B6), price C8), and duration (Day) Also, the “When” parameter is blank. By using the cell reference to the OpenPosition the target order is sent in when this value is available, in other words, there has been a fill.
Cell A10 has the Stop Order. In addition, as with the Exit Target Order, the Exit Stop Order is not using a cell reference for the order but is using the Stop order function with cell references for the parameters. Again, the When parameter is left blank.
Above, the Exit Order is using a Stop order. Also available is a Stop Limit order type. It has an additional parameter for setting the Limit price of the Stop Limit Order.
Below, is an example of the system long a position and working a target and stop orders. After entering 1 into Cell B5 (the “When” parameter for the Buy Order) the system is now long 5 E-mini S&P contracts at 2747.50 and has placed a target order 10 ticks above the entry price and a stop order 10 ticks below the entry price.
If you had multiple entries and your average filled price was not a tradeable price then the target order price and the stop order price would not display tradeable prices in cell C8 and C10. The system will automatically adjust the target price and stop price to tradeable prices for the actual orders.
As an added feature, you may want to have the ability to exit at the market and simultaneously cancel both the target and stop orders. Cell A12 has that feature using Cell B12 for the When parameter. Cell B12 could be a conditional IF Then value using a study returning a 1 if True.
Cell A12 has the Exit function using the Market Order with cell references in the Order parameter. You can see the When parameter is Cell B12, currently showing 0. If you manually enter 1 into Cell B12 the current open position will be exited using a market order. In addition, because Cell B6 (OpenPosition function) will now read 0 then the Exit Target and Exit Stop orders will be canceled.
This sample detailing using a moving average price for the entry price and then a set target and stop loss OCO exits is a straight-forward example of using the XLS Trader functionality. This could be made more complex by using studies, such as Bollinger Bands for a target price and a moving average for a trailing stop price.