Firetron's Candle

stable
By Firetron in Miscellaneous Published October 2020 👁 1,247 views 💬 2 comments

Description

Creates and returns a Candle Class instance. Note that every property is a getter function. Test Script
function Test()
  local closePrices = ClosePrices()
  local highPrices  = HighPrices()
  local lowPrices   = LowPrices()
  local openPrices  = OpenPrices()
  local candle      = CC_Candle(openPrices, highPrices, lowPrices, closePrices)
  Log('-----------')
  Log('Next Candle')
  Log('-----------')
  Log('IsGreen: '..Parse(candle.IsGreen(), StringType))
  Log('IsRed: '..Parse(candle.IsRed(), StringType))
  Log('IsDoji: '..Parse(candle.IsDoji(), StringType))
  Log('IsDragonfly: '..Parse(candle.IsDragonfly(), StringType))
  Log('IsGravestone: '..Parse(candle.IsGravestone(), StringType))
  Log('IsLongLegged: '..Parse(candle.IsLongLegged(), StringType))
  Log('Full: '..candle.Full())
  Log('Body: '..candle.Body())
  Log('Wicks: '..candle.Wicks())
  Log('TopWick: '..candle.TopWick())
  Log('BottomWick: '..candle.BottomWick())
  Log('WithoutTopWick: '..candle.WithoutTopWick())
  Log('WithoutBottomWick: '..candle.WithoutBottomWick())
end
OptimizedForInterval(360, Test)
HaasScript
--  ============================================================================
--    Firetron's Candle
--
--    Creates and returns a Candle Class instance. Note that every property is a
--    getter function.
--
--    Discord: @FiretronP75
--  ============================================================================

--  ========================================================
--    Variables
--  ========================================================

--  ------------------------------------
--    Definition
--  ------------------------------------

local description
local inputSuggestions
local output

--  ------------------------------------
--    Parameter
--  ------------------------------------

local pClosePrices
local pHighPrices
local pIndex
local pLowPrices
local pOpenPrices

--  ========================================================
--    Command Definition
--  ========================================================

description = 'Creates and returns a Candle Class instance. Note that every property is a getter function.'
DefineCommand('Candle', description)

--  ========================================================
--    Parameter Definition
--  ========================================================

description      = 'Open Prices.'
inputSuggestions = 'OpenPrices'
pOpenPrices      = DefineParameter(ListNumberType, 'Open', description, true, {8,7,9}, inputSuggestions)

description      = 'High prices.'
inputSuggestions = 'HighPrices'
pHighPrices      = DefineParameter(ListNumberType, 'High', description, true, {10,9,8}, inputSuggestions)

description      = 'Low prices.'
inputSuggestions = 'LowPrices'
pLowPrices       = DefineParameter(ListNumberType, 'Low', description, true, {7,8,9}, inputSuggestions)

description      = 'Close prices.'
inputSuggestions = 'ClosePrices'
pClosePrices     = DefineParameter(ListNumberType, 'Close', description, true, {9,7,8}, inputSuggestions)

description      = 'Index to use from each price series. (1 for current candle, 2 for previous candle, etc)'
pIndex           = DefineParameter(NumberType, 'Price Index', description, false, 1)

--  ========================================================
--    Class Definition
--  ========================================================

local Candle = function (open, high, low, close)

  --  ------------------------------------
  --    State Getters
  --  ------------------------------------

  local isGreen

  local IsGreen = function ()

    if isGreen != nil then return isGreen end

    isGreen = close > open

    return isGreen

  end

  --  ----------------

  local isRed

  local IsRed = function ()

    if isRed != nil then return isRed end

    isRed = close < open

    return isRed

  end

  --  ----------------

  local isDoji

  local IsDoji = function ()

    if isDoji != nil then return isDoji end

    isDoji = close == open

    return isDoji

  end

  --  ----------------

  local isDragonfly

  local IsDragonfly = function ()

    if isDragonfly != nil then return isDragonfly end

    isDragonfly = IsDoji() and close == high

    return isDragonfly

  end

  --  ----------------

  local isGravestone

  local IsGravestone = function ()

    if isGravestone != nil then return isGravestone end

    isGravestone = IsDoji() and close == low

    return isGravestone

  end

  --  ----------------

  local isLongLegged

  local IsLongLegged = function ()

    if isLongLegged != nil then return isLongLegged end

    isLongLegged = IsDoji() and close != high and close != low

    return isLongLegged

  end

  --  ------------------------------------
  --    Measurement Getters
  --  ------------------------------------

  local full

  local Full = function ()

    if full != nil then return full end

    full = high - low

    return full

  end

  --  ----------------

  local greenBody

  local GreenBody = function ()

    if greenBody != nil then return greenBody end

    greenBody = close - open

    return greenBody

  end

  --  ----------------

  local redBody

  local RedBody = function ()

    if redBody != nil then return redBody end

    redBody = open - close

    return redBody

  end

  --  ----------------

  local body

  local Body = function ()

    if body != nil then return body end

    if IsRed() then
      body = RedBody()
    else
      body = GreenBody()
    end

    return body

  end

  --  ----------------

  local greenTopWick

  local GreenTopWick = function ()

    if greenTopWick != nil then return greenTopWick end

    greenTopWick = high - close

    return greenTopWick

  end

  --  ----------------

  local redTopWick

  local RedTopWick = function ()

    if redTopWick != nil then return redTopWick end

    redTopWick = high - open

    return redTopWick

  end

  --  ----------------

  local topWick

  local TopWick = function ()

    if topWick != nil then return topWick end

    if IsRed() then
      topWick = RedTopWick()
    else
      topWick = GreenTopWick()
    end

    return topWick

  end

  --  ----------------

  local greenBottomWick

  local GreenBottomWick = function ()

    if greenBottomWick != nil then return greenBottomWick end

    greenBottomWick = open - low

    return greenBottomWick

  end

  --  ----------------

  local redBottomWick

  local RedBottomWick = function ()

    if redBottomWick != nil then return redBottomWick end

    redBottomWick = close - low

    return redBottomWick

  end

  --  ----------------

  local bottomWick

  local BottomWick = function ()

    if bottomWick != nil then return bottomWick end

    if IsRed() then
      bottomWick = RedBottomWick()
    else
      bottomWick = GreenBottomWick()
    end

    return bottomWick

  end

  --  ----------------

  local wicks

  local Wicks = function ()

    if wicks != nil then return wicks end

    wicks = Full() - Body()

    return wicks

  end

  --  ----------------

  local greenWithoutTopWick

  local GreenWithoutTopWick = function ()

    if greenWithoutTopWick != nil then return greenWithoutTopWick end

    greenWithoutTopWick = close - low

    return greenWithoutTopWick

  end

  --  ----------------

  local redWithoutTopWick

  local RedWithoutTopWick = function ()

    if redWithoutTopWick != nil then return redWithoutTopWick end

    redWithoutTopWick = open - low

    return redWithoutTopWick

  end

  --  ----------------

  local withoutTopWick

  local WithoutTopWick = function ()

    if withoutTopWick != nil then return withoutTopWick end

    if IsRed() then
      withoutTopWick = RedWithoutTopWick()
    else
      withoutTopWick = GreenWithoutTopWick()
    end

    return withoutTopWick

  end

  --  ----------------

  local greenWithoutBottomWick

  local GreenWithoutBottomWick = function ()

    if greenWithoutBottomWick != nil then return greenWithoutBottomWick end

    greenWithoutBottomWick = high - open

    return greenWithoutBottomWick

  end

  --  ----------------

  local redWithoutBottomWick

  local RedWithoutBottomWick = function ()

    if redWithoutBottomWick != nil then return redWithoutBottomWick end

    redWithoutBottomWick = high - close

    return redWithoutBottomWick

  end

  --  ----------------

  local withoutBottomWick

  local WithoutBottomWick = function ()

    if withoutBottomWick != nil then return withoutBottomWick end

    if IsRed() then
      withoutBottomWick = RedWithoutBottomWick()
    else
      withoutBottomWick = GreenWithoutBottomWick()
    end

    return withoutBottomWick

  end

  --  ------------------------------------
  --    Instance
  --  ------------------------------------

  return {
    IsGreen           = IsGreen,
    IsRed             = IsRed,
    IsDoji            = IsDoji,
    IsDragonfly       = IsDragonfly,
    IsGravestone      = IsGravestone,
    IsLongLegged      = IsLongLegged,
    Full              = Full,
    Body              = Body,
    Wicks             = Wicks,
    TopWick           = TopWick,
    BottomWick        = BottomWick,
    WithoutTopWick    = WithoutTopWick,
    WithoutBottomWick = WithoutBottomWick,
  }

end

--  ========================================================
--    Execution
--  ========================================================

local close = ArrayGet(pClosePrices, pIndex)
local high  = ArrayGet(pHighPrices,  pIndex)
local low   = ArrayGet(pLowPrices,   pIndex)
local open  = ArrayGet(pOpenPrices,  pIndex)

output = Candle(open, high, low, close)

--  ========================================================
--    Output Definitions
--  ========================================================

description = 'ListDynamic where each element is a function.'
DefineOutput(ListDynamicType, output, description)

description = 'Function that returns true if the candle is green.'
DefineOutputIndex( 1, DynamicType, 'IsGreen', description)

description = 'Function that returns true if the candle is red.'
DefineOutputIndex( 2, DynamicType, 'IsRed', description)

description = 'Function that returns true if the candle is a doji.'
DefineOutputIndex( 3, DynamicType, 'IsDoji', description)

description = 'Function that returns true if the candle is a dragonfly doji.'
DefineOutputIndex( 4, DynamicType, 'IsDragonfly', description)

description = 'Function that returns true if the candle is a gravestone doji.'
DefineOutputIndex( 5, DynamicType, 'IsGravestone', description)

description = 'Function that returns true if the candle is a long-legged doji.'
DefineOutputIndex( 6, DynamicType, 'IsLongLegged', description)

description = 'Function that returns the full length of candle from wick to wick.'
DefineOutputIndex( 7, DynamicType, 'Full', description)

description = 'Function that returns the length of the candle body.'
DefineOutputIndex( 8, DynamicType, 'Body', description)

description = 'Function that returns the length of the candle without the body.'
DefineOutputIndex( 9, DynamicType, 'Wicks', description)

description = 'Function that returns the length of the candle top wick.'
DefineOutputIndex(10, DynamicType, 'TopWick', description)

description = 'Function that returns the length of the candle bottom wick.'
DefineOutputIndex(11, DynamicType, 'BottomWick', description)

description = 'Function that returns the length of the candle without the top wick.'
DefineOutputIndex(12, DynamicType, 'WithoutTopWick', description)

description = 'Function that returns the length of the candle without the bottom wick.'
DefineOutputIndex(13, DynamicType, 'WithoutBottomWick', description)

2 Comments

Sign in to leave a comment.

K
Kobalt over 5 years ago

KABOOM! great!

P
pshai over 5 years ago

Nice work! This is great for breaking down candles.