Skip to content

Commit

Permalink
Better handling of message handlers, Jason encoder for messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Sienkiewicz committed Nov 21, 2023
1 parent 2ccb05f commit 087faf3
Show file tree
Hide file tree
Showing 49 changed files with 423 additions and 664 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ npm-debug.log

# VSCode
.vscode/

# PLTs
priv/plts/
79 changes: 0 additions & 79 deletions lib/xtb_client/messages.ex

This file was deleted.

42 changes: 1 addition & 41 deletions lib/xtb_client/messages/balance_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ defmodule XtbClient.Messages.BalanceInfo do
:stock_lock,
:stock_value
]

@derive Jason.Encoder
defstruct balance: 0.0,
cash_stock_value: 0.0,
Expand All @@ -74,37 +73,6 @@ defmodule XtbClient.Messages.BalanceInfo do
"stockLock" => stock_lock,
"stockValue" => stock_value
})
when is_number(balance) and is_number(cash_stock_value) and is_number(credit) and
is_binary(currency) and is_number(equity) and is_number(equity_fx) and
is_number(margin) and is_number(margin_free) and is_number(margin_level) and
is_number(stock_lock) and is_number(stock_value) do
%__MODULE__{
balance: balance,
cash_stock_value: cash_stock_value,
credit: credit,
currency: currency,
equity: equity,
equity_fx: equity_fx,
margin: margin,
margin_free: margin_free,
margin_level: margin_level,
stock_lock: stock_lock,
stock_value: stock_value
}
end

def new(%{
"balance" => balance,
"cashStockValue" => cash_stock_value,
"credit" => credit,
"equity" => equity,
"equityFX" => equity_fx,
"margin" => margin,
"marginFree" => margin_free,
"marginLevel" => margin_level,
"stockLock" => stock_lock,
"stockValue" => stock_value
})
when is_number(balance) and is_number(cash_stock_value) and is_number(credit) and
is_number(equity) and is_number(equity_fx) and
is_number(margin) and is_number(margin_free) and is_number(margin_level) and
Expand All @@ -113,7 +81,7 @@ defmodule XtbClient.Messages.BalanceInfo do
balance: balance,
cash_stock_value: cash_stock_value,
credit: credit,
currency: "",
currency: currency || "",
equity: equity,
equity_fx: equity_fx,
margin: margin,
Expand All @@ -123,12 +91,4 @@ defmodule XtbClient.Messages.BalanceInfo do
stock_value: stock_value
}
end

def match(method, data) when method in ["getBalance", "getMarginLevel"] do
{:ok, __MODULE__.new(data)}
end

def match(_method, _data) do
{:no_match}
end
end
20 changes: 8 additions & 12 deletions lib/xtb_client/messages/calendar_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ defmodule XtbClient.Messages.CalendarInfo do
}

@enforce_keys [:country, :current, :forecast, :impact, :period, :previous, :time, :title]

@derive Jason.Encoder
defstruct country: "",
current: "",
Expand All @@ -45,19 +44,16 @@ defmodule XtbClient.Messages.CalendarInfo do
"previous" => previous,
"time" => time_value,
"title" => title
})
when is_binary(country) and is_binary(current) and is_binary(forecast) and
is_binary(impact) and is_binary(period) and is_binary(previous) and
is_number(time_value) and is_binary(title) do
}) do
%__MODULE__{
country: country,
current: current,
forecast: forecast,
impact: impact,
period: period,
previous: previous,
country: country || "",
current: current || "",
forecast: forecast || "",
impact: impact || "",
period: period || "",
previous: previous || "",
time: DateTime.from_unix!(time_value, :millisecond),
title: title
title: title || ""
}
end
end
17 changes: 5 additions & 12 deletions lib/xtb_client/messages/calendar_infos.ex
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
defmodule XtbClient.Messages.CalendarInfos do
alias XtbClient.Messages.{CalendarInfo}

@moduledoc """
Query result for list of `XtbClient.Messages.CalendarInfo`s.
## Parameters
- `data` array or results.
## Handled Api methods
- `getCalendar`
"""

alias XtbClient.Messages.CalendarInfo

@type t :: %__MODULE__{
data: [CalendarInfo.t()]
}

@enforce_keys [:data]
@derive Jason.Encoder
defstruct data: []

def new(data) when is_list(data) do
Expand All @@ -25,12 +26,4 @@ defmodule XtbClient.Messages.CalendarInfos do
|> Enum.map(&CalendarInfo.new(&1))
}
end

def match(method, data) when method in ["getCalendar"] do
{:ok, __MODULE__.new(data)}
end

def match(_method, _data) do
{:no_match}
end
end
93 changes: 54 additions & 39 deletions lib/xtb_client/messages/candle.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
defmodule XtbClient.Messages.Candle do
alias XtbClient.Messages.QuoteId

@moduledoc """
Info representing aggregated price & volume values for candle.
Expand All @@ -18,6 +16,8 @@ defmodule XtbClient.Messages.Candle do
- `symbol` symbol name.
"""

alias XtbClient.Messages.QuoteId

@type t :: %__MODULE__{
open: float(),
high: float(),
Expand All @@ -41,7 +41,6 @@ defmodule XtbClient.Messages.Candle do
:quote_id,
:symbol
]

@derive Jason.Encoder
defstruct open: 0.0,
high: 0.0,
Expand All @@ -55,58 +54,74 @@ defmodule XtbClient.Messages.Candle do

def new(
%{
"open" => open,
"high" => high,
"low" => low,
"close" => close,
"vol" => vol,
"ctm" => ctm_value,
"ctmString" => ctm_string
},
digits
"ctmString" => ctm_string,
"symbol" => symbol
} = args
)
when is_number(open) and is_number(high) and is_number(low) and is_number(close) and
is_number(vol) and is_number(ctm_value) and is_binary(ctm_string) and
is_number(digits) do
%__MODULE__{
open: to_base_currency(open, digits),
high: to_base_currency(open + high, digits),
low: to_base_currency(open + low, digits),
close: to_base_currency(open + close, digits),
vol: vol,
ctm: DateTime.from_unix!(ctm_value, :millisecond),
ctm_string: ctm_string,
quote_id: nil,
symbol: ""
when is_number(vol) and is_number(ctm_value) do
value = args |> Map.delete(["vol", "ctm", "ctmString", "symbol"]) |> new()

%{
value
| vol: vol,
ctm: DateTime.from_unix!(ctm_value, :millisecond),
ctm_string: ctm_string || "",
symbol: symbol || ""
}
end

def new(%{"quoteId" => quote_id} = args) when is_integer(quote_id) do
value = args |> Map.delete(["quoteId"]) |> new()

%{
value
| quote_id: QuoteId.parse(quote_id)
}
end

def new(%{
"open" => open,
"high" => high,
"low" => low,
"close" => close,
"vol" => vol,
"ctm" => ctm_value,
"ctmString" => ctm_string,
"quoteId" => quote_id,
"symbol" => symbol
"close" => close
})
when is_number(open) and is_number(high) and is_number(low) and is_number(close) and
is_number(vol) and
is_number(ctm_value) and is_binary(ctm_string) and
is_integer(quote_id) and
is_binary(symbol) do
when is_number(open) and is_number(high) and is_number(low) and is_number(close) do
%__MODULE__{
open: open,
high: high,
low: low,
close: close,
vol: vol,
ctm: DateTime.from_unix!(ctm_value, :millisecond),
ctm_string: ctm_string,
quote_id: QuoteId.parse(quote_id),
symbol: symbol
vol: 0.0,
ctm: nil,
ctm_string: nil,
quote_id: nil,
symbol: nil
}
end

def new(
%{
"open" => open,
"high" => high,
"low" => low,
"close" => close
},
digits
)
when is_number(open) and is_number(high) and is_number(low) and is_number(close) and
is_number(digits) do
%__MODULE__{
open: to_base_currency(open, digits),
high: to_base_currency(open + high, digits),
low: to_base_currency(open + low, digits),
close: to_base_currency(open + close, digits),
vol: 0.0,
ctm: nil,
ctm_string: nil,
quote_id: nil,
symbol: nil
}
end

Expand Down
20 changes: 0 additions & 20 deletions lib/xtb_client/messages/candles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,11 @@ defmodule XtbClient.Messages.Candles do
}

@enforce_keys [:symbol]

@derive Jason.Encoder
defstruct symbol: ""

def new(symbol) when is_binary(symbol) do
%__MODULE__{symbol: symbol}
end
end

alias XtbClient.Messages.{Candle}

@moduledoc """
Query result for `XtbClient.Messages.Candle`s.
Returns one `XtbClient.Messages.Candle` at a time.
## Handled Api methods
- `getCandles`
"""

def match(method, data) when method in ["getCandles"] do
{:ok, Candle.new(data)}
end

def match(_method, _data) do
{:no_match}
end
end
Loading

0 comments on commit 087faf3

Please sign in to comment.