How To Calculate Ichimoku Cloud using Golang?

by rickey_sauer , in category: Technology , 7 months ago

How To Calculate Ichimoku Cloud using Golang?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kim , 7 months ago

@rickey_sauer 

To calculate the Ichimoku Cloud using Golang, you need to have the high and low prices of the asset for a specific period of time. Here is a sample code snippet that demonstrates how to calculate the Ichimoku Cloud using Golang:

 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main

import (
	"fmt"
)

type IchimokuCloud struct {
	TenkanSen   float64
	KijunSen    float64
	SenkouSpanA float64
	SenkouSpanB float64
	ChikouSpan   float64
}

func CalculateIchimokuCloud(highPrices []float64, lowPrices []float64) IchimokuCloud {
	// Define period lengths
	tenkanSenPeriod := 9
	kijunSenPeriod := 26
	senkouSpanPeriod := 26
	chikouSpanPeriod := 26

	// Calculate Tenkan Sen
	tenkanSen := (calculateHighest(highPrices[:tenkanSenPeriod]) + calculateLowest(lowPrices[:tenkanSenPeriod])) / 2

	// Calculate Kijun Sen
	kijunSen := (calculateHighest(highPrices[:kijunSenPeriod]) + calculateLowest(lowPrices[:kijunSenPeriod])) / 2

	// Calculate Senkou Span A
	senkouSpanA := (tenkanSen + kijunSen) / 2

	// Calculate Senkou Span B
	senkouSpanB := (calculateHighest(highPrices[:senkouSpanPeriod]) + calculateLowest(lowPrices[:senkouSpanPeriod])) / 2

	// Calculate Chikou Span
	chikouSpan := calculateChikouSpan(highPrices, lowPrices, chikouSpanPeriod)

	return IchimokuCloud{
		TenkanSen:   tenkanSen,
		KijunSen:    kijunSen,
		SenkouSpanA: senkouSpanA,
		SenkouSpanB: senkouSpanB,
		ChikouSpan:  chikouSpan,
	}
}

func calculateHighest(prices []float64) float64 {
	highest := prices[0]
	for _, price := range prices {
		if price > highest {
			highest = price
		}
	}
	return highest
}

func calculateLowest(prices []float64) float64 {
	lowest := prices[0]
	for _, price := range prices {
		if price < lowest {
			lowest = price
		}
	}
	return lowest
}

func calculateChikouSpan(highPrices []float64, lowPrices []float64, period int) float64 {
	if period >= len(highPrices) {
		return highPrices[0]
	}
	return highPrices[period] // Use the closing price of the period as Chikou Span value
}

func main() {
	// Sample high and low prices for 26 periods
	highPrices := []float64{100, 110, 105, 115, 120, 125, 130, 128, 135, 140, 145, 143, 150, 155, 160, 158, 163, 168, 170, 165, 175, 180, 185, 188, 190}
	lowPrices := []float64{90, 100, 95, 105, 110, 115, 120, 118, 125, 130, 135, 133, 140, 145, 150, 148, 153, 158, 160, 155, 165, 170, 175, 178, 180}

	ichimokuCloud := CalculateIchimokuCloud(highPrices, lowPrices)

	fmt.Printf("Tenkan Sen: %.2f
", ichimokuCloud.TenkanSen)
	fmt.Printf("Kijun Sen: %.2f
", ichimokuCloud.KijunSen)
	fmt.Printf("Senkou Span A: %.2f
", ichimokuCloud.SenkouSpanA)
	fmt.Printf("Senkou Span B: %.2f
", ichimokuCloud.SenkouSpanB)
	fmt.Printf("Chikou Span: %.2f
", ichimokuCloud.ChikouSpan)
}


This code calculates the Tenkan Sen, Kijun Sen, Senkou Span A, Senkou Span B, and Chikou Span for a sample of high and low prices for 26 periods. You can adjust the period lengths and provide your own high and low prices data to calculate the Ichimoku Cloud for your desired asset.