adding rsi indicator
This commit is contained in:
parent
a730104083
commit
8d529ffacc
FinLib.NET/FinLib
finlib-cpp
finlib-ffi
finlib-wasm/src
finlib/src
pyfinlib/src
7
FinLib.NET/FinLib/FinLib.Indicators.cs
Normal file
7
FinLib.NET/FinLib/FinLib.Indicators.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace FinLib;
|
||||||
|
|
||||||
|
public static class Indicators
|
||||||
|
{
|
||||||
|
public static double RelativeStrengthIndicator(double timePeriod, double averageGain, double averageLoss) => NativeMethods.relative_strength_indicator(timePeriod, averageGain, averageLoss);
|
||||||
|
public static double RelativeStrengthIndicatorSmoothed(double timePeriod, double previousAverageGain, double currentGain, double previousAverageLoss, double currentLoss) => NativeMethods.relative_strength_indicator_smoothed(timePeriod, previousAverageGain, currentGain, previousAverageLoss, currentLoss);
|
||||||
|
}
|
@ -76,6 +76,12 @@ namespace FinLib
|
|||||||
[DllImport(__DllName, EntryPoint = "portfolio_value_at_risk_percent", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
[DllImport(__DllName, EntryPoint = "portfolio_value_at_risk_percent", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
internal static extern NullableFloat portfolio_value_at_risk_percent(Portfolio_native* portfolio, double confidence);
|
internal static extern NullableFloat portfolio_value_at_risk_percent(Portfolio_native* portfolio, double confidence);
|
||||||
|
|
||||||
|
[DllImport(__DllName, EntryPoint = "relative_strength_indicator", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
|
internal static extern double relative_strength_indicator(double time_period, double average_gain, double average_loss);
|
||||||
|
|
||||||
|
[DllImport(__DllName, EntryPoint = "relative_strength_indicator_smoothed", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
|
internal static extern double relative_strength_indicator_smoothed(double time_period, double previous_average_gain, double current_gain, double previous_average_loss, double current_loss);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ project(finlib-cpp)
|
|||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 23)
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR})
|
set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR})
|
||||||
|
|
||||||
set(FINLIB_INSTALL_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
|
set(FINLIB_INSTALL_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
|
||||||
|
@ -76,6 +76,14 @@ NullableFloat portfolio_value_at_risk(Portfolio *portfolio,
|
|||||||
|
|
||||||
NullableFloat portfolio_value_at_risk_percent(Portfolio *portfolio, double confidence);
|
NullableFloat portfolio_value_at_risk_percent(Portfolio *portfolio, double confidence);
|
||||||
|
|
||||||
|
double relative_strength_indicator(double time_period, double average_gain, double average_loss);
|
||||||
|
|
||||||
|
double relative_strength_indicator_smoothed(double time_period,
|
||||||
|
double previous_average_gain,
|
||||||
|
double current_gain,
|
||||||
|
double previous_average_loss,
|
||||||
|
double current_loss);
|
||||||
|
|
||||||
double scale_value_at_risk(double initial_value, ptrdiff_t time_cycles);
|
double scale_value_at_risk(double initial_value, ptrdiff_t time_cycles);
|
||||||
|
|
||||||
NullableFloat varcovar_value_at_risk(const double *arr, size_t len, double confidence);
|
NullableFloat varcovar_value_at_risk(const double *arr, size_t len, double confidence);
|
||||||
|
@ -2,7 +2,8 @@ cmake_minimum_required(VERSION 3.19)
|
|||||||
project(finexe)
|
project(finexe)
|
||||||
|
|
||||||
add_subdirectory(finlib)
|
add_subdirectory(finlib)
|
||||||
set(SOURCE_FILES main.cpp
|
set(SOURCE_FILES
|
||||||
|
main.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(finexe ${SOURCE_FILES})
|
add_executable(finexe ${SOURCE_FILES})
|
||||||
|
@ -7,6 +7,7 @@ set(SOURCE_FILES
|
|||||||
)
|
)
|
||||||
|
|
||||||
add_library(finlib SHARED STATIC ${SOURCE_FILES})
|
add_library(finlib SHARED STATIC ${SOURCE_FILES})
|
||||||
|
add_library(finlib::finlib ALIAS finlib)
|
||||||
|
|
||||||
ExternalProject_Add(
|
ExternalProject_Add(
|
||||||
finlibrs
|
finlibrs
|
||||||
@ -25,4 +26,4 @@ target_link_libraries(finlib
|
|||||||
)
|
)
|
||||||
|
|
||||||
install(TARGETS finlib DESTINATION ${FINLIB_INSTALL_LIB_DIR})
|
install(TARGETS finlib DESTINATION ${FINLIB_INSTALL_LIB_DIR})
|
||||||
install(FILES finlib.h DESTINATION ${FINLIB_INSTALL_INCLUDE_DIR})
|
install(FILES finlib.hpp DESTINATION ${FINLIB_INSTALL_INCLUDE_DIR})
|
||||||
|
@ -19,6 +19,7 @@ fn main() {
|
|||||||
csbindgen::Builder::default()
|
csbindgen::Builder::default()
|
||||||
.input_extern_file("src/lib.rs")
|
.input_extern_file("src/lib.rs")
|
||||||
.input_extern_file("src/portfolio.rs")
|
.input_extern_file("src/portfolio.rs")
|
||||||
|
.input_extern_file("src/indicators.rs")
|
||||||
.input_extern_file("../finlib/src/risk/portfolio.rs")
|
.input_extern_file("../finlib/src/risk/portfolio.rs")
|
||||||
.csharp_dll_name("libfinlib_ffi")
|
.csharp_dll_name("libfinlib_ffi")
|
||||||
.csharp_namespace("FinLib")
|
.csharp_namespace("FinLib")
|
||||||
|
10
finlib-ffi/src/indicators.rs
Normal file
10
finlib-ffi/src/indicators.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn relative_strength_indicator(time_period: f64, average_gain: f64, average_loss: f64) -> f64 {
|
||||||
|
finlib::indicators::rsi::relative_strength_indicator(time_period, average_gain, average_loss)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn relative_strength_indicator_smoothed(time_period: f64, previous_average_gain: f64, current_gain: f64, previous_average_loss: f64, current_loss: f64) -> f64 {
|
||||||
|
finlib::indicators::rsi::relative_strength_indicator_smoothed(time_period, previous_average_gain, current_gain, previous_average_loss, current_loss)
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
mod portfolio;
|
pub mod portfolio;
|
||||||
|
pub mod indicators;
|
||||||
|
|
||||||
use std::slice;
|
use std::slice;
|
||||||
|
|
||||||
|
@ -14,6 +14,21 @@ pub fn init_logging() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub struct RelativeStrengthIndicator { }
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
impl RelativeStrengthIndicator {
|
||||||
|
pub fn relative_strength_indicator(time_period: f64, average_gain: f64, average_loss: f64) -> f64 {
|
||||||
|
finlib::indicators::rsi::relative_strength_indicator(time_period, average_gain, average_loss)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn relative_strength_indicator_smoothed(time_period: f64, previous_average_gain: f64, current_gain: f64, previous_average_loss: f64, current_loss: f64) -> f64 {
|
||||||
|
finlib::indicators::rsi::relative_strength_indicator_smoothed(time_period, previous_average_gain, current_gain, previous_average_loss, current_loss)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub struct Interest { }
|
pub struct Interest { }
|
||||||
|
|
||||||
|
1
finlib/src/indicators/mod.rs
Normal file
1
finlib/src/indicators/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod rsi;
|
38
finlib/src/indicators/rsi/mod.rs
Normal file
38
finlib/src/indicators/rsi/mod.rs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
|
||||||
|
|
||||||
|
pub fn relative_strength_indicator(time_period: f64, average_gain: f64, average_loss: f64) -> f64 {
|
||||||
|
100. - (
|
||||||
|
100. /
|
||||||
|
(1. + (
|
||||||
|
(average_gain / time_period)
|
||||||
|
/
|
||||||
|
(average_loss / time_period)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn relative_strength_indicator_smoothed(time_period: f64, previous_average_gain: f64, current_gain: f64, previous_average_loss: f64, current_loss: f64) -> f64 {
|
||||||
|
100. - (100. /
|
||||||
|
(1. +
|
||||||
|
(
|
||||||
|
((previous_average_gain * time_period) + current_gain)
|
||||||
|
|
||||||
|
/
|
||||||
|
|
||||||
|
((previous_average_loss * time_period) + current_loss)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn relative_strength_indicator_test() {
|
||||||
|
let result = relative_strength_indicator(14., 1., 0.8);
|
||||||
|
assert_eq!(f64::floor(result * 100.) / 100., 55.55);
|
||||||
|
}
|
||||||
|
}
|
@ -5,4 +5,5 @@ pub mod stats;
|
|||||||
pub mod util;
|
pub mod util;
|
||||||
pub mod risk;
|
pub mod risk;
|
||||||
pub mod ffi;
|
pub mod ffi;
|
||||||
pub mod options;
|
pub mod options;
|
||||||
|
pub mod indicators;
|
@ -15,6 +15,26 @@ mod pyfinlib {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pymodule]
|
||||||
|
mod indicators {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[pymodule]
|
||||||
|
mod rsi {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[pyfunction]
|
||||||
|
pub fn relative_strength_indicator(time_period: f64, average_gain: f64, average_loss: f64) -> PyResult<f64> {
|
||||||
|
Ok(finlib::indicators::rsi::relative_strength_indicator(time_period, average_gain, average_loss))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pyfunction]
|
||||||
|
pub fn relative_strength_indicator_smoothed(time_period: f64, previous_average_gain: f64, current_gain: f64, previous_average_loss: f64, current_loss: f64) -> PyResult<f64> {
|
||||||
|
Ok(finlib::indicators::rsi::relative_strength_indicator_smoothed(time_period, previous_average_gain, current_gain, previous_average_loss, current_loss))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[pymodule]
|
#[pymodule]
|
||||||
mod interest {
|
mod interest {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user