sql - 基于郁金香指标的 C 语言函数,使用 postgresql 计算 RSI

标签 sql c postgresql function

是否可以在数据库中实现RSI功能? https://github.com/TulipCharts/tulipindicators 我在 postgresql 表中有市场数据,我想根据这些数据计算 RSI。我们可以使用用“C”语言编写的代码

#include "../indicators.h" 
int ti_rsi_start(TI_REAL const *options) {
return (int)options[0];
}
int ti_rsi(int size, TI_REAL const *const *inputs, TI_REAL const *options, TI_REAL *const *outputs) {
const TI_REAL *input = inputs[0];
const int period = (int)options[0];
TI_REAL *output = outputs[0];
const TI_REAL per = 1.0 / ((TI_REAL)period);

if (period < 1) return TI_INVALID_OPTION;
if (size <= ti_rsi_start(options)) return TI_OKAY;

TI_REAL smooth_up = 0, smooth_down = 0;

int i;
for (i = 1; i <= period; ++i) {
    const TI_REAL upward = input[i] > input[i-1] ? input[i] - input[i-1] : 0;
    const TI_REAL downward = input[i] < input[i-1] ? input[i-1] - input[i] : 0;
    smooth_up += upward;
    smooth_down += downward;
}

smooth_up /= period;
smooth_down /= period;
*output++ = 100.0 * (smooth_up / (smooth_up + smooth_down));

for (i = period+1; i < size; ++i) {
    const TI_REAL upward = input[i] > input[i-1] ? input[i] - input[i-1] : 0;
    const TI_REAL downward = input[i] < input[i-1] ? input[i-1] - input[i] : 0;

    smooth_up = (upward-smooth_up) * per + smooth_up;
    smooth_down = (downward-smooth_down) * per + smooth_down;

    *output++ = 100.0 * (smooth_up / (smooth_up + smooth_down));
}

assert(output - outputs[0] == size - ti_rsi_start(options));
return TI_OKAY;
}

最佳答案

您当然可以将此代码放入 C 函数中,但您必须修改该函数,以便它可以与 PostgreSQL 一起使用。

最值得注意的是,您必须添加魔术 block ,并且必须根据 Version 1 Calling Conventions 声明函数并传递参数和返回值。 .

关于sql - 基于郁金香指标的 C 语言函数,使用 postgresql 计算 RSI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47398640/

相关文章:

c - PostgreSQL Type 1 接口(interface)和 ImageMagick 代码 - 冲突的 StringInfo 定义?

sql - 组合索引是否仅适用于一列查询?

postgresql - 带有 PostgreSQL 8.4.20 和 PostGIS 2.2 的 Geodjango

不能在 PIC 12F675 中进行模拟读取

c - typedef struct 声明错误?

sql - 仅使用 SQL 的 MySQL INSERT

sql - 如何获取 ID 列表的所有 JPA 对象,或者如何从此 SQL 查询中进行 JPA 查询?

c - 如何从 json 字符串中获取值?

javascript - 插入 SQL 上的双引号和单引号

java - 从 SQL 查询 Oracle 中检索一系列记录