Trading Coffee Futures: Developing, Coding, and Optimizing a Trend-Following Strategy

In this article, we explore the development of a systematic trading strategy for coffee futures (@KC), which are traded on the Intercontinental Exchange (ICE) in New York. Our goal is to diversify a trading portfolio by incorporating lesser-known assets compared to more commonly traded futures.

Coffee futures belong to a category known as "soft commodities," which also includes futures contracts for sugar, cocoa, cotton, and orange juice. This market offers several appealing characteristics, including significant intraday volatility, a low correlation with equity futures, and decent trading volumes.

To analyze this market, we employ a Trend-Following approach, a strategy that has historically performed well across various commodity markets. Our objective is to determine whether this approach is equally effective for trading coffee futures.

Logic and Code Behind a Trend-Following Strategy for Coffee Futures

The strategy we aim to develop follows a Trend-Following approach, based on the breakout of significant highs and lows.

Specifically, the system enters a long position when the price surpasses the highest high recorded over a given number of sessions, defined as Highest(High, n_sessions). Conversely, it enters a short position when the price breaks below the lowest low over a set number of sessions, denoted as Lowest(Low, n_sessions).

The system remains active throughout the entire trading session of Coffee futures, which runs from 4:15 AM to 1:30 PM (exchange time), Monday through Friday. Entries and exits occur within the same trading day, meaning that all positions are closed by the end of the session unless the stop loss is triggered earlier. For this initial analysis, we set the Stop Loss at $1,500.

We evaluate the performance of our trading system in a 15-minute timeframe (data1) for trade execution and a daily timeframe (data2) for calculating entry levels, with historical data spanning from early 2010 to the end of 2023.

This concept translates into a few concise lines of code:

input:n_sessioni(5),mystop(1000),myprofit(0);

if EntriesToday(d)=0 then Buy next  bar at Highest(High,n_sessioni)data2 stop;

if EntriesToday(d)=0 then Sellshort next bar at Lowest(Low,n_sessioni)data2 stop;

if mystop>0 then setstoploss(mystop);

if myprofit>0 then setprofittarget(myprofit);

setstopcontract;

setexitonclose;

The first step in refining the strategy involves optimizing the number of sessions ("n_sessions") used to calculate breakout levels. To do this, we test values from 1 to 10 in increments of 1 and evaluate the impact on performance.

Figure 1, Optimization of the "n_sessions" input to identify the optimal number of sessions for calculating highs and lows.

The optimization reveals that as the number of sessions increases, net profit and overall performance metrics tend to deteriorate, except for maximum drawdown, which remains relatively stable. Based on this analysis, we select n_sessions = 2, meaning that our entry levels are calculated based on the past two sessions’ highs and lows. A breakout of these levels triggers a position entry.

Examining the results from our first test, we observe a profitable and steadily growing equity curve, confirming that Coffee futures respond well to Trend-Following logic. While further refinements will be necessary, the initial results are promising, with an average trade of $62 and a net profit of $151,000. Not bad for an early-stage system without any additional filtering or optimizations!