| Overall Statistics |
|
Total Trades 49 Average Win 0.56% Average Loss -3.24% Compounding Annual Return 28.101% Drawdown 18.400% Expectancy 0.148 Net Profit 26.346% Sharpe Ratio 1.371 Loss Rate 2% Win Rate 98% Profit-Loss Ratio 0.17 Alpha 0.268 Beta -0.074 Annual Standard Deviation 0.188 Annual Variance 0.035 Information Ratio 0.569 Tracking Error 0.22 Treynor Ratio -3.508 |
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;
using System.Linq;
namespace QuantConnect
{
// Name your algorithm class anything, as long as it inherits QCAlgorithm
public class RebalanceAlgorithm : QCAlgorithm
{
DateTime _lastRebalance = new DateTime();
List<string> _assets = new List<string>() { "LWAY", "UUP", "MUB", "VTI", "SPY" };
string symbol_active = "SPY";
int qtyTranch = 777;
List<decimal> prices = new List<decimal>();
List<decimal> takeprofits = new List<decimal>();
//"VYM", "MUB", SHV , UUP
//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
// SetStartDate(2013, 1, 1);
// SetEndDate(DateTime.Now.AddDays(-1));
SetStartDate(2014, 1, 1);
SetEndDate(2015, 12, 30);
SetCash(600000);
foreach (var symbol in _assets)
{
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
}
}
//Simplest implementation of asset rebalancing.
public void OnData(TradeBars data)
{
if (!Portfolio.Invested)
{ Order(symbol_active, qtyTranch);
// // SetHoldings("VTI", 0.3);
// SetHoldings("UUP", 0.1);
// SetHoldings("MUB", 0.6);
}
// Day
if ( _lastRebalance.Date != Time.Date)
{
// decimal.Multiply( data[symbol_active].Close, Decimal.Parse("0.97") ) ;
prices.Add( decimal.Multiply( data[symbol_active].Close, Decimal.Parse("0.975") ) );
takeprofits.Add( decimal.Multiply( data[symbol_active].Close, Decimal.Parse("1.03") ) );
if(Portfolio.TotalUnrealizedProfit>1000)
{
//Order(symbol_active, -qtyTranch);
Liquidate();
SetHoldings(symbol_active, 0.9);
}
if(data[symbol_active].Open<prices.Max())
{
Order(symbol_active, qtyTranch);
prices.Clear();
// Liquidate();
// SetHoldings("VTI", 0.3);
// SetHoldings("UUP", 0.1);
// SetHoldings("MUB", 0.6);
}
_lastRebalance = Time;
}
}
}
}using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;
namespace QuantConnect {
/*
* Average True Range:
*
* Find the volatility of the market based on the average movement between bars.
*
* Typically a 14 Day-Period ATR
*
*/
public class AverageTrueRange
{
//Get the current ATR value for this stock.
public decimal ATR {
get {return _atr;}
}
//Let to caller know when the indicator is ready to use:
public bool Ready {
get { return (_samples >= _period); }
}
//Initialize a new ATR Class:
public AverageTrueRange(int period = 14) {
_period = Convert.ToDecimal(period);
}
//Working variables:
private decimal _samples = 0;
private decimal _atr = 0;
private decimal _tr = 0;
private decimal _period = 14;
private TradeBar _lastBar = new TradeBar();
/*
* Add a new tradebar sample to the ATR:
*/
public decimal AddSample(TradeBar bar) {
//Start calculation on second bar.
if (_samples <= _period) _samples++;
if (_samples == 1) { _lastBar = bar; return 0; }
//Abs Maximum of the Following Ranges:
decimal range1 = Math.Abs(bar.High - _lastBar.Low);
decimal range2 = bar.High - bar.Low;
decimal range3 = Math.Abs(bar.Low - _lastBar.Close);
//True Range of this Bar:
_tr = Math.Max(range1, Math.Max(range2, range3));
//Set first values:
if (_samples == 1) _atr = _tr;
//Find the ATR:
_atr = (((_period - 1) * _atr) + _tr) / _period;
//Save bar:
_lastBar = bar;
return _atr;
}
}
}