Skip to content

Commit 5659c70

Browse files
committed
Output image
1 parent 3091e01 commit 5659c70

15 files changed

+121
-99
lines changed

.idea/.gitignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/Event_driven_framework.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/other.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AAPL.py

Lines changed: 4 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@
44
from __future__ import print_function
55

66
import datetime
7-
8-
import numpy as np
9-
import pandas as pd
10-
import statsmodels.api as sm
117
import math
128
import os
13-
from strategy import Strategy
14-
from event import SignalEvent, OrderEvent
9+
from event import OrderEvent
1510
from backtest import Backtest
1611
from data import HistoricCSVDataHandler
1712
from execution import SimulatedExecutionHandler
1813
from portfolio import Portfolio
19-
14+
from Strategies.MovingAverageCrossStrategy import MovingAverageCrossStrategy
2015
## Tasks to do:
2116
## 1. Complete the sea turtle strategy
2217
## 2. Multiple strategies
@@ -47,66 +42,6 @@ def generate_naive_order(self, signal):
4742

4843
return order
4944

50-
51-
class MovingAverageCrossStrategy(Strategy):
52-
"""
53-
用来进行基本的移动平均跨越测录的实现,这个策略有一组短期和长期的简单移动平均值。
54-
默认的短期/长期的窗口分别是100天和400天。
55-
"""
56-
57-
def __init__(
58-
self, bars, events, short_window=100, long_window=400
59-
):
60-
self.bars = bars
61-
self.symbol_list = self.bars.symbol_list
62-
self.events = events
63-
self.short_window = short_window
64-
self.long_window = long_window
65-
66-
self.bought = self._calculate_initial_bought()
67-
68-
def _calculate_initial_bought(self):
69-
"""
70-
给bought字典增加键,对于所有的代码都设置值为OUT
71-
"""
72-
bought = {}
73-
for s in self.symbol_list:
74-
bought[s] = 'OUT'
75-
return bought
76-
77-
def calculate_signals(self, event):
78-
"""
79-
基于MAC SMA生成一组新的信号,进入市场的标志就是短期的移动平均超过
80-
长期的移动平均。
81-
"""
82-
if event.type == 'MARKET':
83-
for s in self.symbol_list:
84-
bars = self.bars.get_latest_bars_values(
85-
s, "adj_close", N=self.long_window
86-
)
87-
bar_date = self.bars.get_latest_bar_datetime(s)
88-
if bars is not None and bars != []:
89-
short_sma = np.mean(bars[-self.short_window:])
90-
long_sma = np.mean(bars[-self.long_window:])
91-
92-
symbol = s
93-
dt = datetime.datetime.utcnow()
94-
sig_dir = ""
95-
order_price = self.bars.get_latest_bars_values(s, 'adj_close')
96-
if short_sma > long_sma and self.bought[s] == "OUT":
97-
print("LONG: %s" % bar_date)
98-
sig_dir = 'LONG'
99-
signal = SignalEvent(1, bar_date, symbol, dt, sig_dir, order_price ,1.0)
100-
self.events.put(signal)
101-
self.bought[s] = 'LONG'
102-
elif short_sma < long_sma and self.bought[s] == "LONG":
103-
print("SHORT:%s" % bar_date)
104-
sig_dir = 'EXIT'
105-
signal = SignalEvent(1, bar_date, symbol, dt, sig_dir, order_price, 1.0)
106-
self.events.put(signal)
107-
self.bought[s] = 'OUT'
108-
109-
11045
if __name__ == "__main__":
11146
path1 = os.path.abspath('.')
11247
csv_dir = 'data_csv'
@@ -118,7 +53,7 @@ def calculate_signals(self, event):
11853
start_date = datetime.datetime(2015, 5, 1, 0, 0, 0)
11954
backtest = Backtest(
12055
csv_dir, symbol_list, initial_capital, heartbeat,
121-
start_date, HistoricCSVDataHandler, SimulatedExecutionHandler,
122-
My_portfolio, MovingAverageCrossStrategy
56+
start_date, data_handler_cls=HistoricCSVDataHandler, execution_handler_cls=SimulatedExecutionHandler,
57+
portfolio_cls=My_portfolio, strategy_cls=MovingAverageCrossStrategy
12358
)
12459
backtest.run_trading()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from Strategies.strategy import Strategy
2+
import numpy as np
3+
import datetime
4+
from event import SignalEvent
5+
6+
7+
class MovingAverageCrossStrategy(Strategy):
8+
"""
9+
用来进行基本的移动平均跨越测录的实现,这个策略有一组短期和长期的简单移动平均值。
10+
默认的短期/长期的窗口分别是100天和400天。
11+
"""
12+
13+
def __init__(
14+
self, bars, events, short_window=100, long_window=400
15+
):
16+
self.bars = bars
17+
self.symbol_list = self.bars.symbol_list
18+
self.events = events
19+
self.short_window = short_window
20+
self.long_window = long_window
21+
22+
self.bought = self._calculate_initial_bought()
23+
24+
def _calculate_initial_bought(self):
25+
"""
26+
给bought字典增加键,对于所有的代码都设置值为OUT
27+
"""
28+
bought = {}
29+
for s in self.symbol_list:
30+
bought[s] = 'OUT'
31+
return bought
32+
33+
def calculate_signals(self, event):
34+
"""
35+
基于MAC SMA生成一组新的信号,进入市场的标志就是短期的移动平均超过
36+
长期的移动平均。
37+
"""
38+
if event.type == 'MARKET':
39+
for s in self.symbol_list:
40+
bars = self.bars.get_latest_bars_values(
41+
s, "adj_close", N=self.long_window
42+
)
43+
bar_date = self.bars.get_latest_bar_datetime(s)
44+
if bars is not None and bars != []:
45+
short_sma = np.mean(bars[-self.short_window:])
46+
long_sma = np.mean(bars[-self.long_window:])
47+
48+
symbol = s
49+
dt = datetime.datetime.utcnow()
50+
sig_dir = ""
51+
order_price = self.bars.get_latest_bars_values(s, 'adj_close')
52+
if short_sma > long_sma and self.bought[s] == "OUT":
53+
print("LONG: %s" % bar_date)
54+
sig_dir = 'LONG'
55+
signal = SignalEvent(1, bar_date, symbol, dt, sig_dir, order_price, 1.0)
56+
self.events.put(signal)
57+
self.bought[s] = 'LONG'
58+
elif short_sma < long_sma and self.bought[s] == "LONG":
59+
print("SHORT:%s" % bar_date)
60+
sig_dir = 'EXIT'
61+
signal = SignalEvent(1, bar_date, symbol, dt, sig_dir, order_price, 1.0)
62+
self.events.put(signal)
63+
self.bought[s] = 'OUT'

Strategies/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)