Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions apps/indicator/lib/indicator/ohlc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ defmodule Indicator.Ohlc do
require Logger
alias Core.Struct.TradeEvent

@pubsub_client Application.compile_env(:core, :pubsub_client)
@enforce_keys [:symbol, :start_time, :duration]
defstruct [:symbol, :start_time, :duration, :open, :high, :low, :close]

Expand Down Expand Up @@ -71,7 +70,7 @@ defmodule Indicator.Ohlc do
defp maybe_broadcast(%__MODULE__{} = ohlc) do
Logger.debug("Broadcasting OHLC: #{inspect(ohlc)}")

@pubsub_client.broadcast(
Phoenix.PubSub.broadcast(
Core.PubSub,
"OHLC:#{ohlc.symbol}",
ohlc
Expand Down
7 changes: 2 additions & 5 deletions apps/indicator/lib/indicator/ohlc/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ defmodule Indicator.Ohlc.Worker do
require Logger
alias Core.Struct.TradeEvent

@logger Application.compile_env(:core, :logger)
@pubsub_client Application.compile_env(:core, :pubsub_client)

def start_link(symbol) do
GenServer.start_link(__MODULE__, symbol)
end

def init(symbol) do
symbol = String.upcase(symbol)
@logger.debug("Initializing a new OHLC worker for #{symbol}")
Logger.info("Initializing a new OHLC worker for #{symbol}")

@pubsub_client.subscribe(
Phoenix.PubSub.subscribe(
Core.PubSub,
"TRADE_EVENTS:#{symbol}"
)
Expand Down
24 changes: 11 additions & 13 deletions apps/naive/lib/naive/strategy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ defmodule Naive.Strategy do
Module in charge of making and executing decisions
"""
alias Core.Struct.TradeEvent
alias Naive.Repo
alias Naive.Schema.Settings

require Logger

@binance_client Application.compile_env(:naive, :binance_client)
@logger Application.compile_env(:core, :logger)
@pubsub_client Application.compile_env(:core, :pubsub_client)
@repo Application.compile_env(:naive, :repo)

defmodule Position do
@enforce_keys [
Expand Down Expand Up @@ -255,7 +253,7 @@ defmodule Naive.Strategy do
} = position,
_settings
) do
@logger.info(
Logger.info(
"Position (#{symbol}/#{id}): " <>
"Placing a BUY order @ #{price}, quantity: #{quantity}"
)
Expand All @@ -279,7 +277,7 @@ defmodule Naive.Strategy do
} = position,
_settings
) do
@logger.info(
Logger.info(
"Position (#{symbol}/#{id}): The BUY order is now filled. " <>
"Placing a SELL order @ #{sell_price}, quantity: #{quantity}"
)
Expand All @@ -305,7 +303,7 @@ defmodule Naive.Strategy do
} = position,
_settings
) do
@logger.info("Position (#{symbol}/#{id}): The BUY order is now partially filled")
Logger.info("Position (#{symbol}/#{id}): The BUY order is now partially filled")

{:ok, %Binance.Order{} = current_buy_order} =
@binance_client.get_order(symbol, timestamp, order_id)
Expand All @@ -324,7 +322,7 @@ defmodule Naive.Strategy do
settings
) do
new_position = generate_fresh_position(settings)
@logger.info("Position (#{symbol}/#{id}): Trade cycle finished")
Logger.info("Position (#{symbol}/#{id}): Trade cycle finished")
{:ok, new_position}
end

Expand All @@ -341,7 +339,7 @@ defmodule Naive.Strategy do
} = position,
_settings
) do
@logger.info("Position (#{symbol}/#{id}): The SELL order is now partially filled")
Logger.info("Position (#{symbol}/#{id}): The SELL order is now partially filled")

{:ok, %Binance.Order{} = current_sell_order} =
@binance_client.get_order(symbol, timestamp, order_id)
Expand All @@ -360,7 +358,7 @@ defmodule Naive.Strategy do
settings
) do
new_position = generate_fresh_position(settings)
@logger.info("Position (#{symbol}/#{id}): Rebuy triggered. Starting a new position")
Logger.info("Position (#{symbol}/#{id}): Rebuy triggered. Starting a new position")
{:ok, new_position}
end

Expand All @@ -375,7 +373,7 @@ defmodule Naive.Strategy do
end

defp broadcast_order(%Binance.Order{} = order) do
@pubsub_client.broadcast(Core.PubSub, "ORDERS:#{order.symbol}", order)
Phoenix.PubSub.broadcast(Core.PubSub, "ORDERS:#{order.symbol}", order)
end

defp convert_to_order(%Binance.OrderResponse{} = response) do
Expand All @@ -395,7 +393,7 @@ defmodule Naive.Strategy do
exchange_info =
@binance_client.get_exchange_info()

db_settings = @repo.get_by!(Settings, symbol: symbol)
db_settings = Repo.get_by!(Settings, symbol: symbol)

merge_filters_into_settings(exchange_info, db_settings, symbol)
end
Expand Down Expand Up @@ -437,8 +435,8 @@ defmodule Naive.Strategy do
end

def update_status(symbol, status) when is_binary(symbol) and is_binary(status) do
@repo.get_by(Settings, symbol: symbol)
Repo.get_by(Settings, symbol: symbol)
|> Ecto.Changeset.change(%{status: status})
|> @repo.update()
|> Repo.update()
end
end
6 changes: 2 additions & 4 deletions apps/naive/lib/naive/trader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ defmodule Naive.Trader do

require Logger

@logger Application.compile_env(:core, :logger)
@pubsub_client Application.compile_env(:core, :pubsub_client)
@registry :naive_traders
defmodule State do
@moduledoc """
Expand All @@ -27,9 +25,9 @@ defmodule Naive.Trader do
end

def init(symbol) do
@logger.info("Initializing new trader for #{symbol}")
Logger.info("Initializing new trader for #{symbol}")

@pubsub_client.subscribe(Core.PubSub, "TRADE_EVENTS:#{symbol}")
Phoenix.PubSub.subscribe(Core.PubSub, "TRADE_EVENTS:#{symbol}")

{:ok, nil, {:continue, {:start_position, symbol}}}
end
Expand Down
10 changes: 3 additions & 7 deletions apps/naive/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ defmodule Naive.MixProject do
elixirc_options: [
warnings_as_errors: true
],
aliases: aliases(),
elixirc_paths: elixirc_paths(Mix.env())
aliases: aliases()
]
end

Expand Down Expand Up @@ -44,13 +43,10 @@ defmodule Naive.MixProject do
{:decimal, "~> 2.0"},
{:ecto_sql, "~> 3.0"},
{:ecto_enum, "~> 1.4"},
{:mimic, "~> 1.7", only: [:test, :integration]},
{:phoenix_pubsub, "~> 2.0"},
{:postgrex, ">= 0.0.0"},
{:sobelow, "~> 0.13", only: [:dev, :test], runtime: false},
{:mox, "~> 1.0", only: [:test, :integration]}
{:sobelow, "~> 0.13", only: [:dev, :test], runtime: false}
]
end

defp elixirc_paths(:test), do: ["test/support", "lib"]
defp elixirc_paths(_), do: ["lib"]
end
66 changes: 66 additions & 0 deletions apps/naive/test/naive/strategy_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
defmodule Naive.StrategyTest do
use ExUnit.Case, async: true
use Mimic

alias Core.Struct.TradeEvent
alias Naive.Strategy
import ExUnit.CaptureLog

@tag :unit
test "Strategy places a buy order" do
expected_order = %Binance.OrderResponse{
client_order_id: "1",
executed_qty: "0.000",
order_id: "x1",
orig_qty: "50.000",
price: "0.800000",
side: "BUY",
status: "NEW",
symbol: "ABC"
}

BinanceMock
|> stub(
:order_limit_buy,
fn "ABC", "50.000", "0.800000", "GTC" -> {:ok, expected_order} end
)

Phoenix.PubSub
|> stub(
:broadcast,
fn _pubsub, _topic, _message -> :ok end
)

settings = %{
symbol: "ABC",
chunks: "5",
budget: "200",
buy_down_interval: "0.2",
profit_interval: "0.1",
rebuy_interval: "0.5",
tick_size: "0.000001",
step_size: "0.001",
status: :on
}

{{:ok, new_positions}, log} =
with_log(fn ->
Naive.Strategy.execute(
%TradeEvent{
price: "1.00000"
},
[
Strategy.generate_fresh_position(settings)
],
settings
)
end)

assert log =~ "0.8"

assert length(new_positions) == 1

%{buy_order: buy_order} = List.first(new_positions)
assert buy_order == expected_order
end
end
73 changes: 0 additions & 73 deletions apps/naive/test/naive/trader_test.exs

This file was deleted.

13 changes: 5 additions & 8 deletions apps/naive/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
ExUnit.start()
Application.ensure_all_started(:mimic)

Application.ensure_all_started(:mox)
# here I differed a little bit from the book cause in has
# warning_as_errors turned on and i needed compile time checks
# Mox.defmock(Test.BinanceMock, for: BinanceMock)
# Mox.defmock(Test.Naive.LeaderMock, for: Naive.Leader)
# Mox.defmock(Test.LoggerMock, for: Core.Test.Logger)
# Mox.defmock(Test.PubSubMock, for: Core.Test.PubSub)
Mimic.copy(BinanceMock)
Mimic.copy(Phoenix.PubSub)

ExUnit.start(capture_log: true)
6 changes: 0 additions & 6 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import Config
config :binance_mock,
use_cached_exchange_info: false

config :core,
logger: Logger,
pubsub_client: Phoenix.PubSub

config :data_warehouse,
ecto_repos: [DataWarehouse.Repo]

Expand All @@ -29,8 +25,6 @@ config :streamer, Streamer.Repo,
config :naive,
binance_client: BinanceMock,
ecto_repos: [Naive.Repo],
leader: Naive.Leader,
repo: Naive.Repo,
trading: %{
defaults: %{
chunks: 5,
Expand Down
9 changes: 0 additions & 9 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
import Config

config :core,
logger: Test.LoggerMock,
pubsub_client: Test.PubSubMock

config :naive,
binance_client: Test.BinanceMock,
leader: Test.Naive.LeaderMock,
repo: Test.Naive.RepoMock
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
"exconstructor": {:hex, :exconstructor, "1.2.10", "72a540c89b4c5af75f88c076727c0318a8f4038df6412dcf546e8771dcac118d", [:mix], [], "hexpm", "6e504f88cc56c3a7313f0d7687c5d3454b014255867232957512a61e5f90bea7"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"},
"ham": {:hex, :ham, "0.3.0", "7cd031b4a55fba219c11553e7b13ba73bd86eab4034518445eff1e038cb9a44d", [:mix], [], "hexpm", "7d6c6b73d7a6a83233876cc1b06a4d9b5de05562b228effda4532f9a49852bf6"},
"httpoison": {:hex, :httpoison, "1.8.2", "9eb9c63ae289296a544842ef816a85d881d4a31f518a0fec089aaa744beae290", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "2bb350d26972e30c96e2ca74a1aaf8293d61d0742ff17f01e0279fef11599921"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"mimic": {:hex, :mimic, "1.11.1", "08b8f82d760ae19bdca698a2c0714921ba41c854959ff58faebb3021f914f9a7", [:mix], [{:ham, "~> 0.2", [hex: :ham, repo: "hexpm", optional: false]}], "hexpm", "bcb35db46dbf089dd026f3a7a8b772c2220124492eeae0533ac5719c505dec53"},
"mox": {:hex, :mox, "1.1.0", "0f5e399649ce9ab7602f72e718305c0f9cdc351190f72844599545e4996af73c", [:mix], [], "hexpm", "d44474c50be02d5b72131070281a5d3895c0e7a95c780e90bc0cfe712f633a13"},
"parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.3", "3168d78ba41835aecad272d5e8cd51aa87a7ac9eb836eabc42f6e57538e3731d", [:mix], [], "hexpm", "bba06bc1dcfd8cb086759f0edc94a8ba2bc8896d5331a1e2c2902bf8e36ee502"},
Expand Down
Loading
Loading