Skip to content

Instantly share code, notes, and snippets.

@Jay-Jay-D
Created January 19, 2018 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jay-Jay-D/0ff2dbb739ed1fd2286eb298262cca43 to your computer and use it in GitHub Desktop.
Save Jay-Jay-D/0ff2dbb739ed1fd2286eb298262cca43 to your computer and use it in GitHub Desktop.
/*
* 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 NUnit.Framework;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;
using QuantConnect.Orders;
using QuantConnect.Securities;
using QuantConnect.Securities.Forex;
using System;
namespace QuantConnect.Tests.Common.Securities.Forex
{
[TestFixture]
public class ForexHoldingTests
{
[TestCase("EURUSD", "USD", 1, 0.00001, 1000, 1.12345, 5)]
public void TotalProfitIsCorrectlyEstimated(string ticker, string cashSymbol, decimal conversionRate,
decimal minimumPriceVariation, int lotSize, decimal entryPrice, decimal pips)
{
// Arrange
var symbol = Symbol.Create(ticker, SecurityType.Forex, Market.FXCM);
// TODO: add base currency.
var pairQuoteCurrency = symbol.Value.Substring(3);
var pairBaseCurrency = symbol.Value.Substring(0, 3);
var subscription = new SubscriptionDataConfig(typeof(TradeBar), symbol, Resolution.Daily,
TimeZones.NewYork, TimeZones.NewYork, true, true, true);
var timeKeeper = new TimeKeeper(DateTime.Now, new[] { TimeZones.NewYork });
var securities = new SecurityManager(timeKeeper);
var transactions = new SecurityTransactionManager(securities);
var portfolio = new SecurityPortfolioManager(securities, transactions);
portfolio.SetCash(1000000);
portfolio.CashBook.Add(pairQuoteCurrency, 0, conversionRate);
var pair = new QuantConnect.Securities.Forex.Forex(SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork),
portfolio.CashBook[cashSymbol], subscription,
new SymbolProperties("", pairQuoteCurrency, 1, minimumPriceVariation, lotSize));
pair.SetMarketPrice(new IndicatorDataPoint(pair.Symbol, DateTime.Now, entryPrice));
securities.Add(pair);
// Act
var fill = new OrderEvent(1, symbol, DateTime.Now, OrderStatus.Filled, OrderDirection.Buy,
entryPrice, 10000, decimal.Zero);
portfolio.ProcessFill(fill);
var priceVariation = pips * 10 / minimumPriceVariation;
pair.SetMarketPrice(new IndicatorDataPoint(pair.Symbol, DateTime.Now, entryPrice + priceVariation));
var forexHolding = (ForexHolding)portfolio[symbol];
var actualPips = forexHolding.TotalCloseProfitPips();
// Assert
Assert.AreEqual(pips, actualPips);
Assert.Fail("WIP");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment