How To Compute Chaikin Money Flow (CMF) using C#?

by raven_corwin , in category: Third Party Scripts , a month ago

How To Compute Chaikin Money Flow (CMF) using C#?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by shyann , a month ago

@raven_corwin 

To compute Chaikin Money Flow (CMF) using C# programming language, you can use the following code snippet:

 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;

namespace CMFCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            List<double> closePrices = new List<double> { 10.50, 10.70, 10.80, 10.85, 10.90, 10.75, 10.65, 10.50 };
            List<double> highPrices = new List<double> { 10.60, 10.75, 10.85, 10.90, 10.95, 10.85, 10.80, 10.70 };
            List<double> lowPrices = new List<double> { 10.40, 10.65, 10.75, 10.80, 10.85, 10.65, 10.60, 10.45 };
            List<double> volumes = new List<double> { 10000, 15000, 20000, 18000, 12000, 10000, 8000, 11000 };
            
            List<double> moneyFlowVolumes = new List<double>();
            List<double> moneyFlowMultipliers = new List<double>();
            List<double> chaikinMoneyFlow = new List<double>();

            for (int i = 1; i < closePrices.Count; i++)
            {
                double moneyFlowVolume = ((closePrices[i] - lowPrices[i]) - (highPrices[i] - closePrices[i])) / (highPrices[i] - lowPrices[i]) * volumes[i];
                moneyFlowVolumes.Add(moneyFlowVolume);

                double moneyFlowMultiplier = moneyFlowVolume / volumes[i];
                moneyFlowMultipliers.Add(moneyFlowMultiplier);
            }

            for (int i = 0; i < moneyFlowMultipliers.Count; i++)
            {
                double sumOfMoneyFlowMultipliers = 0;
                for (int j = 0; j <= i; j++)
                {
                    sumOfMoneyFlowMultipliers += moneyFlowMultipliers[j];
                }

                double sumOfVolumes = 0;
                for (int j = 0; j <= i; j++)
                {
                    sumOfVolumes += volumes[j];
                }

                double cmf = sumOfMoneyFlowMultipliers / sumOfVolumes;
                chaikinMoneyFlow.Add(cmf);
            }

            for (int i = 0; i < chaikinMoneyFlow.Count; i++)
            {
                Console.WriteLine("Chaikin Money Flow for period {0} is: {1}", i + 2, chaikinMoneyFlow[i]);
            }
        }
    }
}


In this code snippet, we calculate the money flow volume, money flow multiplier, and Chaikin Money Flow (CMF) using the provided close prices, high prices, low prices, and volumes. The computed CMF values are then printed to the console for each period.


Please make sure to replace the sample data with your actual data before running the code.