Current section

Files

Jump to
ex_oanda lib models definitions transaction order_fill_transaction.ex
Raw

lib/models/definitions/transaction/order_fill_transaction.ex

defmodule ExOanda.OrderFillTransaction do
@moduledoc """
Schema for Oanda order fill transaction.
[Oanda Docs](https://developer.oanda.com/rest-live-v20/transaction-df/)
"""
use TypedEctoSchema
import Ecto.Changeset
alias ExOanda.{
ClientPrice,
TradeOpened,
TradeReduce,
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: :ORDER_FILL)
field(:order_id, :string)
field(:instrument, Atom)
field(:units, :integer)
field(:client_order_id, :string)
field(:full_vwap, :float)
field(:reason, Ecto.Enum, values: ~w(LIMIT_ORDER STOP_ORDER MARKET_IF_TOUCHED_ORDER TAKE_PROFIT_ORDER STOP_LOSS_ORDER GUARANTEED_STOP_LOSS_ORDER TRAILING_STOP_LOSS_ORDER MARKET_ORDER MARKET_ORDER_TRADE_CLOSE MARKET_ORDER_POSITION_CLOSEOUT MARKET_ORDER_MARGIN_CLOSEOUT MARKET_ORDER_DELAYED_TRADE_CLOSE FIXED_PRICE_ORDER FIXED_PRICE_ORDER_PLATFORM_ACCOUNT_MIGRATION FIXED_PRICE_ORDER_DIVISION_ACCOUNT_MIGRATION FIXED_PRICE_ORDER_ADMINISTRATIVE_ACTION
)a)
field(:pl, :float)
field(:quote_pl, :float)
field(:financing, :float)
field(:base_financing, :float)
field(:quote_financing, :float)
field(:commission, :float)
field(:guaranteed_execution_fee, :float)
field(:quote_guaranteed_execution_fee, :float)
field(:account_balance, :float)
field(:half_spread_cost, :float)
embeds_one :full_price, ClientPrice
embeds_one :trade_opened, TradeOpened
embeds_one :trade_reduced, TradeReduce
embeds_many :trades_closed, TradeReduce
end
@doc false
def changeset(struct, params) do
struct
|> cast(params, [
:id, :time, :user_id, :account_id, :batch_id, :request_id, :type, :order_id,
:instrument, :units, :client_order_id, :full_vwap, :reason, :pl,
:quote_pl, :financing, :base_financing, :quote_financing, :commission,
:guaranteed_execution_fee, :quote_guaranteed_execution_fee, :account_balance,
:half_spread_cost
])
|> cast_embed(:full_price)
|> cast_embed(:trade_opened)
|> cast_embed(:trade_reduced)
|> cast_embed(:trades_closed)
|> validate_required([
:id, :time, :user_id, :account_id, :batch_id, :request_id, :type, :order_id,
:instrument, :units, :full_vwap, :reason, :pl,
:quote_pl, :financing, :base_financing, :account_balance,
:half_spread_cost
])
end
end