Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9148463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:16:46+00:00 2026-06-17T11:16:46+00:00

say I have a parameter x and have several lines using x to calculate

  • 0

say I have a parameter x and have several lines using x to calculate y, now there are 10 values of x and I need to use each value to calculate a respective y, and I don’t wanna change x each time and run my command lines 10 times, is there any syntax in F# which allows me to repeat those command lines I’ve already wrote so that I only need to execute one time to work out all 10 values of y?

Thanks in advance

EDITED:I pasted my code down below, basically, what I want is geting alphas for different parameter combinations, my parameters are “shreshold”, “WeeksBfReport” and “DaysBfExecution”. I have 30 sets of parameter combinations, so I don’t wanna go change the parameters and run the command for 30 times. Is there any way for not doing this?

let shreshold= 2.0
let ReportDate = "2008/12/15"
let ExeDate = "2009/01/05"
let WeeksBfReport = 1
let DaysBfExecution = 3
let Rf=0.01

let DateIn=ReportDate.ToDateTimeExact("yyyy/MM/dd").AddWeeks(-WeeksBfReport)
let DateOut=ExeDate.ToDateTimeExact("yyyy/MM/dd").AddWorkDays(-DaysBfExecution)
let DateInString=DateIn.ToString("yyyy/MM/dd")
let DateOutString=DateOut.ToString("yyyy/MM/dd")

let mutable FundMV=0.
let FundTicker=csvTable.AsEnumerable().Select(fun (x:DataRow) -> x.Field<string>("Ticker")).ToArray()
for i in 0..csvTable.Rows.Count-1 do
    let FundUnitPrice= float(csvTable.AsEnumerable().Where(fun (x:DataRow) -> x.Field<string>(0) = FundTicker.[i]).First().Field<string>(DateInString))
    let FundShares= float(csvTable1.AsEnumerable().Where(fun (x:DataRow) -> x.Field<string>(0) = FundTicker.[i]).First().Field<string>(DateInString))
    FundMV<-FundMV + FundUnitPrice*FundShares
    printfn "%e" FundMV
//use TMV to calculate weights of CSI300 constitutes
let mutable csiTMV=0.
let CSITMV : float array = Array.zeroCreate 300
let DictionaryCSI = Dictionary<String,float>()
for i in 0..299 do
    let TMV=float(csvTable3.Rows.[i].Field<string>(DateInString))
    csiTMV<-csiTMV + TMV
    CSITMV.[i] <- TMV
for i in 0..299 do
    let Weight=CSITMV.[i]/csiTMV
    DictionaryCSI.[csvTable3.Rows.[i].Field<string>("Stock")]<-Weight

let DictionaryOldOut = Dictionary<String,float>()
let array=csvTable2.AsEnumerable().Select(fun (x:DataRow) -> x.Field<string>("Stock")).ToArray()
let OldOutTMV=ResizeArray<float>()
let DictionaryOldOutWeight = Dictionary<string,float>()
let OldOutWeight : float array = Array.zeroCreate (csvTable2.Rows.Count/2)
for i in 0..(csvTable2.Rows.Count/2)-1 do
    let Weight=DictionaryCSI.Item(array.[i+(csvTable2.Rows.Count/2)])
    DictionaryOldOutWeight.[csvTable2.Rows.[i+csvTable2.Rows.Count/2].Field<string>("Stock")]<-Weight
    OldOutWeight.[i]<- Weight
    DictionaryOldOut.[csvTable2.Rows.[i+csvTable2.Rows.Count/2].Field<string>("Stock")]<- Weight*FundMV //OldOut Moving Value
    OldOutTMV.Add(Weight)
let OldOutTMVarray=OldOutTMV.ToArray() //create an array of OldOut weights and then sum up
let SumOldOutTMV=Array.fold (+) 0. OldOutTMVarray 

let mutable NewInTMV=0.
let NewInWeight : float array = Array.zeroCreate (csvTable2.Rows.Count/2)
let DictionaryNewIn = Dictionary<string,float>()
let DictionaryNewInWeight = Dictionary<string,float>()
for i in 0..csvTable3.Rows.Count-300-1 do
    let TMV=float(csvTable3.Rows.[i+300].Field<string>(DateInString))
    NewInTMV<-NewInTMV + TMV
    let Weight=TMV/(csiTMV+NewInTMV-SumOldOutTMV)
    NewInWeight.[i]<-Weight
    DictionaryNewInWeight.[csvTable3.Rows.[i+300].Field<string>("Stock")]<-Weight
    let MovingValue=Weight*FundMV
    DictionaryNewIn.[csvTable3.Rows.[i+300].Field<string>("Stock")]<-MovingValue //NewIn Moving Value

let table2array=csvTable2.AsEnumerable().Select(fun (x:DataRow) -> x.Field<string>("Stock")).ToArray()
let NewInturnoverArray : float array = Array.zeroCreate (csvTable2.Rows.Count/2) 
for i in 0..(csvTable2.Rows.Count/2)-1 do  
    let lastday= float(csvTable2.AsEnumerable().Where(fun (x:DataRow) -> x.Field<string>(0) = table2array.[i]).First().Field<string>(DateInString)) 
    let turnover = csvTable2.Rows.[i].ItemArray.Skip(3)|>Seq.map(fun (x:obj)-> System.Double.Parse(x.ToString()))|>Seq.toArray
    let lastdayindex : (int) =
        if lastday= 0. then
            let lastdayfake=float(csvTable2.AsEnumerable().Where(fun (x:DataRow) -> x.Field<string>(0) = table2array.[i+2]).First().Field<string>(DateInString)) 
            let turnoverfake = csvTable2.Rows.[i+2].ItemArray.Skip(3)|>Seq.map(fun (x:obj)-> System.Double.Parse(x.ToString()))|>Seq.toArray
            Array.findIndex(fun elem -> elem=lastdayfake) turnoverfake
        else
            let lastdayfake=lastday
            let turnoverfake=turnover
            Array.findIndex(fun elem -> elem=lastdayfake) turnoverfake
    printfn "%A" lastdayindex
    let TurnoverNeed : float array = Array.zeroCreate 21
    for t in 0..20 do
        TurnoverNeed.[t] <- turnover.[lastdayindex - 20 + t]
    let zerotwo : float array = Array.zeroCreate TurnoverNeed.Length
    if TurnoverNeed=zerotwo then 
        let ave_daily_turnover = 0.
        NewInturnoverArray.[i] <- ave_daily_turnover
    else 
        let ave_daily_turnover  = Seq.average(TurnoverNeed|>Seq.filter(fun x-> x > 0.))
        NewInturnoverArray.[i] <- ave_daily_turnover

type totalinfo = {Name:String;Shock:float}
let NewIn=ResizeArray<totalinfo>()
for i in 0..(csvTable2.Rows.Count/2)-1 do
    let MovingValue=DictionaryNewIn.Item(array.[i])
    let Shock=MovingValue/NewInturnoverArray.[i]
    let V= {Name=string(array.[i]); Shock=Shock}
    NewIn.Add(V)
let NewInShock=NewIn.ToArray()

let OldOutturnoverArray : float array = Array.zeroCreate (csvTable2.Rows.Count/2)
for i in 0..(csvTable2.Rows.Count/2)-1 do   
    let turnover = csvTable2.Rows.[i+csvTable2.Rows.Count/2].ItemArray.Skip(3)|>Seq.map(fun (x:obj)-> System.Double.Parse(x.ToString()))
    let zero : float array = Array.zeroCreate (turnover|>Seq.toArray).Length
    if turnover|>Seq.toArray=zero then
        let ave_daily_turnover  = 0.
        OldOutturnoverArray.[i] <- ave_daily_turnover
    else
        let ave_daily_turnover  = Seq.average(turnover|>Seq.filter(fun x-> x > 0.))
        OldOutturnoverArray.[i] <- ave_daily_turnover

let OldOut=ResizeArray<totalinfo>()
for i in 0..(csvTable2.Rows.Count/2)-1 do
    let MovingValue=DictionaryOldOut.Item(array.[i+csvTable2.Rows.Count/2])
    let Shock=MovingValue/OldOutturnoverArray.[i]
    let V= {Name=string(array.[i+csvTable2.Rows.Count/2]); Shock=Shock}
    OldOut.Add(V)
let OldOutShock=OldOut.ToArray()

let DoIn=NewInShock |> Array.filter (fun t -> t.Shock >= shreshold)
let DoOut=OldOutShock |> Array.filter (fun t -> t.Shock >= shreshold)

let DoInTicker= Array.map (fun e -> e.Name) DoIn
let DoOutTicker= Array.map (fun e -> e.Name) DoOut

let DoInWeight : float array = Array.zeroCreate DoInTicker.Length
for i in 0..DoInTicker.Length-1 do
    DoInWeight.[i] <- DictionaryNewInWeight.Item(DoInTicker.[i])

let TotalDoInWeight= Array.fold (+) 0.  DoInWeight
let DoInRatioX : float array = Array.zeroCreate DoInTicker.Length
for i in 0..(DoInTicker.Length)-1 do  
    DoInRatioX.[i] <- DoInWeight.[i]/TotalDoInWeight
let Beta=csvTable2.AsEnumerable().Select(fun (x:DataRow) -> x.Field<string>("Beta")).ToArray()
//let NewInBeta : float array = Array.zeroCreate (csvTable2.Rows.Count/2)
let DictionaryNewInBeta = Dictionary<string,float>()
for i in 0..(csvTable2.Rows.Count/2)-1 do  
//    NewInBeta.[i] <- float(Beta.[i])
    DictionaryNewInBeta.[csvTable3.Rows.[i+300].Field<string>("Stock")]<-float(Beta.[i])
let DoInBeta : float array = Array.zeroCreate DoInTicker.Length
for i in 0..DoInTicker.Length-1 do
    DoInBeta.[i] <- DictionaryNewInBeta.Item(DoInTicker.[i])

let mutable PortfolioBeta=0.
for i in 0..(DoInTicker.Length)-1 do
    PortfolioBeta <- PortfolioBeta + DoInRatioX.[i] * DoInBeta.[i] 

let mutable PortfolioReturn= 0.
for i in 0..DoInTicker.Length-1 do
    let PriceIn= float(csvTable4.AsEnumerable().Where(fun (x:DataRow) -> x.Field<string>(0) = DoInTicker.[i]).First().Field<string>(DateInString))
    let PriceOut= float(csvTable4.AsEnumerable().Where(fun (x:DataRow) -> x.Field<string>(0) = DoInTicker.[i]).First().Field<string>(DateOutString))
    PortfolioReturn <- PortfolioReturn + (1./float(DoInTicker.Length))*(PriceOut - PriceIn)/PriceIn

let IndexIn= float(csvTable4.AsEnumerable().Where(fun (x:DataRow) -> x.Field<string>(0) = "000300.SH").First().Field<string>(DateInString))
let IndexOut= float(csvTable4.AsEnumerable().Where(fun (x:DataRow) -> x.Field<string>(0) = "000300.SH").First().Field<string>(DateOutString))
let MarketReturn= (IndexOut-IndexIn)/IndexIn
let Alpha= PortfolioReturn-Rf-PortfolioBeta*(MarketReturn-Rf)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:16:47+00:00Added an answer on June 17, 2026 at 11:16 am

    Like John said, put it all into a function accepting the changing values as parameters. To can use records to allow you to store the parameter combinations in a list, like so.

    type ReportParameters = {
        shreshold: float;
        ReportDate: string;
        ExeDate: string;
        WeeksBfReport: int;
        DaysBfExecution: int;
        Rf: float;
    }
    
    type Report = {
        NewInShock: totalinfo;
        IndexIn: float;
        // etc
    }
    
    let createReport (reportParams:ReportParameters) : Report = 
        let shreshold = reportParams.shreshold
        let ReportDate = reportParams.ReportDate
        let ExeDate = reportParams.ExeDate
        let WeeksBfReport = reportParams.WeeksBfReport
        let DaysBfExecution = reportParams.DaysBfExecution
        let Rf = reportParams.Rf
    
        // Your function code HERE
        // Remember to move all type definitions out of this scope.
    
        { // Report data to return.
            NewInShock = NewInShock;
            IndexIn = IndexIn;
            // etc
        }
    

    Using the code is as simple as this:

    let reportsToBeGenerated = [
        { shreshold = 2.0; ReportDate = "2008/12/15"; ExeDate = "2009/01/05"; WeeksBfReport = 1; DaysBfExecution = 3; Rf = 0.01 };
        { shreshold = 1.5; ReportDate = "2009/12/15"; ExeDate = "2010/01/05"; WeeksBfReport = 2; DaysBfExecution = 2; Rf = 0.01 };
    ]
    
    let reports = reportsToBeGenerated |> List.map createReport
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a function where the parameter is passed by value instead
Using Delphi 7, I have several functions that take a single integer parameter. These
One can say a type parameter T must have a specific supertype S_1: class
I have a parametric curve, say two vectors of doubles where the parameter is
Lets say I have 10 lines in a file. I have 2 parameters that
Let's say I have several POJOs which all extend a common supertype, BaseObject .
I have a situation where I want to use several server side controls, which
Say I have several ObservableCollections of different classes: public class Employee { public int
Let me use medical doctors to explain my question. Now doctors have different fields
I need to work with several data samples, to say, N . The samples

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.