-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacd.py
More file actions
52 lines (40 loc) · 1.74 KB
/
macd.py
File metadata and controls
52 lines (40 loc) · 1.74 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
from xylem.functions.metrics import alpha
from pendulum import from_format
def compute(context, stats, order):
'''
1. Find the current amount of buying power
2. Find the current price of the security
3. Find how many shares we can buy/sell
4. Order that amount
'''
# If short term momentum exceeds long term momentum
if ((stats["EMA12"] * (1.00 - context["bias"])) > stats["EMA26"] and
not context["long_position"]):
# Enter a long position, selling our short position if we have one
if context["short_position"]:
# Exit the short position with a market buy order
order(1)
context["short_position"] = False
order(1)
context["long_position"] = True
# If short term momentum falls below long term momentum
elif ((stats["EMA12"] * (1.00 + context["bias"])) < stats["EMA26"] and
not context["short_position"]):
# Enter a short position, selling our long position if we have one
if context["long_position"]:
order(-1)
context["long_position"] = False
order(-1)
context['short_position'] = True
def before_exit(context, stats, order):
# if context["long_position"]:
# print("Long position before_exit()")
# if context["short_position"]:
# print("Short position before_exit()")
order(liquidate=True)
def analyze(result_set, barsets):
for results, barset in zip(result_set, barsets):
RI = results["ending_balance"] - results["starting_balance"]
start = from_format(results["start_time"], 'YYYY-MM-DD').date()
stop = from_format(results["stop_time"], 'YYYY-MM-DD').date()
# print("Alpha: ", alpha(RI, results.symbol, start, stop))