Weekly Notes (Feb 13th / Feb 19th, 2018)

I am quite happy. I managed to get quite a bit of work done on my personal projects. I also managed to publish two blog posts.

As a starter, I finished writing My Thoughts on React Native. It ended up being quite a bit of an opinionated piece, even for my taste, but it is what is. The post is out there, so feel free to check it out, and I will be happy to get your feedback on it.


Project Work

CryptoTrader

This week I dusted off my CryptoTrader algotrading project. I started working on a feature I’ve been kind of thinking about for a while, but somehow I did not get to sit down and implement it. When finished, it will allow for placing order pairs (BUY-SELL / SELL-BUY), with a single REST request. The first order will get executed immediately at the market rate (or placed as a limit order if price is specified). The second order will be stored locally, but not executed, until a confirmation gets received, about a successful trade after the first order. The API will be flexible enough to allow for setting a percentage margin (to not have to compute prices manually), as well provide a cancellation window (essentially, executing the second order at a given time, regardless of profit or loss).

This feature would allow essentially anyone running CryptoTrader to test new trading strategies without having to have them built as part of the application. I usually start my experiments by writing a quick and dirty Python script. If the script works well, I would eventually move the idea over to CryptoTrader. Having this feature on board, will allow me to do easier backtesting and figuring out the right parameters.

Enough about the feature, I wanted to demonstrate a couple of things I learned while working on it. The first one is a way to make a Kotlin enum de/serializable using Jackson. Normally, this should work out of the box, but Jackson will serialize enum values to their string name representations, which is not a best practice. Normally, one would map those values to numeric identifiers which will ideally remain unchanged. In this case, even if enum value names changed, this will not break the compatibility with API consumers.

enum class PairType(@JsonValue val id: Long) {
    BUY_SELL(id = 1L),
    SELL_BUY(id = 2L)
    ;

    companion object {
        /**
         * JSON creator
         * NOTE: when giving the creator function parameter the same name as the underlying property we're about to
         * check, the method would not work. Changing the name (to @{link requestId} in this case) seems to have done the job
         *
         * @see <a href="https://github.com/FasterXML/jackson-module-kotlin/issues/75" />
         * @see <a href="https://github.com/FasterXML/jackson-module-kotlin/issues/78" />
         *
         * NOTE: Don't forget the @JvmStatic annotation, Jackson requires a proper
         * static JVM method.
         */
        @JsonCreator
        @JvmStatic
        fun fromId(requestId: Long): PairType {
            return values().first { it. id == requestId }
        }
    }
}

There is however a quirk, which seems to be Kotlin-related, according to this and this issue on GitHub.


More on what goroutines are: https://golangbot.com/goroutines/

Goroutines are **functions* or *methods* that run concurrently with other functions or methods. Goroutines can be thought of as light weight threads. The cost of creating a Goroutine is tiny when compared to a thread. Hence its common for Go applications to have thousands of Goroutines running concurrently.*


Tweets