帮我把下面一段C++代码改写成python代码:#include "Trade.h" #include "WPrice.h" #include <algorithm> double normalCDF(double x) // Phi(-∞, x) aka N(x) { return std::erfc(-x / std::sqrt(2)) / 2; //erfc()是互补误差函数,该返回值表示标准正态分布下var小于x的概率,即N(x) } CTrade::CTrade(double tick) : wp_bid(0.01), wp_ask(0.01), m_tick(tick), m_TimeRound(50) { newday(NULL); } CTrade::~CTrade() { } void CTrade::OnBook(const BTRec& btRec) { wp.setGamma(0.1); wp_bid = wp.getWP(&btRec.Bids); wp_ask = wp.getWP(&btRec.Asks); if (wp_mid > 0){ //wp_mid初始化为-1,仅遇到第一条BTRec记录时条件为false double wp_now = (wp_bid + wp_ask) / 2; //updated wp_mid int volume = btRec.volume; //volume between two orderbook records double ratio = normalCDF((wp_now - wp_mid) / (2 * m_tick)); //m_tick = tick = 0.2 double buyvolume = ratio*volume, sellvolume = (1 - ratio)*volume; m_TimeRound.update(buyvolume, sellvolume, btRec.rec_time.timestamp); //volume moving average if (mv_volume < 0) { mv_volume = volume; mv_buyvolume = buyvolume; } else{ mv_volume += 0.002*(volume - mv_volume); mv_buyvolume += 0.002*(buyvolume - mv_buyvolume); } // round trip volatility if (time_ini < 0 || btRec.rec_time.timestamp - time_ini >= time_scale){ if (time_ini>0){ double dp = wp_now - wp_ini; volatility += 0.05*(dp*dp - volatility); } time_scale = m_TimeRound.getTime() * 1000; //in milliseconds time_ini = btRec.rec_time.timestamp; wp_ini = wp_now; } } wp_mid = (wp_bid + wp_ask) / 2; } void CTrade::newday(const char* p) { wp_mid = -1; m_TimeRound.newday(p); volatility = 16 * m_tick*m_tick; time_slapse = -1, time_scale = -1, wp_ini = -1, time_ini = -1; mv_buyvolume = mv_volume = -1; }
时间: 2023-06-23 08:08:02 浏览: 197
```python
import math
class CTrade:
def __init__(self, tick):
self.wp_bid = 0.01
self.wp_ask = 0.01
self.m_tick = tick
self.m_TimeRound = TimeRound()
self.newday(None)
def OnBook(self, btRec):
self.wp.setGamma(0.1)
self.wp_bid = self.wp.getWP(btRec.Bids)
self.wp_ask = self.wp.getWP(btRec.Asks)
if self.wp_mid > 0:
wp_now = (self.wp_bid + self.wp_ask) / 2
volume = btRec.volume
ratio = normalCDF((wp_now - self.wp_mid) / (2 * self.m_tick))
buyvolume = ratio * volume
sellvolume = (1 - ratio) * volume
self.m_TimeRound.update(buyvolume, sellvolume, btRec.rec_time.timestamp)
if self.mv_volume < 0:
self.mv_volume = volume
self.mv_buyvolume = buyvolume
else:
self.mv_volume += 0.002 * (volume - self.mv_volume)
self.mv_buyvolume += 0.002 * (buyvolume - self.mv_buyvolume)
if self.time_ini < 0 or btRec.rec_time.timestamp - self.time_ini >= self.time_scale:
if self.time_ini > 0:
dp = wp_now - self.wp_ini
self.volatility += 0.05 * (dp * dp - self.volatility)
self.time_scale = self.m_TimeRound.getTime() * 1000
self.time_ini = btRec.rec_time.timestamp
self.wp_ini = wp_now
self.wp_mid = (self.wp_bid + self.wp_ask) / 2
def newday(self, p):
self.wp_mid = -1
self.m_TimeRound.newday(p)
self.volatility = 16 * self.m_tick * self.m_tick
self.time_slapse = -1
self.time_scale = -1
self.wp_ini = -1
self.time_ini = -1
self.mv_buyvolume = -1
self.mv_volume = -1
def normalCDF(x):
return math.erfc(-x / math.sqrt(2)) / 2
class TimeRound:
def __init__(self):
self.buy_volume = 0
self.sell_volume = 0
self.time = 0
def update(self, buyvolume, sellvolume, timestamp):
if timestamp > self.time:
self.buy_volume = 0
self.sell_volume = 0
self.time = timestamp
self.buy_volume += buyvolume
self.sell_volume += sellvolume
def getTime(self):
return self.buy_volume + self.sell_volume
```
阅读全文