Created
February 15, 2018 21:57
-
-
Save multidis/5b1f19411031fc32efd1ce5b2d9bbf25 to your computer and use it in GitHub Desktop.
QuantConnect Crypto GDAX CashBuyingPower SetHoldings issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// QuantConnect Crypto GDAX CashBuyingPower SetHoldings issue | |
using NodaTime; | |
namespace QuantConnect | |
{ | |
public class ExampleAlgorithm : QCAlgorithm | |
{ | |
private const string UnderlyingTicker = "BTCUSD"; | |
private const string UnderlyingCoin = "BTC"; | |
// slippage: NEED it for BTC | |
private const decimal constSlippage = 0.002m; | |
public override void Initialize() | |
{ | |
// set UTC clock | |
// https://www.quantconnect.com/forum/discussion/1995/time-zone-of-time | |
SetTimeZone(TimeZones.Utc); | |
SetStartDate(2015, 9, 1); | |
SetEndDate(2015, 9, 5); | |
SetCash(1000000); | |
//// TODO Live trading caution: | |
//// https://www.quantconnect.com/forum/discussion/3178/quotbtcusdquot---what-are-virtual-positions/p1 | |
//// use CashBook holdings instead of "BTCUSD" for algo restart consistency. | |
SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash); | |
AddCrypto(UnderlyingTicker, Resolution.Minute); | |
Securities[UnderlyingTicker].SlippageModel = new ConstantSlippageModel(constSlippage); | |
// buy-and-hold benchmark | |
SetBenchmark(UnderlyingTicker); | |
} | |
public void OnData(TradeBars bars) | |
{ | |
if (bars == null) { return; } | |
if (!Portfolio.Invested) | |
{ | |
// initial entry: fractional holding | |
SetHoldings(UnderlyingTicker, 0.1m); | |
return; | |
} | |
if (Time > new DateTime(2015, 9, 3, 20, 2, 59) && Time < new DateTime(2015, 9, 3, 20, 3, 59)) | |
{ | |
decimal und_value_now = Portfolio.CashBook[UnderlyingCoin].Amount * bars[UnderlyingTicker].Close; | |
decimal und_frac_current = und_value_now / (und_value_now + Portfolio.CashBook["USD"].Amount); | |
Debug(string.Format(@"Current fraction held = {0:0.00}", und_frac_current)); | |
Debug(string.Format(@"Current cash available = {0:0.00}", Portfolio.CashBook["USD"].Amount)); | |
SetHoldings(UnderlyingTicker, 1.0m); | |
} | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment