-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMACrossover.py
72 lines (54 loc) · 2.73 KB
/
MACrossover.py
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
class MACrossover(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021,1, 1) # Set Start Date
#self.SetEndDate(2017,12, 31) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
#self.SetEndDate(2020,6,30)
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)
self.long_list =[]
self.short_list =[]
self.stop =False
self.currencies = ["EURUSD","GBPJPY"]
self.AddForex("EURUSD", Resolution.Daily)
self.AddForex("GBPJPY", Resolution.Daily)
def OnData(self, data):
if self.stop:
return
currencies = self.currencies
for currency in currencies:
#self.Debug("The currency is running")
currency_data = self.History ([currency], 30 , Resolution.Daily)
MA21_Pre = currency_data.close[8:30].mean()
MA5_Pre = currency_data.close [24:30].mean()
MA21_Current = currency_data.close[9:31].mean()
MA5_Current = currency_data.close[25:31].mean()
#First Trade
#entry
if currency not in self.long_list and currency not in self.short_list:
self.Debug ("found currency" +str(currency))
#Checking Bullish Crossover
if MA5_Pre < MA21_Pre and MA5_Current > MA21_Current:
#self.Debug("checking long condition")
self.SetHoldings (currency, 0.5)
self.long_list.append(currency)
#Checking Bearish Crossover
if MA5_Pre > MA21_Pre and MA5_Current < MA21_Current:
#self.Debug("checking short condition")
self.SetHoldings (currency, -0.5)
self.short_list.append(currency)
#exit and again entry
if currency in self.long_list:
#Checking Bearish Crossover
if MA5_Pre > MA21_Pre and MA5_Current < MA21_Current:
self.SetHoldings (currency, -0.5)
self.long_list.remove(currency)
self.short_list.append(currency)
if currency in self.short_list:
#Checking Bullish Crossover
if MA5_Pre < MA21_Pre and MA5_Current > MA21_Current:
self.SetHoldings (currency, 0.5)
self.short_list.remove(currency)
self.long_list.append(currency)
if self.Portfolio.Cash <85000:
self.stop = True
self.Liquidate()