Cinderella-Man / hands-on-elixir-and-otp-cryptocurrency-trading-bot-source-code

Resources related to the "Hands-on Elixir & OTP: Cryptocurrency trading bot" book
https://elixircryptobot.com
75 stars 24 forks source link

Offering styling for Piping #23

Open jonathanarndt opened 1 year ago

jonathanarndt commented 1 year ago

Hey, thanks so much for all of your work in this book, I've followed along when I first started learning Elixir and have investigated more recently, having been able to understand more of what's going on now. It's been a great help on my Elixir journey.

It's been great going through the book process of building a clever umbrella app one step at a time and then refactoring it and having the "why" explained. If you wanted to add on to this about how piping can help readability then maybe you'd want to include a refactor in /apps/naive/lib/naive/strategy.ex such as:

 def calculate_sell_price(buy_price, profit_interval, tick_size) do
   fee = "1.001"
-  original_price = D.mult(buy_price, fee)
+  adjusted_profit_interval = profit_interval |> D.add("1.0")

-  net_target_price =
-    D.mult(
-      original_price,
-      D.add("1.0", profit_interval)
-    )
-
-  gross_target_price = D.mult(net_target_price, fee)
-
-  D.to_string(
-    D.mult(
-      D.div_int(gross_target_price, tick_size),
-      tick_size
-    ),
-    :normal
-  )
+  buy_price
+  |> D.mult(fee)
+  |> D.mult(adjusted_profit_interval)
+  |> D.mult(fee)
+  |> D.div_int(tick_size)
+  |> D.mult(tick_size)
+  |> D.to_string(:normal)
 end

 def calculate_buy_price(current_price, buy_down_interval, tick_size) do
   # not necessarily legal price
-  exact_buy_price =
-    D.sub(
-      current_price,
-      D.mult(current_price, buy_down_interval)
-    )
+  buy_down_price = current_price |> D.mult(buy_down_interval)

-  D.to_string(
-    D.mult(
-      D.div_int(exact_buy_price, tick_size),
-      tick_size
-    ),
-    :normal
-  )
+  current_price
+  |> D.sub(buy_down_price)
+  |> D.div_int(tick_size)
+  |> D.mult(tick_size)
+  |> D.to_string(:normal)
 end

 def calculate_quantity(budget, price, step_size) do
   # not necessarily legal quantity
-  exact_target_quantity = D.div(budget, price)
-
-  D.to_string(
-    D.mult(
-      D.div_int(exact_target_quantity, step_size),
-      step_size
-    ),
-    :normal
-  )
+  budget
+  |> D.div(price)
+  |> D.div_int(step_size)
+  |> D.mult(step_size)
+  |> D.to_string(:normal)
 end

I'm really looking forward to any future updates you make and would recommend your book to anyone else thinking about it as it's been a great process to learn some of the finer details of how Elixir can be used. Thanks again!

Cinderella-Man commented 1 year ago

Hey Jonathan :wave:

It's fantastic to hear that my book was a great help on your Elixir journey :rocket:

Thank you for your suggestion about the piping. It would take a considerable amount of work to retrofit it to the existing chapters, but I will add it as an appendix at the end of the book. Thank you very much once again for your contribution to making the book better!