-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1% stop loss strategy
42 lines (30 loc) · 1.32 KB
/
1% stop loss strategy
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
# region imports
from AlgorithmImports import *
# endregion
#strategy = take profit with 1% up and take loss with 1% down (SPY)
class FirstCode(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2015, 9, 12) # Set Start Date
self.SetEndDate(2022, 3, 10) # Set End Date
self.SetCash(1000000) # Set Strategy Cash
spy = self.AddEquity("SPY", Resolution.Daily)
spy.SetDataNormalizationMode(DataNormalizationMode.TotalReturn)
self.spy = spy.Symbol
self.SetBenchmark("SPY")
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
self.entryPrice = 0
self.period = timedelta(1)
self.nextEntryTime = self.Time
#OnData -> lets us set historic data and the time frontier
def OnData(self, data: Slice):
if not self.spy in data:
return
price = data.Bars[self.spy].Close
if not self.Portfolio.Invested:
if self.nextEntryTime <= self.Time:
self.SetHoldings(self.spy,1)
self.Log("BUY SPY @"+str(price))
elif self.entryPrice*1.1 < price or self.entryPrice*0.9 > price:
self.Liquidate()
self.Log("SELL SPY @" + str(price))
self.nextEntryTime = self.Time + self.period