gdex-witness plan to adopt below algorithm for bitCNY price feeding, the core idea comes from PID control theory.
Pdex:BTS price in DEX in bitCNY
Pf: current feed price
premium: current premium
Premium0: premium in the last period
Tp=4;Ti=6; Td=1; ##parameters for PID control, here the Ti plays a key role, setting it to 6 will make i reach maximum in 20 hours if the premium keeps 2% or higher.
get Pdex, Pf, premium;
premium0=premium;
p=Tp*premium;
i= Pf/Pdex-p-1; ##set initial value for p,i,d to make the initial calculated feed price equal to the current feed price in system.
d=0;
while True:
get Pdex, Pf, premium;
while (10 minutes passed after the last execution):
i+=premium/Ti;
if i>0.4: ##set limit to i, to speed up the backward adjustment if premium change to cross 0
i=0.4;
if i<-0.3:
i=-0.3;
d=Td*(premium-premium0);
premium0=premium;
p=Tp*premium;
if p+i+d>0:
feed price = Pdex*min(1+p+i+d, 1.5, 1.2*Pf/Pdex) ##limit the feed price not higher than 1.5 times Pdex or 1.2 times current feed price while the adjustment is upward.
else:
feed price = Pdex*max(1+p+i+d, 0.91*Pf/Pdex)##limit the feed price no less than 0.91 times current feed price while the adjustment is downward.