How To Calculate Bollinger Bands using F#?

by darby_thiel , in category: Technical Analysis , 4 months ago

How To Calculate Bollinger Bands using F#?

Facebook Twitter LinkedIn Whatsapp

2 answers

by noemie_rogahn , 4 months ago

@darby_thiel 

To calculate Bollinger Bands using F#, you can follow these steps.


Step 1: Define the necessary variables and input parameters.

1
2
3
let inputPrices = // list of input prices
let period = 20 // period for calculating moving average
let numDeviations = 2.0 // number of standard deviations for upper and lower bands


Step 2: Calculate the moving average of the input prices.

1
let movingAverage = inputPrices |> Seq.windowed period |> Seq.map (Seq.average)


Step 3: Calculate the standard deviation of the input prices.

1
let standardDeviation = inputPrices |> Seq.windowed period |> Seq.map (Seq.stdev)


Step 4: Calculate the upper and lower bands using the moving average and standard deviation.

1
2
let upperBand = Seq.map2 (+) movingAverage (Seq.map (fun x -> numDeviations * x) standardDeviation)
let lowerBand = Seq.map2 (-) movingAverage (Seq.map (fun x -> numDeviations * x) standardDeviation)


Step 5: Return the Bollinger Bands as a tuple of lists.

1
let bollingerBands = (upperBand |> List.ofSeq, lowerBand |> List.ofSeq)


You can then use these steps to calculate Bollinger Bands for any given list of input prices. Keep in mind that Bollinger Bands are a technical analysis tool used to determine possible overbought or oversold conditions in financial markets.

by sadie.maggio , 2 months ago

@darby_thiel 

The provided steps are correct for calculating Bollinger Bands using F#. It is important to have a basic understanding of the Bollinger Bands concept and how they are used in technical analysis. By following the steps outlined above, you can easily implement a function in F# to calculate Bollinger Bands for a given set of input prices.


Here is a complete F# function that encapsulates the steps mentioned:

1
2
3
4
5
6
7
8
let calculateBollingerBands (inputPrices: float list) (period: int) (numDeviations: float) =
    let movingAverage = inputPrices |> Seq.windowed period |> Seq.map Seq.average
    let standardDeviation = inputPrices |> Seq.windowed period |> Seq.map Seq.stdev

    let upperBand = Seq.map2 (+) movingAverage (Seq.map (fun x -> numDeviations * x) standardDeviation)
    let lowerBand = Seq.map2 (-) movingAverage (Seq.map (fun x -> numDeviations * x) standardDeviation)

    (upperBand |> List.ofSeq, lowerBand |> List.ofSeq)


You can use this function by providing the list of input prices, the period for calculating moving averages, and the number of standard deviations for the bands. For example:

1
2
3
4
5
let inputPrices = [10.0; 12.0; 14.0; 16.0; 18.0; 20.0; 22.0; 24.0; 26.0; 28.0]
let period = 5
let numDeviations = 2.0

let bollingerBands = calculateBollingerBands inputPrices period numDeviations


This will give you a tuple of lists containing the upper and lower Bollinger Bands for the given input prices. Feel free to customize the function as needed for your specific requirements or market data.