Skip to content

Commit f816251

Browse files
author
phoenix2082
committed
First working demo
1 parent c0fe923 commit f816251

10 files changed

+25819
-0
lines changed

app.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from flask import Flask, render_template, url_for, Response
2+
import pandas as pd
3+
4+
app = Flask(__name__)
5+
6+
7+
8+
@app.route("/msa")
9+
def moving_average_crossover():
10+
sma = 20 # specify duration for short term moving average
11+
lma = 50 # specify duration for long term moving average
12+
13+
dummysec = pd.read_csv('./dummysec.csv')
14+
15+
dummysec['SMA'] = dummysec['Close Price'].rolling(sma).mean()
16+
dummysec['LMA'] = dummysec['Close Price'].rolling(lma).mean()
17+
dummysec['SMA2'] = dummysec['Close Price'].shift(1).rolling(sma).mean()
18+
dummysec['LMA2'] = dummysec['Close Price'].shift(1).rolling(lma).mean()
19+
20+
# logic to generate 'buy' and 'sell' signals
21+
dummysec['Buy'] = dummysec.apply(lambda x: x['Close Price'] if x['SMA'] > x[
22+
'LMA'] and x['SMA2'] < x['LMA2'] else 0,
23+
axis=1)
24+
25+
dummysec['Sell'] = dummysec.apply(lambda y: -y['Close Price'] if y['SMA'] <
26+
y['LMA'] and y['SMA2'] > y['LMA2'] else 0,
27+
axis=1)
28+
29+
dsec = dummysec[(dummysec['Buy'] != 0.00) | (dummysec['Sell'] != 0.00)]
30+
result = dsec[['Close Price', 'Date', 'Buy', 'Sell']].to_json(orient='records')
31+
return Response(result, mimetype='application/json')
32+
33+
@app.route("/")
34+
def index():
35+
return render_template("index.html")

dummysec.csv

+770
Large diffs are not rendered by default.

static/axios.min.js

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/macs.js

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
'use strict';
2+
3+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4+
5+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6+
7+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
8+
9+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
10+
11+
var ReactTable = window.ReactTable.default;
12+
13+
var MovingAverageCrossoverTable = function (_React$Component) {
14+
_inherits(MovingAverageCrossoverTable, _React$Component);
15+
16+
function MovingAverageCrossoverTable(props) {
17+
_classCallCheck(this, MovingAverageCrossoverTable);
18+
19+
var _this = _possibleConstructorReturn(this, (MovingAverageCrossoverTable.__proto__ || Object.getPrototypeOf(MovingAverageCrossoverTable)).call(this, props));
20+
21+
_this.state = { data: [] };
22+
return _this;
23+
}
24+
25+
_createClass(MovingAverageCrossoverTable, [{
26+
key: 'componentDidMount',
27+
value: function componentDidMount() {
28+
var _this2 = this;
29+
30+
axios.get('/msa').then(function (response) {
31+
_this2.setState({
32+
data: response.data
33+
});
34+
}).catch(function (error) {
35+
// handle error
36+
console.log(error);
37+
}).finally(function () {
38+
// always executed
39+
});
40+
}
41+
}, {
42+
key: 'render',
43+
value: function render() {
44+
var columns = [{
45+
Header: 'Date',
46+
accessor: 'Date', // String-based value accessors!
47+
Cell: function Cell(props) {
48+
return React.createElement(
49+
'div',
50+
{ style: { textAlign: 'center' } },
51+
React.createElement(
52+
'span',
53+
null,
54+
props.value
55+
)
56+
);
57+
}
58+
}, {
59+
Header: 'Close Price',
60+
accessor: 'Close Price', // String-based value accessors!
61+
Cell: function Cell(props) {
62+
return React.createElement(
63+
'div',
64+
{ style: { textAlign: 'center' } },
65+
React.createElement(
66+
'span',
67+
{ className: 'number' },
68+
props.value
69+
)
70+
);
71+
}
72+
}, {
73+
Header: 'Buy',
74+
accessor: 'Buy',
75+
width: 200,
76+
Cell: function Cell(props) {
77+
return React.createElement(
78+
'div',
79+
{ style: {
80+
//width: `${props.value}%`,
81+
height: '100%',
82+
backgroundColor: props.value > 0 ? '#85cc00' : '#FFFFFF',
83+
borderRadius: '2px',
84+
textAlign: 'center'
85+
}
86+
},
87+
React.createElement(
88+
'span',
89+
{ className: 'number' },
90+
props.value
91+
)
92+
);
93+
} // Custom cell components!
94+
}, {
95+
Header: 'Sell',
96+
accessor: 'Sell',
97+
width: 200,
98+
Cell: function Cell(props) {
99+
return React.createElement(
100+
'div',
101+
{ style: {
102+
// width: `${props.value}%`,
103+
height: '100%',
104+
backgroundColor: props.value < 0 ? '#ff2e00' : '#ffffff',
105+
borderRadius: '2px',
106+
textAlign: 'center'
107+
}
108+
},
109+
React.createElement(
110+
'span',
111+
{ className: 'number' },
112+
props.value
113+
)
114+
);
115+
} // Custom cell components!
116+
}];
117+
118+
return React.createElement(ReactTable, { data: this.state.data, columns: columns,
119+
defaultPageSize: 10 });
120+
}
121+
}]);
122+
123+
return MovingAverageCrossoverTable;
124+
}(React.Component);
125+
126+
var domContainer = document.querySelector('#macs');
127+
128+
ReactDOM.render(React.createElement(MovingAverageCrossoverTable, null), domContainer);

0 commit comments

Comments
 (0)