Monday, May 31, 2010

F# Parallelism using Agent


// Learn more about F# at http://fsharp.net


namespace JQD

type Downloader() =

let ParallelWorker n f =
MailboxProcessor.Start( fun inbox->
let workers =
Array.init n ( fun i ->MailboxProcessor.Start(f))
let rec loop i = async {
let! msg=inbox.Receive()
workers.[i].Post(msg)
return! loop( (i+1)%n)
} loop 0
)



let agent =
ParallelWorker 8,
(fun input ->
let rec loop() = async {

do! Async.Sleep(5000)
return! loop()
}
loop()
)


member this.DowloadAll()=
ParallelWorker 8, (fun data ->
let rec loop() = async {

do! Async.Sleep(5000)
return! loop()
}
loop()
)

F# barebone Agent


Agent = using message passing to avoid mutating state
type Downloader() =

let agent =
MailboxProcessor.Start(fun inbox ->
let rec loop() = async {
let msg=inbox.Receive()
do! Async.Sleep(5000)
return! loop()
}
loop()
)

member this.DowloadAll()=
while(true) do
agent.Post("test")

Barebone F# Parallel programming



open System
let prog(i)=
async {
printfn "starting..%d" i
do! Async.Sleep(i%5*1000)
printfn "finished..%d" i
}

let ret=
[1..1000]
|> Seq.map ( fun n->prog(n))
|> Async.Parallel
|> Async.StartAsTask

Console.ReadLine() |> ignore

F# Parallelism: Map=>Parallel=>StartAsTask


Key ---- function called in Map must have async { }

namespace JQD

type Downloader() =

let Download n=
async {
let f=System.IO.File.ReadLines("")
return f
}

member this.DA()=
[|1.100|]
|> Seq.map(fun n->Download n)
|> Async.Parallel
|> Async.StartAsTask

F# Map Reduce


let nums =[|1..100|]

let sqr x = x*x

let sumofsqr nums =
let mutable acc=0
for n in nums do
acc<-acc+sqr n
acc

let rec sumofsqrRec nums=
match nums with
| []-> 0
| n::rest -> (sqr n)+ (sumofsqrRec rest )

let rec sumofsqrPipe nums=
nums
|>Seq.map sqr
|>Seq.sum

Sunday, May 30, 2010

F# Asyn


F# is sensitive to indentation and linage (e.g. the calling "Donwload" [next line] ()
// Learn more about F# at http://fsharp.net
namespace JQD

type Downloader() =
let folder="test"

let Download()=
async {
let f=System.IO.File.ReadLines("")
let g="456"
return f
}

member this.DA()=
async {
while(true) do
let n=Download
()
}
|>Async.StartImmediate

Monday, February 8, 2010

Key elements to set up 15022 in CRD


(1) Batch DoExport ISITC Parameters Sender/Receiver
(2) Delivery type, order entry

(3)
C:\..\bin\DoExport -i C:\..\conf\cm_batch_2.ini -e 25 -o --role IP --party BBH --method FILE --out 15022.BBI. --isitc MyINC,BBHCUS3IMyInc,JQD,N,O,INVBUS33

(4) Parties: (Code, BIC, Type)
BBH xxxxx Custodian

(5) Set Receive/Delivery Free Indicator
Settlement Allocation Blotter ( not needed for Batch DoExport)

(7) ASSI (SSB FUT,102)
ACCOUNT Group=BBH 15022
Security Type=FUT
Interested Party=SSB NOTAG22 (special inst)

(8) Export Dir =\\MyInc\Test\Working\IntraDay override by DoExport.

Steps:
-- No need to set up SysParam
-- Set up Party BBH BIC Code, Custodian
-- Account Group: Custodian code=BBH
-- Export Routing Rule ISITC and 15022 Save to File, ISITC param but be override by DoExport
--- Settlement Default Delivery Type=DTC
--- Broker SSI BIC Codes, Determinant InvClass=FUT
--- Account SSI Determinant=AcctG, Inst=Interest Pary=BBH.
--- Post Trade Routing Rule.

Test Trade with Broker with BSSI and Account with ASSI, send ACCT and post_tradea and run DoExport

Related SQL:


-- (0) No need to change System Param, ISITC_INT_PARTY is obsolete in 8222

-- (1) Setup Party: BBH BIC code =INVBUS33 manual update

select BIC_CODE,* from dbo.CSM_ISSUER

--(2) Set up Account Group and attached Accounts

insert cs_fund
(
ACCT_CD,
ACCT_NAME,
ACCT_SHT_NAME,
ACCT_TYP_CD ,
CRRNCY_CD,
ALLOW_BUY ,
ALLOW_SELL ,
ALLOW_NEG_CASH,
MANAGER,
CRD_LOCK ,
ERISA_ELIGIBLE,
GAIN_LOSS_SENSITIVE,
CUSTODIAN_CD,
INACTIVE,
AUTH_NUM,
COMP_BID_REQ,
MAG_IND ,
CFWD_CLOSE_CPARTY_IND ,
CFWD_NEW_CPARTY_IND ,
AUTO_CFWD_IND ,
USE_SETTLE_DATE_BAL_IND,
DM_IND,
DEF_MDL_REM_IND,
MDL_QTY_BASED_IND ,
MULT_MDL_IND ,
MULT_MDL_NORM_IND,
BLOCK_ORDER_IND ,
TEN_MIN_CHECK_IND
)

values
(
'BBH15022',
'BBH 15022 Group',
'BBH15022G',
'G',
'USD',
'Y',
'Y',
'Y',
'TM_DEV',
'N',
'N',
'N',
'BBH',
'N',
0,
'N',
'N',
'N',
'N',
'N',
'N',
'N',
'P',
'N',
'N',
'N',
'Y',
'P'
)

insert CS_FUND_CONFIG values ('BBH15022','EOLC5','G',100.000)


-- (3) Export Routing Rule:


insert TS_ROUTING_DEF (
ROUTING_DEF_ID,
PARTY_ID,
PARTY_TYPE,
PARTY_ROLE,
PACKAGE_TYPE,
METHOD_CD,
FORMAT,
COMMENTS,
EXECUTION_INFO,
IS_DEFAULT,
CRD_LOCK,
DEF_EXP_PARMS,
SENDER_ID,
SENDER_TID,
SENDER_CONTACT,
ACCT_CD ,
INTERFACE_SYST,
EOD_FLAG,
F_IND,
ROUTING_SERVER,
ROUTING_DEF_NAME,
ROUTING_STATUS_LIST ,
RECEIVER_BIC_CD)

values
(1700047561,
'BBH',
'CSM_ISSUER',
'CUST',
'EXPORT',
'FILE',
25,
'Test by JQD',
'15022.BBH.JQD.',
'Y',
'N',
'ISITC:MYIKK,BBHCUS3IGMO,JQD,N,1,INVBUS33,st:ACCT,st:CNCLACCT',
'MYIAA',
'BBHCUS3IGMO',
'JQD',
NULL,
NULL,
'N',
'N',
NULL,
'BBH 15022',
'ACCT,CNCLACCT',
'INVBUS33'
)


--(4) Set Up Settlement Default --Manual process
Exch Cnty=USA Inv Class=FUT
Delivery= DTC

-- (5) Broker SSI
insert cs_settle_inst values
(1700046932,NULL,NULL,NULL,NULL,NULL,NULL,'Name',
'Address',NULL,'BARCKK5GXXX',NULL,NULL,'MYI',NULL,'MYI',
'MYI',NULL,NULL,NULL,'SSI',
NULL,'BCYD DEBT','BARCKK5GXXX',1700046932
,NULL,NULL,NULL,NULL, NULL,NULL, NULL, NULL,'MYI',
'MYI', NULL,NULL,NULL,NULL,NULL)

INSERT ts_dterm values(1700046932,'BSI','BCYD',3.000, NULL,'Y','N',NULL)

-- (6) Account SSI
insert TS_DETERM values
(1700046916,'ASI','BBH 15022',1.000,NULL,'Y','N',NULL)

insert TS_DETERM_VALUE values
(1700046916,100063,'BBH15022','EQ',NULL)

insert TS_FUND_SETTINST values
(1700046918,'IP','BBH','AcctNum',NULL
, NULL , NULL, NULL,'ABANum','INVBUS33')


--(7) Set up Post-Trade Rounting -- Manual
SI Enrichment = Broker and Account Instruction Attached