Indicator ScopeThe first indicator shall measure the percentage change between the
closeof each bar and compare it to the same percentage change of an alternative asset. In our case, the alternative asset will be the US Dollar Index. Additionally, we shall color the background when the two assets move in the same direction. This should allow us to more easily see when the two assets move together and spot trends in their moment.
//@version=3 study("Synergy") // Select the second instrument alt_sym = input("FX:USDOLLAR", type=symbol, title='Comparison Symbol') // Get OHLC from the other asset alt_o = security(alt_sym, period, open) alt_h = security(alt_sym, period, high) alt_l = security(alt_sym, period, low) alt_c = security(alt_sym, period, close) change = ((close - open) / open) * 100 alt_change = ((alt_c - alt_o) / alt_o ) *100 // Set the background color only if both instruments move in the same direction. bg_col = (change > 0 and alt_change < 0) or (change < 0 and alt_change > 0) ? na : (change != 0 and alt_change == 0) or (change == 0 and alt_change != 0) ? na : black bgcolor(bg_col, transp=90) // Plot the changes plot(alt_change, style=columns, color=blue) plot(change, style=columns, color=orange)
On the ChartsLoading up the indicator on “XAUUSD”, you should see something like this: The yellow bars show the percentage change in the price of gold. The blue bars show the percentage change in the price of the US Dollar Index. If the bar is above zero, it means that the asset closed up. Conversely, if it is below zero, it means the asset closed down. Finally, the grey bars show bars in which the two assets closed in the same direction.
Subjective AnalysisFirst, we will take a simple look through a selection of time-frames and list our observations. To attempt to have some uniformity between the comparisons, we will look at the last 300 bars for each time-frame. It should be noted that these comparisons are a little flawed as we are only looking at a single time period/market cycle. In an ideal world, we should repeat this analysis for other time periods/market cycles. However, on Tradingiview we can only work with the data that we have and for the lower time-frames that can be as little as a couple of weeks. You can, however, repeat part of these tests on higher time-frames if you so wish to. Click on the images below to see the the full chart and cycle through:
- On all time-frames, the instruments appear to move in opposite directions more frequently than they do in the same direction.
- The 1-minute time-frame has the most bars where the two instruments move in opposite directions (but we should be skeptical of this for the reasons outlined above)
- Trends where the two instruments move in the same direction rarely last more than 4 bars.
- Big moves in the US dollar always result in gold moving in the opposite direction
- However, the US dollar can move in the same direction as a big move in gold. Albeit, usually a very small move in the same direction.
- Gold appears to be more volatile than the US Dollar index.
ConfirmationSo now we have made some general observations, let’s see if we can back any of them up with some statistics. To do this we are going to edit and extend the indicator to provide the following statistics:
- A percentage showing how often the two assets move in the opposite direction over a given lookback period.
- Similarly, another percentage showing how often the two assets move in the same direction over the same lookback period.
- Count the number of times (occurrences) when the two assets move in the same direction for more than 4 bars.
- Count the number of times the US Dollar moves more than x%.
- Count the number of times that Gold moved in the same direction of the US Dollar when the US Dollar moved more than x%.
//@version=3 study("Synergy Stats") alt_sym = input("FX:USDOLLAR", type=symbol, title='Comparison Symbol') lkb = input(300, title='Lookback Period For Summing') bars = input(4, title='Both Instruments in Sync > x bars') thresh = input(0.1, step=0.1, title='Alt Instrument Change Threshold %') // Get our data for the other instrument alt_o = security(alt_sym, period, open) alt_h = security(alt_sym, period, high) alt_l = security(alt_sym, period, low) alt_c = security(alt_sym, period, close) // Work out the candle sentiment - this will help us later sent = open < close ? 1 : open > close ? -1 : 0 alt_sent = alt_o < alt_c ? 1 : alt_o > alt_c ? -1 : 0 change = ((close - open) / open) * 100 alt_change = ((alt_c - alt_o) / alt_o ) *100 // Observation: // ------------ // On all time-frames the instruments appear to move in opposite directions more // frequently than they do in the same direction // ----------------------------------------------------------------------------- in_sync = sent == alt_sent ? 1 : 0 opposite = sent != alt_sent ? 1 : 0 count_in_sync = sum(in_sync, lkb) count_opposite = sum(opposite, lkb) perc_in_sync = (count_in_sync / lkb) * 100 perc_opposite = (count_opposite / lkb) * 100 bg_col = (change > 0 and alt_change < 0) or (change < 0 and alt_change > 0) ? na : (change != 0 and alt_change == 0) or (change == 0 and alt_change != 0) ? na : black bgcolor(bg_col, transp=90) // Observation: // ------------ // A. Trends where the two instruments move in the same direction rarely last more // than 4 bars // // B. The 1 minute time-frames has the most bars where the two instruments move in // opposite directions (but we should be skeptical of this for the reasons outlined above). // ----------------------------------------------------------------------------- trend_count(bars)=> result = 1 for i = 0 to (bars) if sent[i] != alt_sent[i] result := 0 break result in_sync_trend = trend_count(bars) // Catch case where trend goes on for 6, 7 or 8 bars. We only want that to count as one // occurence count_in_sync_trend = in_sync_trend == 0 and in_sync_trend == 1 ? 1 : 0 total_in_sync_trend = 0 total_in_sync_trend := count_in_sync_trend == 1 ? nz(total_in_sync_trend) + 1 : total_in_sync_trend // Observation: // ------------ // Big moves in the US dollar always result in gold moving in the opposite direction // ----------------------------------------------------------------------------- above_thresh = alt_change > thresh count_above_thresh = 0 count_above_thresh := above_thresh ? nz(count_above_thresh) + 1 : count_above_thresh above_thresh_and_in_sync = 0 above_thresh_and_in_sync := above_thresh and in_sync ? nz(above_thresh_and_in_sync) + 1 : nz(above_thresh_and_in_sync) // PLOTTING // --------- plot(perc_in_sync, style=line, color=green, title='Percentage: Instruments Are In Sync') plot(perc_opposite, style=line, color=red, title='Percentage: Instruments Are Moving Opposite') plotshape(count_in_sync_trend == 1, style=shape.triangleup, color=purple, location=location.bottom, title='Marker: Trend In Sync > x Input') plotshape(above_thresh, style=shape.triangledown, color=blue, location=location.bottom, title='Marker: Alt Instrument > x Threshold') plot(total_in_sync_trend, style=stepline, color=purple, title='Count: Trend In Sync > x Input') plot(above_thresh_and_in_sync, style=stepline, color=blue, title='Count: Number of times Alt Instrument > x Threshold and In Sync') plot(count_above_thresh, style=stepline, title='Count: Number of times Alt Instrument > x Threshold')
AnalysisLet’s start with the first two observations. Those being that the two instruments close more often than not in opposite directions and that this is most prevalent on the 1-minute chart. If we load up the new indicator on the 1-minute chart we can see that apart from a short period around the 14th of December, we never dip below 80% for the given 300 bar look back period. In addition to this, at the time of writing the approximate/average percentage that the two instruments move in opposite directions on other timeframes break down as follows:
- 15M: Hovers around 70%
- 1H: Also revolves close to approximately 70%
- 4H: Is less consistent, more frequently ranges from 60-80%
- 1D: Similar to above, with a larger range from 60-77%
- 1W: Back to a more consistent 70%
Trends in which the instruments move in the same direction rarely last more than 4 barsMoving onto another observation, we will use the same stats indicator to provide insights on how many times we see the two instruments move in sync for more than 4 bars. In this example, we are now interested in the purple line. This line shows us how many times a trend lasted longer than a given input: And the results are as follows (again at the time of writing!): Very interesting indeed! So overall the observation appears to have some weight. However, it is interesting to note that the occurrences seem to increase as we move through higher timeframes (up to a point) with a big peak on the 4-hour timeframe. Personally, I had expected to higher timeframes to show fewer occurrences as I had assumed there would be more than enough time for traders to adjust to moves in the US Dollar.
Big moves in the US dollar always result in gold moving in the opposite directionNow we move onto the final observation that we will test. Of course, this is not everything observed in the first section but in fear of dragging this post out too long, we will leave it after this. For this statistic, we also count the occurrences instead of percentages. After all, it doesn’t mean much if 80% of the time gold is negatively affected by a big move in the US Dollar if that move only occurred 5 times in total. We are focused on the blue lines of the Indicator. These show us how many times the movement between the open and close in US Dollar was greater than our threshold and of those times, how often Gold moved in the same direction. During the course of these tests, the input needed to be varied across the time-frames. As such, I tried to use a value on each timeframe that resulted in between 100 and 200 occurrences. In other words, larger moves for that timeframe, but not overly rare so that our stats are meaningless. The percentages in the chart above indicate the move required in the US Dollar index to be considered a strong move. Also, note that the 1 minute and weekly timeframes were omitted from the test. The reason for this is that it was difficult to get a good number of occurrences. The weekly did not have enough data and the minute data seemed too sensitive to small changes to the input. A
0.001change saw a swing from 79 to over 350 occurrences! The results again are quite fascinating and unexpected. The first thing is that the initial observation that Gold always move in the opposite direction to a relatively big move in the US Dollar is untrue! Additionally, the trend appears that gold is less affected by dollar strength, the more we move up the time-frames. However, it should still be noted that on even on the daily, gold moves in sync with US Dollar strength only 24% of the time (137 strong moves to 33 moves in sync)!
Visibility NoteSince the indicator plots both counts and percentages, depending on your settings, the scale can sometimes get a bit wild. This can make the percentage lines hard to read if the counts are very high. In these cases, it is worth to remember that the lines that we are not interested in can be turned off in the style tab like so:
Warning / Further ImprovementsThe indicator is not perfect. It can provide incorrect results if you are looking at periods where the two data sets are a different length. In other words, data for one asset starts after the other. Take a look at the weekly chart for instance: The data is actually only valid after we have enough candles on BOTH data sets to cover the lookback period (in our case 300). This is because we need that many candles of data to calculate the value and since the data for the US Dollar starts later than Gold, the candles before this are providing garbage results. Note: This only affects the calculations that use a lookback period, like the percentage calculations. To further improve this, we could add a start date to prevent any calculations from happening until we have 300 bars of data on both assets.
Find This Post Useful?
If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny.
Backtest Rookies is a registered with Brave publisher!
Brave users can drop us a tip.
Alternatively, support us by switching to Brave using this referral link and we will receive some BAT!
Enjoying the content and thinking of subscribing to Tradingview? Support this site by clicking the referral link before you sign up!