How To Calculate Bollinger Bands using F#?

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

How To Calculate Bollinger Bands using F#?

Facebook Twitter LinkedIn Whatsapp

1 answer

by noemie_rogahn , 2 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.