版权声明:本文为博主原创文章,未经博主允许不得转载。
不考虑调仓
static void CalculateNetWorth(float on, NSArray *originalRatios , NSArray *originalNetWorths, NSArray *newNetWorth){
NSInteger len = originalRatios.count;
float k = [originalNetWorths[0] floatValue]/ ([newNetWorth[0] floatValue] *[originalRatios[0] floatValue]);
NSMutableArray *ms = [NSMutableArray arrayWithCapacity:originalRatios.count -1];
float p = 1.f;
for (int i = 1; i < len; i ++) {
CGFloat m = k *(([originalRatios[i] floatValue] * [newNetWorth[i] floatValue])/[originalNetWorths[i] floatValue]);
p += m;
[ms addObject:[NSNumber numberWithFloat:m]];
}
NSMutableArray *newRatios = [NSMutableArray arrayWithCapacity:originalRatios.count];
float r1 = 1.f/p;
[newRatios addObject:[NSNumber numberWithFloat:r1]];
for (NSNumber *px in ms) {
CGFloat newRatio = [px floatValue] *r1;
[newRatios addObject: [NSNumber numberWithFloat:newRatio]];
}
NSLog(@"最新比例\n%@",newRatios);
float yield = 0.f;
for (int i = 0; i < len; i ++) {
float yi = ([newNetWorth[i] floatValue] - [originalNetWorths[i] floatValue])/[originalNetWorths[i] floatValue];
yield += yi *[originalRatios[i] floatValue];
}
NSLog(@"最新日收益\n%f",yield);
float nn = on *(1+yield);
NSLog(@"最新净值\n%.4f",nn);
}
入参为:
t-1的组合净值
t-1成分比例
t-1成分净值
t+0的成分净值
CalculateNetWorth(1.f,
@[@0.2,@0.3,@0.5],
@[@11.7,@10.74,@18.95],
@[@11.79,@10.66,@19.12]);