Files
strategies/经典均线策略.md
T

109 lines
3.1 KiB
Markdown
Raw Normal View History

2018-12-08 12:40:55 +08:00
2023-07-14 18:39:48 +08:00
> Name
2018-12-08 12:40:55 +08:00
经典均线策略
2023-07-14 18:39:48 +08:00
> Author
2018-12-08 12:40:55 +08:00
Zero
2023-07-14 18:39:48 +08:00
> Strategy Description
2018-12-08 12:40:55 +08:00
"Talk is cheap. Show me the code"
教学性质, 实盘慎用.
注: ` 策略使用了交易模板类库`
`希望新手从此策略入门, 一步步学习编写策略, 并体验到模拟与真实环境对交易系统的影响`
https://dn-filebox.qbox.me/fb4d0c7d773ca83e9d0230927705d419dc0bbeaa.png
2023-07-14 18:39:48 +08:00
> Strategy Arguments
2018-12-08 12:40:55 +08:00
2023-07-14 18:39:48 +08:00
|Argument|Default|Description|
2018-12-08 12:40:55 +08:00
|----|----|----|
|FastPeriod|5|入市快线周期|
|SlowPeriod|15|入市慢线周期|
|EnterPeriod|2|入市观察期|
|ExitFastPeriod|7|离市快线周期|
|ExitSlowPeriod|15|离市慢线周期|
|ExitPeriod|true|离市观察期|
|PositionRatio|0.8|仓位比例|
|StopLossRatio|0.05|止损比率|
|Interval|10|轮询周期(秒)|
2023-07-14 18:39:48 +08:00
> Source (javascript)
2018-12-08 12:40:55 +08:00
``` javascript
2020-01-02 17:51:10 +08:00
/*backtest
start: 2019-01-01 00:00:00
end: 2019-07-01 00:00:00
period: 1d
exchanges: [{"eid":"Bitfinex","currency":"BTC_USD"}]
*/
2018-12-08 12:40:55 +08:00
function main() {
var STATE_IDLE = -1;
var state = STATE_IDLE;
var entryPrice = 0;
var initAccount = _C(exchange.GetAccount);
Log(initAccount);
while (true) {
if (state === STATE_IDLE) {
var n = $.Cross(FastPeriod, SlowPeriod);
if (Math.abs(n) >= EnterPeriod) {
var account = _C(exchange.GetAccount);
var ticker = _C(exchange.GetTicker);
var obj = n > 0 ? $.Buy(_N(account.Balance * PositionRatio / ticker.Sell, 3)) : $.Sell(_N(account.Stocks * PositionRatio, 3));
if (obj) {
opAmount = obj.amount;
entryPrice = obj.price;
state = n > 0 ? PD_LONG : PD_SHORT;
Log("开仓详情", obj, "交叉周期", n);
}
}
} else {
var n = $.Cross(ExitFastPeriod, ExitSlowPeriod);
var needCover = Math.abs(n) >= ExitPeriod && ((state === PD_LONG && n < 0) || (state === PD_SHORT && n > 0));
if (needCover) {
Log("离市平仓");
} else {
var ticker = _C(exchange.GetTicker);
if (state === PD_LONG) {
if (ticker.Buy < entryPrice*(1-StopLossRatio)) {
needCover = true;
Log("止损平仓");
}
} else {
if (ticker.Sell > entryPrice*(1+StopLossRatio)) {
needCover = true;
Log("止损平仓");
}
}
}
if (needCover) {
var nowAccount = _C(exchange.GetAccount);
var obj = state === PD_LONG ? $.Sell(_N(nowAccount.Stocks - initAccount.Stocks, 3)) : $.Buy(_N(initAccount.Stocks - nowAccount.Stocks, 3));
state = STATE_IDLE;
nowAccount = _C(exchange.GetAccount);
LogProfit(nowAccount.Balance - initAccount.Balance, '钱:', nowAccount.Balance, '币:', nowAccount.Stocks, '平仓详情:', obj, "交叉周期", n);
}
}
Sleep(Interval*1000);
}
}
```
2023-07-14 18:39:48 +08:00
> Detail
2018-12-08 12:40:55 +08:00
https://www.fmz.com/strategy/12348
2023-07-14 18:39:48 +08:00
> Last Modified
2018-12-08 12:40:55 +08:00
2020-01-02 17:51:10 +08:00
2019-10-08 17:00:12