13
13
* limitations under the License.
14
14
*/
15
15
16
+ using QuantConnect . Algorithm . Framework . Selection ;
16
17
using QuantConnect . Algorithm . Selection ;
18
+ using QuantConnect . Data ;
19
+ using QuantConnect . Data . Market ;
17
20
using QuantConnect . Data . UniverseSelection ;
18
21
using QuantConnect . Interfaces ;
19
- using QuantConnect . Securities ;
20
22
using System ;
21
23
using System . Collections . Generic ;
22
24
using System . Linq ;
@@ -28,52 +30,44 @@ namespace QuantConnect.Algorithm.CSharp
28
30
/// </summary>
29
31
public class OptionChainedUniverseSelectionModelRegressionAlgorithm : QCAlgorithm , IRegressionAlgorithmDefinition
30
32
{
33
+ Symbol _aapl ;
31
34
public override void Initialize ( )
32
35
{
33
- UniverseSettings . Resolution = Resolution . Daily ;
34
- SetStartDate ( 2014 , 03 , 22 ) ;
35
- SetEndDate ( 2014 , 04 , 07 ) ;
36
+ UniverseSettings . Resolution = Resolution . Minute ;
37
+ SetStartDate ( 2014 , 6 , 6 ) ;
38
+ SetEndDate ( 2014 , 6 , 6 ) ;
36
39
SetCash ( 100000 ) ;
40
+ _aapl = QuantConnect . Symbol . Create ( "AAPL" , SecurityType . Equity , Market . USA ) ;
41
+ var config = new SubscriptionDataConfig ( typeof ( TradeBar ) , _aapl , Resolution . Minute , TimeZones . NewYork , TimeZones . NewYork , false , false , true ) ;
42
+ var universe = AddUniverse ( new ManualUniverse ( config , UniverseSettings , new [ ] { _aapl } ) ) ;
37
43
38
- AddSecurity ( SecurityType . Equity , "GOOG" , Resolution . Daily ) ;
39
-
40
- var universe = AddUniverse ( coarse =>
41
- {
42
- // select the various google symbols over the period
43
- return from c in coarse
44
- let sym = c . Symbol . Value
45
- where sym == "GOOG" || sym == "GOOCV" || sym == "GOOAV"
46
- select c . Symbol ;
47
-
48
- // Before March 28th 2014:
49
- // - Only GOOG T1AZ164W5VTX existed
50
- // On March 28th 2014
51
- // - GOOAV VP83T1ZUHROL and GOOCV VP83T1ZUHROL are listed
52
- // On April 02nd 2014
53
- // - GOOAV VP83T1ZUHROL is delisted
54
- // - GOOG T1AZ164W5VTX becomes GOOGL
55
- // - GOOCV VP83T1ZUHROL becomes GOOG
56
- } ) ;
57
-
58
- AddUniverseSelection ( new OptionChainedUniverseSelectionModel ( universe ,
59
- option_filter_universe => option_filter_universe ) ) ;
44
+ AddUniverseSelection ( new OptionChainedUniverseSelectionModel ( universe , u => u . Strikes ( - 2 , + 2 )
45
+ // Expiration method accepts TimeSpan objects or integer for days.
46
+ // The following statements yield the same filtering criteria
47
+ . Expiration ( 0 , 180 ) ) ) ;
60
48
}
61
49
62
- public override void OnEndOfAlgorithm ( )
50
+ public override void OnData ( Slice slice )
63
51
{
64
- if ( ! UniverseManager . ContainsKey ( "?GOOCV" ) )
52
+ if ( ! Portfolio . Invested && IsMarketOpen ( _aapl ) )
65
53
{
66
- throw new Exception ( "Option chain {?GOOCV} should have been in the universe but it was not" ) ;
67
- }
54
+ OptionChain chain ;
55
+ if ( slice . OptionChains . TryGetValue ( "?AAPL" , out chain ) )
56
+ {
57
+ // we find at the money (ATM) put contract with farthest expiration
58
+ var atmContract = chain
59
+ . OrderByDescending ( x => x . Expiry )
60
+ . ThenBy ( x => Math . Abs ( chain . Underlying . Price - x . Strike ) )
61
+ . ThenByDescending ( x => x . Right )
62
+ . FirstOrDefault ( ) ;
68
63
69
- if ( ! UniverseManager . ContainsKey ( "?GOOG" ) )
70
- {
71
- throw new Exception ( "Option chain {?GOOG} should have been in the universe but it was not" ) ;
72
- }
73
-
74
- if ( ! UniverseManager . ContainsKey ( "?GOOAV" ) )
75
- {
76
- throw new Exception ( "Option chain {?GOOAV} should have been in the universe but it was not" ) ;
64
+ if ( atmContract != null )
65
+ {
66
+ // if found, trade it
67
+ MarketOrder ( atmContract . Symbol , 1 ) ;
68
+ MarketOnCloseOrder ( atmContract . Symbol , - 1 ) ;
69
+ }
70
+ }
77
71
}
78
72
}
79
73
@@ -90,7 +84,7 @@ public override void OnEndOfAlgorithm()
90
84
/// <summary>
91
85
/// Data Points count of all timeslices of algorithm
92
86
/// </summary>
93
- public long DataPoints => 78080 ;
87
+ public long DataPoints => 936646 ;
94
88
95
89
/// <summary>
96
90
/// Data Points count of the algorithm history
@@ -102,7 +96,7 @@ public override void OnEndOfAlgorithm()
102
96
/// </summary>
103
97
public Dictionary < string , string > ExpectedStatistics => new Dictionary < string , string >
104
98
{
105
- { "Total Trades" , "0 " } ,
99
+ { "Total Trades" , "2 " } ,
106
100
{ "Average Win" , "0%" } ,
107
101
{ "Average Loss" , "0%" } ,
108
102
{ "Compounding Annual Return" , "0%" } ,
@@ -119,14 +113,14 @@ public override void OnEndOfAlgorithm()
119
113
{ "Beta" , "0" } ,
120
114
{ "Annual Standard Deviation" , "0" } ,
121
115
{ "Annual Variance" , "0" } ,
122
- { "Information Ratio" , "1.552 " } ,
123
- { "Tracking Error" , "0.092 " } ,
116
+ { "Information Ratio" , "0 " } ,
117
+ { "Tracking Error" , "0" } ,
124
118
{ "Treynor Ratio" , "0" } ,
125
- { "Total Fees" , "$0 .00" } ,
126
- { "Estimated Strategy Capacity" , "$0 " } ,
127
- { "Lowest Capacity Asset" , "" } ,
128
- { "Portfolio Turnover" , "0 %" } ,
129
- { "OrderListHash" , "d41d8cd98f00b204e9800998ecf8427e " }
119
+ { "Total Fees" , "$2 .00" } ,
120
+ { "Estimated Strategy Capacity" , "$100000.00 " } ,
121
+ { "Lowest Capacity Asset" , "AAPL 2ZTXYLO9EQPZA|AAPL R735QTJ8XC9X " } ,
122
+ { "Portfolio Turnover" , "8.01 %" } ,
123
+ { "OrderListHash" , "3c4bef29d95bf4c3a566bca7531b1df0 " }
130
124
} ;
131
125
}
132
126
}
0 commit comments