按价格区间记录累积成交量
//假如以当天的开盘价为基数,以10个跳点为区间,计算每个区间的成交量 //1 Params integer set(10); Vars series<numeric> base_price; series<numeric> index; global map<Integer,numeric> vol_value; numeric temp; numeric temp_index; Defs Events OnInit() { printclear; } OnBar(ArrayRef<Integer> indexes) { print("------"); If(TRUEDATE(0)<>TRUEDATE(1))//判断当天第一根tick { mapClear(vol_value); printclear; vol_value[(open)/PriceScale] = vol; vol_value[(open+set*minmove*PriceScale)/PriceScale] = 0; //last_price = open; } Else { numeric val; Integer ret = MapUpperBound(vol_value, close); print("ret:"+text(ret)); If(ret==0)//需要往前插入 { integer key; GetMapKey(vol_value,0,key); numeric delt = roundup((key-close)/(set*minmove*pricescale),0); print("delta:"+text(delt)); integer i; for i = 1 to delt { If(i <> delt) { vol_value[key - i * minmove * PriceScale * set] = 0; }Else { vol_value[key - i * minmove * PriceScale * set] = vol; } } }else if(ret<>InvalidInteger)//中间插入 { integer key; GetMapKey(vol_value,ret-1,key); numeric val; GetMapValue(vol_value,ret-1,val); vol_value[key] = val+vol; }else //尾部插入 { array<integer> keys; GetMapKeys(vol_value,keys); numeric size = GetArraySize(keys); print("size:"+text(size)); integer bottom = keys[size-1]; print("bottom:"+text(bottom)); numeric delt = RoundDown((close-bottom)/(set*minmove*pricescale),0); print("delta:"+text(delt)); integer i; If(delt == 0)//只移动一格 { vol_value[bottom] = vol_value[bottom] + vol; vol_value[bottom+set*minmove*PriceScale] = 0; }Else//移动多格 { for i = 1 to delt { If(i <> delt) { vol_value[bottom + i * minmove * PriceScale * set] = 0; } Else { vol_value[bottom + i * minmove * PriceScale * set] = vol; } } vol_value[bottom+(delt+1)*set*minmove*PriceScale] = 0; } } } PlotString("mark",text(currentbar)); { print("close:"+text(close)); print("vol:"+text(vol)); print("cb:"+text(currentbar)); print("vol_value:" + TextMap(vol_value)); } }