Skip to content

Commit

Permalink
Chapter 20 (#22)
Browse files Browse the repository at this point in the history
* working throug chapter 20

* finished chapter 20

* comparing against franton branch

* updating notes
  • Loading branch information
Bodhert authored Feb 1, 2025
1 parent 4aeb081 commit f9240b8
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 375 deletions.
15 changes: 11 additions & 4 deletions apps/naive/lib/naive.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@ defmodule Naive do
Documentation for `Naive`.
"""

alias Naive.DynamicSymbolSupervisor
alias Naive.DynamicTraderSupervisor
alias Naive.Trader

def start_trading(symbol) do
symbol
|> String.upcase()
|> DynamicSymbolSupervisor.start_worker()
|> DynamicTraderSupervisor.start_worker()
end

def stop_trading(symbol) do
symbol
|> String.upcase()
|> DynamicSymbolSupervisor.stop_worker()
|> DynamicTraderSupervisor.stop_worker()
end

def shutdown_trading(symbol) do
symbol
|> String.upcase()
|> DynamicSymbolSupervisor.shutdown_worker()
|> DynamicTraderSupervisor.shutdown_worker()
end

def get_positions(symbol) do
symbol
|> String.upcase()
|> Trader.get_positions()
end
end
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
defmodule Naive.DynamicSymbolSupervisor do
defmodule Naive.DynamicTraderSupervisor do
@moduledoc """
Dynamic symbol supervisor, in charge of supervise runtime created symbols
"""
use DynamicSupervisor

require Logger

alias Naive.Repo
alias Naive.Schema.Settings
alias Naive.SymbolSupervisor
alias Naive.Strategy
alias Naive.Trader

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

require Logger

@registry :naive_symbol_supervisors
@registry :naive_traders

def start_link(_arg) do
DynamicSupervisor.start_link(__MODULE__, [], name: __MODULE__)
Expand All @@ -22,7 +23,7 @@ defmodule Naive.DynamicSymbolSupervisor do
DynamicSupervisor.init(strategy: :one_for_one)
end

def autostart_workers() do
def autostart_workers do
Repo.all(
from(s in Settings,
where: s.status == "on",
Expand All @@ -34,39 +35,34 @@ defmodule Naive.DynamicSymbolSupervisor do

def start_worker(symbol) do
Logger.info("Starting trading on #{symbol}")
update_status(symbol, "on")
Strategy.update_status(symbol, "on")
start_child(symbol)
end

def stop_worker(symbol) do
Logger.info("Stopping trading on #{symbol}")
update_status(symbol, "off")
Strategy.update_status(symbol, "off")
stop_child(symbol)
end

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

{:ok, settings} =
update_status(
Strategy.update_status(
symbol,
"shutdown"
)

Naive.Leader.notify(:settings_updated, settings)
{:ok, settings}
end
Trader.notify(:settings_updated, settings)

defp update_status(symbol, status) when is_binary(symbol) and is_binary(status) do
Repo.get_by(Settings, symbol: symbol)
|> Ecto.Changeset.change(%{status: status})
|> Repo.update()
{:ok, settings}
end

defp start_child(args) do
DynamicSupervisor.start_child(
__MODULE__,
{SymbolSupervisor, args}
{Trader, args}
)
end

Expand Down
245 changes: 0 additions & 245 deletions apps/naive/lib/naive/leader.ex

This file was deleted.

Loading

0 comments on commit f9240b8

Please sign in to comment.