0bps

Strategy vault / Other

Account Balance Change Email Alerts Supports Adding Multiple Exchanges

Detect changes in currency and money in the account balance and send them to the designated email address. This does not support backtesting Previously, Fetion's SMS was frozen, and because too many people used it…

JavaScriptOther
Author
Zero
Complexity
Intermediate · 70 lines
Parameters
11

Detect changes in currency and money in the account balance and send them to the designated email address. This does not support backtesting Previously, Fetion's SMS was frozen, and because too many people used it, the account was frozen. It couldn't use the SMS interface anymore, so it switched to email.

Parameters

NameDefaultWhat it controls
LoopInterval10Detection interval (seconds))
AlertMode0Reminder methods: Change reminder | Condition alarm | Minimum value alarm
MaxDiffCNYfalseMinimum change in money
MaxDiffCoinfalseMinimum coin fluctuation
MinCoinfalseMinimum coin value
MinCNYfalseMinimum value of money
SMTPServersmtp.163.comSMTPServer
SMTPUser[email protected]Sending email (SMTP username)
SMTPPass***Email password (SMTP password))
SendMode0Receiving email: Self | Other
DstMail[email protected]Recipient's email

Source JavaScript


function Notify(msg) {
    var ret = Mail(SMTPServer, SMTPUser, SMTPPass, SendMode == 0 ? SMTPUser : DstMail , msg, "Balance changes " + msg);
    if (ret) {
        Log("Email notification successful");
    } else {
        Log("Email notification failed");
    }
    Log(ret ? "Email sent successfully" : "Email sending failed");
    return ret;
}

function GetAccount(print) {
    var all = {
        Balance : 0,
        Stocks  : 0,
    };
    var currency = exchange.GetCurrency();
    for (var i = 0; i < exchanges.length; i++) {
        if (exchanges[i].GetCurrency() != currency) {
            throw "Different currencies";
        }
        var account;
        while (!(account = exchanges[i].GetAccount())) {
            Sleep(1000);
        }
        all.Stocks += (account.Stocks + account.FrozenStocks);
        all.Balance += (account.Balance + account.FrozenBalance);
        if (typeof(print) != 'undefined' && print) {
            Log(exchanges[i].GetName(), "Money: ", (account.Balance + account.FrozenBalance), "Currency: ", (account.Stocks + account.FrozenStocks));
        }
    }

    return all;
}

function main() {
    // Disable rate auto convert
    exchange.SetRate(1);
    if (Version() < 2.7) {
        throw "Only supports version 2.7 or above";
    }
    var preAccount = GetAccount(true);
    if (!Notify("Strategy started successfully, Total Money: " + preAccount.Balance + ", Currency: " + preAccount.Stocks)) {
        throw "Exit";
    }
    Log("Initial information: ", "Total Money:", preAccount.Balance, "Total Coins:", preAccount.Stocks);
    var alertAlrelady = false;
    while (true) {
        Sleep(LoopInterval * 1000);
        var account = GetAccount();
        if (AlertMode == 2) {
            if ((MinCoin > 0 && account.Stocks < MinCoin) || (MinCNY > 0 && account.Balance < MinCNY)) {
                if (!alertAlrelady) {
                    Log(account);
                    Notify(exchange.GetName() + "Insufficient funds Total money: " + account.Balance + ", Currency: " + account.Stocks);
                    alertAlrelady = true;
                }
            } else {
                alertAlrelady = false;
            }
        } else if (account.Stocks != preAccount.Stocks || account.Balance != preAccount.Balance) {
            if (AlertMode == 0 || (MaxDiffCoin > 0 && Math.abs(account.Stocks - preAccount.Stocks) >= MaxDiffCoin) || (MaxDiffCNY > 0 && Math.abs(account.Balance - preAccount.Balance) >= MaxDiffCNY)) {
                Log(account);
                preAccount = account;
                Notify(exchange.GetName() + "The account changes to total money: " + account.Balance + ", Currency: " + account.Stocks);
            }
        }
    }
}

Published by Zero on the FMZ strategy square · last modified 2014-12-24. 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.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Account-Balance-Change-Email-Alerts-Supports-Adding-Multiple-Exchanges.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open