Commodity Channel Index (CCI) In Haskell?

7 minutes read

The Commodity Channel Index (CCI) is a popular technical indicator used in financial markets to identify overbought or oversold conditions. It measures the difference between an asset's current price and its average price over a specified period of time.


In Haskell, the CCI can be implemented by first calculating the typical price, which is the average of the high, low, and close prices for each period. Then, the mean deviation is calculated as the average of the absolute differences between the typical price and the simple moving average of the typical price over the same period.


Finally, the CCI value is calculated as the difference between the typical price and the simple moving average of the typical price, divided by 0.015 times the mean deviation. A CCI value above +100 indicates that the asset is overbought, while a value below -100 indicates oversold conditions.


Overall, implementing the Commodity Channel Index in Haskell involves calculating various moving averages and typical prices to determine the current market conditions of an asset.

Best Trading Websites to Read Charts in 2024

1
FinViz

Rating is 5 out of 5

FinViz

2
TradingView

Rating is 4.9 out of 5

TradingView

3
FinQuota

Rating is 4.7 out of 5

FinQuota

4
Yahoo Finance

Rating is 4.8 out of 5

Yahoo Finance


How to combine CCI with other technical indicators in Haskell?

Combining the Commodity Channel Index (CCI) with other technical indicators in Haskell can help to provide a more comprehensive analysis of the market. Here's an example of how you can combine CCI with another technical indicator, such as the Moving Average Convergence Divergence (MACD), in Haskell:

  1. Define functions to calculate CCI and MACD:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
-- CCI calculation function
cci :: [Double] -> Int -> Int -> [Double]
cci prices n period = map calculateCCI [n-1..length prices - 1]
  where
    calculateCCI i = (typicalPrice - sma) / (0.015 * meanDeviation)
      where
        typicalPrice = (high + low + close) / 3
        high = maximum (take period $ drop (i-n+1) prices)
        low = minimum (take period $ drop (i-n+1) prices)
        close = prices !! i
        sma = sum (take n $ drop i prices) / fromIntegral n
        meanDeviation = sum (map (\x -> abs (x - sma)) (take n $ drop i prices)) / fromIntegral n

-- MACD calculation function
macd :: [Double] -> Double -> Double -> Double -> ([Double], [Double])
macd prices shortPeriod longPeriod signalPeriod = (macdLine, signalLine)
  where
    shortEMA = ema prices shortPeriod
    longEMA = ema prices longPeriod
    macdLine = zipWith (-) shortEMA longEMA
    signalLine = ema macdLine signalPeriod


  1. Use the calculated CCI and MACD values to make trading decisions:
1
2
3
4
5
6
7
8
main :: IO ()
main = do
  let prices = [100.0, 105.0, 110.0, 115.0, 120.0, 125.0, 130.0, 135.0, 140.0]
      cciValues = cci prices 5 5
      (macdLine, signalLine) = macd prices 12 26 9
  print cciValues
  print macdLine
  print signalLine


In this example, we first calculate the CCI values and MACD values for a given set of prices. Then, we can use these values to analyze the market trends and make trading decisions based on the signals provided by the indicators.


You can further extend this code by combining more technical indicators, adding trading strategies, or implementing backtesting to validate the effectiveness of the combined indicators.


What is the default period setting for CCI in Haskell?

The default period setting for the CCI (Commodity Channel Index) in Haskell is typically set to 14.


How to plot CCI on a chart in Haskell?

To plot the Commodity Channel Index (CCI) on a chart in Haskell, you can use the Chart library. Here is an example code to plot CCI on a chart:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import Graphics.Rendering.Chart.Easy
import Graphics.Rendering.Chart.Backend.Cairo

cciData :: [(Double, Double)]
cciData = [(1, 100), (2, 105), (3, 110), (4, 95), (5, 85)] -- Replace with your own CCI data

main :: IO ()
main = toFile def "cci_chart.png" $ do
  layout_title .= "CCI Chart"
  plot $ line "CCI" [cciData]


In this code, cciData contains the data points for CCI values. Replace this with your actual CCI data. Then, the main function creates a new chart with title "CCI Chart" and plots the CCI data using a line chart. Finally, it saves the chart as a PNG file named "cci_chart.png".


You can customize the chart further by changing the layout settings or adding additional plots as needed. Make sure to install the required Chart library using cabal before running the code.


How to customize the period setting for CCI in Haskell?

To customize the period setting for CCI (Commodity Channel Index) in Haskell, you can create a function that calculates the CCI value with a specific period length. Here is an example implementation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Data.List

cci :: Int -> [Double] -> [Double] -> [Double]
cci period highs lows closes = map calculateCCI cciData
  where
    typicalPrices = map (\(h, l, c) -> (h + l + c) / 3) $ zip3 highs lows closes
    meanDeviation = map (calculateMeanDeviation period typicalPrices) [0..length typicalPrices - period]
    cciData = zip typicalPrices meanDeviation

calculateMeanDeviation :: Int -> [Double] -> Int -> Double
calculateMeanDeviation period typicalPrices idx = sum deviations / fromIntegral period
  where
    slice = take period $ drop idx typicalPrices
    meanPrice = sum slice / fromIntegral period
    deviations = map (\price -> abs(price - meanPrice)) slice

calculateCCI :: (Double, Double) -> Double
calculateCCI (typicalPrice, meanDeviation) = (typicalPrice - mean) / (factor * meanDeviation)
  where
    mean = sum (map fst cciData) / fromIntegral (length cciData)
    factor = 0.015  -- You can customize this factor as needed

-- Example usage
main :: IO ()
main = do
  let highs = [10.0, 15.0, 20.0, 25.0]
      lows = [8.0, 12.0, 18.0, 22.0]
      closes = [9.0, 14.0, 19.0, 24.0]
      period = 2  -- Customize the period setting here

  let cciValues = cci period highs lows closes
  putStrLn $ "CCI values: " ++ show cciValues


In this implementation, the cci function calculates the CCI values for a given period setting using typical prices calculated from the high, low, and close prices provided. The calculateCCI function calculates the CCI value for each data point based on the typical price and mean deviation. You can customize the period setting by changing the period variable in the main function.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

An annual maintenance contract proposal is a formal document that proposes the maintenance of equipments or concern commodities as identified in the contract annually. This proposal ensures that the damaged equipment or commodity will be repaired effectively a...
Williams %R is a technical indicator used in financial markets to show the current price momentum of a security. It measures the level at which the current closing price is relative to the high-low range over a specified period of time. In Haskell, a programmi...
The Relative Strength Index (RSI) is a popular technical indicator used by traders to gauge the strength and momentum of a security's price movements. It is typically used to identify overbought or oversold conditions in the market.In Lua, the RSI can be c...
To calculate the Average Directional Index (ADX) using C++, you first need to compute the True Range, Positive Directional Movement (+DM), and Negative Directional Movement (-DM) for each period. The True Range is the largest of the following three values: cur...
To calculate the Average Directional Index (ADX) in MATLAB, you can use the function adx from the Financial Toolbox. This function takes in three input arguments: high prices, low prices, and closing prices.
To calculate the Relative Strength Index (RSI) in JavaScript, you can follow these steps:Gather the necessary data: You will need to collect historical price data for a specific asset or security over a chosen time period. This data typically includes the clos...