forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateOrderRegressionAlgorithm.py
More file actions
137 lines (111 loc) · 6.4 KB
/
UpdateOrderRegressionAlgorithm.py
File metadata and controls
137 lines (111 loc) · 6.4 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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.
from math import copysign
from datetime import datetime
import clr
clr.AddReference("System.Core")
clr.AddReference("System.Collections")
clr.AddReference("QuantConnect.Algorithm")
clr.AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data import *
from QuantConnect.Orders import *
from QuantConnect.Securities import *
from QuantConnect.Util import *
clr.ImportExtensions(Extensions)
clr.ImportExtensions(OrderExtensions)
clr.ImportExtensions(Linq)
class UpdateOrderRegressionAlgorithm(QCAlgorithm):
'''Basic template algorithm simply initializes the date range and cash'''
def __init__(self):
self.__LastMonth = -1
self.__Quantity = 100
self.__DeltaQuantity = 10
self.__StopPercentage = 0.025
self.__StopPercentageDelta = 0.005
self.__LimitPercentage = 0.025
self.__LimitPercentageDelta = 0.005
self.__SecType = SecurityType.Equity
self.__Symbol = Symbol.Create("SPY", self.__SecType, "USA")
self.__Security = None
OrderTypeEnum = [OrderType.Market, OrderType.Limit, OrderType.StopMarket, OrderType.StopLimit, OrderType.MarketOnOpen, OrderType.MarketOnClose]
self.__orderTypesQueue = CircularQueue[OrderType](OrderTypeEnum)
self.__tickets = []
def onCircleCompleted(self, sender, event):
'''Flip our signs when we've gone through all the order types'''
self.__Quantity *= -1
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetStartDate(2013,01,01) #Set Start Date
self.SetEndDate(2015,01,01) #Set End Date
self.SetCash(100000) #Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
self.AddSecurity(self.__SecType, self.__Symbol, Resolution.Daily)
self.__Security = self.Securities[self.__Symbol];
self.__orderTypesQueue.CircleCompleted += self.onCircleCompleted
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
if not data.Bars.ContainsKey(self.__Symbol):
return
pyTime = datetime(self.Time)
if pyTime.month != self.__LastMonth:
# we'll submit the next type of order from the queue
orderType = self.__orderTypesQueue.Dequeue();
#Log("");
self.Log("\r\n--------------MONTH: {0}:: {1}\r\n".format(pyTime.strftime("%B"), orderType))
#Log("")
self.__LastMonth = pyTime.month
self.Log("ORDER TYPE:: {0}".format(orderType))
isLong = self.__Quantity > 0
stopPrice = (1 + self.__StopPercentage)*data.Bars[self.__Symbol].High if isLong else (1 - self.__StopPercentage)*data.Bars[self.__Symbol].Low
limitPrice = (1 - self.__LimitPercentage)*stopPrice if isLong else (1 + self.__LimitPercentage)*stopPrice
if orderType == OrderType.Limit:
limitPrice = (1 + self.__LimitPercentage)*data.Bars[self.__Symbol].High if not isLong else (1 - self.__LimitPercentage)*data.Bars[self.__Symbol].Low
request = SubmitOrderRequest(orderType, self.__SecType, self.__Symbol, self.__Quantity, stopPrice, limitPrice, self.Time, orderType.ToString())
ticket = self.Transactions.AddOrder(request)
self.__tickets.append(ticket)
elif len(self.__tickets) > 0:
ticket = self.__tickets[-1]
if pyTime.day > 8 and pyTime.day < 14:
if len(ticket.UpdateRequests) == 0 and ticket.Status.IsOpen():
self.Log("TICKET:: {0}".format(ticket))
updateOrderFields = UpdateOrderFields()
updateOrderFields.Quantity = ticket.Quantity + copysign(self.__DeltaQuantity, self.__Quantity)
updateOrderFields.Tag = "Change quantity: {0}".format(pyTime)
ticket.Update(updateOrderFields)
self.Log("UPDATE1:: {0}".format(ticket.UpdateRequests.Last()))
elif pyTime.day > 13 and pyTime.day < 20:
if len(ticket.UpdateRequests) == 1 and ticket.Status.IsOpen():
self.Log("TICKET:: {0}".format(ticket))
updateOrderFields = UpdateOrderFields()
updateOrderFields.LimitPrice = self.__Security.Price*(1 - copysign(self.__LimitPercentageDelta, ticket.Quantity))
updateOrderFields.StopPrice = self.__Security.Price*(1 + copysign(self.__StopPercentageDelta, ticket.Quantity))
updateOrderFields.Tag = "Change prices: {0}".format(pyTime)
ticket.Update(updateOrderFields)
self.Log("UPDATE2:: {0}".format(ticket.UpdateRequests.Last()))
else:
if len(ticket.UpdateRequests) == 2 and ticket.Status.IsOpen():
self.Log("TICKET:: {0}".format(ticket))
ticket.Cancel("{0} and is still open!".format(pyTime))
self.Log("CANCELLED:: {0}".format(ticket.CancelRequest))
def OnOrderEvent(self, orderEvent):
if orderEvent.Status == OrderStatus.Filled:
self.Log("FILLED:: {0} FILL PRICE:: {1}".format(self.Transactions.GetOrderById(orderEvent.OrderId), orderEvent.FillPrice.SmartRounding()))
else:
self.Log(orderEvent.ToString())
self.Log("TICKET:: {0}".format(self.__tickets[-1]))