金融相关JS

//补仓计算
define(['math', 'jquery'], function (math, $) {
    'use strict';
    //获取数据源
    // $.ajax({
    //     url: '',

    // });
    // 12月中邮
    /*var data = [
        {date: '2016.01.01', val: '4.5230', num: '100'},
        {date: '2016.01.02', val: '4.5160', num: '100'},
        {date: '2016.01.03', val: '4.5620', },
        {date: '2016.01.04', val: '4.9630'},
        {date: '2016.01.05', val: '5.0080'},
        {date: '2016.01.05', val: '5.1030'},
        {date: '2016.01.05', val: '5.0900'},
        {date: '2016.01.05', val: '5.1850'},
        {date: '2016.01.05', val: '5.1540'},
        {date: '2016.01.05', val: '5.1570'},
        {date: '2016.01.05', val: '5.1360'},
        {date: '2016.01.05', val: '5.2260'},
        {date: '2016.01.05', val: '5.2670'},
        {date: '2016.01.05', val: '5.1990'},
        {date: '2016.01.05', val: '4.4860'}
    ];*/

    var datas = [
        {date: '2016.12.28', net: '1.0799', num: '189.98', money: '200'},
        {date: '2016.12.28', net: '1.0939', num: '182.61', money: '200'},
    ];

    //4.714
    /**
     * [get_average_net_value 取得平均净值]
     * @param  {[type]} data [description]
     * @return {[type]}      [description]
     */
    var get_average_net = function (data) {
        var len = data.length, count_num = 0, count_money = 0, count_net = 0, net = 0;
        data.forEach(function (item, index) {
            count_num = math.add(count_num, item.num);
            count_money = math.add(count_money, item.money);
            count_net = math.add(count_net, item.net);       
        });
        //单份净值成本
        net = parseFloat(math.div(count_money, count_num)).toFixed(6);

        return {
            //单份净值成本
            net: net,
            //平均净值
            average_net: parseFloat(math.div(count_net, data.length)).toFixed(4)
        };
    }

    console.log('净值计算', get_average_net(datas));




    //降低成本 



    //复利计算
    /**
     * [get_compound_interest    复利计算]
     * @param  {[type]} capital [本金 1000           ]
     * @param  {[type]} rate    [利率 1%             ]
     * @param  {[type]} time    [时间长度 如365天 1年]
     * @return {[type]}         [description]  
     */
    var get_compound_interest = function (capital, rate, time) {
        var res = capital * Math.pow((1+rate/100), time),
            profit = parseFloat(math.sub(res, capital)).toFixed(6),
            profit_per = parseFloat(math.div(profit, capital)*100).toFixed(6);
        return {
            //到期后本金+收益
            capital_count: parseFloat(res).toFixed(6),
            //到期后的收益
            profit: profit,
            //收益率
            profit_per: profit_per+'%'
        };
    };

    var res = get_compound_interest(45000, 0.05, 365); //{capital_count: "54007.173480", profit: "9007.173480", profit_per: "20.015941%"}
    console.log('复利计算', res);
    // console.log('复利计算', get_compound_interest(100, 0.1, 365), math.sub(get_compound_interest(100, 0.1, 365), 100));


    var datas2 = [
       {date: '2016-12-27', capital: 100, rate: '0.1'},
       {date: '2016-12-28', capital: 100, rate: '0.1'},
       {date: '2016-12-29', capital: 0, rate: '0.1'},
       {date: '2016-12-30', capital: 0, rate: '0.1'},
       {date: '2016-12-31', capital: 0, rate: '0.1'},
       {date: '2017-01-01', capital: 0, rate: '0.1'},
       {date: '2017-01-02', capital: 0, rate: '0.1'},
       {date: '2017-01-03', capital: 0, rate: '0.1'},
       {date: '2017-01-04', capital: 0, rate: '0.1'},
       {date: '2017-01-05', capital: 100, rate: '0.1'}

    ];

    /**
     * [get_fixed_compound_interest 定投复利计算]
     * @param  {[type]} capital [本金]
     * @param  {[type]} rate    [利率]
     * @param  {[type]} time    [时间]
     * @return {[type]}         [description]
     */
    var get_fixed_compound_interest = function (datas) {
            //收益+本金和  此处的叠加需要扣除暂停交易日的本金累计
        var capital_count = 0, 
            //本金和
            count = 0,
            profit_tmp = 0;

        datas.forEach(function (item, index) {
            //累计本金和
            capital_count = math.add(item.capital, capital_count);
            //累计复利总和 本金+收益
            count = math.add(count, item.capital);
            profit_tmp = get_compound_interest(count, item.rate, 1);
            count = profit_tmp.capital_count;  
        });
        var profit = math.sub(count, capital_count),
            date_count = datas.length,
            profit_per = parseFloat(math.div(profit, capital_count)*100).toFixed(6);
        return {
            //投资总时间
            date_count: date_count,
            //本金和
            capital_count: capital_count,
            //本金+收益和
            count: parseFloat(count).toFixed(6),
            //利润
            profit: parseFloat(profit).toFixed(6),
            //收益率
            profit_per: profit_per+'%',
            //日均收益
            profit_day: parseFloat(math.div(profit, date_count)).toFixed(6)
        }; 
    };

    //console.log('复利计算', get_compound_interest(100, 0.1, 365), math.sub(get_compound_interest(100, 0.1, 365), 100));

    console.log('定投复利计算', get_fixed_compound_interest(datas2));

    //套利


    var datas = [
        {date: '2016.12.28', net: '1.0799', num: '189.98', money: '200'},
        {date: '2016.12.28', net: '1.0939', num: '182.61', money: '200'},
    ];

    var get_counts = function (data) {
        var count_num = 0, count_money = 0, profit = 0, ransom, capital;
        data.forEach(function (item, index) {
            count_num = math.add(count_num, item.num);
            count_money = math.add(count_money, item.money);
        });

        return {
            //份额总数
            num: count_num,
            //净值总数
            //net: count_net,
            //金额总数
            money: count_money,
            //投资总天数
            date: data.lenght 
        };
    }

    //赎回获利
    var get_ransom = function (num, money, net, ransom_rate) {
        //1.收益 = 本金收益 - 购买成本 
        var profit = 0, ransom, capital;

        //赎回时的总金额 净值*份额
        capital = math.mul(net*num);
        //赎回费
        ransom = math.mul(capital, ransom_rate);

        //收益 总金额-总成本
        profit = math.sub(capital, money);
        //最终到手的资金
        profit = math.sub(profit, ransom);

        return profit;

    }

    var counts = get_counts(datas);
    //console.log(get_ransom(counts.num, counts.money, '1.11111', '0.5'));

    //大涨大跌   小涨小跌
    // console.log(get_average_net_value(data));        
});

// 1.关于收益率
// 假如你有100万元,收益100%后资产达到200万元,如果接下来亏损50%,则资产回到100万元,显然亏损50%比赚取100%要容易得多。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值