Fourier Analysis Decomposition & RMS Energy For ThinkOrSwim

DXD, indicator testing thru close today.

Yesterday, the dollar fell, and the DeComp saw a radical plsitioning for todays open, it could go either way. let us watch the DXD Chart
 
Only for demonstrations, not investment advice
Sample chart is XFOR in a struggle with average green shorting, heavy greater value (blue)
corrected link for XFOR chart
marked with time sequence and a maybe on the wave change at 12:45 or so.
note: the timing on the change is not always exact, sometime before, sometime afte
 
Last edited by a moderator:
Only for study demonstration, not investment advice

Innovative Eyewear -LUCY- topped the scanner thru-out day

higher time Study Chart: LUCY (90day/30min) set @14

Code:
####################################################
#
#  FOURIER TRANSFORM DECOMPOSITION
#  RMS ENERGY FOR 1st, 3rd, 5th, and 7th Harmonics
#
#  A guage of market instability and flutter
#
#  by mashume for the usethinkscript.com community
#
#  2022.03.28
#
#  Released the MIT License as open source
#  (c) mashume 2022
#
####################################################

declare lower;
declare once_per_bar;

def pi = Double.Pi;
input n = 64;

script ReX_k {
    input k = 0;
    input n = 64;
    def return_value = fold i = 0 to n - 1 with value = 0 do value + close[i] * Cos(2 * Double.Pi * k * i / n);
    plot ReX_k = return_value;
};

script ImX_k {
    input k = 0;
    input n = 64;
    def return_value = fold j = 0 to n - 1 with value = 0 do value + close[j] * Sin(2 * Double.Pi * k * j / n);
    plot ImX_k = -return_value;
};

script ReX {
    input k = 0;
    input n = 64;
    def return_value =
        if (k != 0 and k != (n / 2))
            then 2 * ReX_k(k, n) / n
        else if k == 0
            then ReX_k(k, n) / n
        else if k == n / 2
            then ReX_k(k, n) / n
        else 0;
    plot ReX = return_value;
};

script ImX {
    input k = 0;
    input n = 64;
    plot ImX_value = -2 * ImX_k(k, n) / n;
};

script x {
    input i = 1;
    input k = 0;
    input n = 64;
    def sum1 = fold a = 0 to n / 2 with value = 0.0 do value + ReX(k, n) * Cos(2 * Double.Pi * k * (n - a) / n);
    def sum2 = fold b = 0 to n / 2 with value2 = 0.0 do value2 + ImX(k, n) * Sin(2 * Double.Pi * k * (n - b)/ n);
    plot x = sum1;
};

def curve_1 = x(i = 0, k = 1, n = n);
def curve_2 = x(i = 0, k = 3, n = n);
def curve_3 = x(i = 0, k = 5, n = n);
def curve_4 = x(i = 0, k = 7, n = n);

def d = 2 * n;

def c1_mean = Average(curve_1, d);
def c1_rms = sqrt(fold i1 = 0 to d with ms1 = 0 do ms1 + sqr(curve_1[i1] - c1_mean));

def c2_mean = Average(curve_2, floor(d / 2));
def c2_rms = sqrt(fold i2 = 0 to floor(d / 2) with ms2 = 0 do ms2 + sqr(curve_2[i2] - c2_mean));

def c3_mean = Average(curve_3, floor(d / 4));
def c3_rms = sqrt(fold i3 = 0 to floor(d / 4) with ms3 = 0 do ms3 + sqr(curve_3[i3] - c3_mean));

def c4_mean = Average(curve_4, floor(d / 6));
def c4_rms = sqrt(fold i4 = 0 to floor(d / 6) with ms4 = 0 do ms4 + sqr(curve_4[i4] - c4_mean));

def bar = barNumber();
AddVerticalLine(visible = bar == highestAll(bar) - d, "2n", color.red);
AddVerticalLine(visible = bar == round(highestAll(bar) - (d / 2), 0), "2n / 3", color.orange);
AddVerticalLine(visible = bar == round(highestAll(bar) - (d / 4), 0), "2n / 4", color.green);
AddVerticalLine(visible = bar == round(highestAll(bar) - (d / 6), 0), "2n / 6", color.cyan);

def dist_sum = c1_rms + c2_rms * 4 + c3_rms * 8 + c4_rms * 16;

plot FirstHarmonicPercent = c1_rms / dist_sum;
plot ThirdHarmonicPercent = (c2_rms / dist_sum) * 4;
plot FifthHarmonicPercent = (c3_rms / dist_sum) * 8;
plot SeventhHarmonicPercent = (c4_rms / dist_sum) * 16;

FirstHarmonicPercent.setDefaultColor(color.red);
ThirdHarmonicPercent.setDefaultColor(color.orange);
FifthHarmonicPercent.setDefaultColor(color.green);
SeventhHarmonicPercent.setDefaultColor(color.cyan);

10/3 down trend reversal on news, day before Greater Value (GV), runs well over 0.33 and stays over 0.5 into 2ish, shorts run sell off.
10/5 reaction buyers (RB) cross up and over GV, sellershorts (SS) following trend increasing risk

lower time, Bottom Study Chart: LUCY (30day/2min) set @64

10/6 GV run , support with increasing bigger buyer (bb) support, release of Oct presentation.
1:00-ish SS last effort, concede to RB and BB,
15:45 First wave formation ceases :( <Pierce thinks this one is toast>
 
@mashume
Is there a way to add an alert for when, lets say "the Red wave has the highest value"?
I'm just hacking this together here in UTS, not in the thinkscript editor... it may not work but it should be close:
Code:
Alert(FirstHarmonicPercent > max(ThirdHarmonicPercent, max(FifthHarmonicPercent, SeventhHarmonicPercent)), "First harmonic (red) is more energetic than all others");

-mashume
 
I'm just hacking this together here in UTS, not in the thinkscript editor... it may not work but it should be close:
Code:
Alert(FirstHarmonicPercent > max(ThirdHarmonicPercent, max(FifthHarmonicPercent, SeventhHarmonicPercent)), "First harmonic (red) is more energetic than all others");

-mashume
@mashume Amazing and thank you so much!!! I find this indicator vital in volatile markets.
 

FOURIER TRANSFORM RMS ENERGY

link: http://tos.mx/1Wkl4Ht
DESCRIPTION
This indicator is an attempt to look at the energy present in the market's price action by looking at the RMS (root mean squared) energy shared between four harmonic fourier transformations of the CLOSE price series.

It does this by calculating the Fourier transform approximation of the 1st harmonic (the length parameter defines half a wavelength, so that the default value of 64 is actually looking at a wave 128 bars in length) along with the 3rd, 5th, and 7th harmonics. The RMS energy in these waves is then calculated for lengths of L, L/2, L/4, and L/6. These lengths are shown as vertical dashed lines on the chart for ease of reference. The RMS energy of the four waves is summed, and the portion of that energy possessed by each of the waves is plotted.

In keeping with my love of physics, the longest wavelengths are red, followed by orange, green, and the shortest is blue. This is really the only way the colors make any sense to my poor head.

USAGE
when reading this indicator, note when the red line has the highest position. That indicates that the long-term wave (1st harmonic) has the bulk of the energy in the Fourier decomposition. Conversely if the blue line has the greatest value, the price movements have generally been short term but dramatic. If either of the other lines, orange or green are showing energy, fluctuations in the middle are more likely.

I use this to determine whether I should expect to be in a trade for a short time (measured in bars) or a long time.


Code:
####################################################
#
#  FOURIER TRANSFORM DECOMPOSITION
#  RMS ENERGY FOR 1st, 3rd, 5th, and 7th Harmonics
#
#  A guage of market instability and flutter
#
#  by mashume for the usethinkscript.com community
#
#  2022.03.28
#
#  Released the MIT License as open source
#  (c) mashume 2022
#
####################################################

declare lower;
declare once_per_bar;

def pi = Double.Pi;
input n = 64;

script ReX_k {
    input k = 0;
    input n = 64;
    def return_value = fold i = 0 to n - 1 with value = 0 do value + close[i] * Cos(2 * Double.Pi * k * i / n);
    plot ReX_k = return_value;
};

script ImX_k {
    input k = 0;
    input n = 64;
    def return_value = fold j = 0 to n - 1 with value = 0 do value + close[j] * Sin(2 * Double.Pi * k * j / n);
    plot ImX_k = -return_value;
};

script ReX {
    input k = 0;
    input n = 64;
    def return_value =
        if (k != 0 and k != (n / 2))
            then 2 * ReX_k(k, n) / n
        else if k == 0
            then ReX_k(k, n) / n
        else if k == n / 2
            then ReX_k(k, n) / n
        else 0;
    plot ReX = return_value;
};

script ImX {
    input k = 0;
    input n = 64;
    plot ImX_value = -2 * ImX_k(k, n) / n;
};

script x {
    input i = 1;
    input k = 0;
    input n = 64;
    def sum1 = fold a = 0 to n / 2 with value = 0.0 do value + ReX(k, n) * Cos(2 * Double.Pi * k * (n - a) / n);
    def sum2 = fold b = 0 to n / 2 with value2 = 0.0 do value2 + ImX(k, n) * Sin(2 * Double.Pi * k * (n - b)/ n);
    plot x = sum1;
};

def curve_1 = x(i = 0, k = 1, n = n);
def curve_2 = x(i = 0, k = 3, n = n);
def curve_3 = x(i = 0, k = 5, n = n);
def curve_4 = x(i = 0, k = 7, n = n);

def d = 2 * n;

def c1_mean = Average(curve_1, d);
def c1_rms = sqrt(fold i1 = 0 to d with ms1 = 0 do ms1 + sqr(curve_1[i1] - c1_mean));

def c2_mean = Average(curve_2, floor(d / 2));
def c2_rms = sqrt(fold i2 = 0 to floor(d / 2) with ms2 = 0 do ms2 + sqr(curve_2[i2] - c2_mean));

def c3_mean = Average(curve_3, floor(d / 4));
def c3_rms = sqrt(fold i3 = 0 to floor(d / 4) with ms3 = 0 do ms3 + sqr(curve_3[i3] - c3_mean));

def c4_mean = Average(curve_4, floor(d / 6));
def c4_rms = sqrt(fold i4 = 0 to floor(d / 6) with ms4 = 0 do ms4 + sqr(curve_4[i4] - c4_mean));

def bar = barNumber();
AddVerticalLine(visible = bar == highestAll(bar) - d, "2n", color.red);
AddVerticalLine(visible = bar == round(highestAll(bar) - (d / 2), 0), "2n / 3", color.orange);
AddVerticalLine(visible = bar == round(highestAll(bar) - (d / 4), 0), "2n / 4", color.green);
AddVerticalLine(visible = bar == round(highestAll(bar) - (d / 6), 0), "2n / 6", color.cyan);

def dist_sum = c1_rms + c2_rms * 4 + c3_rms * 8 + c4_rms * 16;

plot FirstHarmonicPercent = c1_rms / dist_sum;
plot ThirdHarmonicPercent = (c2_rms / dist_sum) * 4;
plot FifthHarmonicPercent = (c3_rms / dist_sum) * 8;
plot SeventhHarmonicPercent = (c4_rms / dist_sum) * 16;

FirstHarmonicPercent.setDefaultColor(color.red);
ThirdHarmonicPercent.setDefaultColor(color.orange);
FifthHarmonicPercent.setDefaultColor(color.green);
SeventhHarmonicPercent.setDefaultColor(color.cyan);



EYE CANDY AND DESCRIPTION

This is not investment advice.
View attachment 14145


Several new indicators are included in this screen shot. But I'm lazy and only did one write up. You can find the other threads somewhere on usethinkscript.com


A
The LINEAR REGRESSION CENTERLINE EXCURSION indicator shows a clear enter signal which lasts nicely through the bulk of the upward movement.

B
The PRICE ACTION within LINEAR REGRESSION CHANNEL indicator shows a squeeze and a bounce off the lower line which corresponds nicely with a move upward in the price chart.

C
The LINEAR REGRESSION CENTERLINE EXCURSION indicator shows a good short signal, though the signal does not last as long as the downward trend.

D
Fourier RMS shows an increase in short term energy as a proportion of the total energy (the blue line rises and the red falls).

E
Fourier RMS shows a dramatic decrease in the short term (blue) line and a distinct rise in the long term (red).
How do get the 6th and 9th harmonics or there is no such thing?
 
Last edited:
How do get the 6th and 9th harmonics or there is no such thing?
There are certainly as many harmonics as you care to explore. To change the ones displayed, look for these lines:
Code:
def curve_1 = x(i = 0, k = 1, n = n);
def curve_2 = x(i = 0, k = 3, n = n);
def curve_3 = x(i = 0, k = 5, n = n);
def curve_4 = x(i = 0, k = 7, n = n);

the value of k tells the program which harmonic to calculate. adjust to your liking.

Adding additional plots would be more involved. but possible if you can't get the signals you need from these four.

-mashume
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
411 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top