forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFuturesMomentumAlgorithm.cs
More file actions
105 lines (93 loc) · 3.89 KB
/
Copy pathFuturesMomentumAlgorithm.cs
File metadata and controls
105 lines (93 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Orders;
using QuantConnect.Securities;
using QuantConnect.Indicators;
namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// EMA cross with SP500 E-mini futures
/// In this example, we demostrate how to trade futures contracts using
/// a equity to generate the trading signals
/// It also shows how you can prefilter contracts easily based on expirations.
/// It also shows how you can inspect the futures chain to pick a specific contract to trade.
/// </summary>
/// <meta name="tag" content="using data" />
/// <meta name="tag" content="futures" />
/// <meta name="tag" content="indicators" />
/// <meta name="tag" content="strategy example" />
public class FuturesMomentumAlgorithm : QCAlgorithm
{
private const decimal _tolerance = 0.001m;
private const int _fastPeriod = 20;
private const int _slowPeriod = 60;
private ExponentialMovingAverage _fast;
private ExponentialMovingAverage _slow;
public bool IsReady { get { return _fast.IsReady && _slow.IsReady; } }
public bool IsUpTrend { get { return IsReady && _fast > _slow * (1 + _tolerance); } }
public bool IsDownTrend { get { return IsReady && _fast < _slow * (1 + _tolerance); } }
public override void Initialize()
{
SetStartDate(2016, 1, 1);
SetEndDate(2016, 8, 18);
SetCash(100000);
SetWarmUp(Math.Max(_fastPeriod, _slowPeriod));
// Adds SPY to be used in our EMA indicators
var equity = AddEquity("SPY", Resolution.Daily);
_fast = EMA(equity.Symbol, _fastPeriod, Resolution.Daily);
_slow = EMA(equity.Symbol, _slowPeriod, Resolution.Daily);
// Adds the future that will be traded and
// set our expiry filter for this futures chain
var future = AddFuture(Futures.Indices.SP500EMini);
future.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(182));
}
public override void OnData(Slice slice)
{
if (!Portfolio.Invested && IsUpTrend)
{
foreach (var chain in slice.FutureChains)
{
// find the front contract expiring no earlier than in 90 days
var contract = (
from futuresContract in chain.Value.OrderBy(x => x.Expiry)
where futuresContract.Expiry > Time.Date.AddDays(90)
select futuresContract
).FirstOrDefault();
// if found, trade it
if (contract != null)
{
MarketOrder(contract.Symbol, 1);
}
}
}
if (Portfolio.Invested && IsDownTrend)
{
Liquidate();
}
}
public override void OnEndOfDay()
{
Plot("Indicator Signal", "EOD", IsDownTrend ? -1 : IsUpTrend ? 1 : 0);
}
public override void OnOrderEvent(OrderEvent orderEvent)
{
Log(orderEvent.ToString());
}
}
}