static int LsLuaShmReplace(lua_State *L) { const char *tag = "ls.shared.add"; char namebuf[0x100]; int numElem = 3; LsShmHash *pShared; pShared = LsLuaShmGetHash(L, tag, numElem, namebuf, 0x100); if (pShared) { ls_luashm_t *pShmValue = LsLuaShmFind(pShared, namebuf); if ((pShmValue) && (!isTimeExpired(pShmValue))) return LsLuaShmSetHelper(L, pShared, numElem, namebuf); else { LsLuaApi::pushboolean(L, 0); LsLuaApi::pushstring(L, "not found"); LsLuaApi::pushboolean(L, 0); return 3; } } else { LsLuaApi::pushboolean(L, 0); LsLuaApi::pushstring(L, "bad parameters"); LsLuaApi::pushboolean(L, 0); return 3; } }
int SmallOrder::onMktUpdate(MarketUpdate &mkt) { switch (state) { case OrderState::Init: { halfSpread = calHalfSpread(mkt); arrivalOrderPT = CedarTimeHelper::getCurPTime(); activeLeg = Starting; updateLeg(SwitchSignal::Initial); placeOrder(mkt); LOG(INFO) << "snap half spread: " << halfSpread; logStatusInfo(mkt); break; } case Ready: { placeOrder(mkt); break; } //TODO: move time expiration checking to a separate timer event case Sent: { double currentMid = (mkt.bid_price(0) + mkt.ask_price(0)) * 0.5; double currentPrice = calPlacePrice(mkt); if (isMaxTimeExpired()) { LOG(INFO) << "max order life reached, cancelling..."; cancelOrder(true); } else if (isTimeExpired() || isMktMoved(currentMid)) { LOG(INFO) << "time expired or market moved, switching legs..."; LOG(INFO) << "current mid: " << currentMid; LOG(INFO) << "last mid: " << lastLegMid; updateLeg(SwitchSignal::TimeExpired); cancelOrder(); } else if (placePrice != currentPrice) { LOG(INFO) << "price expired, recalculating limit price..."; cancelOrder(); } break; } case TerminateCancel: default: return 0; } return 0; }
static int LsLuaShmIncr(lua_State *L) { const char *tag = "ls.shared.incr"; char namebuf[0x100]; int numElem = 2; LsShmHash *pShared; pShared = LsLuaShmGetHash(L, tag, numElem, namebuf, 0x100); if (pShared) { ls_luashm_t *pShmValue = LsLuaShmFind(pShared, namebuf); if ((pShmValue) && (!isTimeExpired(pShmValue))) { if (pShmValue->m_type == ls_luashm_long) { pShmValue->m_ulong++; LsLuaApi::pushinteger(L, pShmValue->m_ulong); LsLuaApi::pushnil(L); } else if (pShmValue->m_type == ls_luashm_double) { pShmValue->m_double += 1.0; LsLuaApi::pushnumber(L, pShmValue->m_double); LsLuaApi::pushnil(L); } else { LsLuaApi::pushnil(L); LsLuaApi::pushstring(L, "not a number"); } return 2; } else { LsLuaApi::pushnil(L); LsLuaApi::pushstring(L, "not found"); return 2; } } LsLuaApi::pushnil(L); LsLuaApi::pushstring(L, "bad parameters"); return 2; }
int LsLuaShmFlushExpCb(LsShmHash::iteroffset iterOff, void *pUData) { ls_luashm_scanner_t *pScanner = (ls_luashm_scanner_t *)pUData; LsShmHash::iterator iter = pScanner->handle->offset2iterator(iterOff); ls_luashm_t *pVal = (ls_luashm_t *)iter->getVal(); if ((iter->getValLen() != sizeof(ls_luashm_t)) || (pVal->m_iMagic != LS_LUASHM_MAGIC)) return 0; if (isTimeExpired(pVal)) { pVal->m_iMagic = 0; if (pVal->m_iValueLen > sizeof(double)) pScanner->handle->release2(pVal->m_iOffset, pVal->m_iValueLen); pScanner->handle->eraseIterator(iterOff); ++pScanner->numchecked; if ((pScanner->maxcheck) && (pScanner->numchecked >= pScanner->maxcheck)) return 1; } return 0; }
static int LsLuaShmGetHelper(lua_State *L, int checkExpired) { const char *ptr, *tag = "ls.shared.get_helper"; char namebuf[0x100]; int numElem = 2; LsShmHash *pShared; ls_luashm_t *pVal; pShared = LsLuaShmGetHash(L, tag, numElem, namebuf, 0x100); if (pShared == NULL) { LsLuaApi::pushnil(L); LsLuaApi::pushstring(L, "not a shared OBJECT"); return 2; } pVal = LsLuaShmFind(pShared, namebuf); if (pVal == NULL) { LsLuaApi::pushnil(L); LsLuaApi::pushstring(L, "not found"); return 2; } if (checkExpired && isTimeExpired(pVal)) { LsLuaApi::pushnil(L); LsLuaApi::pushstring(L, "expired"); return 2; } switch (pVal->m_type) { case ls_luashm_long: LsLuaApi::pushinteger(L, pVal->m_ulong); break; case ls_luashm_double: LsLuaApi::pushnumber(L, (lua_Number)pVal->m_double); break; case ls_luashm_string: if (pVal->m_iValueLen > sizeof(double)) { ptr = (const char *)pShared->offset2ptr(pVal->m_iOffset); LsLuaApi::pushlstring(L, ptr, pVal->m_iValueLen); } else { LsLuaApi::pushlstring(L, (const char *)pVal->m_ucharArray, pVal->m_iValueLen); } break; case ls_luashm_boolean: if (pVal->m_boolean) LsLuaApi::pushboolean(L, 1); else LsLuaApi::pushboolean(L, 0); break; case ls_luashm_nil: default: LsLuaApi::pushnil(L); LsLuaApi::pushstring(L, "not a shared value type"); return 2; } if (checkExpired == 0) { LsLuaApi::pushinteger(L, pVal->m_iFlags); if (isTimeExpired(pVal)) LsLuaApi::pushboolean(L, 1); return 3; } if (pVal->m_iFlags) { LsLuaApi::pushinteger(L, pVal->m_iFlags); return 2; } else return 1; }