Strategy vault / Tools & templates
Separate Profit Calculation Code from Zero
Income can be put together with the strategy, but it is better to be independent. Reasons: 1. When the trading strategy change is interrupted, the income will not be reset. 2. The income calculation itself will call…
Income can be put together with the strategy, but it is better to be independent. Reasons: 1. When the trading strategy change is interrupted, the income will not be reset. 2. The income calculation itself will call API functions, which often causes strategy API network errors and affects transaction operation. Being independent can reduce this possibility. 3. You can customize the cycle interval.
Source JavaScript
function adjustFloat(v) {
return Math.floor(v*1000)/1000;
}
function GetAccount() {
var account;
while (!(account = exchange.GetAccount())) {
Sleep(1000);
}
return account;
}
function GetTicker() {
var ticker;
while (!(ticker = exchange.GetTicker())) {
Sleep(1000);
}
return ticker;
}
function updateProfit(accountInit, accountNow, ticker) {
var netNow = accountNow.Balance + accountNow.FrozenBalance + ((accountNow.Stocks + accountNow.FrozenStocks) * ticker.Buy);
var netInit = accountInit.Balance + accountInit.FrozenBalance + ((accountInit.Stocks + accountInit.FrozenStocks) * ticker.Buy);
LogProfit(adjustFloat(netNow - netInit), accountNow);
}
function main() {
InitAccount = GetAccount();
while (true) {
updateProfit(InitAccount, GetAccount(), GetTicker());
Sleep(5000);
}
}Published by 小草 on the FMZ strategy square · last modified 2016-05-10. 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/Separate-Profit-Calculation-Code-from-Zero.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
- Moving Average Bot 30 linesJavaScript · Tools & templates · 1d
- OKEx V5 Candlestick Data Paginated Query ExampleJavaScript · Tools & templates