package trade import ( "git.apinb.com/bsm-sdk/core/utils" "github.com/shopspring/decimal" ) // 计算收益及收益率 func calProfitRate(symbol, side string, leverage, avgPrice, currentPrice, volume decimal.Decimal) (decimal.Decimal, decimal.Decimal) { var profit decimal.Decimal = decimal.Zero var profitRate decimal.Decimal = decimal.Zero // 计算利润 switch side { case "LONG": profit = CalculateProfit_Long(avgPrice, currentPrice, volume) //Debug("CalculateProfit_Long", symbol, side, "AvgPrice:", avgPrice.String(), "CurrentPrice:", currentPrice.String(), "volume:", volume.String()) case "SHORT": //Debug("CalculateProfit_Short", symbol, side, "AvgPrice:", avgPrice.String(), "CurrentPrice:", currentPrice.String(), "volume:", volume.String()) profit = CalculateProfit_Short(avgPrice, currentPrice, volume) } // 计算回报率 if profit.IsZero() { return profit, profitRate } cost := avgPrice.Mul(volume) actualInvestment := cost.Div(leverage) profitRate = profit.Div(actualInvestment) return profit, profitRate } // 计算收益及收益率 func calProfitRate_V2(symbol, side string, leverage, avgPrice, currentPrice, volume float64) (profit, profitRate float64) { // 计算利润 switch side { case "LONG": profit = (currentPrice - avgPrice) * volume //Debug("CalculateProfit_Long", symbol, side, "AvgPrice:", avgPrice.String(), "CurrentPrice:", currentPrice.String(), "volume:", volume.String()) case "SHORT": //Debug("CalculateProfit_Short", symbol, side, "AvgPrice:", avgPrice.String(), "CurrentPrice:", currentPrice.String(), "volume:", volume.String()) profit = (avgPrice - currentPrice) * volume } // 计算回报率 if profit == 0 { return profit, profitRate } cost := avgPrice * volume actualInvestment := cost / leverage profitRate = profit / actualInvestment profitRate = utils.FloatRound(profitRate, 3) return profit, profitRate }