Skip to content

Commit 4d85f27

Browse files
authoredFeb 2, 2023
Add bull/bear put spread sample algorithms for index weeklies (QuantConnect#6914)
* bear put spread * bull put spread
1 parent c2efa65 commit 4d85f27

4 files changed

+236
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Linq;
17+
using System.Collections.Generic;
18+
using QuantConnect.Data;
19+
using QuantConnect.Orders;
20+
21+
namespace QuantConnect.Algorithm.CSharp
22+
{
23+
public class IndexOptionBearPutSpreadAlgorithm : QCAlgorithm
24+
{
25+
private Symbol _spxw;
26+
private List<Leg> _legs = new();
27+
28+
public override void Initialize()
29+
{
30+
SetStartDate(2022, 1, 1);
31+
SetEndDate(2022, 7, 1);
32+
SetCash(100000);
33+
34+
var index = AddIndex("SPX", Resolution.Minute).Symbol;
35+
var option = AddIndexOption(index, "SPXW", Resolution.Minute);
36+
option.SetFilter((x) => x.WeeklysOnly().Strikes(5, 10).Expiration(0, 0));
37+
38+
_spxw = option.Symbol;
39+
}
40+
41+
public override void OnData(Slice slice)
42+
{
43+
// Return if open position exists
44+
if (_legs.Any(x => Portfolio[x.Symbol].Invested)) return;
45+
46+
// Get the OptionChain
47+
if (!slice.OptionChains.TryGetValue(_spxw, out var chain)) return;
48+
49+
// Get the nearest expiry date of the contracts
50+
var expiry = chain.Min(x => x.Expiry);
51+
52+
// Select the put Option contracts with the nearest expiry and sort by strike price
53+
var puts = chain.Where(x => x.Expiry == expiry && x.Right == OptionRight.Put)
54+
.OrderBy(x => x.Strike).ToArray();
55+
if (puts.Length < 2) return;
56+
57+
// Create combo order legs
58+
_legs = new List<Leg>
59+
{
60+
Leg.Create(puts[0].Symbol, -1),
61+
Leg.Create(puts[^1].Symbol, 1)
62+
};
63+
ComboMarketOrder(_legs, 1);
64+
}
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Linq;
17+
using System.Collections.Generic;
18+
using QuantConnect.Data;
19+
using QuantConnect.Orders;
20+
21+
namespace QuantConnect.Algorithm.CSharp
22+
{
23+
public class IndexOptionBullPutSpreadAlgorithm : QCAlgorithm
24+
{
25+
private Symbol _spxw;
26+
private List<Leg> _legs = new();
27+
28+
public override void Initialize()
29+
{
30+
SetStartDate(2019, 1, 1);
31+
SetEndDate(2020, 1, 1);
32+
SetCash(100000);
33+
34+
var index = AddIndex("SPX", Resolution.Minute).Symbol;
35+
var option = AddIndexOption(index, "SPXW", Resolution.Minute);
36+
option.SetFilter((x) => x.WeeklysOnly().Strikes(-10, -5).Expiration(0, 0));
37+
38+
_spxw = option.Symbol;
39+
}
40+
41+
public override void OnData(Slice slice)
42+
{
43+
// Return if open position exists
44+
if (_legs.Any(x => Portfolio[x.Symbol].Invested)) return;
45+
46+
// Get the OptionChain
47+
if (!slice.OptionChains.TryGetValue(_spxw, out var chain)) return;
48+
49+
// Get the nearest expiry date of the contracts
50+
var expiry = chain.Min(x => x.Expiry);
51+
52+
// Select the put Option contracts with the nearest expiry and sort by strike price
53+
var puts = chain.Where(x => x.Expiry == expiry && x.Right == OptionRight.Put)
54+
.OrderBy(x => x.Strike).ToArray();
55+
if (puts.Length < 2) return;
56+
57+
// Create combo order legs
58+
_legs = new List<Leg>
59+
{
60+
Leg.Create(puts[0].Symbol, 1),
61+
Leg.Create(puts[^1].Symbol, -1)
62+
};
63+
ComboMarketOrder(_legs, 1);
64+
}
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
2+
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from AlgorithmImports import *
15+
16+
class IndexOptionBearPutSpreadAlgorithm(QCAlgorithm):
17+
18+
def Initialize(self):
19+
self.SetStartDate(2022, 1, 1)
20+
self.SetEndDate(2022, 7, 1)
21+
self.SetCash(100000)
22+
23+
index = self.AddIndex("SPX", Resolution.Minute).Symbol
24+
option = self.AddIndexOption(index, "SPXW", Resolution.Minute)
25+
option.SetFilter(lambda x: x.WeeklysOnly().Strikes(5, 10).Expiration(0, 0))
26+
27+
self.spxw = option.Symbol
28+
self.legs = []
29+
30+
def OnData(self, slice: Slice) -> None:
31+
# Return if open position exists
32+
if any([self.Portfolio[x.Symbol].Invested for x in self.legs]):
33+
return
34+
35+
# Get option chain
36+
chain = slice.OptionChains.get(self.spxw)
37+
if not chain: return
38+
39+
# Get the nearest expiry date of the contracts
40+
expiry = min([x.Expiry for x in chain])
41+
42+
# Select the put Option contracts with the nearest expiry and sort by strike price
43+
puts = sorted([i for i in chain if i.Expiry == expiry and i.Right == OptionRight.Put],
44+
key=lambda x: x.Strike)
45+
if len(puts) < 2: return
46+
47+
# Create combo order legs
48+
self.legs = [
49+
Leg.Create(puts[0].Symbol, -1),
50+
Leg.Create(puts[-1].Symbol, 1)
51+
]
52+
self.ComboMarketOrder(self.legs, 1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
2+
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from AlgorithmImports import *
15+
16+
class IndexOptionBullPutSpreadAlgorithm(QCAlgorithm):
17+
18+
def Initialize(self):
19+
self.SetStartDate(2019, 1, 1)
20+
self.SetEndDate(2020, 1, 1)
21+
self.SetCash(100000)
22+
23+
index = self.AddIndex("SPX", Resolution.Minute).Symbol
24+
option = self.AddIndexOption(index, "SPXW", Resolution.Minute)
25+
option.SetFilter(lambda x: x.WeeklysOnly().Strikes(-10, -5).Expiration(0, 0))
26+
27+
self.spxw = option.Symbol
28+
self.legs = []
29+
30+
def OnData(self, slice: Slice) -> None:
31+
# Return if open position exists
32+
if any([self.Portfolio[x.Symbol].Invested for x in self.legs]):
33+
return
34+
35+
# Get option chain
36+
chain = slice.OptionChains.get(self.spxw)
37+
if not chain: return
38+
39+
# Get the nearest expiry date of the contracts
40+
expiry = min([x.Expiry for x in chain])
41+
42+
# Select the put Option contracts with the nearest expiry and sort by strike price
43+
puts = sorted([i for i in chain if i.Expiry == expiry and i.Right == OptionRight.Put],
44+
key=lambda x: x.Strike)
45+
if len(puts) < 2: return
46+
47+
# Create combo order legs
48+
self.legs = [
49+
Leg.Create(puts[0].Symbol, 1),
50+
Leg.Create(puts[-1].Symbol, -1)
51+
]
52+
self.ComboMarketOrder(self.legs, 1)

0 commit comments

Comments
 (0)
Please sign in to comment.