Farey Sequence Weighted Moving Average For ThinkOrSwim

Author states: A moving average that weighted with Farey fractions. It matches a standard linear weighted average almost one-to-one. Why? Because both averages have strictly monotonic weighting sequences and assign a higher weight to latests data. So, Farey weights are just scaled to [0, 1] linear ones. Instead of specifing period you specify an order of Farey sequence. To learn more about Farey sequence

U0xoYcs.png

It is a rather unusual version of a weighted moving average. Once translated, we can make comparisons to others in TOS
Background info on Farey Sequence - https://en.wikipedia.org/wiki/Farey_sequence

Here is the original Tradingview code:
https://www.tradingview.com/script/UQ48Qh3y-Farey-Sequence-Weighted-Moving-Average/

For the new ThinkOrSwim code, you must scroll down to the next post
 
Last edited by a moderator:

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

Interesting. I do love interesting mathematical sequences. Thank your for bringing this one to my attention.

Code:
# //@version=4
# // Copyright (c) 2019-present, Alex Orekhov (everget)
# // Farey Sequence Weighted Moving Average script may be freely distributed under the terms of the GPL-3.0 license.
# study("Farey Sequence Weighted Moving Average", shorttitle="FSWMA", overlay=true)
# ThinkScript conversion by mashume for UseThinkScript
input plot_type = {default "5", "2","3","4","6","7","8","9","10","11","12","13","14"};

def src = close;

def src1 = src[1];
def src2 = src[2];
def src3 = src[3];
def src4 = src[4];
def src5 = src[5];
def src6 = src[6];
def src7 = src[7];
def src8 = src[8];
def src9 = src[9];
def src10 = src[10];
def src11 = src[11];
def src12 = src[12];
def src13 = src[13];
def src14 = src[14];
def src15 = src[15];
def src16 = src[16];
def src17 = src[17];
def src18 = src[18];
def src19 = src[19];
def src20 = src[20];
def src21 = src[21];
def src22 = src[22];
def src23 = src[23];
def src24 = src[24];
def src25 = src[25];
def src26 = src[26];
def src27 = src[27];
def src28 = src[28];
def src29 = src[29];
def src30 = src[30];
def src31 = src[31];
def src32 = src[32];
def src33 = src[33];
def src34 = src[34];
def src35 = src[35];
def src36 = src[36];
def src37 = src[37];
def src38 = src[38];
def src39 = src[39];
def src40 = src[40];
def src41 = src[41];
def src42 = src[42];
def src43 = src[43];
def src44 = src[44];
def src45 = src[45];
def src46 = src[46];
def src47 = src[47];
def src48 = src[48];
def src49 = src[49];
def src50 = src[50];
def src51 = src[51];
def src52 = src[52];
def src53 = src[53];
def src54 = src[54];
def src55 = src[55];
def src56 = src[56];
def src57 = src[57];
def src58 = src[58];
def src59 = src[59];
def src60 = src[60];
def src61 = src[61];
def src62 = src[62];
def src63 = src[63];

plot Avg;
Avg.SetDefaultColor(color.dark_orange);
Avg.SetLineWeight(2);
switch (plot_type) {
case "2":
    Avg = (src + 0.5 * src1) / 1.5;
case "3":
    Avg = (src + 0.667 * src1 + 0.5 * src2 + 0.333 * src3) / 2.5;
case "4":
    Avg = (src + 0.75 * src1 + 0.667 * src2 + 0.5 * src3 + 0.333 * src4 + 0.25 * src5) / 3.5;
case "5":
    Avg = (src + 0.8 * src1 + 0.75 * src2 + 0.667 * src3 + 0.6 * src4 + 0.5 * src5 + 0.4 * src6 + 0.333 * src7 + 0.25 * src8 + 0.2 * src9) / 5.5;
case "6":
    Avg = (src + 0.833 * src1 + 0.8 * src2 + 0.75 * src3 + 0.667 * src4 + 0.6 * src5 + 0.5 * src6 + 0.4 * src7 + 0.333 * src8 + 0.25 * src9 + 0.2 * src10 + 0.167 * src11) / 6.5;
case "7":
    Avg = (src + 0.857 * src1 + 0.833 * src2 + 0.8 * src3 + 0.75 * src4 + 0.714 * src5 + 0.667 * src6 + 0.6 * src7 + 0.571 * src8 + 0.5 * src9 + 0.429 * src10 + 0.4 * src11 + 0.333 * src12 + 0.286 * src13 + 0.25 * src14 + 0.2 * src15 + 0.167 * src16 + 0.143 * src17) / 9.5;
case "8":
    Avg = (src + 0.875 * src1 + 0.857 * src2 + 0.833 * src3 + 0.8 * src4 + 0.75 * src5 + 0.714 * src6 + 0.667 * src7 + 0.625 * src8 + 0.6 * src9 + 0.571 * src10 + 0.5 * src11 + 0.429 * src12 + 0.4 * src13 + 0.375 * src14 + 0.333 * src15 + 0.286 * src16 + 0.25 * src17 + 0.2 * src18 + 0.167 * src19 + 0.143 * src20 + 0.125 * src21) / 11.5;
case "9":
    Avg = (src + 0.889 * src1 + 0.875 * src2 + 0.857 * src3 + 0.833 * src4 + 0.8 * src5 + 0.778 * src6 + 0.75 * src7 + 0.714 * src8 + 0.667 * src9 + 0.625 * src10 + 0.6 * src11 + 0.571 * src12 + 0.556 * src13 + 0.5 * src14 + 0.444 * src15 + 0.429 * src16 + 0.4 * src17 + 0.375 * src18 + 0.333 * src19 + 0.286 * src20 + 0.25 * src21 + 0.222 * src22 + 0.2 * src23 + 0.167 * src24 + 0.143 * src25 + 0.125 * src26 + 0.111 * src27) / 14.5;
case "10":
    Avg = (src + 0.9 * src1 + 0.889 * src2 + 0.875 * src3 + 0.857 * src4 + 0.833 * src5 + 0.8 * src6 + 0.778 * src7 + 0.75 * src8 + 0.714 * src9 + 0.7 * src10 + 0.667 * src11 + 0.625 * src12 + 0.6 * src13 + 0.571 * src14 + 0.556 * src15 + 0.5 * src16 + 0.444 * src17 + 0.429 * src18 + 0.4 * src19 + 0.375 * src20 + 0.333 * src21 + 0.3 * src22 + 0.286 * src23 + 0.25 * src24 + 0.222 * src25 + 0.2 * src26 + 0.167 * src27 + 0.143 * src28 + 0.125 * src29 + 0.111 * src30 + 0.1 * src31) / 16.5;
case "11":
    Avg = (src + 0.909 * src1 + 0.9 * src2 + 0.889 * src3 + 0.875 * src4 + 0.857 * src5 + 0.833 * src6 + 0.818 * src7 + 0.8 * src8 + 0.778 * src9 + 0.75 * src10 + 0.727 * src11 + 0.714 * src12 + 0.7 * src13 + 0.667 * src14 + 0.636 * src15 + 0.625 * src16 + 0.6 * src17 + 0.571 * src18 + 0.556 * src19 + 0.545 * src20 + 0.5 * src21 + 0.455 * src22 + 0.444 * src23 + 0.429 * src24 + 0.4 * src25 + 0.375 * src26 + 0.364 * src27 + 0.333 * src28 + 0.3 * src29 + 0.286 * src30 + 0.273 * src31 + 0.25 * src32 + 0.222 * src33 + 0.2 * src34 + 0.182 * src35 + 0.167 * src36 + 0.143 * src37 + 0.125 * src38 + 0.111 * src39 + 0.1 * src40 + 0.091 * src41) / 21.5;
case "12":
    Avg = (src + 0.917 * src1 + 0.909 * src2 + 0.9 * src3 + 0.889 * src4 + 0.875 * src5 + 0.857 * src6 + 0.833 * src7 + 0.818 * src8 + 0.8 * src9 + 0.778 * src10 + 0.75 * src11 + 0.727 * src12 + 0.714 * src13 + 0.7 * src14 + 0.667 * src15 + 0.636 * src16 + 0.625 * src17 + 0.6 * src18 + 0.583 * src19 + 0.571 * src20 + 0.556 * src21 + 0.545 * src22 + 0.5 * src23 + 0.455 * src24 + 0.444 * src25 + 0.429 * src26 + 0.417 * src27 + 0.4 * src28 + 0.375 * src29 + 0.364 * src30 + 0.333 * src31 + 0.3 * src32 + 0.286 * src33 + 0.273 * src34 + 0.25 * src35 + 0.222 * src36 + 0.2 * src37 + 0.182 * src38 + 0.167 * src39 + 0.143 * src40 + 0.125 * src41 + 0.111 * src42 + 0.1 * src43 + 0.091 * src44 + 0.083 * src45) / 23.5;
case "13":
    Avg = (src + 0.923 * src1 + 0.917 * src2 + 0.909 * src3 + 0.9 * src4 + 0.889 * src5 + 0.875 * src6 + 0.857 * src7 + 0.846 * src8 + 0.833 * src9 + 0.818 * src10 + 0.8 * src11 + 0.778 * src12 + 0.769 * src13 + 0.75 * src14 + 0.727 * src15 + 0.714 * src16 + 0.7 * src17 + 0.692 * src18 + 0.667 * src19 + 0.636 * src20 + 0.625 * src21 + 0.615 * src22 + 0.6 * src23 + 0.583 * src24 + 0.571 * src25 + 0.556 * src26 + 0.545 * src27 + 0.538 * src28 + 0.5 * src29 + 0.462 * src30 + 0.455 * src31 + 0.444 * src32 + 0.429 * src33 + 0.417 * src34 + 0.4 * src35 + 0.385 * src36 + 0.375 * src37 + 0.364 * src38 + 0.333 * src39 + 0.308 * src40 + 0.3 * src41 + 0.286 * src42 + 0.273 * src43 + 0.25 * src44 + 0.231 * src45 + 0.222 * src46 + 0.2 * src47 + 0.182 * src48 + 0.167 * src49 + 0.154 * src50 + 0.143 * src51 + 0.125 * src52 + 0.111 * src53 + 0.1 * src54 + 0.091 * src55 + 0.083 * src56 + 0.077 * src57) / 29.5;
case "14":
    Avg = (src + 0.929 * src1 + 0.923 * src2 + 0.917 * src3 + 0.909 * src4 + 0.9 * src5 + 0.889 * src6 + 0.875 * src7 + 0.857 * src8 + 0.846 * src9 + 0.833 * src10 + 0.818 * src11 + 0.8 * src12 + 0.786 * src13 + 0.778 * src14 + 0.769 * src15 + 0.75 * src16 + 0.727 * src17 + 0.714 * src18 + 0.7 * src19 + 0.692 * src20 + 0.667 * src21 + 0.643 * src22 + 0.636 * src23 + 0.625 * src24 + 0.615 * src25 + 0.6 * src26 + 0.583 * src27 + 0.571 * src28 + 0.556 * src29 + 0.545 * src30 + 0.538 * src31 + 0.5 * src32 + 0.462 * src33 + 0.455 * src34 + 0.444 * src35 + 0.429 * src36 + 0.417 * src37 + 0.4 * src38 + 0.385 * src39 + 0.375 * src40 + 0.364 * src41 + 0.357 * src42 + 0.333 * src43 + 0.308 * src44 + 0.3 * src45 + 0.286 * src46 + 0.273 * src47 + 0.25 * src48 + 0.231 * src49 + 0.222 * src50 + 0.214 * src51 + 0.2 * src52 + 0.182 * src53 + 0.167 * src54 + 0.154 * src55 + 0.143 * src56 + 0.125 * src57 + 0.111 * src58 + 0.1 * src59 + 0.091 * src60 + 0.083 * src61 + 0.077 * src62 + 0.071 * src63) / 32.5;
  
}
 
Last edited:
Thank you @mashume. Great Script!

It is a rather unusual version of a weighted moving average. Once translated, we can make comparisons to others in TOS

So here is the first of the comparisons that you requested.

I added the below code snippet to the bottom of your study.
It would seem to highlight that the farey sequence provides the same transitions as the Heiken-Ashi Trend. Yes?

Ruby:
plot sigUP = if isnan(close) then double.NaN else open[1]<avg[1] and close[1]<avg[1] and close>avg;
sigUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
sigUP.SetDefaultColor(color.yellow);
sigUP.SetLineWeight(3);

plot sigDN = if isnan(close) then double.NaN else open[1]>avg[1] and close[1]>avg[1] and close<avg;
sigDN .SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sigDN .SetDefaultColor(color.red);
sigDN .SetLineWeight(2);
EOrEzj1.png
 
Last edited:
Looking at this after a cup of coffee, I wonder if all of the "-1" should be in there. I'm afraid I should probably remove them as they were a bit of a kludge and I note that this means duplicated data will be used in the calculations later (as src with no number does get used)... hmmm.

mashume


EDIT:

I removed the extraneous -1s from the code. It seems this is now correct. @MerryDay you may want to re-run your comparison to the HA candles above now that the code is corrected.
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
436 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