-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThe Musk Tweet Strategy
61 lines (46 loc) · 2.04 KB
/
The Musk Tweet 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# region imports
#trading strategy related to Elon Musk tweets
from AlgorithmImports import *
from nltk.sentiment import *
# endregion
class HyperActiveBluePig(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2012, 11, 1) # Set Start Date
self.SetEndDate(2017,1,1)
self.SetCash(1000000) # Set Strategy Cash
self.tsla = self.AddEquity("TSLA", Resolution.Minute).Symbol
self.musk = self.AddData(MUSKTWEETS, "ELONMSKTWTS", Resolution.Minute).Symbol
def OnData(self, data: Slice):
if self.musk in data:
score = data[self.musk].Value
content = data[self.musk].Tweet
if score > 0.5:
self.SetHoldings(self.tsla,1)
elif score < 0.5:
self.SetHoldings(self.tsla,-1)
if abs(score) > 0.5:
self.Log("Score: " + str(score) + ", Tweet: " + content)
def ExitPositions(self):
self.Liquidate()
class MUSKTWEETS(PythonData):
sia = SentimentIntensityAnalyzer()
def GetSource(self, config, date, isLiveMode):
source = "https://www.dropbox.com/s/ovnsrgg1fou1y0r/MuskTweetsPreProcessed.csv?dl=1" #dl = 1 stands for download = 1 (Yes)
return SubscriptionDataSource(source, Symbol, SubscriptionTransportMedium.RemoteFile)
def Reader(self, config, line, date, isLiveMode):
if not(line.strip() and line[0].isdigit()):
return None
try:
data = line.split(",")
tweet = MUSKTWEETS()
tweet.Symbol = config.Symbol
tweet.Time = datetime.strptime(data[0], "%Y-%m-%d %H:%M:%S") + timedelta(minutes=1)
content = data[1].lower()
if "tsla" in content or "tesla" in content:
tweet.Value = self.sia.polarity_scores(content)["compound"]
else:
tweet.Value = 0
tweet["Tweet"] = str(content)
except:
return None
return tweet