凯利公式测试

package com.djh.test.stock;

import java.util.Random;

public class Kelly {
	static Random random = new Random();
	public static double probalitity() {
		Stat stat = new Stat(Stat.ARITHMETIC, 1, -44.5d, 44.5d);
		stat.setLen(5);
		for(int i=0;i<10000;i++){
			stat.add(random.nextGaussian());
		}
		stat.show();
		return 0;
	}

	public static void kelly() {
		Stat stat = new Stat(Stat.ARITHMETIC, 20, 100d, 250d);
		Stat stat1 = new Stat(Stat.ARITHMETIC, 10, -100d, 100d);
		stat1.setLen(7);
		final double win = 0.01;//猜对盈利
		final double lost = 0.01;//猜错输率
		final double inFee = 0.0005;//入仓费率
		final double outFee = 0.0015;//清仓费率
		final int n = 100000;
		int s = 0;//交易次数
		for(int j=0;j<n;j++){
			double base = 100;
			boolean isRemain = false;//是否在仓
			for(int i=0;i<250;i++){
				double p = Math.random();
				double f1 = (win*p-lost*(1-p))/(win*lost);
				stat1.add(f1);
				if(f1>0){
					if(!isRemain){
						base = base*(1-inFee);
						isRemain = true;
						s++;
					}
					
					if(Math.random()<p){
						base = (1+win) * base;
					}else {
						base = base*(1-lost);
					}
				}else {
					if(isRemain){
						base = base*(1-outFee);
						isRemain = false;
					}
				}
			}
			stat.add(base);
		}
		stat.show();
		stat1.show();
		System.out.println(s/100000);
		//8620
	}
	/**
	 * 最多5%
	 * 3%
	 * @param args
	 */
	public static void main(String[] args) {
		//probalitity();
		kelly();
	}

}

package com.djh.test.stock;

import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

/**
 * @author djh
 *
 */
public class Stat {

	public final static Integer EXPONENT = 1;
	public final static Integer ARITHMETIC = 2;
	private double max;
	private double min;
	private int len = 0;
	
	private Map<Double, Integer> map = new TreeMap<Double,Integer>();
	public Stat(int slope,int inc,Double min,Double max) {
		
		this.max = max;
		this.min = min;
		for(double i=min;;){
			map.put(i, 0);
			double copyI = i;
			if(slope==EXPONENT){
				i = i*inc;
			}else {
				i = i+inc;
			}
			if(i>max){
				this.max = copyI;
				break;
			}
		}
//		if(EXPONENT == slope){
//			map.put(0d, 0);
//			this.min = 0d;
//		}
	}
	public void add(double data) {
		boolean flag = false;
		double last = min;
		Integer lastVal = map.get(min);
		for(Map.Entry<Double, Integer> entry:map.entrySet()){
			if(data<entry.getKey()){
				map.put(last, lastVal+1);
				flag = true;
				break;
			}else {
				last = entry.getKey();
				lastVal = entry.getValue();
			}
		}
		if(!flag){
			map.put(max, map.get(max)+1);
		}
	}
	public void show() {
		StringBuffer s = new StringBuffer("|");
		StringBuffer v = new StringBuffer("|");
		if(len == 0){
			len = String.valueOf(max).length();
			if(max>=1000000.0){
				len = 9;
			}
		}
		int t = 0;
		for(Entry<Double, Integer> entry:map.entrySet()){//"%"+len+"d"
			s.append(String.format("%"+len+"s",entry.getKey().toString())).append("|");
			v.append(String.format("%"+len+"s",entry.getValue().toString())).append("|");
			t += entry.getValue();
		}
		System.out.println("总数为:"+t);
		System.out.println(s);
		System.out.println(v);
	}
	
	public void setLen(int len) {
		this.len = len;
	}
	
	public static void main(String[] args) {
		Stat stat = new Stat(Stat.EXPONENT,10,1d,1e10d);
		for(int i=0;i<1000;i++){
			double k = Math.pow(10, Math.random()*9*Math.random());
			System.out.println(k);
			stat.add(k);
		}
		stat.show();
	}
	
	
	
	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值