Strategy vault / Tools & templates
Moving Average Bot 30 lines
"Talk is cheap. Show me the code" Just for learning, Be carefully when using in real market. Only run on digital currency exchanges. Note: this strategy included template. (function with $.) Beginers can start with…
"Talk is cheap. Show me the code"
Just for learning, Be carefully when using in real market.
Only run on digital currency exchanges.
Note: this strategy included template. (function with $.)
Beginers can start with this strategy, learn how to code, know the difference between backtesting and real market.
https://dn-filebox.qbox.me/fb4d0c7d773ca83e9d0230927705d419dc0bbeaa.png
Parameters
| Name | Default | What it controls |
|---|---|---|
| Slippage | 0.1 | Slippage |
| FastPeriod | 5 | fast line periods |
| SlowPeriod | 15 | slow line periods |
| EnterPeriod | 2 | observe periods |
| Interval | 120 | Interval time(second) |
Source JavaScript
/*backtest
start: 2020-10-11 00:00:00
end: 2020-10-11 23:59:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/
start = false
function main() {
Log('started');
var initAccount = _C(exchange.GetAccount);
var ticker = exchange.GetTicker();
var InitValue = (initAccount.Stocks + initAccount.FrozenStocks)*ticker.Last + initAccount.Balance + initAccount.FrozenBalance;
while (true) {
var records = _C(exchange.GetRecords, PERIOD_M15);
if(!start){
Log(records[0], records.length)
start = true
}
ticker =_C(exchange.GetTicker);
var FastRecords = TA.MA(records,FastPeriod);
var SlowRecords = TA.MA(records,SlowPeriod);
var NowAccount = _C(exchange.GetAccount);
var n = _Cross(FastRecords, SlowRecords);
if (n >= EnterPeriod && NowAccount.Balance > 0) {
var Price = _N(ticker.Sell+Slippage, 2);
var Amount = _N(0.99*NowAccount.Balance/Price, 3);
if(Amount>0.1){
var id = exchange.Buy(Price, Amount);
if(exchange.GetOrders(id).Status == ORDER_STATE_PENDING){exchange.CancelOrder(id);}
LogProfit((NowAccount.Stocks + NowAccount.FrozenStocks)*ticker.Last + NowAccount.Balance + NowAccount.FrozenBalance - InitValue);
}
}
if(n <= -EnterPeriod && NowAccount.Stocks > 0) {
var Price = _N(ticker.Buy-Slippage, 2);
var Amount = _N(NowAccount.Stocks, 3);
if(Amount>0.1){
var id = exchange.Sell(Price, Amount);
if(exchange.GetOrders(id).Status == ORDER_STATE_PENDING){exchange.CancelOrder(id);}
LogProfit((NowAccount.Stocks + NowAccount.FrozenStocks)*ticker.Last + NowAccount.Balance + NowAccount.FrozenBalance - InitValue);
}
}
if(!IsVirtual()){
Sleep(Interval*1000)
}
}
}Published by 小草 on the FMZ strategy square · last modified 2020-10-13. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a tested, production-ready module from strategies/Moving-Average-Bot-30-lines.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Binance Account Cross Transfer Small ToolJavaScript · Tools & templates
- Get cryptocurrency current and max supplyJavaScript · Tools & templates
- Interaction TemplateJavaScript · Tools & templates
- KuCoin Fund Transfer PluginJavaScript · Tools & templates
- OKEx V5 Candlestick Data Paginated Query ExampleJavaScript · Tools & templates
- Single Currency Dynamic Equilibrium V10 District LeaderJavaScript · Tools & templates · 1d