Skip to content

A library for performing technical analysis

License

Notifications You must be signed in to change notification settings

ib-ruby/ib-technical-analysis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IB-Technical-Analysis

A library for performing technical analysis

This Gem is inspired by the intrinio package. However, IB-Technical-Analysis focusses on calculation of indicators from a stream of data.

It fits perfectly into the IB-Ruby suite, but can be used isolated, too.

Indicator classes

Indicators live in the TechnicalAnalysis namespace:

TechnicalAnalysis::MovingAverage::SimpleMA
TechnicalAnalysis::MovingAverage::ExpMA
TechnicalAnalysis::MovingAverage::Wma
TechnicalAnalysis::MovingAverage::Macd
TechnicalAnalysis::MovingAverage::KaMA
TechnicalAnalysis::Momentum::Rsi
TechnicalAnalysis::Momentum::Tsi
TechnicalAnalysis::Momentum::Lanea (Stochastics)

Additional calculations on single Bars are defined as extension of IB::Bar:

  • typical_price
  • pivot
  • true_range
Common Interface

The indicators are initialized by

  indicator = TechnicalAnalysis::MovingAverage::KaMA.new period: 30, fast: 5, slow: 15, data: {an Array}

If an Array is passed, the indicator is calculated immediately.

Subsequent data elements are passed by indicator.add_item {value}. The calculated indicator is returned for further processing.

Perform calculation on time-series

IB-Technical-Analysis calculations are embedded in the Enumerator-class.

The List is simply iterated.

Thus

require 'bundler/setup'
require 'ib-gateway'
require 'ib/eod'
require 'ib/symbols'
require 'technical-analysis'

using TASupport

z = Symbols::Futures.mini_dax.eod( duration: '50 d').each
zz= z.calculate( :ema, period: 15 , use: :close 
zz.first
=> #<struct TechnicalAnalysis::MovingAverage::EMA time=Wed, 10 Mar 2021, value=0.149524e5> 

calculates the Exponential Moving Average on the close property of each item in the Enumerator returned by the contract.eod method.

The same using a conventional array:

u =  z.map( &:close ).each
uu=  u.calculate( :ema, period: 15 ) 
uu.first
=> 0.149524e5 

Signals and Backtesting

The calculate-method of Enumerator passes raw-data and calculated indicator-values to an optional block. This enables the formulation of signal generators.

buffer=[]
zz= z.calculate( :ema, period: 5, use: :typical_price ) do | raw, struct | 
   buffer << struct.value
   buffer.shift if buffer.size >2
   momentum_indicator =  (buffer.first - buffer.last) <=> 0
   crossing = case momentum_indicator
        when +1
          buffer.first > raw.close && buffer.last < raw.close
        when -1
          buffer.first < raw.close && buffer.last > raw.close
        end
   buy_or_sell =  momentum_indicator == 1 ? "buy" : "sell"
   puts "#{buy_or_sell}-Signal: EMA-Indicator-Crossing @ #{struct.time}" if crossing
end

Implemented Indicators

  • Simple Moving Average
zz = z.calculate :sma, period: 15, use: :close 
  • Exponential Moving Average
zz = z.calculate :ema, period: 15  
  • Weighted Moving Average
zz = z.calculate :wma, period: 15 
  • Moving Average Convergence Divergence
zz = z.calculate :macd, signal: 9, fast: 12, slow: 24 
zz = z.calculate :macd, period: 9, fast: 12, slow: 24 
  • Kaufman Moving Average
zz = z.calculate :kama, period: 15, fast: 10, slow: 3 
  • Relative Strength Index
zz = z.calculate :rsi, period: 15 
  • True Strength Index
zz = z.calculate :tsi, high: 15, low: 7 
  • Lane Stochastic
zz = z.calculate :lane, period: 10, fast: 3, slow: 3 , use: :wap 

(work in progress)

About

A library for performing technical analysis

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages