The resource loading... loading...

JavaScript implementation of the Fisher pointer and mapping on FMZ

Author: Inventors quantify - small dreams, Created: 2021-12-27 09:51:39, Updated: 2023-09-15 21:04:46

img

JavaScript implementation of the Fisher pointer and mapping on FMZ

In technical analysis of transactions, traders analyze and study stock price data as normal distribution data. However, the distribution of stock price data does not meet the standard normal distribution.Fisher TransformationThis is a method of converting price data into a normal distribution.Fisher TransformationSmoothing the market data, eliminating some sharp small cyclical oscillations. Trading signals can be sent using the intersection of indicators for the day and the day before.

About theFisher TransformI'm not going to go into details here.

Algorithm of the indicator:

  • The average price today:

    mid=(low + high) / 2

  • To determine the calculation cycle, 10 days can be used as a cycle.

    lowestLow = 周期内最低价highestHigh = 周期内最高价

  • Definition of price variables (includingratioFor a constant between 0 and 1, e.g. 0.5 or 0.33):

    img

  • Parameters of price variationxUseFisherChange, get itFisherIndicators:

    img

This algorithm is implemented using JavaScript

This is a step-by-step implementation of the indicator algorithm. It is important to note that this algorithm is an iterative algorithm, which is used forpreXpreFishThe beginning is set to 0.Math.logThis is the logarithm of the base of the natural constant e. In addition, the above algorithm did not mention the correction for x, which I almost ignored when writing:

The value of the corrected x, greater than 0.99, is forced to be set to 0.999; less than -0.99 is equivalent.

        if (x > 0.99) {
            x = 0.999
        } else if (x < -0.99) {
            x = -0.999
        }

The first time I saw this algorithm, indicator, after the algorithm was transplanted. The implementation was not verified by me, and students interested in the study can verify that there are no errors. Thank you very much for pointing out the errors.

Fisher TransformThe indicator algorithm source code is:

function getHighest(arr, period) {
    if (arr.length == 0 || arr.length - period < 0) {
        return null 
    }
    var beginIndex = arr.length - period
    var ret = arr[beginIndex].High
    for (var i = 0 ; i < arr.length - 1 ; i++) {
        if (arr[i + 1].High > ret) {
            ret = arr[i + 1].High
        }
    }
    return ret
}

function getLowest(arr, period) {
    if (arr.length == 0 || arr.length - period < 0) {
        return null 
    }
    var beginIndex = arr.length - period
    var ret = arr[beginIndex].Low
    for (var i = 0 ; i < arr.length - 1 ; i++) {
        if (arr[i + 1].Low < ret) {
            ret = arr[i + 1].Low
        }
    }
    return ret
}

function calcFisher(records, ratio, period) {
    var preFish = 0 
    var preX = 0
    var arrFish = []
    // 当K线长度不足,不满足周期时
    if (records.length < period) {
        for (var i = 0 ; i < records.length ; i++) {
            arrFish.push(0)
        }
        return arrFish
    }

    // 遍历K线
    for (var i = 0 ; i < records.length ; i++) {
        var fish = 0
        var x = 0
        var bar = records[i]
        var mid = (bar.High + bar.Low) / 2
        // 当前BAR不足period计算时
        if (i < period - 1) {
            fish = 0
            preFish = 0
            arrFish.push(fish)
            continue
        }

        // 计算周期内最高价和最低价
        var bars = []
        for (var j = 0 ; j <= i ; j++) {
            bars.push(records[j])
        }
        var lowestLow = getLowest(bars, period)
        var highestHigh = getHighest(bars, period)               
        
        // 价变参数
        x = ratio * 2 * ((mid - lowestLow) / (highestHigh - lowestLow) - 0.5) + (1 - ratio) * preX
        if (x > 0.99) {
            x = 0.999
        } else if (x < -0.99) {
            x = -0.999
        }
        preX = x 
        fish = 0.5 * Math.log((1 + x) / (1 - x)) + 0.5 * preFish
        preFish = fish
        arrFish.push(fish)
    }
    return arrFish
}

Painting

It's easy to draw a map on FMZ, Strategy Square:https://www.fmz.com/squareThere are a lot of examples above that you can refer to or search for. In between posts, the following diagram test code requires the addition of the above calcFisher function implementation to run.

var cfg = {    // 用来初始化设置图表的对象(即图表设置)
    plotOptions: {
        candlestick: {
            color: '#d75442',  // 颜色值
            upColor: '#6ba583' // 颜色值
        }
    },
    title: { text: 'Fisher Transform'},     //标题
    subtitle: {text: ''},     //副标题
    plotOptions: {
        candlestick: {
        tooltip: {
            pointFormat: 
            '<span style="color:{point.color}">\u25CF</span> <b> {series.name}</b><br/>' +
            '开盘: {point.open}<br/>' +
            '最高: {point.high}<br/>' +
            '最低: {point.low}<br/>' +
            '收盘: {point.close}<br/>'
            }
        }
    },
    yAxis: [{
        title: {
            text: 'K线行情'
        },
        height: '70%',
        lineWidth: 1
    }, {
        title: {
            text: 'Fisher Transform'
        },
        top: '75%',
        height: '30%',
        offset: 0,
        lineWidth: 1
    }],
    series: [//系列
        {
            type: 'candlestick',
            yAxis: 0,
            name: 'K线',
            id: 'KLine',
            // 控制走势为跌的蜡烛颜色
            color: 'green',
            lineColor: 'green',
            // 控制走势为涨的蜡烛颜色
            upColor: 'red',
            upLineColor: 'red',
            data: []
        },{
            type: 'line',         // 设置当前的数据序列 类型为: 线
            yAxis: 1,             // 使用的y轴 为索引为 0 的y轴(highcharts 图表 可以有 多个 y 坐标轴,这里指定索引0的y轴)
            showInLegend: true,   // 
            name: 'fish',          // 根据 函数传入的 参数 label 设置
            lineWidth: 1,
            data: [],             // 数据序列的数据项
            tooltip: {            // 工具提示
                valueDecimals: 2  // 值的小数点 保留5位
            }
        },{
            type: 'line',         // 设置当前的数据序列 类型为: 线
            yAxis: 1,             // 使用的y轴 为索引为 0 的y轴(highcharts 图表 可以有 多个 y 坐标轴,这里指定索引0的y轴)
            showInLegend: true,   // 
            name: 'preFish',      // 根据 函数传入的 参数 label 设置
            lineWidth: 1,
            data: [],             // 数据序列的数据项
            tooltip: {            // 工具提示
                valueDecimals: 2  // 值的小数点 保留5位
            }
        }
    ]
}

var chart = Chart(cfg)
function main() {
    var ts = 0
    chart.reset()
    while (true) {
        var r = exchange.GetRecords()
        var fisher = calcFisher(r, 0.33, 10)                
        if (!r || !fisher) {
            Sleep(500)
            continue
        }
        
        for (var i = 0; i < r.length; i++){
            if (ts == r[i].Time) {
                chart.add([0,[r[i].Time, r[i].Open, r[i].High, r[i].Low, r[i].Close], -1])
                chart.add([1,[r[i].Time, fisher[i]], -1])
                if (i - 1 >= 0) {
                    chart.add([2,[r[i].Time, fisher[i - 1]], -1])
                }
            }else if (ts < r[i].Time) {
                chart.add([0,[r[i].Time, r[i].Open, r[i].High, r[i].Low, r[i].Close]])
                chart.add([1,[r[i].Time, fisher[i]]])
                if (i - 1 >= 0) {
                    chart.add([2,[r[i].Time, fisher[i - 1]]])
                }
                ts = r[i].Time
            }
        }
    }
}

img

So it is very convenient to study data, graphics, design strategies on FMZ. Here you can throw a string and welcome your teachers and classmates to leave comments.


Related

More