Skip to content

Instantly share code, notes, and snippets.

@Jay-Jay-D
Created January 15, 2018 23:44
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/80b33ec27e1a115438a0a4d0541cfacd to your computer and use it in GitHub Desktop.
Save Jay-Jay-D/80b33ec27e1a115438a0a4d0541cfacd to your computer and use it in GitHub Desktop.
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;
using QuantConnect.Securities;
using QuantConnect.Securities.Forex;
using System;
using static System.Diagnostics.Debug;
namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Algorithm used to test the
/// Securities.Forex.ForexHolding.TotalCloseProfitPips method.
/// </summary>
public class ForexHoldingPipReturnsTestAlgorithm : QCAlgorithm
{
Security _eurusd;
Security _jpyusd;
public override void Initialize()
{
SetStartDate(2014, 5, 7); //Set Start Date
SetEndDate(2014, 5, 15); //Set End Date
SetCash(10000000); //Set Strategy Cash
_eurusd = AddForex("EURUSD", Resolution.Daily);
_jpyusd = AddForex("USDJPY", Resolution.Daily);
_eurusd.FeeModel = new ConstantFeeTransactionModel(0m);
_jpyusd.FeeModel = new ConstantFeeTransactionModel(0m);
}
public override void OnData(Slice data)
{
if (!Portfolio.Invested)
{
MarketOrder("EURUSD", 10000);
MarketOrder("USDJPY", 10000);
Log(string.Join(", ", data.Values));
}
else
{
// Test Case 1: Pips are correctly estimated when the bas currency is the same as the Cash currency, USD in this case.
var entryPrice = Transactions.GetOrderTicket(1).AverageFillPrice;
// Add 5 pips to the EURUSD security.
_eurusd.SetMarketPrice(new IndicatorDataPoint(_eurusd.Symbol, Time, entryPrice + 0.0005m));
var usdPip = (ForexHolding)Portfolio["EURUSD"];
var pips = usdPip.TotalCloseProfitPips();
// The return must be 5 pips.
Assert(pips == 5);
// Test Case 1: Pips are correctly estimated when the bas currency is the same as the Cash currency, USD in this case.
entryPrice = Transactions.GetOrderTicket(2).AverageFillPrice;
// Substract 10 pips to the USDJPY security.
_jpyusd.SetMarketPrice(new IndicatorDataPoint(_jpyusd.Symbol, Time, entryPrice - 0.10m));
var jpyPip = (ForexHolding)Portfolio["USDJPY"];
pips = jpyPip.TotalCloseProfitPips();
// The return must be -10 pips.
Assert(pips == -10);
// We are done here, so just close the algorithm.
Quit();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment