Files
strategies/一根均线-趋势-Demo.md
T

107 lines
2.2 KiB
Markdown
Raw Normal View History

2020-04-03 22:05:57 +08:00
2023-07-14 18:39:48 +08:00
> Name
2020-04-03 22:05:57 +08:00
2023-04-18 23:12:35 +08:00
一根均线-趋势-Demo
2020-04-03 22:05:57 +08:00
2023-07-14 18:39:48 +08:00
> Author
2020-04-03 22:05:57 +08:00
扁豆子
2023-07-14 18:39:48 +08:00
> Strategy Arguments
2020-04-03 22:05:57 +08:00
2023-07-14 18:39:48 +08:00
|Argument|Default|Description|
2020-04-03 22:05:57 +08:00
|----|----|----|
|Amount|100|Amount|
2023-07-14 18:39:48 +08:00
> Source (javascript)
2020-04-03 22:05:57 +08:00
``` javascript
// 定义对象
var e = exchange
e.SetContractType('XBTUSD')
var LastBarTime = 0
Idle = -1
status = Idle
// 链接交易所, 获取相关信息
function UpdateInfo() {
var account = exchange.GetAccount()
records = exchange.GetRecords()
ticker = exchange.GetTicker()
balance = account.Stocks
Bar = records[records.length - 1]
}
// 指标计算获取
function Get_MA() {
var MA = TA.MA(records, 30)
MA_close = MA[MA.length - 1]
}
// 开平仓规则
function onTick() {
if (LastBarTime !== Bar.Time) { // K线结束后进行交易
if (status === Idle) {
if (Bar.Close > MA_close) {
exchange.SetDirection("buy")
exchange.Buy(ticker.Sell, Amount)
status = PD_LONG
}
if (Bar.Close < MA_close) {
exchange.SetDirection("sell")
exchange.Sell(ticker.Buy, Amount)
status = PD_SHORT
}
}
if (status === PD_LONG) {
if (Bar.Close < MA_close) {
exchange.SetDirection("closebuy")
exchange.Sell(ticker.Buy, Amount)
exchange.SetDirection("sell")
exchange.Sell(ticker.Buy, Amount)
status = PD_SHORT
}
}
if (status === PD_SHORT) {
if (Bar.Close > MA_close) {
exchange.SetDirection("closesell")
exchange.Buy(ticker.Sell, Amount)
exchange.SetDirection("buy")
exchange.Buy(ticker.Sell, Amount)
status = PD_LONG
}
}
LastBarTime = Bar.Time
}
}
function main() {
// 主函数, 不停循环
while (1) {
// 链接交易所, 获取相关信息
UpdateInfo()
// 指标计算获取
Get_MA()
// 开平仓规则
onTick()
// 打印balance
LogStatus(balance)
// 轮询sleep时间
Sleep(5 * 1000)
}
}
```
2023-07-14 18:39:48 +08:00
> Detail
2020-04-03 22:05:57 +08:00
https://www.fmz.com/strategy/193609
2023-07-14 18:39:48 +08:00
> Last Modified
2020-04-03 22:05:57 +08:00
2020-04-17 16:37:51 +08:00
2020-04-11 21:28:30