Skip to content

Instantly share code, notes, and snippets.

View Swoorup's full-sized avatar
🎲
Focusing

Swoorup Joshi Swoorup

🎲
Focusing
View GitHub Profile
@Swoorup
Swoorup / Subscription.scala
Created February 3, 2021 06:00
Scala 3 Match types Channel subscription
case class TradeEvent()
case class OrderEvent()
type Channel = "trades" | "orders"
// def sub2[Channels <: Tuple](channels: Channels)(using Channel <:< Tuple.Union[Channels]) = channels.toList
type SelChannel[C <: Tuple] = C match {
case "trades" *: xs => TradeEvent | SelChannel[xs]
case "orders" *: xs => OrderEvent | SelChannel[xs]
@Swoorup
Swoorup / FMarshaller.scala
Created April 29, 2021 11:19
Scala 3 - Cats effect - Akka Http Marshalling
import akka.http.scaladsl.marshalling.{GenericMarshallers, ToResponseMarshaller}
import cats.Id
import cats.effect.IO
import cats.effect.std.Dispatcher
import cats.syntax.apply.*
import scala.concurrent.Future
object FMarshaller:
given [A](using m: ToResponseMarshaller[A]): ToResponseMarshaller[Future[A]] =
GenericMarshallers.futureMarshaller
@Swoorup
Swoorup / CustomerMessageBroker.scala
Last active November 1, 2021 15:47
Type safe message router/broker
import cats.implicits.*
import cats.Id
import scala.collection.mutable.ListBuffer
import fs2.Stream
// The message broking interface
trait MessageBroker[F[_], Router[_]]:
def push[MsgKey: ValueOf](value: Router[MsgKey]): F[Unit]
def subscribe[MsgKey: ValueOf]: Stream[F, Router[MsgKey]]
@Swoorup
Swoorup / CustomerMessageBrokerMapK.scala
Last active November 2, 2021 04:45
Type safe Message Broker/routing using MapK
import cats.Id
import cats.effect.{ IO, Resource }
import cats.effect.unsafe.IORuntime
import cats.implicits.*
import com.codedx.util.MapK
import fs2.Stream
import fs2.concurrent.Topic
import scala.collection.mutable.ListBuffer
import scala.concurrent.duration.*
@Swoorup
Swoorup / RangeCodec.scala
Created November 19, 2021 10:00
Range Codec for skunk
package infrastructure.repository.codec
import skunk.Codec
import PgRangeSupportUtils.*
enum EdgeType:
case `[_,_)`
case `(_,_]`
case `(_,_)`
case `[_,_]`
@Swoorup
Swoorup / ArrayLog.scala
Created January 31, 2022 16:28
Append only data structure backed by array.
final case class ArrayLog[A](
private val arr: Array[A],
private val curWriteIdx: Int,
maxLength: Int
)(using ct: ClassTag[A]) { x =>
private inline def offset: Int =
if curWriteIdx < maxLength then 0 else curWriteIdx - maxLength
inline def size: Int =
@Swoorup
Swoorup / Stream.ts
Created May 23, 2022 17:09
Fetch ndjson stream idea...
import * as S from '@typed/fp/Stream';
import * as E from 'fp-ts/Either';
import { pipe } from 'fp-ts/function';
import * as TE from 'fp-ts/TaskEither';
export function streamFromTaskEither<A>(taskEither: TE.TaskEither<Error, A>): S.Stream<A> {
return pipe(
taskEither,
S.fromTask,
S.chain(E.match(S.throwError, S.of)),
@Swoorup
Swoorup / Demo.scala
Created June 28, 2022 07:51
Typed Akka like actors using fs2 and cats
import cats.effect.syntax.all.*
import cats.syntax.all.*
import cats.effect.*
import fs2.Stream
import cats.effect.std.Queue
import scala.concurrent.duration.*
case class FSM[F[_], S, I, O](run: (S, I) => F[(S, O)])
case class Ping[F[_]](replyTo: Deferred[F, String])
@Swoorup
Swoorup / ActorK.scala
Last active July 17, 2023 19:54
Typed Actors using Cats Effect, FS2 and Deferred Magic
import cats.effect.syntax.all.*
import cats.syntax.all.*
import cats.effect.*
import fs2.Stream
import cats.effect.std.Queue
import scala.concurrent.duration.*
import lib.FSM
import lib.actor.{Actor, AskMsg}

https://web.archive.org/web/20110219163448/http://howtohft.wordpress.com/2011/02/15/how-to-build-a-fast-limit-order-book/

The response to my first few posts has been much larger than I’d imagined and I’d like to thank everyone for the encouragement.

If you’re interested in building a trading system I recommend first reading my previous post on general ideas to keep in mind.

My first really technical post will be on how to build a limit order book, probably the single most important component of a trading system. Because the data structure chosen to represent the limit order book will be the primary source of market information for trading models, it is important to make it both absolutely correct and extremely fast.

To give some idea of the data volumes, the Nasdaq TotalView ITCH feed, which is every event in every instrument traded on the Nasdaq, can have data rates of 20+ gigabytes/day with spikes of 3 megabytes/second or more. The individual messages average about 20 bytes each so this means handling