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

leader.ex, budget with from_float #13

Closed juanpabloaj closed 3 years ago

juanpabloaj commented 3 years ago

To try to avoid this error.

[error] GenServer :"Elixir.Naive.Leader-ETHBTC" terminating
** (ArgumentError) implicit conversion of 0.003 to Decimal is not allowed. Use Decimal.from_float/1
    (decimal 2.0.0) lib/decimal.ex:1734: Decimal.decimal/1
    (decimal 2.0.0) lib/decimal.ex:472: Decimal.div/2
    (naive 0.1.0) lib/naive/leader.ex:157: Naive.Leader.fresh_trader_state/1
    (naive 0.1.0) lib/naive/leader.ex:54: Naive.Leader.handle_continue/2
    (stdlib 3.15.1) gen_server.erl:695: :gen_server.try_dispatch/4
    (stdlib 3.15.1) gen_server.erl:437: :gen_server.loop/7
    (stdlib 3.15.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: {:continue, :start_traders}
State: %Naive.Leader.State{settings: nil, symbol: "ETHBTC", traders: []}

When your budget is in bitcoins your budget could be float.

Cinderella-Man commented 3 years ago

Hi @juanpabloaj

Thank you very much for another PR :+1: I think this time, this change is not required but probably it could be more explicitly said inside the book that Decimal works fine with strings :thinking:

I understand why budget would be a float value for currencies like BTC but it could be set as a string, then you don't need to convert it to the Decimal struct to use it in calculations.

I don't understand why would you set chunks as float value though? chunks represent the number of chunks (processes) that the budget will be divided into - could you please explain?

juanpabloaj commented 3 years ago

I understand why budget would be a float value for currencies like BTC but it could be set as a string, then you don't need to convert it to the Decimal struct to use it in calculations.

I agree.

I don't understand why would you set chunks as float value though? chunks represent the number of chunks (processes) that the budget will be divided into - could you please explain?

My mistake, I agree, chunks don't require to be Decimal.