Skip to content

Commit

Permalink
Add missing transactions (#47)
Browse files Browse the repository at this point in the history
* Add missing transactions

* Fix types

* Missed one
  • Loading branch information
nicholasbair authored Jan 27, 2025
1 parent a21e596 commit c742a3e
Show file tree
Hide file tree
Showing 18 changed files with 880 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ Trading foreign exchange (forex) on margin carries a high level of risk and may
This SDK is provided "as-is," without any warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability, fitness for a particular purpose, or non-infringement. The use of this SDK is at your own risk, and we make no guarantees regarding its accuracy, reliability, or suitability for any specific trading strategy or purpose. Users are responsible for their own trading decisions and should seek independent financial advice if necessary.

## TODO / Known Issues
1. Limited test coverage, validaiton of schemas
1. Limited test coverage, validation of schemas
2. Not yet available on hex

## Installation
```elixir
def deps do
[
{:ex_oanda, git: "https://github.com/nicholasbair/ex_oanda.git", tag: "v0.0.9"}
{:ex_oanda, git: "https://github.com/nicholasbair/ex_oanda.git", tag: "v0.0.10"}
]
end
```
Expand Down
70 changes: 64 additions & 6 deletions lib/models/definitions/account/account_changes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,43 @@ defmodule ExOanda.AccountChanges do
import PolymorphicEmbed

alias ExOanda.{
Order,
Position,
TradeSummary,
ClientConfigureRejectTransaction,
ClientConfigureTransaction,
DailyFinancing,
MarketOrderTransaction,
DelayedTradeClosureTransaction,
DividendAdjustmentTransaction,
FixedPriceOrderTransaction,
GuaranteedStopLossOrderRejectTransaction,
GuaranteedStopLossOrderTransaction,
LimitOrderRejectTransaction,
LimitOrderTransaction,
MarginCallEnterTransaction,
MarginCallExtendTransaction,
MarginCallExitTransaction,
MarketIfTouchedOrderRejectTransaction,
MarketIfTouchedOrderTransaction,
MarketOrderRejectTransaction,
MarketOrderTransaction,
Order,
OrderCancelRejectTransaction,
OrderCancelTransaction,
OrderClientExtensionsModifyRejectTransaction,
OrderClientExtensionsModifyTransaction,
OrderFillTransaction,
TradeClientExtensionsModifyTransaction
OrderRejectTransaction,
Position,
ResetResettablePLTransaction,
StopLossOrderRejectTransaction,
StopLossOrderTransaction,
TakeProfitOrderRejectTransaction,
TakeProfitOrderTransaction,
TradeClientExtensionsModifyRejectTransaction,
TradeClientExtensionsModifyTransaction,
TradeSummary,
TrailingStopLossOrderRejectTransaction,
TrailingStopLossOrderTransaction,
TransferFundsRejectTransaction,
TransferFundsTransaction
}

@primary_key false
Expand All @@ -31,13 +60,42 @@ defmodule ExOanda.AccountChanges do
embeds_many :positions, Position
polymorphic_embeds_many :transactions,
types: [
CLIENT_CONFIGURE: ClientConfigureTransaction,
CLIENT_CONFIGURE_REJECT: ClientConfigureRejectTransaction,
DAILY_FINANCING: DailyFinancing,
DELAYED_TRADE_CLOSURE: DelayedTradeClosureTransaction,
DIVIDEND_ADJUSTMENT: DividendAdjustmentTransaction,
FIXED_PRICE_ORDER: FixedPriceOrderTransaction,
GUARANTEED_STOP_LOSS_ORDER: GuaranteedStopLossOrderTransaction,
GUARANTEED_STOP_LOSS_ORDER_REJECT: GuaranteedStopLossOrderRejectTransaction,
LIMIT_ORDER: LimitOrderTransaction,
LIMIT_ORDER_REJECT: LimitOrderRejectTransaction,
MARGIN_CALL_ENTER: MarginCallEnterTransaction,
MARGIN_CALL_EXTEND: MarginCallExtendTransaction,
MARGIN_CALL_EXIT: MarginCallExitTransaction,
MARKET_IF_TOUCHED_ORDER: MarketIfTouchedOrderTransaction,
MARKET_IF_TOUCHED_ORDER_REJECT: MarketIfTouchedOrderRejectTransaction,
MARKET_ORDER: MarketOrderTransaction,
MARKET_ORDER_REJECT: MarketOrderRejectTransaction,
ORDER_CANCEL: OrderCancelTransaction,
ORDER_CANCEL_REJECT: OrderCancelRejectTransaction,
ORDER_CLIENT_EXTENSIONS_MODIFY: OrderClientExtensionsModifyTransaction,
ORDER_CLIENT_EXTENSIONS_MODIFY_REJECT: OrderClientExtensionsModifyRejectTransaction,
ORDER_FILL: OrderFillTransaction,
ORDER_REJECT: OrderRejectTransaction,
RESET_RESETTABLE_PL: ResetResettablePLTransaction,
STOP_LOSS_ORDER: StopLossOrderTransaction,
STOP_LOSS_ORDER_REJECT: StopLossOrderRejectTransaction,
TAKE_PROFIT_ORDER: TakeProfitOrderTransaction,
TAKE_PROFIT_ORDER_REJECT: TakeProfitOrderRejectTransaction,
TRADE_CLIENT_EXTENSIONS_MODIFY: TradeClientExtensionsModifyTransaction,
TRADE_CLIENT_EXTENSIONS_MODIFY_REJECT: TradeClientExtensionsModifyRejectTransaction,
TRAILING_STOP_LOSS_ORDER: TrailingStopLossOrderTransaction,
TRAILING_STOP_LOSS_ORDER_REJECT: TrailingStopLossOrderRejectTransaction,
TRANSFER_FUNDS: TransferFundsTransaction,
TRANSFER_FUNDS_REJECT: TransferFundsRejectTransaction
],
on_type_not_found: :changeset_error,
on_type_not_found: :raise,
on_replace: :delete,
type_field_name: :type
end
Expand Down
2 changes: 1 addition & 1 deletion lib/models/definitions/transaction/daily_financing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule ExOanda.DailyFinancing do
field(:batch_id, :string)
field(:request_id, :string)
field(:type, Atom, default: :DAILY_FINANCING)
field(:financing, :integer)
field(:financing, :float)
field(:account_balance, :float)
field(:position_financings, {:array, :map})
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule Oanda.Transaction.DelayedTradeClosureTransaction do
@moduledoc """
A DelayedTradeClosureTransaction represents the immediate closure of a Trade that was
requested to be delayed.
"""

use TypedEctoSchema
import Ecto.Changeset
alias ExOanda.Type.Atom

@primary_key false

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: :DELAYED_TRADE_CLOSURE)
field(:reason, :string)
field(:trade_ids, {:array, :string})
end

def changeset(struct, data) do
struct
|> cast(data, [
:id,
:time,
:user_id,
:account_id,
:batch_id,
:request_id,
:type,
:reason,
:trade_ids
])
|> validate_required([
:id,
:time,
:user_id,
:account_id,
:batch_id,
:type
])
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule Oanda.Transaction.DividendAdjustmentTransaction do
@moduledoc """
A DividendAdjustmentTransaction represents a dividend adjustment made to an Account.
"""

use TypedEctoSchema
import Ecto.Changeset
alias ExOanda.Type.Atom

@primary_key false

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: :DIVIDEND_ADJUSTMENT)
field(:instrument, Atom)
field(:dividend_rate, :float)
field(:quote_units, :float)
field(:home_conversion_factors, :map)
field(:account_balance, :float)
end

def changeset(struct, data) do
struct
|> cast(data, [
:id,
:time,
:user_id,
:account_id,
:batch_id,
:request_id,
:type,
:instrument,
:dividend_rate,
:quote_units,
:home_conversion_factors,
:account_balance
])
|> validate_required([
:id,
:time,
:user_id,
:account_id,
:batch_id,
:type,
:instrument
])
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
defmodule Oanda.Transaction.FixedPriceOrderTransaction do
@moduledoc """
A FixedPriceOrderTransaction represents the creation of a Fixed Price Order in the user's Account.
"""

use TypedEctoSchema
import Ecto.Changeset
alias ExOanda.Type.Atom

@primary_key false

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: :FIXED_PRICE_ORDER)
field(:instrument, Atom)
field(:units, :float)
field(:price, :float)
field(:position_fill, :string)
field(:trade_state, :string)
field(:reason, :string)
end

def changeset(struct, data) do
struct
|> cast(data, [
:id,
:time,
:user_id,
:account_id,
:batch_id,
:request_id,
:type,
:instrument,
:units,
:price,
:position_fill,
:trade_state,
:reason
])
|> validate_required([
:id,
:time,
:user_id,
:account_id,
:batch_id,
:type,
:instrument,
:units,
:price
])
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
defmodule Oanda.Transaction.LimitOrderRejectTransaction do
@moduledoc """
A LimitOrderRejectTransaction represents the rejection of the creation of a Limit Order.
"""

use TypedEctoSchema
import Ecto.Changeset
alias ExOanda.Type.Atom

alias Oanda.Transaction.{
ClientExtensions,
TakeProfitDetails,
StopLossDetails,
TrailingStopLossDetails,
GuaranteedStopLossDetails
}

@primary_key false

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: :LIMIT_ORDER_REJECT)
field(:instrument, Atom)
field(:units, :float)
field(:price, :float)
field(:time_in_force, Ecto.Enum, values: ~w(GTC GTD GFD FOK IOC)a)
field(:gtd_time, :utc_datetime_usec)
field(:position_fill, :string, default: "DEFAULT")
field(:trigger_condition, Ecto.Enum, values: ~w(DEFAULT INVERSE BID ASK MID)a)
field(:reason, :string)
field(:reject_reason, :string)

embeds_one :client_extensions, ClientExtensions
embeds_one :take_profit_on_fill, TakeProfitDetails
embeds_one :stop_loss_on_fill, StopLossDetails
embeds_one :trailing_stop_loss_on_fill, TrailingStopLossDetails
embeds_one :guaranteed_stop_loss_on_fill, GuaranteedStopLossDetails
end

def changeset(struct, data) do
struct
|> cast(data, [
:id,
:time,
:user_id,
:account_id,
:batch_id,
:request_id,
:type,
:instrument,
:units,
:price,
:time_in_force,
:gtd_time,
:position_fill,
:trigger_condition,
:reason,
:reject_reason
])
|> cast_embed(:client_extensions)
|> cast_embed(:take_profit_on_fill)
|> cast_embed(:stop_loss_on_fill)
|> cast_embed(:trailing_stop_loss_on_fill)
|> cast_embed(:guaranteed_stop_loss_on_fill)
|> validate_required([
:id,
:time,
:user_id,
:account_id,
:batch_id,
:type,
:reject_reason
])
end
end
Loading

0 comments on commit c742a3e

Please sign in to comment.