Sorry that this question became so big. I started out with a simply question in mind. I have historical quote data. I want to do a simulation of a trading agents and an orderbook agent reacting to the data and each other. Can I use an another agent to control the flow of events so that the first two agents don't have a chance to react to event sooner than they would have in R/T. - But that seemed too vague so I went through
But when I look at it, I was really still just asking "How do you do this?" which really is not the right format for SO. As Daniel pointed out that is too broad a question, In the end, Phil's hint and a good night sleep allowed me to come up with a solution which I have included below. Hopefully, other will get some benefit from it. I'm still not happy with my approach, but I think I Code Review is a better place to post in regards to that.
Also, thank you to the SO F# community for not voting down my meager rep into oblivion!
open System
open System.IO
let src = __SOURCE_DIRECTORY__
let myPath = Path.Combine(src, "Test_data.txt")
// Create some test data
let makeTestDataFile path lineCount =
let now = System.DateTime.Now
File.WriteAllLines(path,
seq { for i in 1 .. lineCount do
let dt = now.AddSeconds(float i)
yield sprintf "%s,%d" (dt.ToString("MM/dd/yyyy hh:mm:ss.fff tt")) i })
makeTestDataFile myPath 10
Thanks to Phil I arrived at a working prototype:
type MsgType =
| HistEvent of DateTime * float
| AgentEvent of DateTime * float
type DataPoint = {time:DateTime; value:float}
type Agent<'T> = MailboxProcessor<'T>
type EventTrafficAgent () =
let event = new Event<DataPoint>()
let agent = Agent.Start(fun inbox ->
let rec loop eventQue now () = async {
let! msg = inbox.Receive()
// use some form of que managment to decide
let updatedQue =
match msg with
| HistEvent (dt, v) ->
let now = max now dt // log most recent date
// check if there are any pending events that can now be sent
// if so, send and remove
let updatedQue =
eventQue
|> List.filter(fun e ->
if e.time <= now then
event.Trigger(e)
let now = e.time
printfn "\tDequeing & Sending delayed event: {time = %s, value %f}" (e.time.ToString("mm:ss.fff")) e.value
false
else
true)
// just send the historical event as these are never delayed
event.Trigger({time = dt; value = v})
updatedQue
| AgentEvent (dt, v) ->
let tm = dt.AddSeconds(1.5) // time with lag added i.e. "intended time of arrival"
let cacheIt = tm >= now
// return an updated list of cached orders
(if cacheIt then
printfn "\tAdding to delayed que: {time = %s, value %f}" (tm.ToString("mm:ss.fff")) v
{time = tm; value=v} :: eventQue
else
printfn "\tJust sending without delay: {time = %s, value %f}" (tm.ToString("mm:ss.fff")) v
event.Trigger({time = tm; value = v})
eventQue)
return! loop updatedQue now ()
}
loop List.empty<DataPoint> DateTime.MinValue () )
member x.Post msg = agent.Post msg
member x.EventProduced = event.Publish
type OrderBookAgent () =
let event = new Event<DataPoint>()
let agent = Agent.Start(fun inbox ->
let rec loop () = async {
let! (msg:DataPoint) = inbox.Receive()
if msg.value = 42. then event.Trigger({time = msg.time; value = 99.})
return! loop ()
}
loop () )
member x.Post msg = agent.Post msg
member x.Publish = event.Publish
type TradingAgent () =
let event = new Event<DataPoint>()
let agent = Agent.Start(fun inbox ->
let rec loop () = async {
let! (msg:DataPoint) = inbox.Receive()
if msg.value = 7. then event.Trigger({time = msg.time; value = 42.})
return! loop ()
}
loop () )
member x.Post msg = agent.Post msg
member x.Publish = event.Publish
type StreamData(filePath, eventMgr:EventTrafficAgent) =
let sr = new StreamReader ((filePath:string))
member x.Reply() =
async { while not sr.EndOfStream do
let line = sr.ReadLine ()
let dtVal = line.Split(char(","))
let time =DateTime.Parse (dtVal.[0])
let value = Double.Parse(dtVal.[1])
do! Async.Sleep(250) // here to allow you to see it ticking by. set to 1 for full speed
do eventMgr.Post (HistEvent(time, value))}
|> Async.StartImmediate
let eventCop = new EventTrafficAgent()
let orderBook = new OrderBookAgent()
let tradeBot = new TradingAgent()
eventCop.EventProduced.Add(fun e -> printfn "event Cop publishing {time = %s, value %3f}" (e.time.ToString("mm:ss.fff")) e.value)
eventCop.EventProduced.Add(fun e -> orderBook.Post e )
eventCop.EventProduced.Add(fun e -> tradeBot.Post e )
orderBook.Publish.Add(fun e -> eventCop.Post (AgentEvent( e.time, e.value)) )
tradeBot.Publish.Add(fun e -> eventCop.Post (AgentEvent( e.time, e.value)) )
let stream = StreamData(myPath, eventCop )
do stream.Reply()
The output is
event Cop publishing {time = 26:23.265, value 3.000000}
event Cop publishing {time = 26:24.265, value 4.000000}
event Cop publishing {time = 26:25.265, value 5.000000}
event Cop publishing {time = 26:26.265, value 6.000000}
event Cop publishing {time = 26:27.265, value 7.000000}
Adding to delayed que: {time = 26:28.765, value 42.000000}
event Cop publishing {time = 26:28.265, value 8.000000}
event Cop publishing {time = 26:28.765, value 42.000000}
Dequeing & Sending delayed event: {time = 26:28.765, value 42.000000}
event Cop publishing {time = 26:29.265, value 9.000000}
Adding to delayed que: {time = 26:30.265, value 99.000000}
event Cop publishing {time = 26:30.265, value 99.000000}
Dequeing & Sending delayed event: {time = 26:30.265, value 99.000000}
event Cop publishing {time = 26:30.265, value 10.000000}
I guess the only remaining question I have is would it be better to use something like AsyncSeq<'T> to suck in the data to the event mgr rather than push it in as I am doing now.
Not a bad first attempt, I think you're almost there, one thing that stands out is loop
should be defined as a function, i.e.
let rec loop () = async {
^^
and calls to loop should be using unit as a parameter, i.e.
do! loop ()
^^
}
loop () )
^^
finally I'd recommend using return! over do! for recursion, i.e.
return! loop ()