-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
648c05d
commit f73f028
Showing
10 changed files
with
283 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule ExOanda.DailyFinancing do | ||
@moduledoc """ | ||
Schema for Oanda daily financing. | ||
""" | ||
|
||
use TypedEctoSchema | ||
import Ecto.Changeset | ||
alias ExOanda.Type.Atom | ||
|
||
@primary_key false | ||
|
||
typed_embedded_schema do | ||
field(:id, :string) | ||
field(:time, :utc_datetime_usec) | ||
field(:user_id, :integer) | ||
field(:account_id, :string) | ||
field(:batch_id, :string) | ||
field(:request_id, :string) | ||
field(:type, Atom, default: :DAILY_FINANCING) | ||
field(:financing, :integer) | ||
field(:account_balance, :float) | ||
field(:position_financings, {:array, :map}) | ||
end | ||
|
||
@doc false | ||
def changeset(struct, params) do | ||
struct | ||
|> cast(params, [ | ||
:id, | ||
:time, | ||
:user_id, | ||
:account_id, | ||
:batch_id, | ||
:request_id, | ||
:type, | ||
:financing, | ||
:account_balance, | ||
:position_financings | ||
]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule ExOanda.Response.PricingHeartbeat do | ||
@moduledoc """ | ||
Schema for Oanda pricing heartbeat response. | ||
""" | ||
|
||
use TypedEctoSchema | ||
import Ecto.Changeset | ||
alias ExOanda.Type.Atom | ||
|
||
@primary_key false | ||
|
||
typed_embedded_schema do | ||
field(:time, :utc_datetime_usec) | ||
field(:type, Atom, default: :HEARTBEAT) | ||
end | ||
|
||
@doc false | ||
def changeset(struct, params) do | ||
struct | ||
|> cast(params, [:time, :type]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
defmodule ExOanda.Response.Pricing do | ||
@moduledoc """ | ||
Schema for Oanda streaming pricing response. | ||
""" | ||
|
||
use TypedEctoSchema | ||
import Ecto.Changeset | ||
|
||
@primary_key false | ||
|
||
typed_embedded_schema do | ||
field(:instrument, :string) | ||
field(:status, Ecto.Enum, values: [:tradeable, :non_tradeable, :invalid]) | ||
field(:time, :utc_datetime_usec) | ||
field(:closeout_ask, :float) | ||
field(:closeout_bid, :float) | ||
|
||
embeds_many :asks, Ask, primary_key: false do | ||
field(:liquidity, :integer) | ||
field(:price, :float) | ||
end | ||
|
||
embeds_many :bids, Bid, primary_key: false do | ||
field(:liquidity, :integer) | ||
field(:price, :float) | ||
end | ||
end | ||
|
||
@doc false | ||
def changeset(struct, params) do | ||
struct | ||
|> cast(params, [ | ||
:instrument, | ||
:status, | ||
:time, | ||
:closeout_ask, | ||
:closeout_bid, | ||
]) | ||
|> cast_embed(:asks, with: &price_changeset/2) | ||
|> cast_embed(:bids, with: &price_changeset/2) | ||
end | ||
|
||
defp price_changeset(struct, params) do | ||
struct | ||
|> cast(params, [:liquidity, :price]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule ExOanda.Response.TransactionEvent do | ||
@moduledoc """ | ||
Schema for Oanda streaming transaction response. | ||
""" | ||
|
||
use Ecto.Schema | ||
import Ecto.Changeset | ||
import PolymorphicEmbed | ||
alias ExOanda.{ | ||
OrderFillTransaction, | ||
TakeProfitOrderTransaction, | ||
StopLossOrderTransaction, | ||
TrailingStopLossOrderTransaction, | ||
MarketOrderRejectTransaction, | ||
MarketOrderTransaction, | ||
OrderCancelTransaction, | ||
TradeClientExtensionsModifyTransaction, | ||
DailyFinancingTransaction, | ||
Response.TransactionHeartbeat | ||
} | ||
|
||
@primary_key false | ||
|
||
embedded_schema do | ||
polymorphic_embeds_one :event, | ||
# Note: this list is incomplete based the Oanda docs | ||
types: [ | ||
ORDERFILL: OrderFillTransaction, | ||
TAKE_PROFIT_ORDER: TakeProfitOrderTransaction, | ||
STOP_LOSS_ORDER: StopLossOrderTransaction, | ||
TRAILING_STOP_LOSS_ORDER: TrailingStopLossOrderTransaction, | ||
MARKET_ORDER_REJECT: MarketOrderRejectTransaction, | ||
MARKET_ORDER: MarketOrderTransaction, | ||
ORDER_CANCEL: OrderCancelTransaction, | ||
TRADE_CLIENT_EXTENSIONS_MODIFY: TradeClientExtensionsModifyTransaction, | ||
DAILY_FINANCING: DailyFinancingTransaction, | ||
HEARTBEAT: TransactionHeartbeat | ||
], | ||
type_field_name: :type, | ||
on_type_not_found: :raise, | ||
on_replace: :update | ||
end | ||
|
||
@doc false | ||
def changeset(struct, params) do | ||
struct | ||
|> cast(params, []) | ||
|> cast_polymorphic_embed(:event) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule ExOanda.Response.TransactionHeartbeat do | ||
@moduledoc """ | ||
Schema for Oanda transaction heartbeat response. | ||
""" | ||
|
||
use TypedEctoSchema | ||
import Ecto.Changeset | ||
alias ExOanda.Type.Atom | ||
|
||
@primary_key false | ||
|
||
typed_embedded_schema do | ||
field(:last_transaction_id, :string) | ||
field(:time, :utc_datetime_usec) | ||
field(:type, Atom, default: :HEARTBEAT) | ||
end | ||
|
||
@doc false | ||
def changeset(struct, params) do | ||
struct | ||
|> cast(params, [:last_transaction_id, :time, :type]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
defmodule ExOanda.Streaming do | ||
@moduledoc """ | ||
Interface for Oanda streaming endpoints. | ||
""" | ||
|
||
alias ExOanda.API | ||
alias ExOanda.Connection, as: Conn | ||
alias ExOanda.Transform, as: TF | ||
|
||
@price_stream_params NimbleOptions.new!( | ||
instruments: [ | ||
type: {:list, :string}, | ||
required: true | ||
] | ||
) | ||
|
||
@doc """ | ||
Stream transactions for an account. | ||
## Examples | ||
iex> ExOanda.Streaming.transaction_stream(conn, "101-004-22222222-001", &IO.inspect/1) | ||
:ok | ||
""" | ||
def transaction_stream(%Conn{} = conn, account_id, stream_to, params \\ []) do | ||
stream(conn, account_id, :transactions, stream_to, params) | ||
end | ||
|
||
@doc """ | ||
Stream prices for an instrument(s). | ||
## Examples | ||
iex> ExOanda.Streaming.price_stream(conn, "101-004-22222222-001", &IO.inspect/1, instruments: ["EUR_USD"]) | ||
:ok | ||
## Supported parameters | ||
#{NimbleOptions.docs(@price_stream_params)} | ||
""" | ||
def price_stream(%Conn{} = conn, account_id, stream_to, params \\ []) do | ||
case NimbleOptions.validate(params, @price_stream_params) do | ||
{:ok, params} -> | ||
stream(conn, account_id, :pricing, stream_to, format_instruments(params)) | ||
{:error, errors} -> | ||
{:error, errors} | ||
end | ||
end | ||
|
||
defp stream(%Conn{} = conn, account_id, stream_type, stream_to, params) do | ||
Req.new( | ||
auth: API.auth_bearer(conn), | ||
url: "#{conn.stream_server}/accounts/#{account_id}/#{stream_type}/stream", | ||
method: :get, | ||
headers: API.base_headers(), | ||
params: params, | ||
into: fn {:data, data}, {req, resp} -> | ||
data | ||
|> String.split("\n", trim: true) | ||
|> Enum.each(fn line -> | ||
line | ||
|> TF.transform_stream(stream_type) | ||
|> stream_to.() | ||
end) | ||
|
||
{:cont, {req, resp}} | ||
end | ||
) | ||
|> Req.request(conn.options) | ||
end | ||
|
||
defp format_instruments(params) do | ||
instruments = | ||
params | ||
|> Keyword.fetch!(:instruments) | ||
|> Enum.join(",") | ||
|
||
%{instruments: instruments} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters