Back

SPY Dollar Averaging

2012 was a rough year for SPY.

using System;
using System.Collections;
using System.Collections.Generic;

namespace QuantConnect {

using QuantConnect.Securities;
using QuantConnect.Models;

public partial class BasicTemplateAlgorithm : QCAlgorithm, IAlgorithm {

bool bTraded = false;
DateTime nextDate = new DateTime();

public override void Initialize() {
SetStartDate(2012, 06, 01);
SetEndDate(2012, 06, 30);
SetCash(30000);
SetRunMode(RunMode.Series);
AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
}

public override void OnTradeBar(Dictionary securityData) {
if (Time.Date > nextDate) {
if (Time.Hour > 12 && !bTraded) {
Order("SPY", 10);
bTraded = false;
nextDate = Time.Date.AddHours(48);
}
}
}
}
}
Update Backtest






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.



using System;
using System.Collections;
using System.Collections.Generic;

namespace QuantConnect
{
using QuantConnect.Securities;
using QuantConnect.Models;

public partial class DollarCostAverageAlgorithm : QCAlgorithm, IAlgorithm
{

private string etf = "SPY";
private DateTime startDate = new DateTime(2009, 10, 01);
private DateTime endDate = new DateTime(2010, 03, 31);
private DateTime nextTradeDate = DateTime.MinValue;

// Monthly Dollar Value to be used, say every month you have 1000 to invest.
private decimal monthlyDollarValue = 1000;


public override void Initialize()
{
SetRunMode(RunMode.Series);
Resolution resolution = Resolution.Minute;
nextTradeDate = startDate.AddDays(15);

SetStartDate(startDate);
SetEndDate(endDate);
SetCash(SetupCapital(startDate, endDate));
AddSecurity(SecurityType.Equity, etf, resolution);
}

private decimal SetupCapital(DateTime startDate, DateTime endDate)
{
if (startDate.Date < endDate.Date )
{
int months = (int) ((endDate.Date - startDate.Date).Days / 30);

return (decimal) months * monthlyDollarValue * 10 ;
}

//Default 100,000 dollars,
return (decimal) 100000.0;
}

private int Quantity(decimal price)
{
int quantity = (int) (monthlyDollarValue / price);
return quantity;
}

public override void OnTradeBar(Dictionary securityData)
{
try
{
//trade in the on a midday
if (Time.Date >= nextTradeDate.Date && Time.Hour >= 12)
{

// finally we will make our investment. Ho Ho HO Ho father christmas has arrived early.
int quantity = Quantity(securityData[etf].Price);
Order(etf, quantity); //symbol, quantity

nextTradeDate = nextTradeDate.AddMonths(1);

}
return;
}
catch (Exception err)
{
Error("This is an error handler." + err.Message);
}
}
}
}
0

Hi,

This is my code base. Dollar cost being 1000 Dollar per month say at 15th each month or soon after that.

I get result fine for a small simulation day 4 months (simulation id: b1e60c72-3d4e-48db-a9dd-948dba8d084b). I have checked the trades, they seem to be fine.

Anything longer simulation time seems not be working. The simulation ids are as follows:
c1747beb-2f47-4de5-9e03-6d45b57a7b34
ae50851a-a685-4a04-beeb-39466b8f661d
097beabe-1652-49dc-a972-1a966c7cc985
0

Yeah I am taking slices of time. Any simulation that deals with data more than 6 months seem not to work, at least for this dollar coast average that I have listed above.

Please feel free to copy/paste it and have a play with different start and end dates.



Joyanta
0

Good catch, thank you. We pushed out code a little too fast last night and missed a bug ;) We'll patch this today -
0

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.


cool cheers.
0

Cool done --> 1. Fixed the series bug. It was from the new dynamic scatter plotting, again only visual no backtests affected. 2. Fixed the bug restricting data from July 31st -> December 31st. Gustavo is finishing the charting tonight.

Now will add the daily update server so data is always current until yesterday...

Series analysis take too long I think so perhaps we'll work on some optimization there as well. At least now you'll get dynamic beautiful charts while the results stream in.
0

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.


Update Backtest





0

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.


Loading...

This discussion is closed