def reprice(h):
if h == 0:
return old_price
n_prev = h - check_prev
n_preprev = check_prev - check_preprev
ts_prev = times[h-1] - times[check_prev]
ts_preprev = times[check_prev] - times[check_preprev]
koef = (ts_preprev * n_prev)/(ts_prev * n_preprev+333) # division by zero !!!
price = old_price * koef
return price
Во-первых, нет никакого деления на ноль:
return old_price/4;
if n_prev == 0:
return old_price/4;
if n_preprev == 0:
return old_price*4;
Во вторых, s/+333//g, это оч большой костыль.
В третьих,
price = old_price*4;
if price < old_price/4:
price = old_price/4;
В четвертых, у тебя питон воспринимает koef как integer, а не float. То есть дробную часть откидывает. Я не очень дружу с питоном, но вот это исправило ситуацию
koef = koef * ts_preprev/ts_prev
koef = koef * n_prev/n_preprev
В-пятых, скорость появления блока < 600 секунд маловероятна.
Впрочем, это не важно, можешь оставить -130 .. 130
В шестых, вот этот кусок кода надо сделать вот таким образом:
checks.append(z)
print "==checkpoint by blocknum"
new_price = reprice(z)
check_preprev = check_prev
check_prev = z
else: ###### !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if ((times[z]-times[check_prev]) >= 1209600): ##### !!!!!!!!!!!!!!!1
checks.append(z)
print "==checkpoint by time: span=", times[z]-times[check_prev], " (tgt: 1209600)"
new_price = reprice(z)
check_preprev = check_prev
check_prev = z
ИТОГО:
import time, sys, random
random.seed(time.asctime())
#print random.randint(1,77)
last = 200000
winestim = 600
check_prev = 0
check_preprev = 0
prices = []
checks = []
times = []
deltas = []
start_price = 0.02
def set_time(b):
if b == 0:
return 1370000000
# print times[b-1] + winestim + random.randrange(-130, 130)
return int(times[b-1] + winestim + random.randrange(1, 90))
# return int(times[b-1] + winestim + 30)
def reprice(h):
print "=====checkpoint @", h
if h == 0:
return old_price
if h == 2016:
return old_price
n_prev = h - check_prev
n_preprev = check_prev - check_preprev
if n_prev ==0 and n_preprev ==0:
return old_price/4
if n_prev == 0:
return old_price/4
if n_preprev == 0:
return old_price * 4;
ts_prev = times[h-1] - times[check_prev]
ts_preprev = times[check_prev] - times[check_preprev]
print "check_prev=",check_prev," check_preprev=",check_preprev
print "nprev=", n_prev, " npprev=",n_preprev
print "tsprev=", ts_prev, " tspprev=",ts_preprev
koef = float(1)
koef = koef * ts_preprev/ts_prev
koef = koef * n_prev/n_preprev
print "k=",koef
price = old_price * koef
if price > old_price*4:
price = old_price*4
if price < old_price/4:
price = old_price/4
print "old=", old_price," new=", price
return price
old_price = start_price
for x in range(0, last):
times.append(0)
for z in range(0, last):
times[z] = set_time(z)
# if z !=0:
# print "Block #",z," time=", times[z], "diff=", times[z]-times[z-1]
if z < 2016:
continue
if ((z - check_prev) % 2016 == 0):
checks.append(z)
print "==checkpoint by blocknum"
new_price = reprice(z)
check_preprev = check_prev
check_prev = z
else:
if ((times[z]-times[check_prev]) >= 1209600):
checks.append(z)
print "==checkpoint by time: span=", times[z]-times[check_prev], " (tgt: 1209600)"
new_price = reprice(z)
check_preprev = check_prev
check_prev = z
if new_price != old_price:
old_price = new_price
print "====== repricing ======="
print "price =", old_price, "at block #", z
prices.append(old_price)
print "min & max prices were =", min(prices), max(prices)
print "checkpoints were : "
print checks
РЕЗУЛЬТАТ при равномерном распределении спроса (random) и стартовой цене 0.02, за 200к блоков
min & max prices were = 0.0200060868679 0.0211689919176
checkpoints were :
[2016, 3892, 5768, 7644, 9521, 11396, 13272, 15147, 17024, 18901, 20774, 22648, 24526, 26401, 28277, 30151, 32030, 33905, 35779, 37655, 39530, 41406, 43283, 45157, 47033, 48907, 50783, 52659, 54534, 56410, 58289, 60167, 62043, 63918, 65792, 67671, 69543, 71419, 73297, 75173, 77051, 78927, 80804, 82682, 84558, 86435, 88311, 90189, 92064, 93938, 95815, 97689, 99567, 101444, 103317, 105193, 107067, 108946, 110822, 112700, 114577, 116455, 118330, 120207, 122080, 123958, 125833, 127709, 129581, 131458, 133332, 135209, 137086, 138963, 140838, 142715, 144592, 146466, 148345, 150225, 152100, 153975, 155850, 157726, 159601, 161479, 163356, 165233, 167109, 168985, 170861, 172738, 174616, 176490, 178367, 180245, 182120, 183996, 185869, 187739, 189618, 191496, 193370, 195244, 197122, 198997]
Цена гуляла в течении 4 лет аж в пределах 10%. Но это без обратки. "Общественность" в виде функции random не "возмущалась" по поводу скачка цены, понижая спрос И вообще цена random() устраивала похоже По этому наблюдался стабильный рост.
Полный лог http://pastebin.com/2TvsE3pJ