Skip to content

Commit f9240b8

Browse files
authored
Chapter 20 (#22)
* working throug chapter 20 * finished chapter 20 * comparing against franton branch * updating notes
1 parent 4aeb081 commit f9240b8

11 files changed

Lines changed: 296 additions & 375 deletions

File tree

apps/naive/lib/naive.ex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,30 @@ defmodule Naive do
33
Documentation for `Naive`.
44
"""
55

6-
alias Naive.DynamicSymbolSupervisor
6+
alias Naive.DynamicTraderSupervisor
7+
alias Naive.Trader
78

89
def start_trading(symbol) do
910
symbol
1011
|> String.upcase()
11-
|> DynamicSymbolSupervisor.start_worker()
12+
|> DynamicTraderSupervisor.start_worker()
1213
end
1314

1415
def stop_trading(symbol) do
1516
symbol
1617
|> String.upcase()
17-
|> DynamicSymbolSupervisor.stop_worker()
18+
|> DynamicTraderSupervisor.stop_worker()
1819
end
1920

2021
def shutdown_trading(symbol) do
2122
symbol
2223
|> String.upcase()
23-
|> DynamicSymbolSupervisor.shutdown_worker()
24+
|> DynamicTraderSupervisor.shutdown_worker()
25+
end
26+
27+
def get_positions(symbol) do
28+
symbol
29+
|> String.upcase()
30+
|> Trader.get_positions()
2431
end
2532
end

apps/naive/lib/naive/dynamic_symbol_supervisor.ex renamed to apps/naive/lib/naive/dynamic_trader_supervisor.ex

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
defmodule Naive.DynamicSymbolSupervisor do
1+
defmodule Naive.DynamicTraderSupervisor do
22
@moduledoc """
33
Dynamic symbol supervisor, in charge of supervise runtime created symbols
44
"""
55
use DynamicSupervisor
66

7+
require Logger
8+
79
alias Naive.Repo
810
alias Naive.Schema.Settings
9-
alias Naive.SymbolSupervisor
11+
alias Naive.Strategy
12+
alias Naive.Trader
1013

1114
import Ecto.Query, only: [from: 2]
1215

13-
require Logger
14-
15-
@registry :naive_symbol_supervisors
16+
@registry :naive_traders
1617

1718
def start_link(_arg) do
1819
DynamicSupervisor.start_link(__MODULE__, [], name: __MODULE__)
@@ -22,7 +23,7 @@ defmodule Naive.DynamicSymbolSupervisor do
2223
DynamicSupervisor.init(strategy: :one_for_one)
2324
end
2425

25-
def autostart_workers() do
26+
def autostart_workers do
2627
Repo.all(
2728
from(s in Settings,
2829
where: s.status == "on",
@@ -34,39 +35,34 @@ defmodule Naive.DynamicSymbolSupervisor do
3435

3536
def start_worker(symbol) do
3637
Logger.info("Starting trading on #{symbol}")
37-
update_status(symbol, "on")
38+
Strategy.update_status(symbol, "on")
3839
start_child(symbol)
3940
end
4041

4142
def stop_worker(symbol) do
4243
Logger.info("Stopping trading on #{symbol}")
43-
update_status(symbol, "off")
44+
Strategy.update_status(symbol, "off")
4445
stop_child(symbol)
4546
end
4647

4748
def shutdown_worker(symbol) when is_binary(symbol) do
4849
Logger.info("Shutdown of trading on #{symbol} initialized")
4950

5051
{:ok, settings} =
51-
update_status(
52+
Strategy.update_status(
5253
symbol,
5354
"shutdown"
5455
)
5556

56-
Naive.Leader.notify(:settings_updated, settings)
57-
{:ok, settings}
58-
end
57+
Trader.notify(:settings_updated, settings)
5958

60-
defp update_status(symbol, status) when is_binary(symbol) and is_binary(status) do
61-
Repo.get_by(Settings, symbol: symbol)
62-
|> Ecto.Changeset.change(%{status: status})
63-
|> Repo.update()
59+
{:ok, settings}
6460
end
6561

6662
defp start_child(args) do
6763
DynamicSupervisor.start_child(
6864
__MODULE__,
69-
{SymbolSupervisor, args}
65+
{Trader, args}
7066
)
7167
end
7268

apps/naive/lib/naive/leader.ex

Lines changed: 0 additions & 245 deletions
This file was deleted.

0 commit comments

Comments
 (0)