-
Notifications
You must be signed in to change notification settings - Fork 40
/
strategy-dingtou.js
125 lines (103 loc) · 4.22 KB
/
strategy-dingtou.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var ETF = require('./data-r40/500ETF.json');
var baseAccount = 100000; //初始账户钱:10W
var baseShou = 10; //每次交易购买10手
var sumAccount = 100000; // 账户钱:10W
var sumStoke = 0; // 账户总手数,一手100股,初始为0(有多少股)
var sumValue = 0; // 账户总价值(账户股票对应价值)
var sumGainLossRate = 0; // 账户总盈亏
var sumFee = 0; // 总交易费用
var tradeType = 0; //交易类型: 1,2,3,4
var chicangRate = 0; //持仓盈亏比
var chicangStokeAvgCost = 0; //股票成本价
var lastPrice = 0; //股票最后交易日价格
var dingtouBase = 1000; // 定投金额(元),每次购股花费
var dingtouCycle = 20; // 定投周期(日),五个交易日为一周,20个交易日为一月
var clearCount = 0; //清仓次数
// var startTime = '2013-03-30';
// var startTime = '2014-03-30';
var startTime = '2015-03-30';
// var startTime = '2016-03-30';
// var startTime = '2017-03-30';
// var startTime = '2018-03-30';
var endTime = "2017-01-01";
// var endTime = "2018-03-30";
/**
* 每月(20个交易日),定投
*/
var qtAvg = function () {
for (let index = 0; index < ETF.length; index++) {
const element = ETF[index];
lastPrice = element.close;
let stoke = 0;//当前交易日购股数量
let oneUnitStoke = Math.round(dingtouBase / (lastPrice * 100)); //一个单位的交易数量
let startChicangStokeAvgCost = chicangStokeAvgCost; // 交易前持仓成本价
let startChicangRate = 0;
if (chicangStokeAvgCost == 0) {
startChicangRate = 0;
} else {
startChicangRate = ((lastPrice - chicangStokeAvgCost) / chicangStokeAvgCost).toFixed(3) - 0; // 交易钱持仓盈亏比
}
// 日期优先
if (element.time_key < startTime) {
continue;
}
if (element.time_key > endTime) {
break;
}
if (sumStoke == 0) {
tradeType = "空仓状态,加购1个单位"
stoke = oneUnitStoke;
chicangRate = 0;
chicangStokeAvgCost = lastPrice;
} else {
if (index % dingtouCycle == 0 && sumAccount > 0) {
stoke = oneUnitStoke;
tradeType = "周期到了,加购1个单位";
chicangStokeAvgCost = (sumStoke * chicangStokeAvgCost + stoke * lastPrice) / (sumStoke + stoke);
chicangStokeAvgCost = chicangStokeAvgCost.toFixed(4) - 0;
chicangRate = (lastPrice - chicangStokeAvgCost) / chicangStokeAvgCost;
chicangRate = chicangRate.toFixed(4) - 0;
}
}
// 未做交易
if (stoke == 0 || sumAccount < dingtouBase * 1.2) {
continue
}
sumStoke = sumStoke + stoke;
sumFee = sumFee + 5;
sumAccount = sumAccount - (stoke * 100 * lastPrice) - 5;
sumValue = sumAccount + sumStoke * 100 * lastPrice;
sumGainLossRate = (((sumValue - baseAccount) / baseAccount) * 100).toFixed(3) + '%';
var res = `
----${element.time_key} (${index})--${tradeType}--${oneUnitStoke} (手)----
***交易前概览***
交易前成本价:${ startChicangStokeAvgCost}
当日价格:${ lastPrice}
交易前持仓总盈亏比: ${(startChicangRate * 100).toFixed(3) + '%'}
***当日交易***
今日交易数:${stoke > 0 ? '+' + stoke : stoke}
交易后成本价:${ chicangStokeAvgCost}
交易后持仓总盈亏比:${(chicangRate * 100).toFixed(3) + '%'}
***当日结算***
账户总持仓:${ sumStoke} 手(${sumStoke * 100 * chicangStokeAvgCost}元)
账户总现金:${ sumAccount} 元
账户总价值:${ sumValue} 元
账户总盈亏:${ sumValue - baseAccount} 元
账户总盈亏比例:${ sumGainLossRate}
交易手续费: ${sumFee}
`
console.log(res)
}
}
qtAvg();
console.log(`-------${startTime}~${endTime}--定投+止盈 ${ETF[0].code}-------`)
console.log(`当日价格:${lastPrice}`)
console.log(`交易后成本价:${chicangStokeAvgCost}`);
console.log(`账户总持仓:${sumStoke}手 (${sumStoke * 100 * chicangStokeAvgCost}元)`);
console.log(`账户总现金:${sumAccount} 元`)
console.log(`账户总价值:${sumValue} 元`)
console.log(`账户总盈亏:${sumValue - baseAccount} 元`)
console.log(`账户总盈亏比例:${sumGainLossRate}`)
console.log('交易手续费:', sumFee)
console.log('清仓次数: ', clearCount)
console.log(``)