Блог им. XXM |Macd.lua

    • 23 ноября 2014, 14:48
    • |
    • XXM
  • Еще
                                                       
                                                       Воскресное чтиво.
                                                       В образовательных целях.

------------------------------------------------------------------------
— Macd.lua, © [email protected], 23.11.2014
— Короткий период: period1
— Длинный период: period2
— Количество периодов сигнальной скользящей средней: period3
— метод усреднения линий: Exponential
------------------------------------------------------------------------
Settings =
     {
          Name = «Macd»,
          period1 = 12, period2 = 26, period3 = 9,
          line=
               {
                    {Name = «Macd», Color = 8404992, Type = 1, Width = 2},
                    {Name = «Sign», Color = 32768, Type = 1, Width = 2}
               }
     }
-------------------------------
function Init()
     Macd = cached_Macd()
     return 2
end
-------------------------------
function OnCalculate(index)
     return Macd(index, Settings.period1, Settings.period2, Settings.period3)
end
-------------------------------
function average(_start, _end)
     local sum=0
     for i = _start, _end do
          sum=sum+C(i)
     end
     return sum/(_end-_start+1)
end
-------------------------------
function cached_Macd()
     local cache_EMA_long={}
     local cache_EMA_short={}
     local cache_MACD={}
     local cache_Sign={}
     return function(ind, _p01, _p02, _p03)
          local n_ema_short = 0 --теущий EMA короткий
          local p_ema_short = 0 --предыдущий EMA короткий
          local n_sign = 0 --теущий sign
          local p_sign = 0 --предыдущий sign
          local period_short = _p01
          local period_long = _p02
          local period_sign = _p03
          local index = ind
          local k_short = 2/(period_short+1)
          local k_long = 2/(period_long+1)
          local k_sign = 2/(period_sign+1)
          if index == 1 then
               cache_EMA_long = {}
               cache_EMA_short = {}
               cache_MACD = {}
               cache_Sign={}
          end
          -----------------------------------------------
          if index < period_long then
               cache_EMA_long[index] = average(1,index)
               return nil
          end
          p_ema_long = cache_EMA_long[index-1] or C(index)
          n_ema_long = k_long*C(index)+(1-k_long)*p_ema_long
          cache_EMA_long[index] = n_ema_long
          -----------------------------------------------
          if index < period_short then
               cache_EMA_short[index] = average(1,index)
               return nil
          end
          p_ema_short = cache_EMA_short[index-1] or C(index)
          n_ema_short = k_short*C(index)+(1-k_short)*p_ema_short
          cache_EMA_short[index] = n_ema_short
          -----------------------------------------------
          --считаем сигнальную
          cache_MACD[index] = n_ema_short-n_ema_long
          p_sign = cache_Sign[index-1] or cache_MACD[index]
          n_sign = k_sign*cache_MACD[index]+(1-k_sign)*p_sign
          cache_Sign[index] = n_sign
          -----------------------------------------------
          return n_ema_short-n_ema_long, n_sign
     end
end
------------------------------------------------------------------------ 

....все тэги
UPDONW
Новый дизайн