/// <summary> /// Create a new order /// </summary> /// <param name="instrument">Required Instrument to open the order on</param> /// <param name="units">Required The number of units to open order for</param> /// <param name="side">Required Direction of the order, either "buy" or "sell"</param> /// <param name="type">Required The type of the order "limit", "stop", "marketIfTouched’ or "market"</param> /// <param name="expiry">Required If order type is "limit", "stop", or "marketIfTouched". The order expiration time in UTC. The value specified must be in a valid datetime format</param> /// <param name="price">Required If order type is "limit", "stop", or "marketIfTouched". The price where the order is set to trigger at</param> /// <param name="lowerBound">Optional The minimum execution price</param> /// <param name="upperBound">Optional The maximum execution price</param> /// <param name="stopLoss">The stop loss price</param> /// <param name="takeProfit">Optional The take profit price</param> /// <param name="trailingStop">Optional The trailing stop distance in pips, up to one decimal place</param> /// <returns></returns> public async Task <OrderOpen> CreateOrder(string instrument, int units, OandaTypes.Side side, OandaTypes.OrderType type, DateTime?expiry, float?price, float?lowerBound, float?upperBound, float?stopLoss, float?takeProfit, float?trailingStop) { Dictionary <string, string> routeParams = new Dictionary <string, string>(); routeParams.Add("accountId", _accountId.ToString()); Dictionary <string, string> properties = new Dictionary <string, string>(); properties.Add("instrument", instrument); properties.Add("units", units.ToString()); properties.Add("side", side.ToString()); properties.Add("type", type.ToString()); if (expiry != null) { properties.Add("expiry", expiry.Value.ToString("o")); } if (price != null) { properties.Add("price", price.ToString()); } if (lowerBound != null) { properties.Add("lowerBound", lowerBound.ToString()); } if (upperBound != null) { properties.Add("upperBound", upperBound.ToString()); } if (stopLoss != null) { properties.Add("stopLoss", stopLoss.ToString()); } if (takeProfit != null) { properties.Add("takeProfit", takeProfit.ToString()); } if (trailingStop != null) { properties.Add("trailingStop", trailingStop.ToString()); } if (type == OandaTypes.OrderType.market) { OrderMarketOpen orderMarket = await Post <OrderMarketOpen>(routeParams, properties, _ordersRoute); return(orderMarket); } else { OrderCustumOpen customOrder = await Post <OrderCustumOpen>(routeParams, properties, _ordersRoute); return(customOrder); } }
/// <summary> /// Get a list of open trades (especially usefull for closing trades in FIFO style) /// </summary> /// <param name="instrument">Retrieve open trades for a specific instrument only</param> /// <param name="side">Retrieve open trades for a specific side only</param> /// <param name="units">Retrieve open trades with a specific number of units</param> /// <returns></returns> public async Task <List <Trade> > GetTrades(string instrument, OandaTypes.Side side, int units) { Dictionary <string, string> routeParams = new Dictionary <string, string>(); routeParams.Add("accountId", _accountId.ToString()); Dictionary <string, object> properties = new Dictionary <string, object>(); properties.Add("instrument", instrument); TradesWrapper tradesWrapper = await Get <TradesWrapper>(routeParams, properties, _tradesRoute); return(tradesWrapper.Trades.Where(x => x.Side == side && x.Units == units).OrderBy(x => x.Id).ToList()); }
/// <summary> /// Close an open trade by using th FIFO algorithem... /// </summary> /// <param name="instrument">Retrieve open trades for a specific instrument only</param> /// <param name="side">Retrieve open trades for a specific side only</param> /// <param name="units">Retrieve open trades with a specific number of units</param> /// <returns></returns> public async Task <TradeClosed> CloseTrade(string instrument, OandaTypes.Side side, int units) { TradeClosed tradeClosed; List <Trade> trades = await GetTrades(instrument, side, units); if (trades.Any()) { // FIFO algorithem is implemented because GetTrades() is ordered by TradeId ascending... tradeClosed = await CloseTrade(trades.First().Id); } else { throw new Exception(string.Format("No trade closed because no trades were found for Instrument: {0}, Side: {1} and Units: {2}.", instrument, side, units)); } return(tradeClosed); }
public async Task <int> GetAvailableUnitsToTrade(string instrument, OandaTypes.Side side, float maxMarginRatioToUse = 1) { AccountDetails accountDetails = await new AccountEndpoints(_key, _accountType).GetAccountDetails(_accountId); OandaTypes.TradeCurrency baseCurrency = (OandaTypes.TradeCurrency)Enum.Parse(typeof(OandaTypes.TradeCurrency), new string(instrument.ToCharArray().TakeWhile(x => x != '_').ToArray()), true); OandaTypes.AccountCurrency accountCurrency = accountDetails.AccountCurrency; float marginInstrumentPrice = 1; if (baseCurrency.ToString() != accountCurrency.ToString()) { string marginInstrument = baseCurrency.ToString() + "_" + accountCurrency.ToString(); if (!Enum.GetNames(typeof(OandaTypes.TradeCurrencyPair)).Contains(marginInstrument)) { marginInstrument = accountCurrency.ToString() + "_" + baseCurrency.ToString(); } try { if (side == OandaTypes.Side.buy) { marginInstrumentPrice = (await GetPrices(marginInstrument)).First().Ask; } else { marginInstrumentPrice = (await GetPrices(marginInstrument)).First().Bid; } } catch { return(0); } } return(Convert.ToInt32(((accountDetails.MarginAvail * maxMarginRatioToUse) / accountDetails.MarginRate) / marginInstrumentPrice)); }
/// <summary> /// Create a new order /// </summary> /// <param name="instrument">Required Instrument to open the order on</param> /// <param name="units">Required The number of units to open order for</param> /// <param name="side">Required Direction of the order, either "buy" or "sell"</param> /// <param name="expiry">Required If order type is "limit", "stop", or "marketIfTouched". The order expiration time in UTC. The value specified must be in a valid datetime format</param> /// <param name="price">Required If order type is "limit", "stop", or "marketIfTouched". The price where the order is set to trigger at</param> /// <returns></returns> public async Task <OrderMarketIfTouchedOpen> CreateMarketIfTouchedOrder(string instrument, int units, OandaTypes.Side side, DateTime expiry, float price) { Dictionary <string, string> routeParams = new Dictionary <string, string>(); routeParams.Add("accountId", _accountId.ToString()); Dictionary <string, string> properties = new Dictionary <string, string>(); properties.Add("instrument", instrument); properties.Add("units", units.ToString()); properties.Add("side", side.ToString()); properties.Add("type", OandaTypes.OrderType.marketIfTouched.ToString()); properties.Add("expiry", expiry.ToString("o")); properties.Add("price", price.ToString()); OrderMarketIfTouchedOpen orderMarketIfTouchedOpen = await Post <OrderMarketIfTouchedOpen>(routeParams, properties, _ordersRoute); return(orderMarketIfTouchedOpen); }
/// <summary> /// Create a new market order /// </summary> /// <param name="instrument">Required Instrument to open the order on</param> /// <param name="units">Required The number of units to open order for</param> /// <param name="side">Required Direction of the order, either "buy" or "sell"</param> /// <param name="stopLoss">The stop loss price</param> /// <param name="takeProfit">Optional The take profit price</param> /// <returns></returns> public async Task <OrderMarketOpen> CreateMarketOrder(string instrument, int units, OandaTypes.Side side, float stopLoss, float takeProfit) { Dictionary <string, string> routeParams = new Dictionary <string, string>(); routeParams.Add("accountId", _accountId.ToString()); Dictionary <string, string> properties = new Dictionary <string, string>(); properties.Add("instrument", instrument); properties.Add("units", units.ToString()); properties.Add("side", side.ToString()); properties.Add("stopLoss", stopLoss.ToString()); properties.Add("takeProfit", takeProfit.ToString()); properties.Add("type", OandaTypes.OrderType.market.ToString()); OrderMarketOpen orderMarketOpen = await Post <OrderMarketOpen>(routeParams, properties, _ordersRoute); return(orderMarketOpen); }