Source code to generate the "Hands-on Elixir & OTP: Cryptocurrency trading bot" book

  • By Kamil Skowron
  • Last update: Dec 27, 2022
  • Comments: 11

This repository is the home of Hands-on Elixir & OTP: Cryptocurrency trading bot book.

Want to learn Elixir & OTP by creating a real-world project?

The book cover

With “Hands-on Elixir & OTP: Cryptocurrency trading bot”, you will gain hands-on experience by writing an interesting software project from scratch. We will explore all the key abstractions and essential principles through iterative implementation improvements.

We will start by creating a new umbrella application, subscribing to WebSocket streams, implementing a basic trading flow, and focusing on improving it by expanding on the topics like supervision trees, resiliency, refactoring using macros, utilising the Registry, testing and others.

This book is 80% complete - chapters 1-17 are finished, and I’ll add more content soon. It’s also a loosely written representation of the Hands-on Elixir & OTP: Cryptocurrency trading bot video course released on YouTube.

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International CC BY-NC-SA 4.0.

Limit of Liability/Disclaimer of Warranty

THIS BOOK IS NOT FINANCIAL ADVICE

THE SOFTWARE/BOOK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE/BOOK OR THE USE OR OTHER DEALINGS IN THE SOFTWARE/BOOK.

Contributing, Errata and Source code {-}

The book is written using R Markdown(it's a very similar syntax to the GitHub markdown but supports many more features including code execution, etc.) and converted to final form (for example PDF) using the bookdown app. This means that editing a chapter is as simple as editing the markdown source of that chapter.

There are two repositories related to this book(both hosted on Github):

In regards to contributions - I would love to follow the standard process of forking, making changes, opening PR(please look is there a branch for the next version and point to it instead of main), merging, and releasing a new version of the book.

This books has also the GitHub Discussions enabled for both the book's repo as well as source code's repo, please feel welcome to start any discussions related to book there.

Github

https://github.com/Cinderella-Man/hands-on-elixir-and-otp-cryptocurrency-trading-bot

Comments(11)

  • 1

    Chapter 2: Use gitignored secrets.exs for Binance keys

    Updated:

    This PR updates the section where we set the Binance API keys in order to avoid accidentally checking these keys into source control. We put the Binance config in a new config/secrets.exs which is added to .gitignore and included in config/config.exsif it exists using import_config.


    Originally:

    I appreciate that this is maybe a bit opinionated, but I think it's worth showing a source-control-safe method of setting the keys for Binance. I'm following the book in a public repo and wouldn't want my keys in that repo. In this PR I'm using environment variables in config/runtime.exs. I've chosen to use runtime.exs in case the guide gets into compiling later on - compile time environment variable resolution is quite confusing/unexpected IMO. I've demonstrated setting these environment variables in the following test run.

  • 2

    Unable to start Streamer app with Phoenix.PubSub

    I've been following along the book as a practice method until I found myself blocked with the following issue: Environment: Elixir 1.11.4 Erlang/OTP 23

    Relevant code snippet

    # apps/streamer/lib/streamer/application.ex
    def start(_type, _args) do
        children = [
          {
            Phoenix.PubSub,
            name: Streamer.PubSub, adapter_name: Phoenix.PubSub.PG2
          }
        ]
    
        opts = [strategy: :one_for_one, name: Streamer.Supervisor]
        Supervisor.start_link(children, opts)
      end
    

    Expected: iex -S mix starts the application correctly.

    Actual result: iex -S mix produces error: ** (Mix) Could not start application streamer: Streamer.Application.start(:normal, []) returned an error: shutdown: failed to start child: Phoenix.PubSub.Supervisor ** (EXIT) shutdown: failed to start child: Phoenix.PubSub.PG2 ** (EXIT) exited in: :gen_server.call(Phoenix.PubSub, {:join_local, Streamer.PubSub.Adapter, #PID<0.251.0>}, :infinity) ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started

    Besides changing the Elixir/OTP versions, I see no way of debugging this. Any idea?

  • 3

    Typo at the end of 2.3.2 (i think)

    Reading through your book. Really enjoying it so far. I came across this piece of code at the end of 2.3.2

    defp calculate_sell_price(buy_price, profit_interval, tick_size) do
        fee = D.new("1.001")
        original_price = D.mult(D.new(buy_price), fee)
    
        net_target_price =
          D.mult(
            original_price,
            D.add("1.0", profit_interval)
          )
    
        gross_target_price = D.mult(net_target_price, fee)
    
        D.to_float(
          D.mult(
            D.div_int(gross_target_price, tick_size),
            tick_size
          )
        )
      end
    

    However the commentary that is provided immediate after is a little confusing. For instance there is this quote,

    We started by calculating the gross_buy_price which is a sum of buy price together with the fee that we paid on top of it.

    I do not see gross_buy_price, but perhaps that was intended to be original_price, however the original_price variable is not calculate with a sum like the above quote states it is D.mult(D.new(buy_price), fee). There is another quote that is similar,

    As we will be charged a fee for selling, we need to add the fee again on top of this target sell price(gross_target_price).

    Again, that is referring to the line gross_target_price = D.mult(net_target_price, fee) which is also a multiplication and not a sum.

  • 4

    Code mismatch at 6.4

    Hi, I noticed 2 mismatch with map returned by fetch_symbol_settings/1:

    1. The key :symbol added in chapter 5 now is disappeared

    2. In chapter intro we can read

      should never be smaller than double the fee(at the moment of writting transaction fee is 0.1%) that you are paying per transaction

      The value assigned is 0.0001 but I expect 2 times 0.1% and so 0.002. I probably misunderstood how you assigned this value. Can you clarify me this point?

    Thank you!

  • 5

    Update single to double quotes inside config secrets.exs import code

    Hello there 😀.

    This change was introduced on the source code repo, and this PR intends to reflect the exact change in the book.

    See: https://github.com/Cinderella-Man/hands-on-elixir-and-otp-cryptocurrency-trading-bot-source-code/commit/19ffb3676742a48814e8275843020927a827270a

  • 6

    Fix `postgrex` version spell

    ** (Mix) Required version ">= 0.0" for package postgrex is incorrectly specified 
    

    Core: https://github.com/frathon/hands-on-elixir-and-otp-cryptocurrency-trading-bot/pull/22/files#diff-8a524efdf9e62755d23c18299df4a9c21c290ede690f63c2930b884043571c41R107

    Other is auto format

  • 7

    Change from step to step_size

    By the end of the sub-chapter 7.5 "Calculate quantity" on the function declaration for calculate_quantity, you are using in the body just "step" instead of "step_size".

    Best regards

  • 8

    Small grammar change

    "Financial Advice" for some reason is never singular, to a native English speaker this sounds wrong. You could give advice but not an advice, I guess one of the many weirdnesses of English because you can give a talk or give a tutorial but not give an advice. It makes no sense.

  • 9

    V0.4.1

    This release contains a refreshing of all drawings throughout the book(including reformatting when size changed) + fixes to chapter 18 and 19 (lack of proper syntax highlighting)

  • 10

    Chapters 18, 19 and 20

    A result of months of work (primarily personal growth and questioning my own understanding of things like "functional programming"):

    * Chapter 18 - Functional Elixir
    
    In this chapter, we will venture into the functional world by looking at how could we improve our code to push side effects to the edge. We will revise the `Naive.Trader` code to abstract away our strategy code into a new module called `Naive.Strategy`. From this place, we will reorganise the code to maximise the amount of easily testable pure functions. Finally, we will explore hypothetical implementations that will allow us to inject data into function or even manage effects to aid testability. We will compare those to solutions built into Elixir like the `with` statement.
    
    * Chapter 19 - Idiomatic OTP
    
    In the last chapter, we were looking into how we could reorganise *the code* to maximise the amount of pure code. In this chapter, we will look into different ways of implementing the OHLC(open-high-low-close) aggregator, considering similar optimisation but expanding to limit the number of processes to aid testability and maintainability.
    
    * Chapter 20 - Idiomatic trading strategy
    
    We will use the knowledge gained in the last chapter to revise our Naive trading strategy so we will minimise the number of processes required to trade. We will move the functionalities provided by the `Naive.Leader` and the `Naive.SymbolSupervisor` into our strategy, taking care to put as much of it as possible into the pure part. In the end, our `Naive.Trader` will be able to manage multiple positions(trade cycles), and the vast majority of code previously scattered across multiple modules/processes will become easily testable pure functions inside our `Naive.Strategy`.
    

    Additionally, some changes around the book, including fixing links, adding aliases for easier reading etc.

  • 11

    V0.3.0

    Updates:

    • chapter 17 added - "Mox rocks"
    • line spacing increased
    • font updated to Garamond
    • PDF version updated as spacing was updated, which cause the layout to get out of whack
    • fixes to chapter 16 - environment for e2e test set to integration
    • versions of dependencies truncated after minor version to ease the pain of new non-breaking feature releases
    • DB password updated to non-default one

    Currently, the book is 226 pages long :rocket: