top of page

Divergence RSI on US Crude Oil

A Mean-Reversion Strategy for US Crude Oil WTI


The Idea

This strategy is mainly built on a single indicator, the RSI Divergence from ProRealCode. This indicator detects bullish and bearish divergences between price and the RSI. A bullish divergence occurs when the stock price makes new lows while the indicator starts to climb upward. A bearish divergence occurs when the stock price makes new highs while the indicator starts to go lower. We also implement a moving average crossover as a filter. So with something as simple as one indicator and one filter we can get something quite interesting. Out-of-sample for this strategy is since 2021-01-01.



Setup for Backtest

Market: US Crude Oil (WTI)

Contract: 1 € per point

Broker: IG

Testing environment: ProRealtime 12

Timeframe: Daily

Time zone: CET

No fees and commissions are included.


Result

Total gain: 28 699.3 €

Average gain: 123.17 €

Total trades: 233

Winners: 172

Losers: 61

Breakeven: 0

Max drawdown: 2 887.7 €

Risk/reward ratio: 1.15

Total time in the market: 35.52 %

Average time in the market: 11 days, 15 hours

CAGR (10 000 € in starting capital): 4.61 %


Backtest Results

Entry Conditions

Long Entry

  1. MA[20] is higher today than yesterday.

  2. A bullish signal from the RSI Divergence Indicator [3,40,70,20].

Short Entry

  1. MA[20] is lower than yesterday.

  2. MA[10] is also lower than yesterday.

  3. A bearish signal from the RSI Divergence Indicator [3,20,70,20].


Exit Conditions

Long Exit

  1. A bearish signal from the RSI Divergence Indicator [3,40,70,20]

  2. Or if the number of bars since entry exceeds 40.

Short Exit

  1. A bullish signal from the RSI Divergence Indicator [3,20,70,20]

  2. Or if the number of bars since entry exceeds 40.


Code

ProRealTime

//-------------------------------------------------------------------------

// Concept: RSI Divergence (v.0.2 = RSI div for both entry and exit)

// Market: WTI

// Direction: Long and short

// Timeframe: Daily

// Timezone: CET

// Versions: v.0.1 | Algomatictrading.com | 2024-03-03 (OOS since: 2021-01-01)

//-------------------------------------------------------------------------

defparam preloadbars = 5000

defparam cumulateorders = false


// POSITION MANAGEMENT

//-------------------------------------------------------------------------

once positionsize = 1


// RISK MANAGEMENT

//-------------------------------------------------------------------------

once sll = 0

once sls = 0


// INDICATORS

//-------------------------------------------------------------------------

bst = barindex-tradeindex

ibs = (close-low)/max(range,0.0001)

ma1 = average[20]

ma2 = average[10]


// TRADING CONDITIONS

//-------------------------------------------------------------------------

// Long - Enter

// ------------------

cl = ma1 > ma1[1]

cl = cl and divergencersi[3,40,70,20] = 1


// Long - Exit

// ------------------

clx = divergencersi[3,40,70,20] = -1 or bst >= 40


// Short - Enter

// ------------------

cs = ma1 < ma1[1]

cs = cs and ma2 < ma2[1]

cs = cs and divergencersi[3,20,70,20] = -1


// Short - Exit

// ------------------

csx = divergencersi[3,20,70,20] = 1 or bst >=40


// TRADING ACTION

//-------------------------------------------------------------------------

// Enter

// ------------------

if cl then

buy positionsize contract at market

set stop %loss sll

elsif cs then

sellshort positionsize contract at market

set stop %loss sls

endif


// Exit

// ------------------

if longonmarket and clx then

sell at market

elsif shortonmarket and csx then

exitshort at market

endif


Read more about the RSI Divergence Indicator here: https://www.prorealcode.com/documentation/divergencersi/


FAQ

Q: Am I free to use this strategy however I want?

A: Sure! But we're happy if you refer to our website and keep the creator's name in the code.


Q: I'm using another platform, can you write the code in another language?

A: No, sorry. We might add code in more languages later to the website, but until then, we recommend you try ChatGPT or something similar to help you rewrite it.


Q: Why does my backtest look different when I run the code?

A: The strategy might give different backtest results for several reasons, like added spread or fees, the wrong time-zone settings, or that you're using another type of contract or instrument.


Q: Will I make money if I run this strategy live?

A: We don't know. We're not financial advisors, we're just traders sharing our ideas.


If you want to start trading with ProRealTime, you can try it here for free:



bottom of page