basically, i added
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
AddSecurity(SecurityType.Equity, spy_symbol, Resolution.Minute);
and in public void OnData(TradeBars data)
i handled uvxy, I didn't handle spy....
I think thats why it has reported the error...
But i dont know how to fix that... can someone give me some insights?
Best
Lifan
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using QuantConnect.Algorithm;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;
using QuantConnect.Securities;
namespace QuantConnect
{
public class UVXYwithCatango : QCAlgorithm
{
private string symbol="UVXY";
private string spy_symbol="SPY";
private double percent=0.8; // total use 20% of cash to short uvxy in the total Portfolio;
// private int quantity=500;
private int trigger=0;
private int vixtrigger=0;
decimal currCatango;
RelativeStrengthIndex rsi;
public override void Initialize()
{
SetCash(1000000);
SetStartDate(2011,01,01);
SetEndDate(2015, 03, 31);
AddData<VIX>("VIX");
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
AddSecurity(SecurityType.Equity, spy_symbol, Resolution.Minute);
// rsi = RSI("SPY",14, MovingAverageType.Simple, Resolution.Daily);
// SetWarmUp(TimeSpan.FromDays(30));
Schedule.On(DateRules.EveryDay("UVXY"), TimeRules.AfterMarketOpen("UVXY",0), () =>
{
trigger=-1;
//Log(Time.ToString()+"");
});
Schedule.On(DateRules.EveryDay("UVXY"), TimeRules.BeforeMarketClose("UVXY",0), () =>
{
trigger=1;
//Log(Time.ToString()+"");
});
}
public void OnData(VIX data)
{
//currCatango=(data.vix_n/data.vix_f)-1;
//Log("current catango: "+currCatango+ data.Time+"\t"+currCatango+"\t"+Convert.ToDouble(currCatango));
//Log(data.Value+"");
currCatango=data.Value;
if(Convert.ToDouble(currCatango)<=0.05)
{
//Log("current catango: "+currCatango+ data.Time+"\t"+currCatango+"\t"+Convert.ToDouble(currCatango));
vixtrigger=1;
}
else
vixtrigger=-1;
}
public void OnData(TradeBars data)
{
// if (!rsi.IsReady) return;
// if(Time.Hour==9&&Time.Minute==35)
// Log(Time.ToString()+""+"HELLO");
double avaiblecash=Convert.ToDouble(percent*Convert.ToDouble(Portfolio.Cash));
double price=Convert.ToDouble(Portfolio[symbol].Price);
int quantity=Convert.ToInt32(avaiblecash/price);
//int quantity=(percent*Convert.ToDouble(Portfolio.Cash))/Convert.ToDouble(Portfolio[symbol].Price);
if(trigger==-1&&vixtrigger==-1&&!Portfolio.HoldStock)
{
Log(currCatango+"rsi"+rsi);
// Log(Portfolio[symbol].Price+"");
Order(symbol,-quantity);
// Log(Time.ToString()+""+"short"+quantity);
}
else if (trigger==1&&Portfolio.HoldStock)
{
Liquidate("UVXY");
//Order(symbol,Portfolio.quantity);
// Log(Time.ToString()+""+"close"+quantity);
}
}
public class VIX : BaseData
{
public decimal Catango = 0;
public decimal vix_n=0;
public decimal vix_f=0;
string format ="yyyy-MM-dd";
public string url="";
CultureInfo provider = CultureInfo.InvariantCulture;
public VIX()
{
this.Symbol = "VIX";
}
public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
{
return new SubscriptionDataSource(url, SubscriptionTransportMedium.RemoteFile);
}
public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
{
var index = new VIX();
try
{
string[] data = line.Split(',');
//Dates must be in the format YYYY-MM-DD. If your data source does not have this format, you must use
//DateTime.ParseExact() and explicit declare the format your data source has.
string dateString = data[0].Replace("$","");
index.Time = DateTime.ParseExact(dateString, format, provider);
index.Symbol = "VIX";
index.vix_n=Convert.ToDecimal(data[1]);
index.vix_f=Convert.ToDecimal(data[2]);
index.Value=(index.vix_n/index.vix_f)-1;
}
catch
{
}
return index;
}
}
}
}
using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using QuantConnect.Algorithm; using QuantConnect.Data; using QuantConnect.Data.Market; using QuantConnect.Indicators; using QuantConnect.Securities; namespace QuantConnect { public class UVXYwithCatango : QCAlgorithm { private string symbol="UVXY"; private string spy_symbol="SPY"; private double percent=0.8; // total use 20% of cash to short uvxy in the total Portfolio; // private int quantity=500; private int trigger=0; private int vixtrigger=0; decimal currCatango; RelativeStrengthIndex rsi; public override void Initialize() { SetCash(1000000); SetStartDate(2011,01,01); SetEndDate(2015, 03, 31); AddData<VIX>("VIX"); AddSecurity(SecurityType.Equity, symbol, Resolution.Minute); AddSecurity(SecurityType.Equity, spy_symbol, Resolution.Minute); // rsi = RSI("SPY",14, MovingAverageType.Simple, Resolution.Daily); // SetWarmUp(TimeSpan.FromDays(30)); Schedule.On(DateRules.EveryDay("UVXY"), TimeRules.AfterMarketOpen("UVXY",0), () => { trigger=-1; //Log(Time.ToString()+""); }); Schedule.On(DateRules.EveryDay("UVXY"), TimeRules.BeforeMarketClose("UVXY",0), () => { trigger=1; //Log(Time.ToString()+""); }); } public void OnData(VIX data) { //currCatango=(data.vix_n/data.vix_f)-1; //Log("current catango: "+currCatango+ data.Time+"\t"+currCatango+"\t"+Convert.ToDouble(currCatango)); //Log(data.Value+""); currCatango=data.Value; if(Convert.ToDouble(currCatango)<=0.05) { //Log("current catango: "+currCatango+ data.Time+"\t"+currCatango+"\t"+Convert.ToDouble(currCatango)); vixtrigger=1; } else vixtrigger=-1; } public void OnData(TradeBars data) { // if (!rsi.IsReady) return; // if(Time.Hour==9&&Time.Minute==35) // Log(Time.ToString()+""+"HELLO"); double avaiblecash=Convert.ToDouble(percent*Convert.ToDouble(Portfolio.Cash)); double price=Convert.ToDouble(Portfolio[symbol].Price); int quantity=Convert.ToInt32(avaiblecash/price); //int quantity=(percent*Convert.ToDouble(Portfolio.Cash))/Convert.ToDouble(Portfolio[symbol].Price); if(trigger==-1&&vixtrigger==-1&&!Portfolio.HoldStock) { Log(currCatango+"rsi"+rsi); // Log(Portfolio[symbol].Price+""); Order(symbol,-quantity); // Log(Time.ToString()+""+"short"+quantity); } else if (trigger==1&&Portfolio.HoldStock) { Liquidate("UVXY"); //Order(symbol,Portfolio.quantity); // Log(Time.ToString()+""+"close"+quantity); } } public class VIX : BaseData { public decimal Catango = 0; public decimal vix_n=0; public decimal vix_f=0; string format ="yyyy-MM-dd"; public string url=""; CultureInfo provider = CultureInfo.InvariantCulture; public VIX() { this.Symbol = "VIX"; } public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode) { return new SubscriptionDataSource(url, SubscriptionTransportMedium.RemoteFile); } public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode) { var index = new VIX(); try { string[] data = line.Split(','); //Dates must be in the format YYYY-MM-DD. If your data source does not have this format, you must use //DateTime.ParseExact() and explicit declare the format your data source has. string dateString = data[0].Replace("$",""); index.Time = DateTime.ParseExact(dateString, format, provider); index.Symbol = "VIX"; index.vix_n=Convert.ToDecimal(data[1]); index.vix_f=Convert.ToDecimal(data[2]); index.Value=(index.vix_n/index.vix_f)-1; } catch { } return index; } } } }
Jared Broad
Hi Lifan. Please attach backtests; don't copy and paste entire algorithms. If you do need to attach a small code snippet - use the code widget. The code needs a good tidy up. I think if you spend some time tidying it you'll fix the bug.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Lifan
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute); AddSecurity(SecurityType.Equity, spy_symbol, Resolution.Minute); public void OnData(TradeBars data) { double avaiblecash=Convert.ToDouble(percent*Convert.ToDouble(Portfolio.Cash)); double price=Convert.ToDouble(Portfolio[symbol].Price); int quantity=Convert.ToInt32(avaiblecash/price); }
hmmm... this is the code... basically, i added two symbols and i handled on in ondata function...
Alexandre Catarino
Your algorithm start date is Jan 1st 2011 and UVXY has started on Oct 4th 2011 (you can check out this information here).
Since you didn't put any check on whether there is information about it on data object, this is happening:
double price=Convert.ToDouble(Portfolio[symbol].Price); // price = 0 int quantity=Convert.ToInt32(avaiblecash/price); // avaiblecash/price = Infinity (because division by zero) // Convert.ToInt32 throws: // Value was either too large or too small for an Int32.
To fix that, you need to check if data contains UVXY:
// Do not continue if we do not have UVXY if(!data.ContainsKey(symbol)) return; double price=Convert.ToDouble(data[symbol].Price); int quantity=Convert.ToInt32(avaiblecash/price);
Lifan
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!