-- Id: 2367 --+------------------------------------------------------------------+ --| Copyright © 2018, Gehtsoft USA LLC | --| http://fxcodebase.com | --+------------------------------------------------------------------+ --| Developed by : Mario Jemic | --| mario.jemic@gmail.com | --+------------------------------------------------------------------+ --| Support our efforts by donating | --| Paypal: https://goo.gl/9Rj74e | --+------------------------------------------------------------------+ --| Patreon : https://goo.gl/GdXWeN | --| BitCoin : 15VCJTLaz12Amr7adHSBtL9v8XomURo9RF | --| BitCoin Cash: 1BEtS465S3Su438Kc58h2sqvVvHK9Mijtg | --| Ethereum : 0x8C110cD61538fb6d7A2B47858F0c0AaBd663068D | --| LiteCoin : LLU8PSY2vsq7B9kRELLZQcKf5nJQrdeqwD | --+------------------------------------------------------------------+ -- based on BF_BollingerBands.lua -- 11/17/2010 v1.0 skipper : inital version function Init() indicator:name("Bigger timeframe Bollinger Bands Width Oscillator"); indicator:description("The indicator measure BB width in times, pips as well as in % of price"); indicator:requiredSource(core.Bar); indicator:type(core.Oscillator); indicator.parameters:addGroup("Calculation"); indicator.parameters:addString("BS", "Time frame to calculate stochastic", "", "M5"); indicator.parameters:setFlag("BS", core.FLAG_PERIODS); indicator.parameters:addInteger("N", "Number of periods", "" , 75, 1, 10000); indicator.parameters:addDouble("Dev", "Deviation", "", 2.0, 0.0001, 1000.0); indicator.parameters:addString("Output", "A way to calculate output", "A way to calculate output", "price"); indicator.parameters:addStringAlternative("Output", "Price Percent", "", "price"); indicator.parameters:addStringAlternative("Output", "Price Points", "", "pip"); indicator.parameters:addStringAlternative("Output", "Traditional(number of times)", "", "times"); indicator.parameters:addGroup("Style"); indicator.parameters:addColor("clrBBP", "Line color", "", core.rgb(0, 255, 0)); indicator.parameters:addInteger("widthBBP", "Line width", "", 1, 1, 5); indicator.parameters:addInteger("styleBBP", "Line style", "", core.LINE_SOLID); indicator.parameters:setFlag("styleBBP", core.FLAG_LINE_STYLE); indicator.parameters:addGroup("Levels"); indicator.parameters:addColor("clrL", "Line color", "", core.rgb(192, 192, 192)); indicator.parameters:addInteger("widthL", "Line width", "", 1, 1, 5); indicator.parameters:addInteger("styleL", "Line style", "", core.LINE_DOT); indicator.parameters:setFlag("styleL", core.FLAG_LINE_STYLE); indicator.parameters:addBoolean("customLevels", "Show Levels", "You can specify two levels", false); indicator.parameters:addDouble("customLevel1", "Upper level", "", 1.4, 0, 1000); indicator.parameters:addDouble("customLevel2", "Lower level", "", 0.28, 0, 1000); end local source; -- the source local bf_data = nil; -- the high/low data local N; local Dev; local output; local BS; local bf_length; -- length of the bigger frame in seconds local dates; -- candle dates local host; local BB; local day_offset; local week_offset; local extent; local BBW = nil; local PointSize; function Prepare(nameOnly) source = instance.source; host = core.host; day_offset = host:execute("getTradingDayOffset"); week_offset = host:execute("getTradingWeekOffset"); BS = instance.parameters.BS; N = instance.parameters.N; Dev = instance.parameters.Dev; output = instance.parameters.Output; extent = N*2; local name = profile:id() .. "(" .. source:name() .. "," .. BS .. "," .. N .. "," .. Dev .. ")"; instance:name(name); if (nameOnly) then return; end local s, e, s1, e1; s, e = core.getcandle(source:barSize(), core.now(), 0, 0); s1, e1 = core.getcandle(BS, core.now(), 0, 0); -- assert ((e - s) <= (e1 - s1), "The chosen time frame must be bigger than the chart time frame!"); bf_length = math.floor((e1 - s1) * 86400 + 0.5); PointSize = 0.0001; if string.find(source:name(), "JPY", 1, true) ~= nil then PointSize = 0.01; end BBW = instance:addStream("BBW", core.Line, name .. ".BBW", "BBW", instance.parameters.clrBBP, 0); BBW:setWidth(instance.parameters.widthBBP); BBW:setStyle(instance.parameters.styleBBP); BBW:setPrecision(math.max(2, instance.source:getPrecision())); if (instance.parameters.customLevels) then BBW:addLevel(instance.parameters.customLevel1, instance.parameters.styleL, instance.parameters.widthL, instance.parameters.clrL); BBW:addLevel(instance.parameters.customLevel2, instance.parameters.styleL, instance.parameters.widthL, instance.parameters.clrL); end end local loading = false; local loadingFrom, loadingTo; local pday = nil; -- the function which is called to calculate the period function Update(period, mode) -- get date and time of the hi/lo candle in the reference data local bf_candle; bf_candle = core.getcandle(BS, source:date(period), day_offset, week_offset); -- if data for the specific candle are still loading -- then do nothing if loading and bf_candle >= loadingFrom and (loadingTo == 0 or bf_candle <= loadingTo) then return ; end -- if the period is before the source start -- the do nothing if period < source:first() then return ; end -- if data is not loaded yet at all -- load the data if bf_data == nil then -- there is no data at all, load initial data local to, t; local from; if (source:isAlive()) then -- if the source is subscribed for updates -- then subscribe the current collection as well to = 0; else -- else load up to the last currently available date t, to = core.getcandle(BS, source:date(period), day_offset, week_offset); end from = core.getcandle(BS, source:date(source:first()), day_offset, week_offset); BBW:setBookmark(1, period); -- shift so the bigger frame data is able to provide us with the stoch data at the first period from = math.floor(from * 86400 - (bf_length * extent) + 0.5) / 86400; local nontrading, nontradingend; nontrading, nontradingend = core.isnontrading(from, day_offset); if nontrading then -- if it is non-trading, shift for two days to skip the non-trading periods from = math.floor((from - 2) * 86400 - (bf_length * extent) + 0.5) / 86400; end loading = true; loadingFrom = from; loadingTo = to; bf_data = host:execute("getHistory", 1, source:instrument(), BS, loadingFrom, to, source:isBid()); BB = core.indicators:create("BB", bf_data.close, N, Dev,false); return ; end -- check whether the requested candle is before -- the reference collection start if (bf_candle < bf_data:date(0)) then BBW:setBookmark(1, period); if loading then return ; end -- shift so the bigger frame data is able to provide us with the stoch data at the first period from = math.floor(bf_candle * 86400 - (bf_length * extent) + 0.5) / 86400; local nontrading, nontradingend; nontrading, nontradingend = core.isnontrading(from, day_offset); if nontrading then -- if it is non-trading, shift for two days to skip the non-trading periods from = math.floor((from - 2) * 86400 - (bf_length * extent) + 0.5) / 86400; end loading = true; loadingFrom = from; loadingTo = bf_data:date(0); host:execute("extendHistory", 1, bf_data, loadingFrom, loadingTo); return ; end -- check whether the requested candle is after -- the reference collection end if (not(source:isAlive()) and bf_candle > bf_data:date(bf_data:size() - 1)) then BBW:setBookmark(1, period); if loading then return ; end loading = true; loadingFrom = bf_data:date(bf_data:size() - 1); loadingTo = bf_candle; host:execute("extendHistory", 1, bf_data, loadingFrom, loadingTo); return ; end BB:update(mode); local p; p = findDateFast(bf_data, bf_candle, true); if p == -1 then return ; end if BB:getStream(0):hasData(p) and BB:getStream(1):hasData(p) then local tl = BB:getStream(0)[p]; local bl = BB:getStream(1)[p]; local ml = BB:getStream(2)[p]; if output == "price" then BBW[period] = (tl - bl) * 100 / ml; elseif output == "pip" then BBW[period] = (tl - bl) / PointSize; elseif output == "times" then BBW[period] = (tl - bl) / ml; end end end -- the function is called when the async operation is finished function AsyncOperationFinished(cookie) local period; pday = nil; period = BBW:getBookmark(1); if (period < 0) then period = 0; end loading = false; instance:updateFrom(period); end function findDateFast(stream, date, precise) local datesec = nil; local periodsec = nil; local min, max, mid; datesec = math.floor(date * 86400 + 0.5) min = 0; max = stream:size() - 1; while true do mid = math.floor((min + max) / 2); periodsec = math.floor(stream:date(mid) * 86400 + 0.5); if datesec == periodsec then return mid; elseif datesec > periodsec then min = mid + 1; else max = mid - 1; end if min > max then if precise then return -1; else return min - 1; end end end end