Beispiel #1
0
 def ClearStock(self, code):  #以当前市场价清空对应的股票
     if code in self.StockCodeList:
         tempDir = {}
         for dir in self.position:
             if dir[POS_SCo] == code:
                 tempDir = dir
                 break
         Fac = PriceLoopFactory()
         loop = Fac.GetPriceLoop()
         pricedata = loop.GetPriceData(code)
         CurrentPrice = float(pricedata['price'])
         ableCount = tempDir[POS_ABa]
         if ableCount == 0:
             return False
         self.balance[BAL_AFu] = self.balance[
             BAL_AFu] + CurrentPrice * ableCount  #增加可用资金
         self.balance[BAL_CBa] = self.balance[
             BAL_CBa] + CurrentPrice * ableCount  #增加资金余额
         self.__S_ReduceStock(CurrentPrice, ableCount, code)
         self.CalAllData()
         self.RemoveEmptyStock()
         logger.info('卖出股票%s成功', code)
         self.XMLLog.TodayLog.SellOper(code, ableCount, CurrentPrice)
         return True
     else:
         inofr = self.Number + '未拥有股票:' + code + ' 无法卖出。'
         logger.info(inofr)
         return False
Beispiel #2
0
 def CalAllData(self):
     Fac = PriceLoopFactory()
     loop = Fac.GetPriceLoop()
     StockMarketValue = 0
     for dir in self.position:
         pricedata = loop.GetPriceData(dir[POS_SCo])
         if not type(pricedata) == type(0):
             if not (dir[POS_SBa]
                     == dir[POS_ABa] + dir[POS_FQu] + dir[POS_QOTW]):
                 logger.warning('股票数量不对,股票余额:%d 可用余额:%d 冻结数量%d 在途数量:%d ',
                                dir[POS_SBa], dir[POS_ABa], dir[POS_FQu],
                                dir[POS_QOTW])
                 logger.warning('程序自动更正股票余额。')
             dir[POS_SBa] = dir[POS_ABa] + dir[POS_FQu] + dir[POS_QOTW]
             dir[POS_MPr] = float(pricedata['price'])
             dir[POS_SNa] = pricedata['name']
             dir[POS_MVa] = dir[POS_MPr] * dir[POS_SBa]
             dir[POS_PLo] = dir[POS_MVa] - dir[POS_GPr] * dir[POS_SBa]
             if dir[POS_SBa] == 0 or dir[POS_GPr] == 0:
                 dir[POS_PLR] = 0
             else:
                 dir[POS_PLR] = dir[POS_PLo] / (dir[POS_GPr] *
                                                dir[POS_SBa]) * 100
             StockMarketValue = StockMarketValue + dir[POS_MVa]
     self.balance[BAL_RMV] = StockMarketValue
     self.balance[BAL_TAs] = StockMarketValue + self.balance[BAL_CBa]
Beispiel #3
0
 def __S_CheckMarketPrice(self, price, count, stockcode):  #检查当前价格是否可以成功交易
     Fac = PriceLoopFactory()
     loop = Fac.GetPriceLoop()
     pricedata = loop.GetPriceData(stockcode)
     if not type(pricedata) == type(0):
         if price <= float(pricedata['price']):
             return True
         else:
             logger.warning('出价太高,无法以%f元卖出%s,股票当前价%s.', price, stockcode,
                            pricedata['price'])
     return False
Beispiel #4
0
 def __B_CheckMarketPrice(self, price, stockcode):  #当前价格是否可以买入
     PriceConditions = False
     Fac = PriceLoopFactory()
     loop = Fac.GetPriceLoop()
     pricedata = loop.GetPriceData(stockcode)
     if not type(pricedata) == type(0):
         if price >= float(pricedata['price']):
             return True
         else:
             logger.warning('出价太低,无法以%f元购入%s,股票当前价%s.', price, stockcode,
                            pricedata['price'])
     return False
Beispiel #5
0
def GetCDP(code):
    Fac = PriceLoopFactory()
    loop = Fac.GetPriceLoop()
    pricedata = loop.GetFrontDayData(code)
    if len(pricedata) == 0:
        return False
    high = float(pricedata['high'])
    low = float(pricedata['low'])
    close = float(pricedata['close'])
    cdp = (high + low + 2 * close) / 4
    AH = cdp + high - low
    NH = cdp * 2 - low
    NL = cdp * 2 - high
    AL = cdp + low - high
    return [AH, NH, cdp, NL, AL]
Beispiel #6
0
 def __TrackAllStock(self):
     if self.stockCDPListMutex.acquire(1):
         try:
             for key in self.stockCDPList:
                 if not self.CheckUnusefuleCount(key):
                     continue
                 datadir = self.stockCDPList[key]
                 CurrentPrice = float(
                     PriceLoopFactory().GetPriceLoop().GetPriceData(
                         key)['price'])
                 if not CurrentPrice == 0:
                     self.__TrackStockPrice(datadir['code'], datadir['up'],
                                            datadir['down'], datadir['Aup'],
                                            datadir['Adown'],
                                            datadir['time'], CurrentPrice)
                 else:
                     self.UnunsfulCodeTimes[
                         key] = self.UnunsfulCodeTimes[key] + 1
                     logger.warning('无法获得' + key + '的价格。')
             #logger.info('进入操作程序。')
             self.UserAccount.OperationByCDPState(self.stockStateList)
             self.PrintUserData()
         except:
             logger.error('__TrackAllStock 出错退出。', exc_info=True)
         finally:
             self.stockCDPListMutex.release()
Beispiel #7
0
 def __CheckLoopHearBrat(self):
     newCount = PriceLoopFactory().GetPriceLoop().GetLoopCount()
     if newCount - self.__FrontLoopCount == 0:
         print('price loop 心跳停止。')
         self.__StopHeartBeat = self.__StopHeartBeat + 1
     else:
         self.__StopHeartBeat = 0
         self.__FrontLoopCount = newCount
     if self.__StopHeartBeat > 10:
         print('price loop 心跳停止超过10次。')
         self.Loopflag = False
Beispiel #8
0
 def __init__(self, threadID, StockCode):  #
     threading.Thread.__init__(self)
     self.threadID = threadID
     PriceLoopFactory().GetPriceLoop().AddStockCode(StockCode)
     self.stockCDPListMutex = threading.Lock()
     self.stockCDPList = {}
     self.stockStateList = {}
     self.AddStockData(StockCode)
     self.ProcePrint = MmapPrint.MmapClass('TrackStock')
     self.UnunsfulCodeTimes = {}  #记录在循环中有多少次没有成功读取到价格
     self.__FrontLoopCount = 0  #上一次的心跳
     self.__StopHeartBeat = 0  #停止心跳的次数
     self.Loopflag = True
     self.UserDataFlag = False
     self.UserAccount = AccountGroup()
     self.UserAccount.FInitionAccount(500)
     self.curtime = datetime.datetime.now()
     self.MorningBeginTime = datetime.datetime.strptime(
         self.curtime.strftime('%Y-%m-%d') + ' 9:29:00',
         '%Y-%m-%d %H:%M:%S')
Beispiel #9
0
 def run(self):  #
     self.Loopflag = True
     print("开始线程:")
     if not StockTimer.TodayIsTradingday():
         print('今天不是交易日,退出。')
         self.Loopflag = False
     else:
         print('今天是开盘日,进入下一步。')
     self.__FrontLoopCount = PriceLoopFactory().GetPriceLoop().GetLoopCount(
     )
     while (self.Loopflag):
         self.curtime = datetime.datetime.now()
         if ((StockTimer.TradTime(self.curtime) == TimeType.Mon)
                 or (StockTimer.TradTime(self.curtime) == TimeType.Aft)):
             #正常交易时间内
             self.ProcePrint.write_mmap_info(
                 self.curtime.strftime('%Y-%m-%d %H:%M:%S'))
             self.__TrackAllStock()
             time.sleep(3)
             self.__CheckLoopHearBrat()
         elif (StockTimer.TradTime(self.curtime) == TimeType.Pre):
             deltaTime = self.MorningBeginTime - self.curtime
             print('早上还未开市,等待开市。', deltaTime.seconds, '秒后重试。', '开市时间',
                   self.MorningBeginTime.strftime('%Y-%m-%d %H:%M:%S'),
                   '当前时间', self.curtime.strftime('%Y-%m-%d %H:%M:%S'))
             #time.sleep(deltatime.seconds)
             time.sleep(10)
             continue
         elif (StockTimer.TradTime(self.curtime) == TimeType.Clo):
             self.Loopflag = False
             self.UserAccount.ReFlushPrice()  #收盘刷新所有股票价格
             self.UserAccount.UnfreezingAll()  #收盘后将所有股票解冻
             self.UserAccount.FSaveToXML()  #
             print('今天已经收盘.解冻所有股票并退出。')
         elif (StockTimer.TradTime(self.curtime) == TimeType.Noo):
             print('午间休市期间.等待10秒重试。',
                   self.curtime.strftime('%Y-%m-%d %H:%M:%S'))
             time.sleep(10)
     print("退出线程:\n")