Skip to content

Commit

Permalink
Use order instance converted from json to access order properties (#15)
Browse files Browse the repository at this point in the history
This helps avoiding manually getting and using properties that might not be defined for some order types.
  • Loading branch information
jhonabreul authored Jan 18, 2024
1 parent 14ea8aa commit 15ba338
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 16 deletions.
60 changes: 60 additions & 0 deletions QuantConnect.OandaBrokerage.Tests/OandaBrokerageAdditionalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using NUnit.Framework;
using QuantConnect.Brokerages.Oanda;
using QuantConnect.Configuration;
using QuantConnect.Lean.Engine.DataFeeds;
using Environment = QuantConnect.Brokerages.Oanda.Environment;

namespace QuantConnect.Tests.Brokerages.Oanda
{
[TestFixture]
public class OandaBrokerageAdditionalTests
{
[Test]
[Explicit("This test will only pass if the Practice account at least one open order all of them are of a supported type")]
public void GetsOpenOrders()
{
var brokerage = CreateBrokerage();
var openOrders = brokerage.GetOpenOrders();
Assert.That(openOrders, Is.Not.Null.And.Not.Empty);
}

[Test]
[Explicit("This test will only pass if the Practice account has an open order of an unsupported type, like STOP_LOSS")]
public void ThrowsOnUnsupportedOpenOrders()
{
var brokerage = CreateBrokerage();
Assert.Throws<NotSupportedException>(() => brokerage.GetOpenOrders());
}

private OandaBrokerage CreateBrokerage()
{
var environment = Config.Get("oanda-environment").ConvertTo<Environment>();
var accessToken = Config.Get("oanda-access-token");
var accountId = Config.Get("oanda-account-id");
var aggregator = new AggregationManager();

var brokerage =new OandaBrokerage(new OrderProvider(), new SecurityProvider(), aggregator, environment, accessToken, accountId);

brokerage.Connect();
Assert.IsTrue(brokerage.IsConnected);

return brokerage;
}
}
}
36 changes: 20 additions & 16 deletions QuantConnect.OandaBrokerage/OandaRestApiV20.cs
Original file line number Diff line number Diff line change
Expand Up @@ -603,36 +603,40 @@ private string Authorization
/// </summary>
private Order ConvertOrder(JToken order)
{
var type = order["type"].ToString();
Symbol getSymbol(string instrument)
{
var securityType = SymbolMapper.GetBrokerageSecurityType(instrument);
return SymbolMapper.GetLeanSymbol(instrument, securityType, Market.Oanda);
}

var type = order["type"].ToString();
Order qcOrder;

var instrument = order["instrument"].ToString();
var id = order["id"].ToString();
var units = order["units"].ConvertInvariant<decimal>();
var createTime = order["createTime"].ToString();
var securityType = SymbolMapper.GetBrokerageSecurityType(instrument);
var symbol = SymbolMapper.GetLeanSymbol(instrument, securityType, Market.Oanda);
var time = GetTickDateTimeFromString(createTime);
var quantity = units;

switch (type)
{
case "MARKET_IF_TOUCHED":
var stopOrder = order.ToObject<MarketIfTouchedOrder>();
qcOrder = new StopMarketOrder(symbol, quantity, stopOrder.Price.ToDecimal(), time);
qcOrder = new StopMarketOrder(getSymbol(stopOrder.Instrument),
stopOrder.Units.ConvertInvariant<decimal>(),
stopOrder.Price.ConvertInvariant<decimal>(),
GetTickDateTimeFromString(stopOrder.CreateTime));
break;

case "LIMIT":
var limitOrder = order.ToObject<OandaLimitOrder>();
qcOrder = new LimitOrder(symbol, quantity, limitOrder.Price.ToDecimal(), time);
qcOrder = new LimitOrder(getSymbol(limitOrder.Instrument),
limitOrder.Units.ConvertInvariant<decimal>(),
limitOrder.Price.ConvertInvariant<decimal>(),
GetTickDateTimeFromString(limitOrder.CreateTime));
break;

case "STOP":
var stopLimitOrder = order.ToObject<StopOrder>();
var price = stopLimitOrder.Price.ConvertInvariant<decimal>();
var limitPrice = stopLimitOrder.PriceBound.ConvertInvariant<decimal>();
qcOrder = new StopLimitOrder(symbol, quantity, price, limitPrice, time);
qcOrder = new StopLimitOrder(getSymbol(stopLimitOrder.Instrument),
stopLimitOrder.Units.ConvertInvariant<decimal>(),
stopLimitOrder.Price.ConvertInvariant<decimal>(),
stopLimitOrder.PriceBound.ConvertInvariant<decimal>(),
GetTickDateTimeFromString(stopLimitOrder.CreateTime));
break;

case "MARKET":
Expand All @@ -645,7 +649,7 @@ private Order ConvertOrder(JToken order)
}

qcOrder.Status = OrderStatus.None;
qcOrder.BrokerId.Add(id);
qcOrder.BrokerId.Add(order["id"].ToString());

var gtdTime = order["gtdTime"];
if (gtdTime != null)
Expand Down

0 comments on commit 15ba338

Please sign in to comment.