Exemple #1
0
def update_obj_list(pCurrentDataRow):
    l_obj = ticks_values_to_be_stored()
    l_obj.AskP[0] = float(pCurrentDataRow[colNumberOfData.AskP0])
    l_obj.AskQ[0] = float(pCurrentDataRow[colNumberOfData.AskQ0])
    l_obj.BidP[0] = float(pCurrentDataRow[colNumberOfData.BidP0])
    l_obj.BidQ[0] = float(pCurrentDataRow[colNumberOfData.BidQ0])
    l_obj.AskP[1] = float(pCurrentDataRow[colNumberOfData.AskP1])
    l_obj.AskQ[1] = float(pCurrentDataRow[colNumberOfData.AskQ1])
    l_obj.BidP[1] = float(pCurrentDataRow[colNumberOfData.BidP1])
    l_obj.BidQ[1] = float(pCurrentDataRow[colNumberOfData.BidQ1])
    l_obj.AskP[2] = float(pCurrentDataRow[colNumberOfData.AskP2])
    l_obj.AskQ[2] = float(pCurrentDataRow[colNumberOfData.AskQ2])
    l_obj.BidP[2] = float(pCurrentDataRow[colNumberOfData.BidP2])
    l_obj.BidQ[2] = float(pCurrentDataRow[colNumberOfData.BidQ2])
    l_obj.AskP[3] = float(pCurrentDataRow[colNumberOfData.AskP3])
    l_obj.AskQ[3] = float(pCurrentDataRow[colNumberOfData.AskQ3])
    l_obj.BidP[3] = float(pCurrentDataRow[colNumberOfData.BidP3])
    l_obj.BidQ[3] = float(pCurrentDataRow[colNumberOfData.BidQ3])
    l_obj.AskP[4] = float(pCurrentDataRow[colNumberOfData.AskP4])
    l_obj.AskQ[4] = float(pCurrentDataRow[colNumberOfData.AskQ4])
    l_obj.BidP[4] = float(pCurrentDataRow[colNumberOfData.BidP4])
    l_obj.BidQ[4] = float(pCurrentDataRow[colNumberOfData.BidQ4])
    
    l_obj.currentTimeStamp = common.convertTimeStampFromStringToFloat(pCurrentDataRow[colNumberOfData.TimeStamp])
    l_obj.LTP = float(pCurrentDataRow[colNumberOfData.LTP])
    l_obj.TTQ = float(pCurrentDataRow[colNumberOfData.TTQ])
    l_obj.MsgCode = pCurrentDataRow[colNumberOfData.MsgCode]
    l_obj.OrderType = pCurrentDataRow[colNumberOfData.OrderType]
    l_obj.NewP = float(pCurrentDataRow[colNumberOfData.NewP])
    l_obj.NewQ = float(pCurrentDataRow[colNumberOfData.NewQ])
    if(l_obj.MsgCode == "M"):
        l_obj.OldP = float(pCurrentDataRow[colNumberOfData.OldP])
        l_obj.OldQ = float(pCurrentDataRow[colNumberOfData.OldQ])
    return l_obj
Exemple #2
0
def update_obj_list(pCurrentDataRow):
    l_obj = ticks_values_to_be_stored()
    l_obj.AskP = float(pCurrentDataRow[colNumberOfData.AskP0])
    l_obj.AskQ = float(pCurrentDataRow[colNumberOfData.AskQ0])
    l_obj.BidP = float(pCurrentDataRow[colNumberOfData.BidP0])
    l_obj.BidQ = float(pCurrentDataRow[colNumberOfData.BidQ0])
    l_obj.currentTimeStamp = common.convertTimeStampFromStringToFloat(
        pCurrentDataRow[colNumberOfData.TimeStamp])
    l_obj.LTP = float(pCurrentDataRow[colNumberOfData.LTP])
    l_obj.TTQ = float(pCurrentDataRow[colNumberOfData.TTQ])
    l_obj.MsgCode = float(pCurrentDataRow[colNumberOfData.MsgCode])
    l_obj.OrderType = float(pCurrentDataRow[colNumberOfData.OrderType])
    l_obj.NewP = float(pCurrentDataRow[colNumberOfData.NewP])
    l_obj.NewQ = float(pCurrentDataRow[colNumberOfData.NewPNewQ])
    l_obj.OldP = float(pCurrentDataRow[colNumberOfData.OldP])
    l_obj.OldQ = float(pCurrentDataRow[colNumberOfData.OldQ])
    return l_obj
def extractAttributeFromDataMatrix(args):
   if args.n == None:
      N = 5
   else:
      N = int(args.n) 
   try:
      #Value of argument c can be taken as BidP0 and AskP0 
      args.c
   except:
      print "Since -c has not been specified I cannot proceed"
      os._exit()
   if args.m == None:
       M = 1
   else:
       M = int(args.m.split(".")[0])
       M1 = int(args.m.split(".")[1])
       print "Values of N and N1 are ", M, M1
     
   PIP_SIZE = 5
      
   colNumberOfAttribute = eval("colNumberOfData."+ args.c )
   colNumberOfOppositeAttribute = eval("colNumberOfData.BestBidP") if "ask" in args.c else eval("colNumberOfData.BestAskP")
   colNumberOfTimeStamp = colNumberOfData.TimeStamp
   if "ask" in args.c.lower():
      colAttributeRowNo = [colNumberOfData.BestAskP1, colNumberOfData.BestAskP2]#, colNumberOfData.BestAskP2, colNumberOfData.BestAskP3, colNumberOfData.BestAskP4] 
      colAttributeRowNoBand = [colNumberOfData.AskP1, colNumberOfData.AskP2]
   else:
      colAttributeRowNo = [colNumberOfData.BestBidP1, colNumberOfData.BestBidP2]#, colNumberOfData.BestBidP2, colNumberOfData.BestBidP3, colNumberOfData.BestBidP4]
      colAttributeRowNoBand = [colNumberOfData.BidP1, colNumberOfData.BidP2]
   numberOfRowsInLastNSecs = 0
   queueOfValuesInLastNQty = []
   lenQ = 0
   totalOfQty = 0.0
   currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
   lengthOfDataMatrix = len(dataFile.matrix)
   while (currentRowNumberForWhichFeatureValueIsBeingCalculated < lengthOfDataMatrix):
      totalOfRowsInLastNQty = 0.0
      totalOfQtyInLastNQty = 0.0
      cellValueTotal = 0
      colAttributeRow = map(float, [dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][i] for i in colAttributeRowNo])
      colAttributeRowBand = map(float, [dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][i] for i in colAttributeRowNoBand])
      timeOfCurrentRow = common.convertTimeStampFromStringToFloat(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfTimeStamp],args.cType)
      oppositeAttribute = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfOppositeAttribute])
      if  currentRowNumberForWhichFeatureValueIsBeingCalculated != 0:
          previousRowPrice = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated - 1][colNumberOfAttribute])
      else:
          previousRowPrice = 0
      currentPrice = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfAttribute])
      currentLTP = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.LTP])
      currentMsgCode = dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.MsgCode]
      newQty = int(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.NewQ])
      newPrice = int(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.NewP])
      oldQty = int(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.OldQ])
      oldPrice = int(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.OldP])
      if ( currentMsgCode == 'M' and ( newPrice in colAttributeRow ) and ((newPrice > oldPrice) if "bid" in args.c.lower() else (newPrice < oldPrice))):
          cellValueTotal = newPrice * newQty
          totalOfQty += newQty
          cellValueQty = newQty
          queueOfValuesInLastNQty.append([cellValueTotal, cellValueQty, colAttributeRowBand[0]])
          lenQ += 1
      elif ( currentMsgCode == 'N' and ( newPrice in colAttributeRow )):
          cellValueTotal = newPrice * newQty
          totalOfQty += newQty
          cellValueQty = newQty
          queueOfValuesInLastNQty.append([cellValueTotal, cellValueQty, colAttributeRowBand[0]])
          lenQ += 1
          
      if totalOfQty > N:
          while totalOfQty > N:
              excess = totalOfQty - N
              if queueOfValuesInLastNQty[0][1] < excess:
                  totalOfQty -= queueOfValuesInLastNQty[0][1]
                  queueOfValuesInLastNQty = queueOfValuesInLastNQty[1:]
                  lenQ -= 1
              else:
                  totalOfQty -= excess
                  price = float(queueOfValuesInLastNQty[0][0]) / queueOfValuesInLastNQty[0][1]
                  queueOfValuesInLastNQty[0][0] = price * (queueOfValuesInLastNQty[0][1] - excess)
                  queueOfValuesInLastNQty[0][1] -= excess
      
      cell = lenQ - 1
      if cell >= 0:
          while ((queueOfValuesInLastNQty[cell][2] > colAttributeRowBand[0] - M * PIP_SIZE and colAttributeRowBand[0] > queueOfValuesInLastNQty[cell][2] - M1 * PIP_SIZE) and ("bid" in args.c.lower())) or ((queueOfValuesInLastNQty[cell][2] < colAttributeRowBand[0] + M * PIP_SIZE and colAttributeRowBand[0] < queueOfValuesInLastNQty[cell][2] + M1 * PIP_SIZE) and ("ask" in args.c.lower())):
              totalOfRowsInLastNQty += queueOfValuesInLastNQty[cell][0]
              totalOfQtyInLastNQty +=  queueOfValuesInLastNQty[cell][1]
              cell -= 1
              if cell < 0:
                  break
              
      exPrice = colAttributeRowBand[0] - M * PIP_SIZE if "bid" in args.c.lower() else colAttributeRowBand[0] + M * PIP_SIZE
      exQty = N - totalOfQtyInLastNQty
      wtdAvg = float(totalOfRowsInLastNQty + exQty * exPrice) / N
           
      attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][0] = common.convertTimeStampFromStringToDecimal(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfTimeStamp],args.cType)
      attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][1] = wtdAvg # in 1st iteration currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
      attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][2] = str(currentPrice) + ";" + str(colAttributeRowBand[0]) + ";" + str(cellValueTotal) + ";" + str(currentMsgCode) + ";" + str(totalOfQtyInLastNQty) + ";" + str(totalOfRowsInLastNQty)
      currentRowNumberForWhichFeatureValueIsBeingCalculated += 1
      continue     # Since we are going back 1 row from current we cannot get data from current row
      print "Processed row number " + str(currentRowNumberForWhichFeatureValueIsBeingCalculated)
   
   lNameOfFeaturePrinted = "fMarketMNTSumOfCol" + args.c + "InLast" + str(args.n) + "Qty" + "WithDiff" + str(args.m) + "Pip"
   return [ "TimeStamp", lNameOfFeaturePrinted , args.c , "LTP" , "TradedQtyWhichIs"+ args.c , "MsgCode", "totalQty", "totalCellValue"]
Exemple #4
0
def getDataFileAndPredictionsIntoObjectList(dataFileObject,buyPredictFileObject,sellPredictFileObject,lMinOfExitCl):
    global gNoOfLineReadPerChunk,gTickSize
    lObjectList = []
    lCurrentDataRowCount = 0
    lPrevObj = None
    fileHasHeader = 1
    headerSkipped = 0
    dataFileSep = ";"
    predictFileSep = ","
    lListOfBidP = []
    lListOfAskP = []
    l_data_row_list =  list(islice(dataFileObject,10000))
    while True:
        lDataFileRowsList = list(islice(dataFileObject,gNoOfLineReadPerChunk))
        lBuyPredictedFileRowList = list(islice(buyPredictFileObject,gNoOfLineReadPerChunk))
        lSellPredictedFileRowList = list(islice( sellPredictFileObject,gNoOfLineReadPerChunk ))
        if not lDataFileRowsList :
            print("Finished reading file")
            lObjectList.append(lPrevObj)    
            lPrevObj = None          
            break
        lengthOfDataList = len(lDataFileRowsList)
        lengthOfBuyPredList = len(lBuyPredictedFileRowList)
        lengthOfSellPredList = len(lSellPredictedFileRowList)
        if lengthOfDataList!=lengthOfBuyPredList or lengthOfBuyPredList!=lengthOfSellPredList:
            print("Length of data file and predicted buy and sell values file are not same ")
            os._exit(-1)
        for currentRowIndex in range(lengthOfDataList):
            if(fileHasHeader == 1 and headerSkipped != 1):
                headerSkipped =1
                continue
            lDataRow = lDataFileRowsList[currentRowIndex].rstrip().split(dataFileSep)
            if((args.e).find("nsefut") >= 0):
                lAskP = float(lDataRow[colNumberOfData.BestAskP])
                lBidP = float(lDataRow[colNumberOfData.BestBidP])
                lAskQ = int(float(lDataRow[colNumberOfData.BestAskQ]))
                lBidQ = int(float(lDataRow[colNumberOfData.BestBidQ]))
            else:
                lAskP = float(lDataRow[colNumberOfData.AskP0])
                lBidP = float(lDataRow[colNumberOfData.BidP0])
                lAskQ = int(lDataRow[colNumberOfData.AskQ0])
                lBidQ = int(lDataRow[colNumberOfData.BidQ0])
            lTTQ = int(lDataRow[colNumberOfData.TTQ])
            lLTP = float(lDataRow[colNumberOfData.LTP]) 
            lCurrentDataRowTimeStamp = common.convertTimeStampFromStringToFloat(lDataRow[colNumberOfData.TimeStamp])
            lBuyPredictedRow = lBuyPredictedFileRowList[currentRowIndex].rstrip().split(predictFileSep)
            lBuyPredictedTimeStamp = float(lBuyPredictedRow[1])
            lBuyPredictedValue = float(lBuyPredictedRow[2])
            lSellPredictedRow = lSellPredictedFileRowList[currentRowIndex].rstrip().split(predictFileSep)
            lSellPredictedTimeStamp = float(lSellPredictedRow[1])
            lSellPredictedValue = float(lSellPredictedRow[2])
            
            if( lCurrentDataRowTimeStamp != lBuyPredictedTimeStamp or lBuyPredictedTimeStamp!=lSellPredictedTimeStamp):
                lDataRow = lDataFileRowsList[currentRowIndex].rstrip().split(dataFileSep)
                print('Time stamp of data row with predicted value is not matching .\n Data row time stamp :- ' , lCurrentDataRowTimeStamp,'BuyPredicted Time Stamp :- ' , lBuyPredictedTimeStamp\
                      ,"SellPredicted Time Stamp :- ",lSellPredictedTimeStamp)
                os._exit(-1)
            else:
                lObj = Tick(lCurrentDataRowTimeStamp,lAskP,lBidP,lAskQ,lBidQ,lLTP,lTTQ,lBuyPredictedValue,lSellPredictedValue)
                if lPrevObj!=None:
                    lPrevObj.TTQChange  = lObj.TTQ - lPrevObj.TTQ
                    lPrevObj.NextLTP = lObj.LTP
                    if(lListOfAskP != [] and lPrevObj.AskP != lListOfAskP[0]):
                        lPrevObj.askPChangedInBetweenLastTickAndCurrentTick = 1
                    if(lListOfBidP != [] and lPrevObj.BidP != lListOfBidP[0]):
                        lPrevObj.askPChangedInBetweenLastTickAndCurrentTick = 1
                    lObjectList.append(lPrevObj)  
                    lListOfBidP = [lPrevObj.BidP] 
                    lListOfAskP = [lPrevObj.AskP]
                lPrevObj = lObj
            if lCurrentDataRowCount%50000 ==0:
                print("Completed reading ",lCurrentDataRowCount)
            lCurrentDataRowCount = lCurrentDataRowCount + 1 
    return lObjectList
Exemple #5
0
def readOnceAndWrite(pFileName, entryCL1 , entryCL2 , entryCL3 , entryCL4 , exitCL1 , exitCL2 , exitCL3 , exitCL4 , predictedValuesDict):
    global g_bestqty_list_for_sell, g_bestqty_list_for_buy
    attribute.initList()
    tradeStats = dict()
    tradeStats['totalSellAmountShort'] = 0
    tradeStats['totalBuyAmountShort'] = 0
    tradeStats['totalSellAmountLong'] = 0
    tradeStats['totalBuyAmountLong'] = 0
    tradeStats['NumberOfOpenBuy'] = 0
    tradeStats['NumberOfCloseSell'] = 0
    tradeStats['NumberOfOpenSell'] = 0
    tradeStats['NumberOfCloseBuy'] = 0
    tradeStats['currentPositionShort'] = 0
    tradeStats['currentPositionLong'] = 0
    noPredictionForThisRow = 0
    currentSellPredictedValue = 0
    currentBuyPredictedValue = 0
    currentIndex = 0
    l_previous_obj = None
    l_obj= None
    print("Processing the data file for trades :")
    attribute.initList()
    print("EntryLevels",entryCL1 , entryCL2 , entryCL3 , entryCL4 , exitCL1 , exitCL2 , exitCL3 , exitCL4)
    for currentDataRow in dataFile.matrix:
        
        l_obj = update_obj_list(currentDataRow)
        if(l_previous_obj != None):
            lReturnList = checkIfPreviousDecisionToEnterOrExitTradeWasSuccessful( l_obj , l_previous_obj , tradeStats )
            
            lDummyBidQ0 = lReturnList[0]
            lDummyAskQ0 = lReturnList[1]
            lDummyTTQForBuy = lReturnList[2]
            lDummyTTQForSell = lReturnList[3]
            lBuyTradedPrice = lReturnList[4]
            lBuyTradedQty = lReturnList[5]
            lSellTradedPrice = lReturnList[6]
            lSellTradedQty = lReturnList[7]
            if currentIndex > 0:
                if(g_bestqty_list_for_buy != {}):
                    l_best_bidq = g_bestqty_list_for_buy['qty']
                    l_best_bidp = g_bestqty_list_for_buy['price']
                else:
                    l_best_bidq = 0
                    l_best_bidp = 0
                if(g_bestqty_list_for_sell != {}):
                    l_best_askq = g_bestqty_list_for_sell['qty']
                    l_best_askp = g_bestqty_list_for_sell['price'] 
                else:
                    l_best_askq = 0
                    l_best_askp = 0
                attribute.aList[currentIndex-1][0] = l_obj.currentTimeStamp
                attribute.aList[currentIndex-1][1] = tradeStats['currentPositionLong']
                attribute.aList[currentIndex-1][2] = tradeStats['currentPositionShort']
                listOfStringsToPrint = [ str(l_previous_obj.BidQ) , str(l_previous_obj.BidP) , str(l_previous_obj.AskP) , \
                                        str(l_previous_obj.AskQ) , str(l_previous_obj.TTQ) , str(l_previous_obj.LTP) ,\
                                        str(currentSellPredictedValue) , str(l_previous_obj.EnterTradeShort) ,l_previous_obj.ReasonForTradingOrNotTradingShort , str(currentBuyPredictedValue) ,\
                                        str(l_previous_obj.EnterTradeLong) ,l_previous_obj.ReasonForTradingOrNotTradingLong , str(tradeStats['NumberOfCloseBuy']),\
                                        str(tradeStats['NumberOfOpenBuy']),str(tradeStats['NumberOfOpenSell']),\
                                        str(tradeStats['NumberOfCloseBuy']),str(lDummyBidQ0),str(lDummyAskQ0),\
                                        str(lDummyTTQForBuy),str(lDummyTTQForSell),str(l_best_bidq),str(l_best_bidp),str(l_best_askp),\
                                        str(l_best_askq) ,str(lBuyTradedPrice), str(lBuyTradedQty), str(lSellTradedPrice),str(lSellTradedQty)]
                attribute.aList[currentIndex-1][3] =  ";".join(listOfStringsToPrint)
            currentTimeStamp = common.convertTimeStampFromStringToFloat(currentDataRow[colNumberOfData.TimeStamp])
            
            try:
                currentSellPredictedValue = float(predictedValuesDict[l_obj.currentTimeStamp]['sell']) 
                currentBuyPredictedValue  = float(predictedValuesDict[l_obj.currentTimeStamp]['buy'])
            except:
                noPredictionForThisRow += 1

            #Open sell and Close Buy
            if(currentSellPredictedValue >= entryCL4 and tradeStats['currentPositionLong'] == 0):
                g_bestqty_list_for_sell = {}
                l_obj.EnterTradeShort = 4       #For close by hitting
            elif(currentSellPredictedValue >= entryCL3 and tradeStats['currentPositionLong'] == 0):
                g_bestqty_list_for_sell = {}
                l_obj.EnterTradeShort = 3       #For standing at Bid +1 
            elif (currentSellPredictedValue >= entryCL2 and currentBuyPredictedValue < exitCL1 and tradeStats['currentPositionLong'] == 0):
                if(l_previous_obj.EnterTradeShort  not in [2,1] ):
                    g_bestqty_list_for_sell = {}
                l_obj.EnterTradeShort = 2       #For standing at bid
            elif (l_previous_obj.EnterTradeShort > 0 and currentSellPredictedValue >= entryCL1 and currentBuyPredictedValue < exitCL1 and tradeStats['currentPositionLong'] == 0):
                l_obj.EnterTradeShort = 1        ##For continuing to stand at bid
            elif(currentBuyPredictedValue >= exitCL4 and tradeStats['currentPositionShort'] > 0):
                g_bestqty_list_for_buy = {}
                l_obj.EnterTradeShort = -4       #For close by hitting
            elif(currentBuyPredictedValue >= exitCL3 and tradeStats['currentPositionShort'] > 0):
                g_bestqty_list_for_buy = {}
                l_obj.EnterTradeShort = -3       #For standing at Bid +1 
            elif (currentBuyPredictedValue >= exitCL2 and tradeStats['currentPositionShort'] > 0):
                if(l_previous_obj.EnterTradeShort  not in [-2,-1]):
                    g_bestqty_list_for_buy = {}
                l_obj.EnterTradeShort = -2       #For standing at bid
            elif (l_previous_obj.EnterTradeShort < 0 and currentBuyPredictedValue >= exitCL1 and tradeStats['currentPositionShort'] > 0):
                l_obj.EnterTradeShort = -1        ##For continuing to stand at bid
            else:
                l_obj.EnterTradeShort = 0

            #Open Buy
            if(currentBuyPredictedValue >= entryCL4 and tradeStats['currentPositionShort'] == 0):
                g_bestqty_list_for_buy = {}
                l_obj.EnterTradeLong = 4       #For close by hitting
            elif(currentBuyPredictedValue >= entryCL3 and tradeStats['currentPositionShort'] == 0):
                g_bestqty_list_for_buy = {}
                l_obj.EnterTradeLong = 3       #For standing at Bid +1 
            elif (currentBuyPredictedValue >= entryCL2 and currentSellPredictedValue < exitCL1  and tradeStats['currentPositionShort'] == 0):
                if(l_previous_obj.EnterTradeLong  not in [2,1]):
                    g_bestqty_list_for_buy = {}
                l_obj.EnterTradeLong = 2       #For standing at bid
            elif (l_previous_obj.EnterTradeLong < 0 and currentBuyPredictedValue >= entryCL1 and currentSellPredictedValue < exitCL1 and tradeStats['currentPositionShort'] == 0):
                l_obj.EnterTradeLong = 1       #For continuing to stand at bid
            elif(currentSellPredictedValue >= exitCL4 and tradeStats['currentPositionLong'] > 0):
                g_bestqty_list_for_sell = {}
                l_obj.EnterTradeLong = -4       #For close by hitting
            elif(currentSellPredictedValue >= exitCL3 and tradeStats['currentPositionLong'] > 0):
                g_bestqty_list_for_sell = {}
                l_obj.EnterTradeLong = -3       #For standing at Bid +1 
            elif (currentSellPredictedValue >= exitCL2  and tradeStats['currentPositionLong'] > 0):
                if(l_previous_obj.EnterTradeLong  not in [-2,-1] ):
                    g_bestqty_list_for_sell = {}
                l_obj.EnterTradeLong = -2       #For standing at bid
            elif (l_previous_obj.EnterTradeLong > 0 and currentSellPredictedValue >= exitCL1 and tradeStats['currentPositionLong'] > 0):
                l_obj.EnterTradeLong = -1        ##For continuing to stand at bid            
            else:
                l_obj.EnterTradeLong = 0        
            

        
        l_previous_obj = l_obj
        currentIndex = currentIndex + 1

# Squaring off if some open position there   
    if tradeStats['currentPositionLong'] > 0:
        tradeStats['NumberOfCloseSell'] += tradeStats['currentPositionLong']
        tradeStats['totalSellAmountLong'] += tradeStats['currentPositionLong'] * (l_previous_obj.BidP)
        tradeStats['currentPositionLong'] = 0
        l_obj.ReasonForTradingOrNotTradingLong = 'CloseSell(Hitting)'
    if tradeStats['currentPositionShort'] > 0:
        tradeStats['NumberOfCloseBuy'] += tradeStats['currentPositionShort']
        tradeStats['totalBuyAmountShort'] += tradeStats['currentPositionShort'] * (l_previous_obj.AskP)
        tradeStats['currentPositionShort'] = 0
        l_obj.ReasonForTradingOrNotTradingLong = 'CloseBuy(Hitting)'
    
    attribute.aList[currentIndex-1][0] = currentTimeStamp
    attribute.aList[currentIndex-1][1] = tradeStats['currentPositionLong']
    attribute.aList[currentIndex-1][2] = tradeStats['currentPositionShort']
    if(g_bestqty_list_for_buy != {}):
        l_best_bidq = g_bestqty_list_for_buy['qty']
        l_best_bidp = g_bestqty_list_for_buy['price'] 
    else:
        l_best_bidq = 0
        l_best_bidp = 0
    if(g_bestqty_list_for_sell != {}):
        l_best_askq = g_bestqty_list_for_sell['qty']
        l_best_askp = g_bestqty_list_for_sell['price'] 
    else:
        l_best_askq = 0
        l_best_askp = 0
    listOfStringsToPrint = [ str(l_previous_obj.BidQ) , str(l_previous_obj.BidP) , str(l_previous_obj.AskP) , \
                            str(l_previous_obj.AskQ) , str(l_previous_obj.TTQ) , str(l_previous_obj.LTP) ,\
                            str(currentSellPredictedValue) , str(l_previous_obj.EnterTradeShort) ,l_previous_obj.ReasonForTradingOrNotTradingShort , str(currentBuyPredictedValue) ,\
                            str(l_previous_obj.EnterTradeLong) ,l_previous_obj.ReasonForTradingOrNotTradingLong , str(tradeStats['NumberOfCloseBuy']),\
                            str(tradeStats['NumberOfOpenBuy']),str(tradeStats['NumberOfOpenSell']),\
                            str(tradeStats['NumberOfCloseBuy']),str(lDummyBidQ0),str(lDummyAskQ0),\
                            str(lDummyTTQForBuy),str(lDummyTTQForSell),str(l_best_bidq),str(l_best_bidp),str(l_best_askp), str(l_best_askq) , "0;0;0;0"]
    attribute.aList[currentIndex-1][3] =  ";".join(listOfStringsToPrint) 
    
    dirName = args.pd.replace('/ro/','/rs/')
    tradeLogMainDirName = dirName+"/t/"
    if not os.path.exists(tradeLogMainDirName):
        os.mkdir(tradeLogMainDirName)
    tradeLogSubDirectoryName =  tradeLogMainDirName + mainExperimentName+"/"
    if not os.path.exists(tradeLogSubDirectoryName):
        os.mkdir(tradeLogSubDirectoryName)
    
    fileName = tradeLogSubDirectoryName + pFileName + ".trade" 
    lHeaderColumnNamesList  = ['TimeStamp','CurrentPositionLong','CurrentPositionShort','BidQ0','BidP0','AskP0','AskQ0','TTQ','LTP','CurPredValueShort',\
                               'EnterTradeShort','ReasonForTradingOrNotTradingShort','CurPredValueLong','EnterTradeLong','ReasonForTradingOrNotTradingLong',\
                               'totalBuyTradeShort','totalBuyLong','totalSellShort','totalSellLong','DummyBidQ0','DummyAskQ0','DummyTTQChangeForSell','DummyTTQChangeForBuy' \
                               ,'BestBidQ','BestBidP','BestAskP','BestAskQ','BuyTradedPrcie','BuyTradedQty','SellTradedPrice','SellTradedQty']
    
    #attribute.writeToFile(fileName , lHeaderColumnNamesList)

    
    tradeResultMainDirName = dirName+"/r/"
    if not os.path.exists(tradeResultMainDirName):
        os.mkdir(tradeResultMainDirName)
    tradeResultSubDirectoryName =  tradeResultMainDirName + mainExperimentName+"/"
    if not os.path.exists(tradeResultSubDirectoryName):
        os.mkdir(tradeResultSubDirectoryName)
    fileName = tradeResultSubDirectoryName+pFileName+".result" 
    outputFile = open(fileName,"w")
    
    #changed file write to modify it to Short Long version
    print("Starting to write: "+fileName)
    print("The gross profit for Short are: " + str(tradeStats['totalSellAmountShort'] - tradeStats['totalBuyAmountShort']), file = outputFile)
    print("The gross profit for Long are: " + str(tradeStats['totalSellAmountLong'] - tradeStats['totalBuyAmountLong']), file = outputFile)
    print("The total open sell amount is: " + str(tradeStats['totalSellAmountShort']), file = outputFile)
    print("The total close sell amount is: " + str(tradeStats['totalSellAmountLong']), file = outputFile)
    print("The total close buy amount is: " + str(tradeStats['totalBuyAmountShort']), file = outputFile)
    print("The total open buy amount is: " + str(tradeStats['totalBuyAmountLong']), file = outputFile)
    print("Number open sell trade happened: " + str(tradeStats['NumberOfOpenSell']), file = outputFile)
    print("Number close sell trade happened: " + str(tradeStats['NumberOfCloseSell']), file = outputFile)
    print("Number close buy trade happened: " + str(tradeStats['NumberOfCloseBuy']), file = outputFile)
    print("Number open buy trade happened: " + str(tradeStats['NumberOfOpenBuy']), file = outputFile)
       
    try:
        averageOpenSellPrice = tradeStats['totalSellAmountShort']/tradeStats['NumberOfOpenSell']
        averageCloseBuyPrice = tradeStats['totalBuyAmountShort']/tradeStats['NumberOfCloseBuy']
    except:
        averageOpenSellPrice = 0 
        averageCloseBuyPrice = 0
    try:
        averageCloseSellPrice = tradeStats['totalSellAmountLong']/tradeStats['NumberOfCloseSell']
        averageOpenBuyPrice = tradeStats['totalBuyAmountLong']/tradeStats['NumberOfOpenBuy']
    except:
        averageCloseSellPrice = 0
        averageOpenBuyPrice = 0 
    
    print("Average open sell price per unit is: " + str(averageOpenSellPrice), file = outputFile)
    print("Average close sell price per unit is: " + str(averageCloseSellPrice), file = outputFile)
    print("Average open buy price per unit is: " + str(averageOpenBuyPrice), file = outputFile)
    print("Average close buy price per unit is: " + str(averageCloseBuyPrice), file = outputFile)
    print("The current position Short: " + str(tradeStats['currentPositionShort']), file = outputFile)
    print("The current position Long: " + str(tradeStats['currentPositionLong']), file = outputFile)
    print("Profit or loss per Qty traded Short is: " + str(averageOpenSellPrice - averageCloseBuyPrice), file = outputFile)
    print("Profit or loss per Qty traded Long is: " + str(averageCloseSellPrice - averageOpenBuyPrice), file = outputFile)
    pLPerLotShort=(averageOpenSellPrice - averageCloseBuyPrice)* 1000
    pLPerLotLong=(averageCloseSellPrice - averageOpenBuyPrice)* 1000
    print("1 lot has 1000 qty's so P/L Short per lot is: " + str(pLPerLotShort), file = outputFile)
    print("1 lot has 1000 qty's so P/L Long per lot is: " + str(pLPerLotLong), file = outputFile)
    print("P/L for Short trading 10 lots is: " + str(pLPerLotShort * 10), file = outputFile)
    print("P/L for Long trading 10 lots is: " + str(pLPerLotLong * 10), file = outputFile)
Exemple #6
0
def getDataFileAndPredictionsIntoObjectList(dataFileObject,buyPredictFileObject,sellPredictFileObject,lMinOfExitCl):
    global gNoOfLineReadPerChunk,gTickSize
    lObjectList = []
    lCurrentDataRowCount = 0
    lPrevObj = None
    fileHasHeader = 1
    headerSkipped = 0
    dataFileSep = ";"
    predictFileSep = ","
    lListOfBidP = []
    lListOfAskP = []
    while True:
        lDataFileRowsList = list(islice(dataFileObject,gNoOfLineReadPerChunk))
        lBuyPredictedFileRowList = list(islice(buyPredictFileObject,gNoOfLineReadPerChunk))
        lSellPredictedFileRowList = list(islice( sellPredictFileObject,gNoOfLineReadPerChunk ))
        if not lDataFileRowsList :
            print("Finished reading file")
            lObjectList.append(lPrevObj)    
            lPrevObj = None          
            break
        lengthOfDataList = len(lDataFileRowsList)
        lengthOfBuyPredList = len(lBuyPredictedFileRowList)
        lengthOfSellPredList = len(lSellPredictedFileRowList)
        if lengthOfDataList!=lengthOfBuyPredList or lengthOfBuyPredList!=lengthOfSellPredList:
            print("Length of data file and predicted buy and sell values file are not same ")
            os._exit(-1)
        for currentRowIndex in range(lengthOfDataList):
            if(fileHasHeader == 1 and headerSkipped != 1):
                headerSkipped = 1 
                continue
            lDataRow = lDataFileRowsList[currentRowIndex].rstrip().split(dataFileSep)
            lAskP = float(lDataRow[colNumberOfData.BestAskP])
            lBidP = float(lDataRow[colNumberOfData.BestBidP])
            lAskQ = int(lDataRow[colNumberOfData.BestAskQ])
            lBidQ = int(lDataRow[colNumberOfData.BestBidQ])
            lTTQ = int(lDataRow[colNumberOfData.TTQ])
            lLTP = float(lDataRow[colNumberOfData.LTP]) 
            lCurrentDataRowTimeStamp = common.convertTimeStampFromStringToFloat(lDataRow[colNumberOfData.TimeStamp])
            lBuyPredictedRow = lBuyPredictedFileRowList[currentRowIndex].rstrip().split(predictFileSep)
            lBuyPredictedTimeStamp = float(lBuyPredictedRow[1])
            lBuyPredictedValue = float(lBuyPredictedRow[2])
            lSellPredictedRow = lSellPredictedFileRowList[currentRowIndex].rstrip().split(predictFileSep)
            lSellPredictedTimeStamp = float(lSellPredictedRow[1])
            lSellPredictedValue = float(lSellPredictedRow[2])
            
            if lCurrentDataRowTimeStamp != lBuyPredictedTimeStamp or lBuyPredictedTimeStamp!=lSellPredictedTimeStamp:
                print('Time stamp of data row with predicted value is not matching .\n Data row time stamp :- ' , lCurrentDataRowTimeStamp,'BuyPredicted Time Stamp :- ' , lBuyPredictedTimeStamp\
                      ,"SellPredicted Time Stamp :- ",lSellPredictedTimeStamp)
                os._exit(-1)
            else:
                lObj = Tick(lCurrentDataRowTimeStamp,lAskP,lBidP,lAskQ,lBidQ,lLTP,lTTQ,lBuyPredictedValue,lSellPredictedValue)
                if lPrevObj!=None:
                    lPrevObj.TTQChange  = lObj.TTQ - lPrevObj.TTQ
                    lPrevObj.NextLTP = lObj.LTP
                    if ( lPrevObj.AskP -lPrevObj.BidP > gTickSize ) and ( lPrevObj.TTQChange == 0 ):
                        if lPrevObj.BidP not in lListOfBidP:
                            lListOfBidP.append(lPrevObj.BidP)
                        if lPrevObj.AskP not in lListOfAskP:
                            lListOfAskP.append(lPrevObj.AskP)
                        pass
                    else:
                        if lPrevObj.currentBuyPredictedValue >= lMinOfExitCl or lPrevObj.currentSellPredictedValue>=lMinOfExitCl:
                            if len(lListOfBidP) > 1:
                                lPrevObj.bidPChangedInBetweenLastTickAndCurrentTick = 1
                            if len(lListOfAskP) > 1:
                                lPrevObj.askPChangedInBetweenLastTickAndCurrentTick = 1
                            lObjectList.append(lPrevObj)  
                            lListOfBidP = [lPrevObj.BidP]
                            lListOfAskP = [lPrevObj.AskP]
                        else:
                            if lPrevObj.BidP not in lListOfBidP:
                                lListOfBidP.append(lPrevObj.BidP)
                            if lPrevObj.AskP not in lListOfAskP:
                                lListOfAskP.append(lPrevObj.AskP)
                lPrevObj = lObj
            if lCurrentDataRowCount%50000 ==0:
                print("Completed reading ",lCurrentDataRowCount)
            lCurrentDataRowCount = lCurrentDataRowCount + 1 
    return lObjectList
def extractAttributeFromDataMatrix(args):
   if args.n == None:
      N = 5
   else:
      N = int(args.n) 
   try:
      #Value of argument c can be taken as BidP0 and AskP0 
      args.c
   except:
      print "Since -c has not been specified I cannot proceed"
      os._exit()
   colNumberOfAttribute = eval("colNumberOfData."+ args.c )
   colNumberOfTimeStamp = colNumberOfData.TimeStamp

   numberOfRowsInLastNSecs = 0
   queueOfValuesInLastNSecs = deque()
   totalOfRowsInLastNSecs = 0.0
   timeOfOldestRow = common.convertTimeStampFromStringToFloat(dataFile.matrix[0][colNumberOfTimeStamp],args.cType)
   currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
   lengthOfDataMatrix = len(dataFile.matrix)
   while (currentRowNumberForWhichFeatureValueIsBeingCalculated < lengthOfDataMatrix):
      timeOfCurrentRow = common.convertTimeStampFromStringToFloat(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfTimeStamp],args.cType)
      timeElapsed = timeOfCurrentRow - timeOfOldestRow
      if (timeElapsed < N):
         if  currentRowNumberForWhichFeatureValueIsBeingCalculated != 0:
             previousRowPrice = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated - 1][colNumberOfAttribute])
         else:
             previousRowPrice = 0
         currentPrice = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfAttribute])
         currentLTP = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.LTP])
         currentMsgCode = dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.MsgCode]
         currentLTQ = int(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.NewQ])
         if ( currentMsgCode =='T' and  ( previousRowPrice == currentLTP ) ):
             totalOfRowsInLastNSecs += currentLTQ
             cellValue = currentLTQ
         else:
             cellValue = 0
         attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][0] = common.convertTimeStampFromStringToDecimal(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfTimeStamp],args.cType)
         attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][1] = totalOfRowsInLastNSecs # in 1st iteration currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
         attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][2] = str(currentPrice) + ";" + str(currentLTP) + ";" + str(cellValue) + ";" + str(currentMsgCode) + ";" + str(timeElapsed)
         queueOfValuesInLastNSecs.append([cellValue,timeOfCurrentRow])
         numberOfRowsInLastNSecs += 1   # Every append gets a +1 
         currentRowNumberForWhichFeatureValueIsBeingCalculated += 1
         continue     # Since we are going back 1 row from current we cannot get data from current row
      else:
         # We need to reset the timeOfOldestRow since timeElapsed has exceeded N seconds
         while(timeElapsed >= N):
            if(len(queueOfValuesInLastNSecs) == 0):
               timeOfOldestRow = timeOfCurrentRow
               timeElapsed = 0
               if(numberOfRowsInLastNSecs != 0):
                  print "Sanity check: This condition is not possible logically. numberOfRowsInLastNSecs should been 0. There has been an unknown error"
                  sys.exit(-1)
               if(totalOfRowsInLastNSecs != 0):
                  print "Sanity check: This condition is not possible logically. totalOfRowsInLastNSecs should have been 0. There has been an unknown error"
                  sys.exit(-1)   
            else:   
               oldestElementInQueue = queueOfValuesInLastNSecs.popleft()
               colValueInOldestElementInQueue = oldestElementInQueue[0]
               totalOfRowsInLastNSecs -= colValueInOldestElementInQueue
               if len(queueOfValuesInLastNSecs) == 0:
                    timeElapsed = 0
                    timeOfOldestRow = timeOfCurrentRow
               else:
                    timeOfOldestRow = queueOfValuesInLastNSecs[0][1]
               numberOfRowsInLastNSecs -= 1 # every pop from the queue gets a -1
               timeElapsed = timeOfCurrentRow - timeOfOldestRow
               if(len(queueOfValuesInLastNSecs) != numberOfRowsInLastNSecs):
                  print "Sanity check: This condition is not possible logically. There has been an unknown error"
                  sys.exit(-1)
 
      print "Processed row number " + str(currentRowNumberForWhichFeatureValueIsBeingCalculated)
   
   lNameOfFeaturePrinted = "fMarketTradedQtySumOfCol" + args.c + "InLast" + str(args.n) + "Secs"
   return [ "TimeStamp", lNameOfFeaturePrinted , args.c , "LTP" , "TradedQtyWhichIs="+ args.c , "MsgCode" , "TimeElapsed"]
def getDataFileAndPredictionsIntoObjectList(targetFileObject, pFileObjectList,
                                            pOldTree, config):
    lObjectList = []
    lCurrentDataRowCount = 0
    lPrevObj = None
    fileHasHeader = 1
    headerSkipped = 0
    dataFileSep = ";"
    l_buy_data_row_list = list(islice(targetFileObject['buy'], 10000))
    l_sell_data_row_list = list(islice(targetFileObject['sell'], 10000))
    lFeatureFileRowList = {}

    for index in xrange(len(pFileObjectList)):
        if config['predictions-buy'].get(dd.gTreeVariablesPresent[index],"f").lower()!="buyprob" and config['predictions-sell'].get(dd.gTreeVariablesPresent[index],"f").lower()!="sellprob" \
        and config['predictions-sell'].get(dd.gTreeVariablesPresent[index],"f").lower()!="buyprob" and config['predictions-buy'].get(dd.gTreeVariablesPresent[index],"f").lower()!="sellprob":
            lFeatureFileRowList[dd.gTreeVariablesPresent[index]] = (list(
                islice(pFileObjectList[index], 10000)))
    while True:
        l_buy_data_row_list = list(islice(targetFileObject['buy'], 10000))
        l_sell_data_row_list = list(islice(targetFileObject['sell'], 10000))
        index = 0
        lFeatureFileRowList = {}
        for index in xrange(len(pFileObjectList)):
            lFeatureFileRowList[dd.gTreeVariablesPresent[index]] = (list(
                islice(pFileObjectList[index], dd.gNoOfLineReadPerChunk)))
        if not l_buy_data_row_list:
            print("Finished reading file")
            lObjectList.append(lPrevObj)
            lPrevObj = None
            break
        lengthOfDataList = len(l_buy_data_row_list)
        for features in lFeatureFileRowList:
            if lengthOfDataList != len(lFeatureFileRowList[features]):
                print("Length of data file and feature file are not same ")
                os._exit(-1)
        for currentRowIndex in range(lengthOfDataList):
            if (fileHasHeader == 1 and headerSkipped != 1):
                headerSkipped = 1
                continue
            lBuyDataRow = l_buy_data_row_list[currentRowIndex].rstrip().split(
                dataFileSep)
            lSellDataRow = l_sell_data_row_list[currentRowIndex].rstrip(
            ).split(dataFileSep)

            lCurrentDataRowTimeStamp = common.convertTimeStampFromStringToFloat(
                lBuyDataRow[0])

            lBuyTargetVariable = int(lBuyDataRow[1])
            lSellTargetVariable = int(lSellDataRow[1])

            lFeatureValueDict = {}
            for feature in lFeatureFileRowList:
                if config['predictions-buy'].get(feature, "f").lower() == "buyprob" or config['predictions-sell'].get(feature, "f").lower()  == "sellprob"\
                or  config['predictions-buy'].get(feature, "f").lower() == "sellprob" or config['predictions-sell'].get(feature, 'f').lower()  == "buyprob":
                    lSep = ","
                    lTimeStampIndex = 1
                    lFeatureIndex = 2
                else:
                    lSep = ";"
                    lTimeStampIndex = 0
                    lFeatureIndex = 1
                #print("Varible name" , feature)
                lFeatureFileRow = lFeatureFileRowList[feature][
                    currentRowIndex].rstrip().split(lSep)
                lFeatureFileTimeStamp = float(lFeatureFileRow[lTimeStampIndex])
                lFeatureFileValue = float(lFeatureFileRow[lFeatureIndex])
                if lCurrentDataRowTimeStamp != lFeatureFileTimeStamp:
                    print(
                        'Time stamp of data row with feature value is not matching .\n Data row time stamp :- ',
                        lCurrentDataRowTimeStamp,
                        'Feature value Time Stamp :- ', lFeatureFileTimeStamp)
                    os._exit(-1)
                lFeatureValueDict[feature] = lFeatureFileValue

            dd.gOutputGlobalTree['buy'] = reading_tree.breadth_first_traversal(
                dd.gGlobalTree['buy'], dd.gOutputGlobalTree['buy'],
                lBuyTargetVariable, lFeatureValueDict)
            dd.gOutputGlobalTree[
                'sell'] = reading_tree.breadth_first_traversal(
                    dd.gGlobalTree['sell'], dd.gOutputGlobalTree['sell'],
                    lSellTargetVariable, lFeatureValueDict)

            if lCurrentDataRowCount % 50000 == 0:
                print("Completed reading ", lCurrentDataRowCount)
            lCurrentDataRowCount = lCurrentDataRowCount + 1
    return lObjectList
def extractAttributeFromDataMatrix(args):
   if args.n == None:
      N = 5
   else:
      N = int(args.n) 
   try:
      #Value of argument c can be taken as BidP0 and AskP0 
      args.c
   except:
      print "Since -c has not been specified I cannot proceed"
      os._exit()
   try:
      M = int(args.m)
   except:
      print "-m wrong!!"
   colNumberOfAttribute = eval("colNumberOfData."+ args.c )
   colNumberOfOppositeAttribute = eval("colNumberOfData.BestBidP") if "ask" in args.c else eval("colNumberOfData.BestAskP")
   colNumberOfTimeStamp = colNumberOfData.TimeStamp
   if "ask" in args.c.lower():
      colAttributeRowNo = [colNumberOfData.AskP0, colNumberOfData.AskP1]#, colNumberOfData.AskP2, colNumberOfData.AskP3, colNumberOfData.AskP4] 
   else:
      colAttributeRowNo = [colNumberOfData.BidP0, colNumberOfData.BidP1]#, colNumberOfData.BidP2, colNumberOfData.BidP3, colNumberOfData.BidP4]
   numberOfRowsInLastNSecs = 0
   queueOfValuesInLastNSecsN = []
   queueOfValuesInLastNSecsX = []
   totalOfRowsInLastNSecsX = 0.0
   totalOfRowsInLastNSecsN = 0.0
   totalOfQtyInLastNSecsX = 0.0
   totalOfQtyInLastNSecsN = 0.0
   
   timeOfOldestRow = common.convertTimeStampFromStringToFloat(dataFile.matrix[0][colNumberOfTimeStamp],args.cType)
   currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
   lengthOfDataMatrix = len(dataFile.matrix)
   while (currentRowNumberForWhichFeatureValueIsBeingCalculated < lengthOfDataMatrix):
      cellValueQtyN, cellValueQtyX, cellValueTotalN, cellValueTotalX = [0,0,0,0]
      colAttributeRow = map(float, [dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][i] for i in colAttributeRowNo])
      timeOfCurrentRow = common.convertTimeStampFromStringToFloat(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfTimeStamp],args.cType)
      if  currentRowNumberForWhichFeatureValueIsBeingCalculated != 0:
          previousRowPrice = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated - 1][colNumberOfAttribute])
          previousRowOppPrice = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated - 1][colNumberOfOppositeAttribute])
      else:
          previousRowPrice = 0
          previousRowOppPrice = 0
      currentPrice = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfAttribute])
      currentLTP = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.LTP])
      currentMsgCode = dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.MsgCode]
      newQty = int(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.NewQ])
      newPrice = int(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.NewP])
      oldQty = int(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.OldQ])
      oldPrice = int(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.OldP])
      if ( currentMsgCode =='T' and (( previousRowOppPrice <= currentLTP ) if "bid" in args.c.lower() else (previousRowOppPrice >= currentLTP))):
          totalOfRowsInLastNSecsN += newQty * currentLTP
          cellValueTotalN = newQty * currentLTP
          totalOfQtyInLastNSecsN += newQty
          cellValueQtyN = newQty
          queueOfValuesInLastNSecsN.append([cellValueTotalN,colAttributeRow[-1],cellValueQtyN,currentLTP,timeOfCurrentRow])
         
      elif currentMsgCode == 'M':
          if ("ask" in args.c.lower() and newPrice <= colAttributeRow[-1] and newPrice >= currentPrice) or ("bid" in args.c.lower() and newPrice >= colAttributeRow[-1] and newPrice <= currentPrice):
              check = True
          else:
              check = False
          totalOfRowsInLastNSecsN += (newPrice * newQty) if check else 0 
          cellValueTotalN = newPrice * newQty if check else 0
          totalOfQtyInLastNSecsN += newQty if check else 0
          cellValueQtyN = newQty if check else 0
          if check:
              queueOfValuesInLastNSecsN.append([cellValueTotalN,colAttributeRow[-1],cellValueQtyN,newPrice,timeOfCurrentRow])              
          
          if ("ask" in args.c.lower() and oldPrice <= colAttributeRow[-1] and oldPrice >= previousRowPrice) or ("bid" in args.c.lower() and oldPrice >= colAttributeRow[-1] and oldPrice <= previousRowPrice):
              check = True
          else:
              check = False
          totalOfRowsInLastNSecsX += oldPrice * oldQty if check else 0
          cellValueTotalX = oldPrice * oldQty if check else 0
          totalOfQtyInLastNSecsX += oldQty if check else 0
          cellValueQtyX = oldQty if check else 0
          if check:
              queueOfValuesInLastNSecsX.append([cellValueTotalX,colAttributeRow[-1],cellValueQtyX,oldPrice,timeOfCurrentRow])
        
      elif ( currentMsgCode == 'N'):
          if ("ask" in args.c.lower() and newPrice <= colAttributeRow[-1] and newPrice >= currentPrice) or ("bid" in args.c.lower() and newPrice >= colAttributeRow[-1] and newPrice <= currentPrice):
              check = True
          else:
              check = False
          totalOfRowsInLastNSecsN += newPrice * newQty if check else 0
          cellValueTotalN = newPrice * newQty if check else 0
          totalOfQtyInLastNSecsN += newQty if check else 0
          cellValueQtyN = newQty if check else 0
          if check:
              queueOfValuesInLastNSecsN.append([cellValueTotalN,colAttributeRow[-1],cellValueQtyN,newPrice,timeOfCurrentRow])
         
      elif ( currentMsgCode == 'X'):
          if ("ask" in args.c.lower() and newPrice <= colAttributeRow[-1] and newPrice >= previousRowPrice) or ("bid" in args.c.lower() and newPrice >= colAttributeRow[-1] and newPrice <= previousRowPrice):
              check = True
          else:
              check = False
          totalOfRowsInLastNSecsX += newPrice * newQty if check else 0
          cellValueTotalX = newPrice * newQty if check else 0
          totalOfQtyInLastNSecsX += newQty if check else 0
          cellValueQtyX = newQty if check else 0
          if check:
              queueOfValuesInLastNSecsX.append([cellValueTotalX,colAttributeRow[-1],cellValueQtyX,newPrice,timeOfCurrentRow])

      if(totalOfQtyInLastNSecsN) > N:
          while totalOfQtyInLastNSecsN > N:
              excess = totalOfQtyInLastNSecsN - N
              if queueOfValuesInLastNSecsN[0][2] <= excess:
                  totalOfQtyInLastNSecsN -= queueOfValuesInLastNSecsN[0][2]
                  totalOfRowsInLastNSecsN -= queueOfValuesInLastNSecsN[0][0]
                  queueOfValuesInLastNSecsN = queueOfValuesInLastNSecsN[1:]
              else:
                  totalOfQtyInLastNSecsN -= excess 
                  totalOfRowsInLastNSecsN -= excess * float(queueOfValuesInLastNSecsN[0][0]) / queueOfValuesInLastNSecsN[0][2]
                  queueOfValuesInLastNSecsN[0][2] -= excess
                  queueOfValuesInLastNSecsN[0][0] -= excess * queueOfValuesInLastNSecsN[0][3]

      if(totalOfQtyInLastNSecsX) > N:
          while totalOfQtyInLastNSecsX > N:
              excess = totalOfQtyInLastNSecsX - N
              if queueOfValuesInLastNSecsX[0][2] <= excess:
                  totalOfQtyInLastNSecsX -= queueOfValuesInLastNSecsX[0][2]
                  totalOfRowsInLastNSecsX -= queueOfValuesInLastNSecsX[0][0]
                  queueOfValuesInLastNSecsX = queueOfValuesInLastNSecsX[1:]
              else:
                  totalOfQtyInLastNSecsX -= excess 
                  totalOfRowsInLastNSecsX -= excess * float(queueOfValuesInLastNSecsX[0][0]) / queueOfValuesInLastNSecsX[0][2]
                  queueOfValuesInLastNSecsX[0][2] -= excess
                  queueOfValuesInLastNSecsX[0][0] -= excess * queueOfValuesInLastNSecsX[0][3]
                  
      if M == 1:
          exPrice = colAttributeRow[-1]
      elif M == 2:
          try:
              exPrice = queueOfValuesInLastNSecsN[0][1] if queueOfValuesInLastNSecsN[0][-1] < queueOfValuesInLastNSecsX[0][-1] else queueOfValuesInLastNSecsX[0][1]
          except:
              print("Warning!!!!")
              exPrice = colAttributeRow[-1]
      elif M == 3:
          try:
            exPrice = totalOfRowsInLastNSecsN / float(totalOfQtyInLastNSecsN)
          except:
            print("Warning!!!!")
            exPrice = colAttributeRow[-1]
      elif M == 4:
          try:
              exPrice = totalOfRowsInLastNSecsX / float(totalOfQtyInLastNSecsX)
          except:
              print("Warning!!!!")
              exPrice = colAttributeRow[-1]
      
      newSide = (float(totalOfRowsInLastNSecsN + (N - totalOfQtyInLastNSecsN) * exPrice) / N)
      cancelSide = (float(totalOfRowsInLastNSecsX + (N - totalOfQtyInLastNSecsX) * exPrice) / N)
      attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][0] = common.convertTimeStampFromStringToDecimal(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfTimeStamp],args.cType)
      attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][1] = (float(newSide) - cancelSide) # in 1st iteration currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
      attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][2] = str(currentPrice) + ";" + str(cellValueQtyN) + ";" + str(cellValueQtyX) + ";" + str(cellValueTotalN) + ";" + str(cellValueTotalX) + ";" + str(currentMsgCode)
      numberOfRowsInLastNSecs += 1   # Every append gets a +1 
#      if currentRowNumberForWhichFeatureValueIsBeingCalculated == 164:
#          import pdb
#          pdb.set_trace()
      currentRowNumberForWhichFeatureValueIsBeingCalculated += 1
      continue     # Since we are going back 1 row from current we cannot get data from current row
      print "Processed row number " + str(currentRowNumberForWhichFeatureValueIsBeingCalculated)
   
   lNameOfFeaturePrinted = "fMarketModNewSumOfCol" + args.c + "InLast" + str(args.n) + "Secs"
   return [ "TimeStamp", lNameOfFeaturePrinted , args.c , "QtyForN" , "QtyForX", "AmountN" , "AmountX", "MsgCode"]
Exemple #10
0
def main():
    attribute.initList()
    dataFile.getDataIntoMatrix(args.pd)
    predictedValuesDict = dict()
    getPredictedValuesIntoDict(predictedValuesDict)
    enterTradeShort = 0
    enterTradeLong = 0
    ttqAtTimeOfPreviousDataRow = 0
    askP0AtTimeOfPreviousDataRow = 0
    bidP0AtTimeOfPreviousDataRow = 0
    askQ0AtTimeOfPreviousDataRow = 0
    bidQ0AtTimeOfPreviousDataRow = 0
    tradeStats = dict()
    tradeStats['totalSellValueShort'] = 0
    tradeStats['totalBuyValueShort'] = 0
    tradeStats['currentPositionShort'] = 0
    tradeStats['totalSellValueLong'] = 0
    tradeStats['totalBuyValueLong'] = 0
    tradeStats['currentPositionLong'] = 0
    noPredictionForThisRow = 0
    currentPredictedValueShort = 0
    currentPredictedValueLong = 0
    entryCL = float(args.entryCL) / 100
    exitCL = float(args.exitCL) / 100
    #  entryCLCutoff = float(args.entryCLCutoff)
    #  exitCLCutoff = float(args.exitCLCutoff)
    numberOfTimesAskedToEnterTradeShort = 0
    numberOfTimesAskedToEnterTradeLong = 0
    numberOfTimesAskedToExitTradeShort = 0
    numberOfTimesAskedToExitTradeLong = 0
    reasonForTrade = dict()
    reasonForTrade['LTPDoesNotEqualBidP0Short'] = 0
    reasonForTrade['LTPDoesNotEqualAskP0Short'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptShort'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptLong'] = 0
    reasonForTrade['AssumingBuyTradeHappenedShort'] = 0
    reasonForTrade['AssumingBuyTradeHappenedLong'] = 0
    reasonForTrade['LTPDoesNotEqualAskP0Long'] = 0
    reasonForTrade['LTPDoesNotEqualBidP0Long'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptShort'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptLong'] = 0
    reasonForTrade['AssumingSellTradeHappenedShort'] = 0
    reasonForTrade['AssumingSellTradeHappenedLong'] = 0
    currentIndex = 0
    print("Processing the data file for trades :")
    attribute.initList()
    for currentDataRow in dataFile.matrix:

        lReturnList = checkIfPreviousDecisionToEnterOrExitTradeWasSuccessful(
            currentDataRow, ttqAtTimeOfPreviousDataRow,
            askP0AtTimeOfPreviousDataRow, bidP0AtTimeOfPreviousDataRow,
            askQ0AtTimeOfPreviousDataRow, bidQ0AtTimeOfPreviousDataRow,
            enterTradeShort, enterTradeLong, tradeStats, reasonForTrade)

        lReasonForTradingOrNotTradingShort = lReturnList[0]
        lReasonForTradingOrNotTradingLong = lReturnList[1]
        lDummyBidQ0 = lReturnList[2]
        lDummyAskQ0 = lReturnList[3]
        lDummyTTQForBuy = lReturnList[4]
        lDummyTTQForSell = lReturnList[5]
        if currentIndex > 0:
            attribute.aList[currentIndex - 1][0] = currentTimeStamp
            attribute.aList[currentIndex -
                            1][1] = tradeStats['currentPositionLong']
            attribute.aList[currentIndex -
                            1][2] = tradeStats['currentPositionShort']
            listOfStringsToPrint = [
                str(bidQ0AtTimeOfPreviousDataRow),
                str(bidP0AtTimeOfPreviousDataRow),
                str(askP0AtTimeOfPreviousDataRow),
                str(askQ0AtTimeOfPreviousDataRow),
                str(ttqAtTimeOfPreviousDataRow),
                str(ltpAtTimeOfPreviousDataRow),
                str(currentPredictedValueShort),
                str(enterTradeShort), lReasonForTradingOrNotTradingShort,
                str(currentPredictedValueLong),
                str(enterTradeLong), lReasonForTradingOrNotTradingLong,
                str(reasonForTrade['AssumingBuyTradeHappenedShort']),
                str(reasonForTrade['AssumingBuyTradeHappenedLong']),
                str(reasonForTrade['AssumingSellTradeHappenedShort']),
                str(reasonForTrade['AssumingSellTradeHappenedLong']),
                str(lDummyBidQ0),
                str(lDummyAskQ0),
                str(lDummyTTQForBuy),
                str(lDummyTTQForSell)
            ]
            attribute.aList[currentIndex -
                            1][3] = ";".join(listOfStringsToPrint)
        currentTimeStamp = common.convertTimeStampFromStringToFloat(
            currentDataRow[colNumberOfData.TimeStamp])

        try:
            currentPredictedValueShort = (
                float(predictedValuesDict[currentTimeStamp][-2]) +
                float(predictedValuesDict[currentTimeStamp][-1]))
            currentPredictedValueLong = (
                float(predictedValuesDict[currentTimeStamp][2]) +
                float(predictedValuesDict[currentTimeStamp][1]))
        except:
            noPredictionForThisRow += 1

        #short decisions
        if (currentPredictedValueShort > entryCL):
            enterTradeShort = 1
            numberOfTimesAskedToEnterTradeShort += 1
        elif (currentPredictedValueShort < exitCL
              and tradeStats['currentPositionShort'] > 0):
            numberOfTimesAskedToExitTradeShort += 1
            enterTradeShort = -1  # Implies to exit the trade
        else:
            enterTradeShort = 0  # Implies make no change

#       if enterTradeShort == 1:
#           if currentPredictedValue <= entryCLCutoff:
#               enterTrade = 0
#       if enterTrade == -1:
#           if currentPredictedValue >= exitCLCutoff:
#               enterTrade = 0

#long decisions
        if (currentPredictedValueLong > entryCL):
            enterTradeLong = 1
            numberOfTimesAskedToEnterTradeLong += 1
        elif (currentPredictedValueLong < exitCL
              and tradeStats['currentPositionLong'] > 0):
            numberOfTimesAskedToExitTradeLong += 1
            enterTradeLong = -1  # Implies to exit the trade
        else:
            enterTradeLong = 0  # Implies make no change

        ttqAtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.TTQ])
        askP0AtTimeOfPreviousDataRow = float(
            currentDataRow[colNumberOfData.AskP0])
        bidP0AtTimeOfPreviousDataRow = float(
            currentDataRow[colNumberOfData.BidP0])
        askQ0AtTimeOfPreviousDataRow = float(
            currentDataRow[colNumberOfData.AskQ0])
        bidQ0AtTimeOfPreviousDataRow = float(
            currentDataRow[colNumberOfData.BidQ0])
        ltpAtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.LTP])
        currentIndex = currentIndex + 1


# Squaring off if some open position there
    if tradeStats['currentPositionLong'] > 0:
        reasonForTrade['AssumingSellTradeHappenedLong'] += tradeStats[
            'currentPositionLong']
        tradeStats['totalSellValueLong'] += tradeStats[
            'currentPositionLong'] * (bidP0AtTimeOfPreviousDataRow)
        tradeStats['currentPositionLong'] = 0
        lReasonForTradingOrNotTradingLong = 'CloseSell(Hitting)'
    elif tradeStats['currentPositionShort'] > 0:
        reasonForTrade['AssumingBuyTradeHappenedShort'] += tradeStats[
            'currentPositionShort']
        tradeStats['totalBuyValueShort'] += tradeStats[
            'currentPositionShort'] * (askP0AtTimeOfPreviousDataRow)
        tradeStats['currentPositionShort'] = 0
        lReasonForTradingOrNotTradingLong = 'CloseBuy(Hitting)'

    attribute.aList[currentIndex - 1][0] = currentTimeStamp
    attribute.aList[currentIndex - 1][1] = tradeStats['currentPositionLong']
    attribute.aList[currentIndex - 1][2] = tradeStats['currentPositionShort']
    listOfStringsToPrint = [
        str(bidQ0AtTimeOfPreviousDataRow),
        str(bidP0AtTimeOfPreviousDataRow),
        str(askP0AtTimeOfPreviousDataRow),
        str(askQ0AtTimeOfPreviousDataRow),
        str(ttqAtTimeOfPreviousDataRow),
        str(ltpAtTimeOfPreviousDataRow),
        str(currentPredictedValueShort),
        str(enterTradeShort), "",
        str(currentPredictedValueLong),
        str(enterTradeLong), "",
        str(reasonForTrade['AssumingBuyTradeHappenedShort']),
        str(reasonForTrade['AssumingBuyTradeHappenedLong']),
        str(reasonForTrade['AssumingSellTradeHappenedShort']),
        str(reasonForTrade['AssumingSellTradeHappenedLong']),
        str(lDummyBidQ0),
        str(lDummyAskQ0),
        str(lDummyTTQForBuy),
        str(lDummyTTQForSell)
    ]
    attribute.aList[currentIndex - 1][3] = ";".join(listOfStringsToPrint)
    dirName = args.pd.replace('/ro/', '/rs/')
    tradeLogMainDirName = dirName + "/t/"
    if not os.path.exists(tradeLogMainDirName):
        os.mkdir(tradeLogMainDirName)
    tradeLogSubDirectoryName = tradeLogMainDirName + mainExperimentName + "/"
    if not os.path.exists(tradeLogSubDirectoryName):
        os.mkdir(tradeLogSubDirectoryName)
    fileName = tradeLogSubDirectoryName + experimentName + initialFileName + ".trade"
    lHeaderColumnNamesList = [
        'TimeStamp', 'CurrentPositionLong', 'CurrentPositionShort', 'BidQ0',
        'BidP0', 'AskP0', 'AskQ0', 'TTQ', 'LTP', 'CurPredValueShort',
        'EnterTradeShort', 'ReasonForTradingOrNotTradingShort',
        'CurPredValueLong', 'EnterTradeLong',
        'ReasonForTradingOrNotTradingLong', 'totalBuyTradeShort',
        'totalBuyLong', 'totalSellShort', 'totalSellLong', 'DummyBidQ0',
        'DummyAskQ0', 'DummyTTQChangeForSell', 'DummyTTQChangeForBuy'
    ]
    attribute.writeToFile(fileName, lHeaderColumnNamesList)

    tradeResultMainDirName = dirName + "/r/"
    if not os.path.exists(tradeResultMainDirName):
        os.mkdir(tradeResultMainDirName)
    tradeResultSubDirectoryName = tradeResultMainDirName + mainExperimentName + "/"
    if not os.path.exists(tradeResultSubDirectoryName):
        os.mkdir(tradeResultSubDirectoryName)
    fileName = tradeResultSubDirectoryName + experimentName + initialFileName + ".result"
    outputFile = open(fileName, "w")

    #changed file write to modify it to Short Long version
    print("Starting to write: " + fileName)
    print("The net results for Short are: " +
          str(tradeStats['totalSellValueShort'] -
              tradeStats['totalBuyValueShort']),
          file=outputFile)
    print("The net results for Long are: " +
          str(tradeStats['totalSellValueLong'] -
              tradeStats['totalBuyValueLong']),
          file=outputFile)
    print("Number of rows for which there is no prediction: " +
          str(noPredictionForThisRow),
          file=outputFile)
    print("Number of times asked to enter trade Short: " +
          str(numberOfTimesAskedToEnterTradeShort),
          file=outputFile)
    print("Number of times asked to enter trade Long: " +
          str(numberOfTimesAskedToEnterTradeLong),
          file=outputFile)
    print("Number of times asked to exit trade Short: " +
          str(numberOfTimesAskedToExitTradeShort),
          file=outputFile)
    print("Number of times asked to exit trade Long: " +
          str(numberOfTimesAskedToExitTradeLong),
          file=outputFile)
    print(
        "Assumed close buy trade did not happen since volume did not increase: "
        + str(reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptShort']),
        file=outputFile)
    print(
        "Assumed open buy trade did not happen since volume did not increase: "
        + str(reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptLong']),
        file=outputFile)
    print(
        "Assumed close buy trade did not happen since bidP0 not same as LTP: "
        + str(reasonForTrade['LTPDoesNotEqualBidP0Short']),
        file=outputFile)
    print(
        "Assumed open buy trade did not happen since bidP0 not same as LTP: " +
        str(reasonForTrade['LTPDoesNotEqualBidP0Long']),
        file=outputFile)
    print("Assumed close buy trade happened: " +
          str(reasonForTrade['AssumingBuyTradeHappenedShort']),
          file=outputFile)
    print("Assumed open buy trade happened: " +
          str(reasonForTrade['AssumingBuyTradeHappenedLong']),
          file=outputFile)
    print(
        "Assumed open sell trade did not happen since volume did not increase: "
        + str(reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptShort']),
        file=outputFile)
    print(
        "Assumed close sell trade did not happen since volume did not increase: "
        + str(reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptLong']),
        file=outputFile)
    print(
        "Assumed open sell trade did not happen since bidP0 not same as LTP: "
        + str(reasonForTrade['LTPDoesNotEqualAskP0Short']),
        file=outputFile)
    print(
        "Assumed close sell trade did not happen since bidP0 not same as LTP: "
        + str(reasonForTrade['LTPDoesNotEqualAskP0Long']),
        file=outputFile)
    print("Assumed open sell trade happened: " +
          str(reasonForTrade['AssumingSellTradeHappenedShort']),
          file=outputFile)
    print("Assumed close sell trade happened: " +
          str(reasonForTrade['AssumingSellTradeHappenedLong']),
          file=outputFile)
    print("The total open sell value is: " +
          str(tradeStats['totalSellValueShort']),
          file=outputFile)
    print("The total close sell value is: " +
          str(tradeStats['totalSellValueLong']),
          file=outputFile)
    print("The total close buy value is: " +
          str(tradeStats['totalBuyValueShort']),
          file=outputFile)
    print("The total open buy value is: " +
          str(tradeStats['totalBuyValueLong']),
          file=outputFile)

    try:
        averageOpenSellPrice = tradeStats[
            'totalSellValueShort'] / reasonForTrade[
                'AssumingSellTradeHappenedShort']
        averageCloseBuyPrice = tradeStats[
            'totalBuyValueShort'] / reasonForTrade[
                'AssumingBuyTradeHappenedShort']
    except:
        averageOpenSellPrice = 0
        averageCloseBuyPrice = 0
    try:
        averageCloseSellPrice = tradeStats[
            'totalSellValueLong'] / reasonForTrade[
                'AssumingSellTradeHappenedLong']
        averageOpenBuyPrice = tradeStats['totalBuyValueLong'] / reasonForTrade[
            'AssumingBuyTradeHappenedLong']
    except:
        averageCloseSellPrice = 0
        averageOpenBuyPrice = 0

    print("Average open sell price per unit is: " + str(averageOpenSellPrice),
          file=outputFile)
    print("Average close sell price per unit is: " +
          str(averageCloseSellPrice),
          file=outputFile)
    print("Average open buy price per unit is: " + str(averageOpenBuyPrice),
          file=outputFile)
    print("Average close buy price per unit is: " + str(averageCloseBuyPrice),
          file=outputFile)
    print("The current position Short: " +
          str(tradeStats['currentPositionShort']),
          file=outputFile)
    print("The current position Long: " +
          str(tradeStats['currentPositionLong']),
          file=outputFile)
    print("Profit or loss per Qty traded Short is: " +
          str(averageOpenSellPrice - averageCloseBuyPrice),
          file=outputFile)
    print("Profit or loss per Qty traded Long is: " +
          str(averageCloseSellPrice - averageOpenBuyPrice),
          file=outputFile)
    pLPerLotShort = (averageOpenSellPrice - averageCloseBuyPrice) * 1000
    pLPerLotLong = (averageCloseSellPrice - averageOpenBuyPrice) * 1000
    print("1 lot has 1000 qty's so P/L Short per lot is: " +
          str(pLPerLotShort),
          file=outputFile)
    print("1 lot has 1000 qty's so P/L Long per lot is: " + str(pLPerLotLong),
          file=outputFile)
    print("P/L for Short trading 10 lots is: " + str(pLPerLotShort * 10),
          file=outputFile)
    print("P/L for Long trading 10 lots is: " + str(pLPerLotLong * 10),
          file=outputFile)
Exemple #11
0
def readOnceAndWrite(pFileName, pEntryList, pExitList, predictedValuesDict):
    global g_bestqty_list_for_sell, g_bestqty_list_for_buy
    attribute.initList()
    tradeStats = dict()
    tradeStats['totalSellAmountShort'] = 0
    tradeStats['totalBuyAmountShort'] = 0
    tradeStats['totalSellAmountLong'] = 0
    tradeStats['totalBuyAmountLong'] = 0
    tradeStats['NumberOfOpenBuy'] = 0
    tradeStats['NumberOfCloseSell'] = 0
    tradeStats['NumberOfOpenSell'] = 0
    tradeStats['NumberOfCloseBuy'] = 0
    tradeStats['currentPositionShort'] = 0
    tradeStats['currentPositionLong'] = 0
    noPredictionForThisRow = 0
    currentSellPredictedValue = 0
    currentBuyPredictedValue = 0
    entryCL = float(pEntryList[3])/100
    entryCl0 = float(pEntryList[2])/100
    entryCL1 = float(pEntryList[1])/100
    entryCL2 = float(pEntryList[0])/100
    exitCL = float(pExitList[3])/100
    exitCl0 = float(pExitList[2])/100
    exitCL1 = float(pExitList[1])/100
    exitCL2 = float(pExitList[0])/100   
    print (entryCL, exitCL ,entryCl0, entryCL1 , entryCL2 )
    #  entryCLCutoff = float(args.entryCLCutoff)
    #  exitCLCutoff = float(args.exitCLCutoff)
    currentIndex = 0
    l_previous_obj = None
    l_obj= None
    print("Processing the data file for trades :")
    attribute.initList()
    for currentDataRow in dataFile.matrix:
        
        l_obj = update_obj_list(currentDataRow)
        if(l_previous_obj != None):
            lReturnList = checkIfPreviousDecisionToEnterOrExitTradeWasSuccessful( l_obj , l_previous_obj , tradeStats )
            
            lDummyBidQ0 = lReturnList[0]
            lDummyAskQ0 = lReturnList[1]
            lDummyTTQForBuy = lReturnList[2]
            lDummyTTQForSell = lReturnList[3]
            if currentIndex > 0:
                if(g_bestqty_list_for_buy != {}):
                    l_best_bidq = g_bestqty_list_for_buy['qty']
                    l_best_bidp = g_bestqty_list_for_buy['price']
                else:
                    l_best_bidq = 0
                    l_best_bidp = 0
                if(g_bestqty_list_for_sell != {}):
                    l_best_askq = g_bestqty_list_for_sell['qty']
                    l_best_askp = g_bestqty_list_for_sell['price'] 
                else:
                    l_best_askq = 0
                    l_best_askp = 0
                attribute.aList[currentIndex-1][0] = l_obj.currentTimeStamp
                attribute.aList[currentIndex-1][1] = tradeStats['currentPositionLong']
                attribute.aList[currentIndex-1][2] = tradeStats['currentPositionShort']
                listOfStringsToPrint = [ str(l_previous_obj.BidQ) , str(l_previous_obj.BidP) , str(l_previous_obj.AskP) , \
                                        str(l_previous_obj.AskQ) , str(l_previous_obj.TTQ) , str(l_previous_obj.LTP) ,\
                                        str(currentSellPredictedValue) , str(l_previous_obj.EnterTradeShort) ,l_previous_obj.ReasonForTradingOrNotTradingShort , str(currentBuyPredictedValue) ,\
                                        str(l_previous_obj.EnterTradeLong) ,l_previous_obj.ReasonForTradingOrNotTradingLong , str(tradeStats['NumberOfCloseBuy']),\
                                        str(tradeStats['NumberOfOpenBuy']),str(tradeStats['NumberOfOpenSell']),\
                                        str(tradeStats['NumberOfCloseBuy']),str(lDummyBidQ0),str(lDummyAskQ0),\
                                        str(lDummyTTQForBuy),str(lDummyTTQForSell),str(l_best_bidq),str(l_best_bidp),str(l_best_askp), str(l_best_askq) ]
                attribute.aList[currentIndex-1][3] =  ";".join(listOfStringsToPrint)
            currentTimeStamp = common.convertTimeStampFromStringToFloat(currentDataRow[colNumberOfData.TimeStamp])
            
            try:
                currentSellPredictedValue = float(predictedValuesDict[l_obj.currentTimeStamp]['sell']) 
                currentBuyPredictedValue  = float(predictedValuesDict[l_obj.currentTimeStamp]['buy'])
            except:
                noPredictionForThisRow += 1
            
            #short decisions
            if(currentSellPredictedValue >= entryCL):
                g_bestqty_list_for_sell = {}
                l_obj.standingAtAsk = 0
                l_obj.EnterTradeShort = 1
                l_obj.standingAtAskMinus1pip = 0
            elif(currentSellPredictedValue >= entryCL0):
                g_bestqty_list_for_sell = {}
                l_obj.standingAtAsk = 0
                l_obj.standingAtAskMinus1pip = 1
                l_obj.EnterTradeShort = 1
            elif (currentSellPredictedValue >= entryCL1):
                l_obj.standingAtAskMinus1pip = 0
                if(l_previous_obj.standingAtAsk  == 0):
                    g_bestqty_list_for_sell = {}
                l_obj.standingAtAsk = 1
                l_obj.EnterTradeShort = 1
            elif (l_previous_obj.standingAtAsk == 1 and currentSellPredictedValue >= entryCL2):
                l_obj.standingAtAsk = 1
                l_obj.EnterTradeShort = 1
            #close Long positions
            elif((currentBuyPredictedValue >= exitCL)and tradeStats['currentPositionShort'] > 0):
                l_obj.EnterTradeShort = -1  # Implies to exit the trade
                g_bestqty_list_for_buy = {}
                l_obj.CloseStandingAtBid = 0
            elif((currentBuyPredictedValue >= exitCL0)and tradeStats['currentPositionShort'] > 0):
                l_obj.EnterTradeShort = -1  # Implies to exit the trade
                g_bestqty_list_for_buy = {}
                l_obj.CloseStandingAtBidPlus1pip = 1
                l_obj.CloseStandingAtBid = 0
            elif((currentBuyPredictedValue >= exitCL1)and tradeStats['currentPositionShort'] > 0):
                l_obj.EnterTradeShort = -1  # Implies to exit the trade
                if(l_previous_obj.CloseStandingAtBid  == 0):
                    g_bestqty_list_for_sell = {}
                g_bestqty_list_for_buy = {}
                l_obj.CloseStandingAtBidPlus1pip = 0
                l_obj.CloseStandingAtBid = 1
            elif(l_previous_obj.CloseStandingAtBid  == 1 and (currentBuyPredictedValue >= exitCL2)and tradeStats['currentPositionShort'] > 0):
                l_obj.EnterTradeShort = -1  # Implies to exit the trade
                l_obj.CloseStandingAtBidPlus1pip = 0
                l_obj.CloseStandingAtBid = 1                              
            else:
                g_bestqty_list_for_sell = {}
                l_obj.standingAtAsk = 0
                l_obj.standingAtAskMinus1pip = 0
                l_obj.CloseStandingAtBidPlus1pip = 0
                l_obj.CloseStandingAtBid = 0
                l_obj.EnterTradeShort = 0  # Implies make no change
                
            #       if EnterTradeShort == 1:
            #           if currentPredictedValue <= entryCLCutoff:
            #               enterTrade = 0
            #       if enterTrade == -1:
            #           if currentPredictedValue >= exitCLCutoff:
            #               enterTrade = 0
                    
            #long decisions
            if(currentBuyPredictedValue >= entryCL):
                g_bestqty_list_for_buy = {}
                l_obj.standingAtBid = 0
                l_obj.EnterTradeLong = 1
            elif(currentBuyPredictedValue >= entryCL0):
                g_bestqty_list_for_buy = {}
                l_obj.standingAtBid = 0
                l_obj.standingAtBidPlus1pip = 1
                l_obj.EnterTradeLong = 1
            elif (currentBuyPredictedValue >= entryCL1):
                if(l_previous_obj.standingAtBid  == 0):
                    g_bestqty_list_for_buy = {}
                l_obj.standingAtBid = 1
                l_obj.EnterTradeLong = 1
                l_obj.standingAtBidPlus1pip = 0
            elif (l_previous_obj.standingAtBid == 1 and currentBuyPredictedValue >= entryCL2):
                l_obj.standingAtBid = 1
                l_obj.EnterTradeLong = 1
            #Close Long positions
            elif((currentSellPredictedValue >= exitCL ) and tradeStats['currentPositionLong'] > 0):
                l_obj.EnterTradeLong = -1  # Implies to exit the trade
                l_obj.CloseStandingAtAsk = 0
                l_obj.CloseStandingAtAskMinus1pip = 0
            elif((currentSellPredictedValue >= exitCL0 ) and tradeStats['currentPositionLong'] > 0):
                l_obj.EnterTradeLong = -1  # Implies to exit the trade
                l_obj.CloseStandingAtAsk = 0
                l_obj.CloseStandingAtAskMinus1pip = 1
            elif((currentSellPredictedValue >= exitCL1 ) and tradeStats['currentPositionLong'] > 0):
                l_obj.EnterTradeLong = -1  # Implies to exit the trade
                if(l_previous_obj.CloseStandingAtAsk  == 0):
                    g_bestqty_list_for_sell = {}
                l_obj.CloseStandingAtAsk = 1
                l_obj.CloseStandingAtAskMinus1pip = 0
            elif(l_previous_obj.CloseStandingAtAsk == 1 and (currentSellPredictedValue >= exitCL2 ) and tradeStats['currentPositionLong'] > 0):
                l_obj.EnterTradeLong = -1  # Implies to exit the trade
                l_obj.CloseStandingAtAsk = 1
                l_obj.CloseStandingAtAskMinus1pip = 0
            else:
                g_bestqty_list_for_buy = {}
                l_obj.standingAtBid = 0
                l_obj.CloseStandingAtAsk = 0
                l_obj.standingAtBidPlus1pip = 0
                l_obj.CloseStandingAtAskMinus1pip = 0
                l_obj.EnterTradeLong = 0  # Implies make no change
            

        
        l_previous_obj = l_obj
        currentIndex = currentIndex + 1

# Squaring off if some open position there   
    if tradeStats['currentPositionLong'] > 0:
        tradeStats['NumberOfCloseSell'] += tradeStats['currentPositionLong']
        tradeStats['totalSellAmountLong'] += tradeStats['currentPositionLong'] * (l_previous_obj.BidP)
        tradeStats['currentPositionLong'] = 0
        l_obj.ReasonForTradingOrNotTradingLong = 'CloseSell(Hitting)'
    if tradeStats['currentPositionShort'] > 0:
        tradeStats['NumberOfCloseBuy'] += tradeStats['currentPositionShort']
        tradeStats['totalBuyAmountShort'] += tradeStats['currentPositionShort'] * (l_previous_obj.AskP)
        tradeStats['currentPositionShort'] = 0
        l_obj.ReasonForTradingOrNotTradingLong = 'CloseBuy(Hitting)'
    
    attribute.aList[currentIndex-1][0] = currentTimeStamp
    attribute.aList[currentIndex-1][1] = tradeStats['currentPositionLong']
    attribute.aList[currentIndex-1][2] = tradeStats['currentPositionShort']
    if(g_bestqty_list_for_buy != {}):
        l_best_bidq = g_bestqty_list_for_buy['qty']
        l_best_bidp = g_bestqty_list_for_buy['price'] 
    else:
        l_best_bidq = 0
        l_best_bidp = 0
    if(g_bestqty_list_for_sell != {}):
        l_best_askq = g_bestqty_list_for_sell['qty']
        l_best_askp = g_bestqty_list_for_sell['price'] 
    else:
        l_best_askq = 0
        l_best_askp = 0
    listOfStringsToPrint = [ str(l_previous_obj.BidQ) , str(l_previous_obj.BidP) , str(l_previous_obj.AskP) , \
                            str(l_previous_obj.AskQ) , str(l_previous_obj.TTQ) , str(l_previous_obj.LTP) ,\
                            str(currentSellPredictedValue) , str(l_previous_obj.EnterTradeShort) ,l_previous_obj.ReasonForTradingOrNotTradingShort , str(currentBuyPredictedValue) ,\
                            str(l_previous_obj.EnterTradeLong) ,l_previous_obj.ReasonForTradingOrNotTradingLong , str(tradeStats['NumberOfCloseBuy']),\
                            str(tradeStats['NumberOfOpenBuy']),str(tradeStats['NumberOfOpenSell']),\
                            str(tradeStats['NumberOfCloseBuy']),str(lDummyBidQ0),str(lDummyAskQ0),\
                            str(lDummyTTQForBuy),str(lDummyTTQForSell),str(l_best_bidq),str(l_best_bidp),str(l_best_askp), str(l_best_askq) ]
    attribute.aList[currentIndex-1][3] =  ";".join(listOfStringsToPrint) 
    
    dirName = args.pd.replace('/ro/','/rs/')
    tradeLogMainDirName = dirName+"/t/"
    if not os.path.exists(tradeLogMainDirName):
        os.mkdir(tradeLogMainDirName)
    tradeLogSubDirectoryName =  tradeLogMainDirName + mainExperimentName+"/"
    if not os.path.exists(tradeLogSubDirectoryName):
        os.mkdir(tradeLogSubDirectoryName)
    
    fileName = tradeLogSubDirectoryName + pFileName + ".trade" 
    lHeaderColumnNamesList  = ['TimeStamp','CurrentPositionLong','CurrentPositionShort','BidQ0','BidP0','AskP0','AskQ0','TTQ','LTP','CurPredValueShort','EnterTradeShort','ReasonForTradingOrNotTradingShort','CurPredValueLong','EnterTradeLong','ReasonForTradingOrNotTradingLong','totalBuyTradeShort','totalBuyLong','totalSellShort','totalSellLong','DummyBidQ0','DummyAskQ0','DummyTTQChangeForSell','DummyTTQChangeForBuy' ,'BestBidQ','BestBidP','BestAskP','BestAskQ']
    attribute.writeToFile(fileName , lHeaderColumnNamesList)
    
    tradeResultMainDirName = dirName+"/r/"
    if not os.path.exists(tradeResultMainDirName):
        os.mkdir(tradeResultMainDirName)
    tradeResultSubDirectoryName =  tradeResultMainDirName + mainExperimentName+"/"
    if not os.path.exists(tradeResultSubDirectoryName):
        os.mkdir(tradeResultSubDirectoryName)
    fileName = tradeResultSubDirectoryName+pFileName+".result" 
    outputFile = open(fileName,"w")
    
    #changed file write to modify it to Short Long version
    print("Starting to write: "+fileName)
    print("The gross profit for Short are: " + str(tradeStats['totalSellAmountShort'] - tradeStats['totalBuyAmountShort']), file = outputFile)
    print("The gross profit for Long are: " + str(tradeStats['totalSellAmountLong'] - tradeStats['totalBuyAmountLong']), file = outputFile)
    print("The total open sell amount is: " + str(tradeStats['totalSellAmountShort']), file = outputFile)
    print("The total close sell amount is: " + str(tradeStats['totalSellAmountLong']), file = outputFile)
    print("The total close buy amount is: " + str(tradeStats['totalBuyAmountShort']), file = outputFile)
    print("The total open buy amount is: " + str(tradeStats['totalBuyAmountLong']), file = outputFile)
    print("Number open sell trade happened: " + str(tradeStats['NumberOfOpenSell']), file = outputFile)
    print("Number close sell trade happened: " + str(tradeStats['NumberOfCloseSell']), file = outputFile)
    print("Number close buy trade happened: " + str(tradeStats['NumberOfCloseBuy']), file = outputFile)
    print("Number open buy trade happened: " + str(tradeStats['NumberOfOpenBuy']), file = outputFile)
       
    try:
        averageOpenSellPrice = tradeStats['totalSellAmountShort']/tradeStats['NumberOfOpenSell']
        averageCloseBuyPrice = tradeStats['totalBuyAmountShort']/tradeStats['NumberOfCloseBuy']
    except:
        averageOpenSellPrice = 0 
        averageCloseBuyPrice = 0
    try:
        averageCloseSellPrice = tradeStats['totalSellAmountLong']/tradeStats['NumberOfCloseSell']
        averageOpenBuyPrice = tradeStats['totalBuyAmountLong']/tradeStats['NumberOfOpenBuy']
    except:
        averageCloseSellPrice = 0
        averageOpenBuyPrice = 0 
    
    print("Average open sell price per unit is: " + str(averageOpenSellPrice), file = outputFile)
    print("Average close sell price per unit is: " + str(averageCloseSellPrice), file = outputFile)
    print("Average open buy price per unit is: " + str(averageOpenBuyPrice), file = outputFile)
    print("Average close buy price per unit is: " + str(averageCloseBuyPrice), file = outputFile)
    print("The current position Short: " + str(tradeStats['currentPositionShort']), file = outputFile)
    print("The current position Long: " + str(tradeStats['currentPositionLong']), file = outputFile)
    print("Profit or loss per Qty traded Short is: " + str(averageOpenSellPrice - averageCloseBuyPrice), file = outputFile)
    print("Profit or loss per Qty traded Long is: " + str(averageCloseSellPrice - averageOpenBuyPrice), file = outputFile)
    pLPerLotShort=(averageOpenSellPrice - averageCloseBuyPrice)* 1000
    pLPerLotLong=(averageCloseSellPrice - averageOpenBuyPrice)* 1000
    print("1 lot has 1000 qty's so P/L Short per lot is: " + str(pLPerLotShort), file = outputFile)
    print("1 lot has 1000 qty's so P/L Long per lot is: " + str(pLPerLotLong), file = outputFile)
    print("P/L for Short trading 10 lots is: " + str(pLPerLotShort * 10), file = outputFile)
    print("P/L for Long trading 10 lots is: " + str(pLPerLotLong * 10), file = outputFile)
Exemple #12
0
def extractAttributeFromDataMatrix(args):
    if args.n == None:
        N = 5
    else:
        N = int(args.n)
    try:
        #Value of argument c can be taken as BidP0 and AskP0
        args.c
    except:
        print "Since -c has not been specified I cannot proceed"
        os._exit()
    colNumberOfAttribute = eval("colNumberOfData." + args.c)
    colNumberOfOppositeAttribute = eval(
        "colNumberOfData.BestBidP") if "ask" in args.c else eval(
            "colNumberOfData.BestAskP")
    colNumberOfTimeStamp = colNumberOfData.TimeStamp
    if "ask" in args.c.lower():
        colAttributeRowNo = [
            colNumberOfData.BestAskP, colNumberOfData.BestAskP1
        ]  #, colNumberOfData.BestAskP2, colNumberOfData.BestAskP3, colNumberOfData.BestAskP4]
    else:
        colAttributeRowNo = [
            colNumberOfData.BestBidP, colNumberOfData.BestBidP1
        ]  #, colNumberOfData.BestBidP2, colNumberOfData.BestBidP3, colNumberOfData.BestBidP4]
    numberOfRowsInLastNSecs = 0
    queueOfValuesInLastNSecs = deque()
    totalOfRowsInLastNSecs = 0.0
    totalOfRowsInLastNSecs = 0.0
    totalOfQtyInLastNSecs = 0.0
    timeOfOldestRow = common.convertTimeStampFromStringToFloat(
        dataFile.matrix[0][colNumberOfTimeStamp], args.cType)
    currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
    lengthOfDataMatrix = len(dataFile.matrix)
    while (currentRowNumberForWhichFeatureValueIsBeingCalculated <
           lengthOfDataMatrix):
        colAttributeRow = map(float, [
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][i]
            for i in colAttributeRowNo
        ])
        timeOfCurrentRow = common.convertTimeStampFromStringToFloat(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated]
            [colNumberOfTimeStamp], args.cType)
        timeElapsed = timeOfCurrentRow - timeOfOldestRow
        oppositeAttribute = float(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated]
            [colNumberOfOppositeAttribute])
        if (timeElapsed < N):
            if currentRowNumberForWhichFeatureValueIsBeingCalculated != 0:
                previousRowPrice = float(dataFile.matrix[
                    currentRowNumberForWhichFeatureValueIsBeingCalculated -
                    1][colNumberOfAttribute])
            else:
                previousRowPrice = 0
            currentPrice = float(
                dataFile.
                matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated]
                [colNumberOfAttribute])
            currentLTP = float(
                dataFile.
                matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    colNumberOfData.LTP])
            currentMsgCode = dataFile.matrix[
                currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    colNumberOfData.MsgCode]
            newQty = int(
                dataFile.
                matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    colNumberOfData.NewQ])
            newPrice = int(
                dataFile.
                matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    colNumberOfData.NewP])
            oldQty = int(
                dataFile.
                matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    colNumberOfData.OldQ])
            oldPrice = int(
                dataFile.
                matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    colNumberOfData.OldP])
            if (currentMsgCode == 'T' and ((oppositeAttribute <= currentLTP)
                                           if "bid" in args.c.lower() else
                                           (oppositeAttribute >= currentLTP))):
                totalOfRowsInLastNSecs += newQty * currentLTP
                cellValueTotal = newQty * currentLTP
                totalOfQtyInLastNSecs += newQty
                cellValueQty = newQty
            if (currentMsgCode == 'M' and (newPrice in colAttributeRow)
                    and ((newPrice > oldPrice) if "bid" in args.c.lower() else
                         (newPrice < oldPrice))):
                totalOfRowsInLastNSecs += newPrice * newQty
                cellValueTotal = newPrice * newQty
                totalOfQtyInLastNSecs += newQty
                cellValueQty = newQty
            elif (currentMsgCode == 'N' and (newPrice in colAttributeRow)):
                totalOfRowsInLastNSecs += newPrice * newQty
                cellValueTotal = newPrice * newQty
                totalOfQtyInLastNSecs += newQty
                cellValueQty = newQty
            else:
                cellValueTotal = 0
                cellValueQty = 0
            attribute.aList[
                currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    0] = common.convertTimeStampFromStringToDecimal(
                        dataFile.matrix[
                            currentRowNumberForWhichFeatureValueIsBeingCalculated]
                        [colNumberOfTimeStamp], args.cType)
            attribute.aList[
                currentRowNumberForWhichFeatureValueIsBeingCalculated][1] = (
                    float(totalOfRowsInLastNSecs) / totalOfQtyInLastNSecs
                ) if totalOfQtyInLastNSecs > 0 else currentPrice  # in 1st iteration currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
            attribute.aList[
                currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    2] = str(currentPrice) + ";" + str(currentLTP) + ";" + str(
                        cellValueTotal) + ";" + str(
                            currentMsgCode) + ";" + str(timeElapsed)
            queueOfValuesInLastNSecs.append(
                [cellValueTotal, timeOfCurrentRow, cellValueQty])
            numberOfRowsInLastNSecs += 1  # Every append gets a +1
            currentRowNumberForWhichFeatureValueIsBeingCalculated += 1
            continue  # Since we are going back 1 row from current we cannot get data from current row
        else:
            # We need to reset the timeOfOldestRow since timeElapsed has exceeded N seconds
            while (timeElapsed >= N):
                if (len(queueOfValuesInLastNSecs) == 0):
                    timeOfOldestRow = timeOfCurrentRow
                    timeElapsed = 0
                    if (numberOfRowsInLastNSecs != 0):
                        print "Sanity check: This condition is not possible logically. numberOfRowsInLastNSecs should been 0. There has been an unknown error"
                        sys.exit(-1)
                    if (totalOfRowsInLastNSecs != 0):
                        print "Sanity check: This condition is not possible logically. totalOfRowsInLastNSecs should have been 0. There has been an unknown error"
                        sys.exit(-1)
                else:
                    oldestElementInQueue = queueOfValuesInLastNSecs.popleft()
                    colValueInOldestElementInQueue = oldestElementInQueue[0]
                    colQtyInOldestElementInQueue = oldestElementInQueue[2]
                    totalOfRowsInLastNSecs -= colValueInOldestElementInQueue
                    totalOfQtyInLastNSecs -= colQtyInOldestElementInQueue
                    if len(queueOfValuesInLastNSecs) == 0:
                        timeElapsed = 0
                        timeOfOldestRow = timeOfCurrentRow
                    else:
                        timeOfOldestRow = queueOfValuesInLastNSecs[0][1]
                    numberOfRowsInLastNSecs -= 1  # every pop from the queue gets a -1
                    timeElapsed = timeOfCurrentRow - timeOfOldestRow
                    if (len(queueOfValuesInLastNSecs) !=
                            numberOfRowsInLastNSecs):
                        print "Sanity check: This condition is not possible logically. There has been an unknown error"
                        sys.exit(-1)

        print "Processed row number " + str(
            currentRowNumberForWhichFeatureValueIsBeingCalculated)

    lNameOfFeaturePrinted = "fMarketTradedQtySumOfCol" + args.c + "InLast" + str(
        args.n) + "Secs"
    return [
        "TimeStamp", lNameOfFeaturePrinted, args.c, "LTP",
        "TradedQtyWhichIs=" + args.c, "MsgCode", "TimeElapsed"
    ]
Exemple #13
0
def main():
    
   dirName = args.pd.replace('/ro/','/rs/')  
   tradeResultMainDirName = dirName+"/r/"
   if not os.path.exists(tradeResultMainDirName):
        os.mkdir(tradeResultMainDirName)
   tradeResultSubDirectoryName =  tradeResultMainDirName + mainExperimentName+"/"
   if not os.path.exists(tradeResultSubDirectoryName):
        os.mkdir(tradeResultSubDirectoryName)
   outputFileName = tradeResultSubDirectoryName+experimentName+initialFileName+".result" 
   
   #if os.path.isfile(outputFileName):
   #    print("The results file already exisits. Delete it to run the program again" + outputFileName)
   #    return 1
   dataFile.getDataIntoMatrix(args.pd)
   predictedValuesDict = dict()
   getPredictedValuesIntoDict(predictedValuesDict)
   enterTrade = 0
   ttqAtTimeOfPreviousDataRow = 0
   askP0AtTimeOfPreviousDataRow = 0
   bidP0AtTimeOfPreviousDataRow = 0
   tradeStats = dict()
   tradeStats['totalSellValue'] = 0
   tradeStats['totalBuyValue'] = 0
   tradeStats['currentPosition'] = 0
   noPredictionForThisRow = 0
   currentPredictedValue = 0
   entryCL = float(args.entryCL)/100
   exitCL = float(args.exitCL)/100
   numberOfTimesAskedToEnterTrade = 0
   numberOfTimesAskedToExitTrade = 0
   reasonForTrade = dict()
   reasonForTrade['LTPDoesNotEqualBidP0'] = 0
   reasonForTrade['VolumeDidNotIncreaseSufficientlyDuringBuyAttempt'] = 0
   reasonForTrade['AssumingBuyTradeHappened'] = 0
   reasonForTrade['LTPDoesNotEqualAskP0'] = 0
   reasonForTrade['VolumeDidNotIncreaseSufficientlyDuringSellAttempt'] = 0
   reasonForTrade['AssumingSellTradeHappened'] = 0
   varsAtTimeOfTradeDecision = dict()
   varsAtTimeOfTradeDecision['decision'] = 0
   varsAtTimeOfTradeDecision['bidP0'] = 0
   varsAtTimeOfTradeDecision['qtyTradedAtBidP0'] = 0
   varsAtTimeOfTradeDecision['ttq'] = 0
   varsAtTimeOfTradeDecision['askP0'] = 0
   varsAtTimeOfTradeDecision['qtyTradedAtAskP0'] = 0

   print("Processing the data file for trades :")

   for currentDataRow in dataFile.matrix: #currentDataRow has the new tick
       if(enterTrade == varsAtTimeOfTradeDecision['decision'] and enterTrade == 1 and varsAtTimeOfTradeDecision['bidP0'] >= float(currentDataRow[colNumberOfData.BidP0])):
           print("Position not lost in the queue")
           if(varsAtTimeOfTradeDecision['bidP0'] == float(currentDataRow[colNumberOfData.LTP]) and float(currentDataRow[colNumberOfData.TTQ]) > varsAtTimeOfTradeDecision['ttq']):
               varsAtTimeOfTradeDecision['qtyTradedAtBidP0'] += float(currentDataRow[colNumberOfData.TTQ]) - varsAtTimeOfTradeDecision['ttq']
       elif(enterTrade == varsAtTimeOfTradeDecision['decision'] and enterTrade == -1 and varsAtTimeOfTradeDecision['askP0'] <= float(currentDataRow[colNumberOfData.AskP0])):
           print("Position not lost in the queue")
           if(varsAtTimeOfTradeDecision['askP0'] == float(currentDataRow[colNumberOfData.LTP]) and float(currentDataRow[colNumberOfData.TTQ]) > varsAtTimeOfTradeDecision['ttq']):
               varsAtTimeOfTradeDecision['qtyTradedAtAskP0'] += float(currentDataRow[colNumberOfData.TTQ]) - varsAtTimeOfTradeDecision['ttq']
       else:
           positionLostInQueue = True

       checkIfPreviousDecisionToEnterOrExitTradeWasSuccessful(currentDataRow,varsAtTimeOfTradeDecision,enterTrade,tradeStats,reasonForTrade)
       
       currentTimeStamp = common.convertTimeStampFromStringToFloat(currentDataRow[colNumberOfData.TimeStamp])

       try:
           currentPredictedValue = float(predictedValuesDict[currentTimeStamp])
       except:
           noPredictionForThisRow += 1

       if(currentPredictedValue > entryCL):
           enterTrade = 1
           numberOfTimesAskedToEnterTrade += 1
       elif(currentPredictedValue < exitCL and tradeStats['currentPosition'] > 0):
           numberOfTimesAskedToExitTrade += 1
           enterTrade = -1  # Implies to exit the trade
       else:
           enterTrade = 0  # Implies make no change
       
       if(positionLostInQueue == True):
           varsAtTimeOfTradeDecision = updateVarsAtTimeOfTradeDecision(currentDataRow,enterTrade)   # This implies that the position in the queue is lost.

    
   outputFile = open(outputFileName,"w")
   print("Starting to write: "+outputFileName)
   print("The net results are: " + str(tradeStats['totalSellValue'] - tradeStats['totalBuyValue']), file = outputFile)    
   print("Number of rows for which there is no prediction: " + str(noPredictionForThisRow), file = outputFile)    
   print("Number of times asked to enter trade: " + str(numberOfTimesAskedToEnterTrade), file = outputFile)    
   print("Number of times asked to exit trade: " + str(numberOfTimesAskedToExitTrade), file = outputFile)    
   print("Assumed buy trade did not happen since volume did not increase: " + str(reasonForTrade['VolumeDidNotIncreaseSufficientlyDuringBuyAttempt']), file = outputFile)
   print("Assumed buy trade did not happen since bidP0 not same as LTP: " + str(reasonForTrade['LTPDoesNotEqualBidP0']), file = outputFile)
   print("Assumed buy trade happened: " + str(reasonForTrade['AssumingBuyTradeHappened']), file = outputFile)
   print("Assumed sell trade did not happen since volume did not increase: " + str(reasonForTrade['VolumeDidNotIncreaseSufficientlyDuringSellAttempt']), file = outputFile)
   print("Assumed sell trade did not happen since bidP0 not same as LTP: " + str(reasonForTrade['LTPDoesNotEqualAskP0']), file = outputFile)
   print("Assumed sell trade happened: " + str(reasonForTrade['AssumingSellTradeHappened']), file = outputFile)
   print("The total sell value is: " + str(tradeStats['totalSellValue']), file = outputFile)
   print("The total buy value is: " + str(tradeStats['totalBuyValue']), file = outputFile)
   try:
       averageSellPrice = tradeStats['totalSellValue']/reasonForTrade['AssumingSellTradeHappened']
       averageBuyPrice = tradeStats['totalBuyValue']/reasonForTrade['AssumingBuyTradeHappened']
   except:
       averageSellPrice = 0 
       averageBuyPrice = 0
   print("Average sell price per unit is: " + str(averageSellPrice), file = outputFile)
   print("Average buy price per unit is: " + str(averageBuyPrice), file = outputFile)
   print("The current position: " + str(tradeStats['currentPosition']), file = outputFile)
   print("Profit or loss per Qty traded is: " + str(averageSellPrice - averageBuyPrice), file = outputFile)
   pLPerLot=(averageSellPrice - averageBuyPrice)* 1000
   print("1 lot has 1000 qty's so P/L per lot is: " + str(pLPerLot), file = outputFile)
   print("P/L for trading 10 lots is: " + str(pLPerLot * 10), file = outputFile)
Exemple #14
0
def extractAttributeFromDataMatrix(args):
   global currentRowPriceList , currentRowQtyList , currentRowLTP , currentRowTTQ , currentRowMsgCode , currentRowOrderType , currentRowNewP , currentRowNewQ , currentRowOldP , currentRowOldQ
   global prevRowPriceList , prevRowQtyList , prevRowLTP , prevRowTTQ , prevRowMsgCode , prevRowOrderType , prevRowNewP , prevRowNewQ , prevRowOldP , prevRowOldQ
   global new_theta , cancel_theta , mod_theta , trade_theta
   if args.n == None:
      N = 5
   else:
      N = int(args.n) 
   try:
      args.c
   except:
      print "Since -c has not been specified I cannot proceed"
      os._exit()
   colNumberOfPrice = eval("colNumberOfData."+ args.c + "P0" )
   colNumberOfQty = eval("colNumberOfData."+ args.c + "Q0")
   colNumberOfTimeStamp = colNumberOfData.TimeStamp
   lFirstTimeWindowPrepared = False 
   tickSize = int(args.tickSize)
   numberOfRowsInLastNSecs = 0
   queueOfValuesInLastNSecs = deque()
   dict_sum_for_price = {}
   timeOfOldestRow = common.convertTimeStampFromStringToFloat(dataFile.matrix[0][colNumberOfTimeStamp],args.cType)
   currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
   lengthOfDataMatrix = len(dataFile.matrix)
   while (currentRowNumberForWhichFeatureValueIsBeingCalculated < lengthOfDataMatrix):
      timeOfCurrentRow = common.convertTimeStampFromStringToFloat(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfTimeStamp],args.cType)
      timeElapsed = timeOfCurrentRow - timeOfOldestRow
      dataFileCurrentRow = dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated]
      currentRowPriceList = [ float(dataFileCurrentRow[colNumberOfPrice]) , float(dataFileCurrentRow[colNumberOfPrice+2]) , float(dataFileCurrentRow[colNumberOfPrice + 4]) , float(dataFileCurrentRow[colNumberOfPrice + 6]) , float(dataFileCurrentRow[colNumberOfPrice + 8]) ]
      currentRowQtyList = [int(dataFileCurrentRow[colNumberOfQty]),int(dataFileCurrentRow[colNumberOfQty + 2]),int(dataFileCurrentRow[colNumberOfQty + 4]),int(dataFileCurrentRow[colNumberOfQty + 6]),int(dataFileCurrentRow[colNumberOfQty + 8])]
      currentRowLTP = float(dataFileCurrentRow[colNumberOfData.LTP])
      currentRowTTQ = float(dataFileCurrentRow[colNumberOfData.TTQ])
      currentRowMsgCode = dataFileCurrentRow[colNumberOfData.MsgCode]
      currentRowOrderType = dataFileCurrentRow[colNumberOfData.OrderType]
      currentRowNewP = float(dataFileCurrentRow[colNumberOfData.NewP])
      currentRowNewQ = int(dataFileCurrentRow[colNumberOfData.NewQ])
      if currentRowMsgCode == 'M':
          currentRowOldP = float(dataFileCurrentRow[colNumberOfData.OldP])
          currentRowOldQ = int(dataFileCurrentRow[colNumberOfData.OldQ])
      if (timeElapsed < N):
         theta_calculation_and_calculate_corresponding_qty(args.c,tickSize,queueOfValuesInLastNSecs,dict_sum_for_price,lFirstTimeWindowPrepared)
         priceDictionary = create_price_list_dictionary(args.c,tickSize)
         cellValue = (g_weight_list[0]*dict_sum_for_price[currentRowPriceList[0]]) + (g_weight_list[1]*dict_sum_for_price[currentRowPriceList[1]]) + (g_weight_list[2]*dict_sum_for_price[currentRowPriceList[2]]) + \
                     (g_weight_list[3]*dict_sum_for_price[currentRowPriceList[3]]) +  (g_weight_list[4]*dict_sum_for_price[currentRowPriceList[4]])
         attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][0] = common.convertTimeStampFromStringToDecimal(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfTimeStamp],args.cType)
         attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][1] = cellValue/(numberOfRowsInLastNSecs+1) # in 1st iteration currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
         attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][2] = str(cellValue) + ";" + str(numberOfRowsInLastNSecs) + ";" + str(timeElapsed) + ";" + str(dict_sum_for_price[currentRowPriceList[0]]+currentRowQtyList[0]) +\
                                                                                        ";" + str(dict_sum_for_price[currentRowPriceList[1]]+currentRowQtyList[1]) + ";" + str(dict_sum_for_price[currentRowPriceList[2]]+currentRowQtyList[2]) +\
                                                                                        ";" + str(dict_sum_for_price[currentRowPriceList[3]]+currentRowQtyList[3]) +\
                                                                                        ";" + str(dict_sum_for_price[currentRowPriceList[4]]+currentRowQtyList[4]) + ";" + str(new_theta) + ";" + str(cancel_theta) +\
                                                                                        ";" + str(trade_theta)
                                                                                        #new_theta , cancel_theta , mod_theta , trade_theta
         queueOfValuesInLastNSecs.append([cellValue,timeOfCurrentRow,priceDictionary])
         numberOfRowsInLastNSecs += 1   # Every append gets a +1 
         currentRowNumberForWhichFeatureValueIsBeingCalculated += 1
         prevRowPriceList = currentRowPriceList
         prevRowQtyList = currentRowQtyList
         prevRowLTP  = currentRowLTP
         prevRowTTQ =  currentRowTTQ
         prevRowMsgCode = currentRowMsgCode
         prevRowOrderType = currentRowOrderType
         prevRowNewP = currentRowNewP
         prevRowNewQ = currentRowNewQ
         if prevRowOrderType == 'M':
             prevRowOldP = currentRowOldP
             prevRowOldQ = currentRowOldQ
         continue     # Since we are going back 1 row from current we cannot get data from current row
      else:
         # We need to reset the timeOfOldestRow since timeElapsed has exceeded N seconds
         while(timeElapsed >= N):
            lFirstTimeWindowPrepared = True 
            if(len(queueOfValuesInLastNSecs) == 0):
               timeOfOldestRow = timeOfCurrentRow
               timeElapsed = 0
               if(numberOfRowsInLastNSecs != 0):
                  print "Sanity check: This condition is not possible logically. numberOfRowsInLastNSecs should been 0. There has been an unknown error"
                  sys.exit(-1)
            else:   
               oldestElementInQueue = queueOfValuesInLastNSecs.popleft()
               colDictInOldestElementInQueue = oldestElementInQueue[2]
               if len(queueOfValuesInLastNSecs) == 0:
                    timeElapsed = 0
                    timeOfOldestRow = timeOfCurrentRow
               else:
                    timeOfOldestRow = queueOfValuesInLastNSecs[0][1]
               numberOfRowsInLastNSecs -= 1 # every pop from the queue gets a -1
               timeElapsed = timeOfCurrentRow - timeOfOldestRow

               oldestPricesList = colDictInOldestElementInQueue.keys()
               for lPrice in oldestPricesList:
                    try:
                        dict_sum_for_price[lPrice] -= colDictInOldestElementInQueue[lPrice]
                    except:
                        pass

               if(len(queueOfValuesInLastNSecs) != numberOfRowsInLastNSecs):
                  print "Sanity check: This condition is not possible logically. There has been an unknown error"
                  sys.exit(-1)

      print "Processed row number " + str(currentRowNumberForWhichFeatureValueIsBeingCalculated)
   
   lNameOfFeaturePrinted = "fOBWaveHistQtyOf" + args.c + "InLast" + str(args.n) + "Secs"
   return [ "TimeStamp", lNameOfFeaturePrinted , "TotalOfRowsInLastNSecs" , "NumberOfRowsInLastNSecs" , "TimeElapsed","HP0","HP1","HP2","HP3","HP4","NewTheta","CancelTheta","TradeTheta"]
def getDataFileAndPredictionsIntoObjectList(targetFileObject,pFileObjectList,pOldTree,config):
    lObjectList = []
    lCurrentDataRowCount = 0
    lPrevObj = None
    fileHasHeader = 1
    headerSkipped = 0
    dataFileSep = ";"
    l_buy_data_row_list =  list(islice(targetFileObject['buy'],10000)) 
    l_sell_data_row_list =  list(islice(targetFileObject['sell'],10000)) 
    lFeatureFileRowList = {}

    for index in xrange(len(pFileObjectList)):
        if config['predictions-buy'].get(dd.gTreeVariablesPresent[index],"f").lower()!="buyprob" and config['predictions-sell'].get(dd.gTreeVariablesPresent[index],"f").lower()!="sellprob" \
        and config['predictions-sell'].get(dd.gTreeVariablesPresent[index],"f").lower()!="buyprob" and config['predictions-buy'].get(dd.gTreeVariablesPresent[index],"f").lower()!="sellprob":  
            lFeatureFileRowList[ dd.gTreeVariablesPresent[index] ] = (list(islice(pFileObjectList[index],10000)))
    while True:
        l_buy_data_row_list =  list(islice(targetFileObject['buy'],10000)) 
        l_sell_data_row_list =  list(islice(targetFileObject['sell'],10000)) 
        index = 0
        lFeatureFileRowList = {}
        for index in xrange(len(pFileObjectList)):
            lFeatureFileRowList[ dd.gTreeVariablesPresent[index] ] = (list(islice(pFileObjectList[index],dd.gNoOfLineReadPerChunk)))
        if not l_buy_data_row_list:
            print("Finished reading file")
            lObjectList.append(lPrevObj)    
            lPrevObj = None          
            break
        lengthOfDataList = len(l_buy_data_row_list)
        for features in lFeatureFileRowList:
            if lengthOfDataList != len(lFeatureFileRowList[features]):
                print("Length of data file and feature file are not same ")
                os._exit(-1)                
        for currentRowIndex in range(lengthOfDataList):
            if(fileHasHeader == 1 and headerSkipped != 1):
                headerSkipped = 1 
                continue
            lBuyDataRow = l_buy_data_row_list[currentRowIndex].rstrip().split(dataFileSep)
            lSellDataRow = l_sell_data_row_list[currentRowIndex].rstrip().split(dataFileSep)

            lCurrentDataRowTimeStamp = common.convertTimeStampFromStringToFloat(lBuyDataRow[0])
            
            lBuyTargetVariable = int(lBuyDataRow[1])
            lSellTargetVariable = int(lSellDataRow[1])
            
            lFeatureValueDict = {}
            for feature in lFeatureFileRowList:
                if config['predictions-buy'].get(feature, "f").lower() == "buyprob" or config['predictions-sell'].get(feature, "f").lower()  == "sellprob"\
                or  config['predictions-buy'].get(feature, "f").lower() == "sellprob" or config['predictions-sell'].get(feature, 'f').lower()  == "buyprob":
                    lSep = ","
                    lTimeStampIndex = 1
                    lFeatureIndex = 2
                else:
                    lSep = ";"
                    lTimeStampIndex = 0
                    lFeatureIndex = 1
                #print("Varible name" , feature)
                lFeatureFileRow = lFeatureFileRowList[feature][currentRowIndex].rstrip().split(lSep)
                lFeatureFileTimeStamp = float(lFeatureFileRow[lTimeStampIndex])
                lFeatureFileValue = float(lFeatureFileRow[lFeatureIndex])
                if lCurrentDataRowTimeStamp != lFeatureFileTimeStamp:
                    print('Time stamp of data row with feature value is not matching .\n Data row time stamp :- ' , lCurrentDataRowTimeStamp,'Feature value Time Stamp :- ' , lFeatureFileTimeStamp)
                    os._exit(-1)
                lFeatureValueDict[feature]=lFeatureFileValue
                
            dd.gOutputGlobalTree['buy'] = reading_tree.breadth_first_traversal(dd.gGlobalTree['buy'] , dd.gOutputGlobalTree['buy'] ,lBuyTargetVariable ,lFeatureValueDict  )
            dd.gOutputGlobalTree['sell'] = reading_tree.breadth_first_traversal(dd.gGlobalTree['sell'] , dd.gOutputGlobalTree['sell'] ,lSellTargetVariable ,lFeatureValueDict  )
                
            if lCurrentDataRowCount%50000 ==0:
                print("Completed reading ",lCurrentDataRowCount)
            lCurrentDataRowCount = lCurrentDataRowCount + 1 
    return lObjectList
Exemple #16
0
def readOnceAndWrite(pFileName, pIndexOfEntryOrExitCL, predictedValuesDict):
    global transactionCost , currencyDivisor 
    attribute.initList()
    enterTradeShort = 0
    enterTradeLong = 0
    ltpAtTimeOfPreviousDataRow = 0
    ttqAtTimeOfPreviousDataRow = 0
    askP0AtTimeOfPreviousDataRow = 0
    bidP0AtTimeOfPreviousDataRow = 0
    askQ0AtTimeOfPreviousDataRow = 0
    bidQ0AtTimeOfPreviousDataRow = 0
    currentTimeStamp = 0
    tradeStats = dict()
    tradeStats['totalSellValueShort'] = 0
    tradeStats['totalBuyValueShort'] = 0
    tradeStats['currentPositionShort'] = 0
    tradeStats['totalSellValueLong'] = 0
    tradeStats['totalBuyValueLong'] = 0
    tradeStats['currentPositionLong'] = 0
    noPredictionForThisRow = 0
    currentSellPredictedValue = 0
    currentBuyPredictedValue = 0
    entryCL = float(totalEntryCL[pIndexOfEntryOrExitCL])/100
    exitCL = float(totalExitCL[pIndexOfEntryOrExitCL])/100
    print (entryCL, exitCL)
    #  entryCLCutoff = float(args.entryCLCutoff)
    #  exitCLCutoff = float(args.exitCLCutoff)
    numberOfTimesAskedToEnterTradeShort = 0
    numberOfTimesAskedToEnterTradeLong = 0
    numberOfTimesAskedToExitTradeShort = 0
    numberOfTimesAskedToExitTradeLong = 0
    reasonForTrade = dict()
    reasonForTrade['LTPDoesNotEqualBidP0Short'] = 0
    reasonForTrade['LTPDoesNotEqualAskP0Short'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptShort'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptLong'] = 0
    reasonForTrade['CloseBuyTradeHappened'] = 0
    reasonForTrade['OpenBuyTradeHappened'] = 0
    reasonForTrade['LTPDoesNotEqualAskP0Long'] = 0
    reasonForTrade['LTPDoesNotEqualBidP0Long'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptShort'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptLong'] = 0
    reasonForTrade['OpenSellTradeHappened'] = 0
    reasonForTrade['CloseSellTradeHappened'] = 0
    reasonForTrade['PrevWasOurOrder'] = 0
    lReasonForTradingOrNotTradingShort = ""
    lReasonForTradingOrNotTradingLong = ""
    currentIndex = 0
    print("Processing the data file for trades :")
    attribute.initList()
    for currentDataRow in dataFile.matrix[10000:]:
        
        lReturnList = checkIfPreviousDecisionToEnterOrExitTradeWasSuccessful(currentDataRow,ttqAtTimeOfPreviousDataRow,askP0AtTimeOfPreviousDataRow,bidP0AtTimeOfPreviousDataRow,\
                                                                             askQ0AtTimeOfPreviousDataRow , bidQ0AtTimeOfPreviousDataRow , enterTradeShort,enterTradeLong,tradeStats,reasonForTrade,\
                                                                             lReasonForTradingOrNotTradingLong,lReasonForTradingOrNotTradingShort )
    
        lReasonForTradingOrNotTradingShort = lReturnList[0]
        lReasonForTradingOrNotTradingLong = lReturnList[1] 
        lDummyBidQ0 = lReturnList[2]
        lDummyAskQ0 = lReturnList[3]
        lDummyTTQForBuy = lReturnList[4]
        lDummyTTQForSell = lReturnList[5]
        if currentIndex > 0:
            attribute.aList[currentIndex-1][0] = currentTimeStamp
            attribute.aList[currentIndex-1][1] = tradeStats['currentPositionLong']
            attribute.aList[currentIndex-1][2] = tradeStats['currentPositionShort']
            listOfStringsToPrint = [ str(bidQ0AtTimeOfPreviousDataRow) , str(bidP0AtTimeOfPreviousDataRow) , str(askP0AtTimeOfPreviousDataRow) , \
                                    str(askQ0AtTimeOfPreviousDataRow) , str(ttqAtTimeOfPreviousDataRow) , str(ltpAtTimeOfPreviousDataRow) ,\
                                    str(currentSellPredictedValue) , str(enterTradeShort) ,lReasonForTradingOrNotTradingShort , str(currentBuyPredictedValue) ,\
                                    str(enterTradeLong) ,lReasonForTradingOrNotTradingLong , str(reasonForTrade['CloseBuyTradeHappened']),\
                                    str(reasonForTrade['OpenBuyTradeHappened']),str(reasonForTrade['OpenSellTradeHappened']),\
                                    str(reasonForTrade['CloseSellTradeHappened']),str(lDummyBidQ0),str(lDummyAskQ0),\
                                    str(lDummyTTQForBuy),str(lDummyTTQForSell)]
            attribute.aList[currentIndex-1][3] =  ";".join(listOfStringsToPrint)
        currentTimeStamp = common.convertTimeStampFromStringToFloat(currentDataRow[colNumberOfData.TimeStamp])
    
        try:
            currentSellPredictedValue = float(predictedValuesDict[currentTimeStamp]['sell']) 
            currentBuyPredictedValue  = float(predictedValuesDict[currentTimeStamp]['buy'])
        except:
            noPredictionForThisRow += 1
    
        #short decisions
        if(currentSellPredictedValue >= entryCL and tradeStats['currentPositionLong'] == 0):
            enterTradeShort = 1
            numberOfTimesAskedToEnterTradeShort += 1
        elif(currentBuyPredictedValue >= exitCL and tradeStats['currentPositionShort'] > 0):
            numberOfTimesAskedToExitTradeShort += 1
            enterTradeShort = -1  # Implies to exit the trade
        else:
            enterTradeShort = 0  # Implies make no change
            
    #       if enterTradeShort == 1:
    #           if currentPredictedValue <= entryCLCutoff:
    #               enterTrade = 0
    #       if enterTrade == -1:
    #           if currentPredictedValue >= exitCLCutoff:
    #               enterTrade = 0
                
        #long decisions
        if(currentBuyPredictedValue >= entryCL and tradeStats['currentPositionShort'] == 0):
            enterTradeLong = 1
            numberOfTimesAskedToEnterTradeLong += 1
        elif(currentSellPredictedValue >= exitCL and tradeStats['currentPositionLong'] > 0):
            numberOfTimesAskedToExitTradeLong += 1
            enterTradeLong = -1  # Implies to exit the trade
        else:
            enterTradeLong = 0  # Implies make no change
        
        ttqAtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.TTQ])
        if((args.e).find("nsefut") >= 0):
            askP0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.BestAskP])
            bidP0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.BestBidP])
            askQ0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.BestAskQ])
            bidQ0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.BestBidQ])
        else:
            askP0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.AskP0])
            bidP0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.BidP0])
            askQ0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.AskQ0])
            bidQ0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.BidQ0])
        ltpAtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.LTP])
        currentIndex = currentIndex + 1
    
    # Squaring off if some open position there   
    if tradeStats['currentPositionLong'] > 0:
        reasonForTrade['CloseSellTradeHappened'] += tradeStats['currentPositionLong']
        tradeStats['totalSellValueLong'] += tradeStats['currentPositionLong'] * (bidP0AtTimeOfPreviousDataRow)
        tradeStats['currentPositionLong'] = 0
        lReasonForTradingOrNotTradingLong = 'CloseSell(Hitting)'
    if tradeStats['currentPositionShort'] > 0:
        reasonForTrade['CloseBuyTradeHappened'] += tradeStats['currentPositionShort']
        tradeStats['totalBuyValueShort'] += tradeStats['currentPositionShort'] * (askP0AtTimeOfPreviousDataRow)
        tradeStats['currentPositionShort'] = 0
        lReasonForTradingOrNotTradingLong = 'CloseBuy(Hitting)'
    
    attribute.aList[currentIndex-1][0] = currentTimeStamp
    attribute.aList[currentIndex-1][1] = tradeStats['currentPositionLong']
    attribute.aList[currentIndex-1][2] = tradeStats['currentPositionShort']
    listOfStringsToPrint = [ str(bidQ0AtTimeOfPreviousDataRow) , str(bidP0AtTimeOfPreviousDataRow) , str(askP0AtTimeOfPreviousDataRow) , str(askQ0AtTimeOfPreviousDataRow) , str(ttqAtTimeOfPreviousDataRow) , str(ltpAtTimeOfPreviousDataRow) , str(currentSellPredictedValue) , str(enterTradeShort) , "" , str(currentBuyPredictedValue) , str(enterTradeLong) ,"" , str(reasonForTrade['CloseBuyTradeHappened']),str(reasonForTrade['OpenBuyTradeHappened']),str(reasonForTrade['OpenSellTradeHappened']),str(reasonForTrade['CloseSellTradeHappened']),str(lDummyBidQ0),str(lDummyAskQ0),str(lDummyTTQForBuy),str(lDummyTTQForSell)]
    attribute.aList[currentIndex-1][3] =  ";".join(listOfStringsToPrint) 
    
    dirName = args.pd.replace('/ro/','/rs/')
    tradeLogMainDirName = dirName+"/t/"
    if not os.path.exists(tradeLogMainDirName):
         os.mkdir(tradeLogMainDirName)
    tradeLogSubDirectoryName =  tradeLogMainDirName + mainExperimentName+"/"
    if not os.path.exists(tradeLogSubDirectoryName):
         os.mkdir(tradeLogSubDirectoryName)
    
    fileName = tradeLogSubDirectoryName + pFileName + ".trade" 
    lHeaderColumnNamesList  = ['TimeStamp','CurrentPositionLong','CurrentPositionShort','BidQ0','BidP0','AskP0','AskQ0','TTQ','LTP','CurPredValueShort','EnterTradeShort','ReasonForTradingOrNotTradingShort','CurPredValueLong','EnterTradeLong','ReasonForTradingOrNotTradingLong','totalBuyTradeShort','totalBuyLong','totalSellShort','totalSellLong','DummyBidQ0','DummyAskQ0','DummyTTQChangeForSell','DummyTTQChangeForBuy']
    attribute.writeToFile(fileName , lHeaderColumnNamesList)
    
    tradeResultMainDirName = dirName+"/r/"
    if not os.path.exists(tradeResultMainDirName):
         os.mkdir(tradeResultMainDirName)
    tradeResultSubDirectoryName =  tradeResultMainDirName + mainExperimentName+"/"
    if not os.path.exists(tradeResultSubDirectoryName):
         os.mkdir(tradeResultSubDirectoryName)
    fileName = tradeResultSubDirectoryName+pFileName+".result" 
    outputFile = open(fileName,"w")
    gross_short_profit = tradeStats['totalSellValueShort'] - tradeStats['totalBuyValueShort']
    gross_long_profit = tradeStats['totalSellValueLong'] - tradeStats['totalBuyValueLong']
    gross_profit = gross_short_profit + gross_long_profit
    net_short_profit = gross_short_profit - ( transactionCost * ( tradeStats['totalSellValueShort'] +  tradeStats['totalBuyValueShort'] ) ) 
    net_long_profit = gross_long_profit - ( transactionCost * ( tradeStats['totalSellValueLong'] +  tradeStats['totalBuyValueLong'] ) )
    net_profit = net_short_profit + net_long_profit
    
    gross_short_profit_in_dollars = gross_profit / (currencyDivisor * 60)
    net_profit_in_dollars = net_profit / (currencyDivisor * 60 )
    #changed file write to modify it to Short Long version
    print("Starting to write: "+pFileName)
    print("The gross results for Short are: %.6f" %gross_short_profit, file = outputFile)
    print("The gross results for Long are: %.6f" %gross_long_profit, file = outputFile)
    print("Number of rows for which there is no prediction: " + str(0), file = outputFile)    
    print("Number of times asked to enter trade Short: " + str(numberOfTimesAskedToEnterTradeShort), file = outputFile)    
    print("Number of times asked to enter trade Long: " + str(numberOfTimesAskedToEnterTradeLong), file = outputFile)    
    print("Number of times asked to exit trade Short: " + str(numberOfTimesAskedToExitTradeShort), file = outputFile)
    print("Number of times asked to exit trade Long: " + str(numberOfTimesAskedToExitTradeLong), file = outputFile)
    print("The net results for Short are: %.6f" %net_short_profit, file = outputFile)
    print("The net results for Long are: %.6f" %net_long_profit, file = outputFile)
    print("Gross Results in Dollars: %.6f" %gross_short_profit_in_dollars, file = outputFile)
    print("Net Results in Dollars: %.6f" %net_profit_in_dollars, file = outputFile)
    print("Number of times Close buy trade happened: " + str(reasonForTrade['CloseBuyTradeHappened']), file = outputFile)
    print("Number of times open buy trade happened: " + str(reasonForTrade['OpenBuyTradeHappened']), file = outputFile)
    print("Assumed open sell trade did not happen since volume did not increase: " + str(reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptShort']), file = outputFile)
    print("Assumed close sell trade did not happen since volume did not increase: " + str(reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptLong']), file = outputFile)
    print("Assumed open sell trade did not happen since bidP0 not same as LTP: " + str(reasonForTrade['LTPDoesNotEqualAskP0Short']), file = outputFile)
    print("Assumed close sell trade did not happen since bidP0 not same as LTP: " + str(reasonForTrade['LTPDoesNotEqualAskP0Long']), file = outputFile)
    print("Number of Open sell trade happened: " + str(reasonForTrade['OpenSellTradeHappened']), file = outputFile)
    print("Number of Close sell trade happened: " + str(reasonForTrade['CloseSellTradeHappened']), file = outputFile)
    print("The total open sell value is: " + str(tradeStats['totalSellValueShort']), file = outputFile)
    print("The total close sell value is: " + str(tradeStats['totalSellValueLong']), file = outputFile)
    print("The total close buy value is: " + str(tradeStats['totalBuyValueShort']), file = outputFile)
    print("The total open buy value is: " + str(tradeStats['totalBuyValueLong']), file = outputFile)

    try:
        averageOpenSellPrice = tradeStats['totalSellValueShort']/reasonForTrade['OpenSellTradeHappened']
        averageCloseBuyPrice = tradeStats['totalBuyValueShort']/reasonForTrade['CloseBuyTradeHappened']
    except:
        averageOpenSellPrice = 0 
        averageCloseBuyPrice = 0
    try:
        averageCloseSellPrice = tradeStats['totalSellValueLong']/reasonForTrade['CloseSellTradeHappened']
        averageOpenBuyPrice = tradeStats['totalBuyValueLong']/reasonForTrade['OpenBuyTradeHappened']
    except:
        averageCloseSellPrice = 0
        averageOpenBuyPrice = 0 
    
    print("Average open sell price per unit is: " + str(averageOpenSellPrice), file = outputFile)
    print("Average close sell price per unit is: " + str(averageCloseSellPrice), file = outputFile)
    print("Average open buy price per unit is: " + str(averageOpenBuyPrice), file = outputFile)
    print("Average close buy price per unit is: " + str(averageCloseBuyPrice), file = outputFile)
    print("The current position Short: " + str(tradeStats['currentPositionShort']), file = outputFile)
    print("The current position Long: " + str(tradeStats['currentPositionLong']), file = outputFile)
    print("Profit or loss per Qty traded Short is: " + str(averageOpenSellPrice - averageCloseBuyPrice), file = outputFile)
    print("Profit or loss per Qty traded Long is: " + str(averageCloseSellPrice - averageOpenBuyPrice), file = outputFile)
    pLPerLotShort=(averageOpenSellPrice - averageCloseBuyPrice)* 1000
    pLPerLotLong=(averageCloseSellPrice - averageOpenBuyPrice)* 1000
    print("1 lot has 1000 qty's so P/L Short per lot is: " + str(pLPerLotShort), file = outputFile)
    print("1 lot has 1000 qty's so P/L Long per lot is: " + str(pLPerLotLong), file = outputFile)
    print("P/L for Short trading 10 lots is: " + str(pLPerLotShort * 10), file = outputFile)
    print("P/L for Long trading 10 lots is: " + str(pLPerLotLong * 10), file = outputFile)
def extractAttributeFromDataMatrix(args):
    N = 5
    if args.n == None:
        N = 5
    else:
        N = int(args.n)

    try:
        args.c
    except:
        print "Since -c has not been specified I cannot proceed"
        os._exit()

    try:
        args.o
    except:
        print "Since -o has not been specified I cannot proceed"
        os._exit()

    colNumberOfTimeStamp = colNumberOfData.TimeStamp
    colNumberOfExchangeStamp = colNumberOfData.ExchangeTS
    numberOfRowsInLastNSecs = 0
    l_first_time_elapsed = False
    queueOfValuesInLastNSecs = deque()
    timeOfOldestRow = common.convertTimeStampFromStringToFloat(
        dataFile.matrix[0][colNumberOfExchangeStamp], "synthetic")
    currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
    lengthOfDataMatrix = len(dataFile.matrix)
    totalTradedQty = 0
    lPreviousTickObject = None
    priceIndex = eval('colNumberOfData.' + args.c + 'P0')
    qtyIndex = [
        eval('colNumberOfData.' + args.c + 'Q0'),
        eval('colNumberOfData.' + args.c + 'Q1'),
        eval('colNumberOfData.' + args.c + 'Q2'),
        eval('colNumberOfData.' + args.c + 'Q3'),
        eval('colNumberOfData.' + args.c + 'Q4')
    ]
    if args.c == 'Ask':
        orderTypeToBeTracked = 'S'
    else:
        orderTypeToBeTracked = 'B'
    while (currentRowNumberForWhichFeatureValueIsBeingCalculated <
           lengthOfDataMatrix):

        lCurrentTickObject = ticks_values_to_be_stored()
        lCurrentDataRow = dataFile.matrix[
            currentRowNumberForWhichFeatureValueIsBeingCalculated]
        timeOfCurrentRow = common.convertTimeStampFromStringToFloat(
            lCurrentDataRow[colNumberOfExchangeStamp], "synthetic")
        timeElapsed = timeOfCurrentRow - timeOfOldestRow
        if (timeElapsed < N):
            lCurrentTickObject.MsgCode = lCurrentDataRow[
                colNumberOfData.MsgCode]
            lCurrentTickObject.OrderType = lCurrentDataRow[
                colNumberOfData.OrderType]
            lCurrentTickObject.NewP = float(
                lCurrentDataRow[colNumberOfData.NewP])
            lCurrentTickObject.NewQ = int(
                lCurrentDataRow[colNumberOfData.NewQ])
            lCurrentTickObject.Price = float(lCurrentDataRow[priceIndex])
            lCurrentTickObject.QtyList = [
                int(lCurrentDataRow[qtyIndex[0]]),
                int(lCurrentDataRow[qtyIndex[1]]),
                int(lCurrentDataRow[qtyIndex[2]]),
                int(lCurrentDataRow[qtyIndex[3]]),
                int(lCurrentDataRow[qtyIndex[4]])
            ]

            totalTradedQty = updateCurrentTickAdditionToQueue(
                lCurrentTickObject, lPreviousTickObject, totalTradedQty,
                timeElapsed, l_first_time_elapsed, N, orderTypeToBeTracked,
                args.o, numberOfRowsInLastNSecs)

            attribute.aList[
                currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    0] = common.convertTimeStampFromStringToDecimal(
                        lCurrentDataRow[colNumberOfTimeStamp])
            attribute.aList[
                currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    1] = str(lCurrentTickObject.IntensityValue)

            attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][2] = ";".join( map( str , [ lCurrentDataRow[colNumberOfExchangeStamp] , timeElapsed , totalTradedQty , lCurrentTickObject.MsgCode ,\
                                                                                                          lCurrentTickObject.OrderType , lCurrentTickObject.NewP , lCurrentTickObject.NewQ ] ) )
            attribute.aList[
                currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    3] = str(lCurrentTickObject.Price)
            queueOfValuesInLastNSecs.append(
                [lCurrentTickObject, timeOfCurrentRow])
            lPreviousTickObject = lCurrentTickObject
            numberOfRowsInLastNSecs += 1  # Every append gets a +1
            currentRowNumberForWhichFeatureValueIsBeingCalculated += 1
            continue  # Since we are going back 1 row from current we cannot get data from current row

        else:
            l_first_time_elapsed = True
            # We need to reset the timeOfOldestRow since timeElapsed has exceeded N seconds
            while (timeElapsed >= N):
                if (len(queueOfValuesInLastNSecs) == 0):
                    timeOfOldestRow = timeOfCurrentRow
                    timeElapsed = 0
                    if (numberOfRowsInLastNSecs != 0):
                        print "Sanity check: This condition is not possible logically. numberOfRowsInLastNSecs should been 0. There has been an unknown error"
                        sys.exit(-1)
                else:
                    oldestElementInQueue = queueOfValuesInLastNSecs.popleft()
                    colValueInOldestElementInQueue = oldestElementInQueue[0]
                    totalTradedQty = updateTickDeletionFromQueue(
                        colValueInOldestElementInQueue, totalTradedQty)
                    if len(queueOfValuesInLastNSecs) == 0:
                        timeElapsed = 0
                        timeOfOldestRow = timeOfCurrentRow
                    else:
                        timeOfOldestRow = queueOfValuesInLastNSecs[0][1]
                    numberOfRowsInLastNSecs -= 1  # every pop from the queue gets a -1
                    timeElapsed = timeOfCurrentRow - timeOfOldestRow
                    if (len(queueOfValuesInLastNSecs) !=
                            numberOfRowsInLastNSecs):
                        print "Sanity check: This condition is not possible logically. There has been an unknown error"
                        sys.exit(-1)

        print "Processed row number " + str(
            currentRowNumberForWhichFeatureValueIsBeingCalculated)

    lNameOfFeaturePrinted = "fIntensityOfCol" + str(args.c) + "InOrder" + str(
        args.o) + "InLast" + str(args.n) + "Secs"
    return [
        "TimeStamp", lNameOfFeaturePrinted, "ExTimeStamp", "TimeElapsed",
        "totalQty", "MsgCode", "Ordertype", "NewP", "NewQ", "Price"
    ]
def readOnceAndWrite(pFileName, targetValuesDict):
    global gOpenBuyPrice , gCloseSellPrice , gOpenSellPrice , gCloseBuyPrice
    attribute.initList()
    enterTradeShort = 0
    enterTradeLong = 0
    ltpAtTimeOfPreviousDataRow = 0
    ttqAtTimeOfPreviousDataRow = 0
    askP0AtTimeOfPreviousDataRow = 0
    bidP0AtTimeOfPreviousDataRow = 0
    askQ0AtTimeOfPreviousDataRow = 0
    bidQ0AtTimeOfPreviousDataRow = 0
    exchangeTimeStamp = 0 
    currentTimeStamp = 0
    tradeStats = dict()
    tradeStats['totalSellValueShort'] = 0
    tradeStats['totalBuyValueShort'] = 0
    tradeStats['currentPositionShort'] = 0
    tradeStats['totalSellValueLong'] = 0
    tradeStats['totalBuyValueLong'] = 0
    tradeStats['currentPositionLong'] = 0
    noTargetForThisRow = 0
    currentSellTargetValue = 0
    currentBuyTargetValue = 0
    #  entryCLCutoff = float(args.entryCLCutoff)
    #  exitCLCutoff = float(args.exitCLCutoff)
    numberOfTimesAskedToEnterTradeShort = 0
    numberOfTimesAskedToEnterTradeLong = 0
    numberOfTimesAskedToExitTradeShort = 0
    numberOfTimesAskedToExitTradeLong = 0
    reasonForTrade = dict()
    reasonForTrade['LTPDoesNotEqualBidP0Short'] = 0
    reasonForTrade['LTPDoesNotEqualAskP0Short'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptShort'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptLong'] = 0
    reasonForTrade['CloseBuyTradeHappened'] = 0
    reasonForTrade['OpenBuyTradeHappened'] = 0
    reasonForTrade['LTPDoesNotEqualAskP0Long'] = 0
    reasonForTrade['LTPDoesNotEqualBidP0Long'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptShort'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptLong'] = 0
    reasonForTrade['OpenSellTradeHappened'] = 0
    reasonForTrade['CloseSellTradeHappened'] = 0
    reasonForTrade['PrevWasOurOrder'] = 0
    lReasonForTradingOrNotTradingShort = ""
    lReasonForTradingOrNotTradingLong = ""
    currentIndex = 0
    lDummyBidQ0 = 0
    lDummyAskQ0=0 
    lDummyTTQForBuy =0 
    lDummyTTQForSell = 0
    print("Processing the data file for trades :")
    attribute.initList()
    prevBuyTargetValue = 0
    prevSellTargetValue = 0
    exchangeTimeStamp = 0
    lOpenBuyIndex = 0
    lOpenSellIndex = 0
    l_exchange_time_stamp_to_print= ""
    for currentDataRow in dataFile.matrix:
     
        l_expiry_wrt_1970 = exchangeTimeStamp + 315513000.0
        
        l_dt = datetime.datetime.fromtimestamp(l_expiry_wrt_1970)
        l_exchange_time_stamp_to_print = str(l_dt.hour) + ":" + str(l_dt.minute) + ":" + str(l_dt.second)
        lReturnList = checkIfPreviousDecisionToEnterOrExitTradeWasSuccessful(currentDataRow,ttqAtTimeOfPreviousDataRow,askP0AtTimeOfPreviousDataRow,bidP0AtTimeOfPreviousDataRow,\
                                                                                 askQ0AtTimeOfPreviousDataRow , bidQ0AtTimeOfPreviousDataRow , enterTradeShort,enterTradeLong,tradeStats,reasonForTrade,\
                                                                                 lReasonForTradingOrNotTradingLong,lReasonForTradingOrNotTradingShort )
     
        lReasonForTradingOrNotTradingShort = lReturnList[0]
        lReasonForTradingOrNotTradingLong = lReturnList[1] 
        lDummyBidQ0 = lReturnList[2]
        lDummyAskQ0 = lReturnList[3]
        lDummyTTQForBuy = lReturnList[4]
        lDummyTTQForSell = lReturnList[5]
        if currentIndex > 0:
            lFlag = 0
            if "CloseSell" in lReasonForTradingOrNotTradingLong:
                 if gCloseSellPrice - gOpenBuyPrice <= 0 :
                     for i in range(lOpenBuyIndex,currentIndex):
                         attribute.aList[i][1] = 0
                 else:
                     lFlag = 1
                 lOpenBuyIndex = 0
            if "CloseBuy" in lReasonForTradingOrNotTradingShort:
                 if gOpenSellPrice - gCloseBuyPrice <= 0 :
                     for i in range(lOpenSellIndex,currentIndex):
                         attribute.aList[i][1] = 0
                 else:
                     lFlag = 1
                 lOpenSellIndex=0
            if "OpenSell" in lReasonForTradingOrNotTradingShort:
                 lOpenSellIndex = currentIndex-1
            if "OpenBuy" in lReasonForTradingOrNotTradingLong:
                 lOpenBuyIndex = currentIndex-1

            if (lOpenSellIndex !=0 or lOpenBuyIndex!=0):
                lFlag = 1
                

            attribute.aList[currentIndex-1][0] = currentTimeStamp
            attribute.aList[currentIndex-1][1] = lFlag
            attribute.aList[currentIndex-1][2] = tradeStats['currentPositionLong']
            listOfStringsToPrint = [ str(tradeStats['currentPositionShort']) , str(bidQ0AtTimeOfPreviousDataRow) , str(bidP0AtTimeOfPreviousDataRow) , str(askP0AtTimeOfPreviousDataRow) , \
                                    str(askQ0AtTimeOfPreviousDataRow) , str(ttqAtTimeOfPreviousDataRow) , str(ltpAtTimeOfPreviousDataRow) ,\
                                    str(currentSellTargetValue) , str(enterTradeShort) ,lReasonForTradingOrNotTradingShort , str(currentBuyTargetValue) ,\
                                    str(enterTradeLong) ,lReasonForTradingOrNotTradingLong ,l_exchange_time_stamp_to_print, str(reasonForTrade['CloseBuyTradeHappened']),\
                                    str(reasonForTrade['OpenBuyTradeHappened']),str(reasonForTrade['OpenSellTradeHappened']),str(reasonForTrade['CloseSellTradeHappened']),\
                                    str(gCloseSellPrice),str(gOpenBuyPrice),str(gOpenSellPrice),str(gCloseBuyPrice)]
            attribute.aList[currentIndex-1][3] =  ";".join(listOfStringsToPrint)
        currentTimeStamp = common.convertTimeStampFromStringToFloat(currentDataRow[colNumberOfData.TimeStamp])
    
        try:
            currentSellTargetValue = float(targetValuesDict[currentTimeStamp]['sell']) 
            currentBuyTargetValue  = float(targetValuesDict[currentTimeStamp]['buy'])
        except:
            noTargetForThisRow += 1
    
        #short decisions
        if(currentSellTargetValue == 1):
            enterTradeShort = 1
            numberOfTimesAskedToEnterTradeShort += 1
        elif(currentSellTargetValue == 0 and tradeStats['currentPositionShort'] > 0):
            numberOfTimesAskedToExitTradeShort += 1
            enterTradeShort = -1  # Implies to exit the trade
        else:
            enterTradeShort = 0  # Implies make no change
            
        #long decisions
        if(currentBuyTargetValue == 1):
            enterTradeLong = 1
            numberOfTimesAskedToEnterTradeLong += 1
        elif(currentBuyTargetValue == 0 and tradeStats['currentPositionLong'] > 0):
            numberOfTimesAskedToExitTradeLong += 1
            enterTradeLong = -1  # Implies to exit the trade
        else:
            enterTradeLong = 0  # Implies make no change
        
        ttqAtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.TTQ]) 
        askP0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.AskP0])
        bidP0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.BidP0])
        askQ0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.AskQ0])
        bidQ0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.BidQ0])
        ltpAtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.LTP])
        currentIndex = currentIndex + 1
        prevBuyTargetValue = currentBuyTargetValue
        prevSellTargetValue = currentSellTargetValue
        exchangeTimeStamp = float(currentDataRow[colNumberOfData.ExchangeTS])
    
    # Squaring off if some open position there   
    if tradeStats['currentPositionLong'] > 0:
        reasonForTrade['CloseSellTradeHappened'] += tradeStats['currentPositionLong']
        tradeStats['totalSellValueLong'] += tradeStats['currentPositionLong'] * (bidP0AtTimeOfPreviousDataRow)
        tradeStats['currentPositionLong'] = 0
        lReasonForTradingOrNotTradingLong = 'CloseSell(Hitting)'
        gCloseSellPrice = bidP0AtTimeOfPreviousDataRow
        if gCloseSellPrice - gOpenBuyPrice < 0 :
             for i in range(lOpenBuyIndex,currentIndex):
                 attribute.aList[i][1] = 0
                 
    if tradeStats['currentPositionShort'] > 0:
        reasonForTrade['CloseBuyTradeHappened'] += tradeStats['currentPositionShort']
        tradeStats['totalBuyValueShort'] += tradeStats['currentPositionShort'] * (askP0AtTimeOfPreviousDataRow)
        tradeStats['currentPositionShort'] = 0
        lReasonForTradingOrNotTradingLong = 'CloseBuy(Hitting)'
        gCloseBuyPrice = askP0AtTimeOfPreviousDataRow
        if gOpenSellPrice - gCloseBuyPrice < 0 :
             for i in range(lOpenSellIndex,currentIndex):
                 attribute.aList[i][1] = 0 
                    
    attribute.aList[currentIndex-1][0] = currentTimeStamp
    attribute.aList[currentIndex-1][1] = attribute.aList[currentIndex-2][1]
    attribute.aList[currentIndex-1][2] = tradeStats['currentPositionLong']
    listOfStringsToPrint = [ str(tradeStats['currentPositionShort']),str(bidQ0AtTimeOfPreviousDataRow) , str(bidP0AtTimeOfPreviousDataRow) , str(askP0AtTimeOfPreviousDataRow) ,\
                             str(askQ0AtTimeOfPreviousDataRow) , str(ttqAtTimeOfPreviousDataRow) , str(ltpAtTimeOfPreviousDataRow) , \
                             str(currentSellTargetValue) , str(enterTradeShort) , "" , str(currentBuyTargetValue) , str(enterTradeLong) ,\
                             "" ,l_exchange_time_stamp_to_print, str(reasonForTrade['CloseBuyTradeHappened']),str(reasonForTrade['OpenBuyTradeHappened']),str(reasonForTrade['OpenSellTradeHappened']),\
                             str(reasonForTrade['CloseSellTradeHappened']),str(gCloseSellPrice),str(gOpenBuyPrice),str(gOpenSellPrice),str(gCloseBuyPrice)]
    attribute.aList[currentIndex-1][3] =  ";".join(listOfStringsToPrint) 
    
    dirName = args.d.replace('/ro/','/wf/')
    tradeLogMainDirName = dirName+"/tr/"
    if not os.path.exists(tradeLogMainDirName):
         os.mkdir(tradeLogMainDirName)
    tradeLogSubDirectoryName =  tradeLogMainDirName + mainExperimentName+"/"
    if not os.path.exists(tradeLogSubDirectoryName):
         os.mkdir(tradeLogSubDirectoryName)
    
    fileName = tradeLogSubDirectoryName + pFileName + ".targetTrade" 
    lHeaderColumnNamesList  = ['TimeStamp','targetValue','CurrentPositionLong','CurrentPositionShort','BidQ0','BidP0','AskP0','AskQ0','TTQ','LTP',\
                               'CurTargetValueShort','EnterTradeShort','ReasonForTradingOrNotTradingShort','CurTargetValueLong','EnterTradeLong',\
                               'ReasonForTradingOrNotTradingLong','Exchange_TS','totalBuyShort','totalBuyLong','totalSellShort','totalSellLong','CloseSellPrice',\
                               'OpenBuyPrice','OpenSellPrice','CloseBuyPrice']
    attribute.writeToFile(fileName , lHeaderColumnNamesList)
    
    tradeResultMainDirName = dirName+"/r/"
    if not os.path.exists(tradeResultMainDirName):
         os.mkdir(tradeResultMainDirName)
    tradeResultSubDirectoryName =  tradeResultMainDirName + mainExperimentName+"/"
    if not os.path.exists(tradeResultSubDirectoryName):
         os.mkdir(tradeResultSubDirectoryName)
    fileName = tradeResultSubDirectoryName+pFileName+".targetResult" 
    outputFile = open(fileName,"w")
    
    #changed file write to modify it to Short Long version
    print("Starting to write: "+fileName)
    print("The net results for Short are: " + str(tradeStats['totalSellValueShort'] - tradeStats['totalBuyValueShort']), file = outputFile)
    print("The net results for Long are: " + str(tradeStats['totalSellValueLong'] - tradeStats['totalBuyValueLong']), file = outputFile)
    print("Number of rows for which there is no target: " + str(noTargetForThisRow), file = outputFile)    
    print("Number of times asked to enter trade Short: " + str(numberOfTimesAskedToEnterTradeShort), file = outputFile)    
    print("Number of times asked to enter trade Long: " + str(numberOfTimesAskedToEnterTradeLong), file = outputFile)    
    print("Number of times asked to exit trade Short: " + str(numberOfTimesAskedToExitTradeShort), file = outputFile)
    print("Number of times asked to exit trade Long: " + str(numberOfTimesAskedToExitTradeLong), file = outputFile)
    print("Assumed close buy trade did not happen since volume did not increase: " + str(reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptShort']), file = outputFile)
    print("Assumed open buy trade did not happen since volume did not increase: " + str(reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptLong']), file = outputFile)
    print("Assumed close buy trade did not happen since bidP0 not same as LTP: " + str(reasonForTrade['LTPDoesNotEqualBidP0Short']), file = outputFile)
    print("Assumed open buy trade did not happen since bidP0 not same as LTP: " + str(reasonForTrade['LTPDoesNotEqualBidP0Long']), file = outputFile)
    print("Assumed close buy trade happened: " + str(reasonForTrade['CloseBuyTradeHappened']), file = outputFile)
    print("Assumed open buy trade happened: " + str(reasonForTrade['OpenBuyTradeHappened']), file = outputFile)
    print("Assumed open sell trade did not happen since volume did not increase: " + str(reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptShort']), file = outputFile)
    print("Assumed close sell trade did not happen since volume did not increase: " + str(reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptLong']), file = outputFile)
    print("Assumed open sell trade did not happen since bidP0 not same as LTP: " + str(reasonForTrade['LTPDoesNotEqualAskP0Short']), file = outputFile)
    print("Assumed close sell trade did not happen since bidP0 not same as LTP: " + str(reasonForTrade['LTPDoesNotEqualAskP0Long']), file = outputFile)
    print("Assumed open sell trade happened: " + str(reasonForTrade['OpenSellTradeHappened']), file = outputFile)
    print("Assumed close sell trade happened: " + str(reasonForTrade['CloseSellTradeHappened']), file = outputFile)
    print("The total open sell value is: " + str(tradeStats['totalSellValueShort']), file = outputFile)
    print("The total close sell value is: " + str(tradeStats['totalSellValueLong']), file = outputFile)
    print("The total close buy value is: " + str(tradeStats['totalBuyValueShort']), file = outputFile)
    print("The total open buy value is: " + str(tradeStats['totalBuyValueLong']), file = outputFile)
    
    try:
        averageOpenSellPrice = tradeStats['totalSellValueShort']/reasonForTrade['OpenSellTradeHappened']
        averageCloseBuyPrice = tradeStats['totalBuyValueShort']/reasonForTrade['CloseBuyTradeHappened']
    except:
        averageOpenSellPrice = 0 
        averageCloseBuyPrice = 0
    try:
        averageCloseSellPrice = tradeStats['totalSellValueLong']/reasonForTrade['CloseSellTradeHappened']
        averageOpenBuyPrice = tradeStats['totalBuyValueLong']/reasonForTrade['OpenBuyTradeHappened']
    except:
        averageCloseSellPrice = 0
        averageOpenBuyPrice = 0 
    
    print("Average open sell price per unit is: " + str(averageOpenSellPrice), file = outputFile)
    print("Average close sell price per unit is: " + str(averageCloseSellPrice), file = outputFile)
    print("Average open buy price per unit is: " + str(averageOpenBuyPrice), file = outputFile)
    print("Average close buy price per unit is: " + str(averageCloseBuyPrice), file = outputFile)
    print("The current position Short: " + str(tradeStats['currentPositionShort']), file = outputFile)
    print("The current position Long: " + str(tradeStats['currentPositionLong']), file = outputFile)
    print("Profit or loss per Qty traded Short is: " + str(averageOpenSellPrice - averageCloseBuyPrice), file = outputFile)
    print("Profit or loss per Qty traded Long is: " + str(averageCloseSellPrice - averageOpenBuyPrice), file = outputFile)
    pLPerLotShort=(averageOpenSellPrice - averageCloseBuyPrice)* 1000
    pLPerLotLong=(averageCloseSellPrice - averageOpenBuyPrice)* 1000
    print("1 lot has 1000 qty's so P/L Short per lot is: " + str(pLPerLotShort), file = outputFile)
    print("1 lot has 1000 qty's so P/L Long per lot is: " + str(pLPerLotLong), file = outputFile)
    print("P/L for Short trading 10 lots is: " + str(pLPerLotShort * 10), file = outputFile)
    print("P/L for Long trading 10 lots is: " + str(pLPerLotLong * 10), file = outputFile)
Exemple #19
0
def readOnceAndWrite(pFileName, entryCL1, entryCL2, entryCL3, entryCL4,
                     exitCL1, exitCL2, exitCL3, exitCL4, predictedValuesDict):
    global g_bestqty_list_for_sell, g_bestqty_list_for_buy
    attribute.initList()
    tradeStats = dict()
    tradeStats['totalSellAmountShort'] = 0
    tradeStats['totalBuyAmountShort'] = 0
    tradeStats['totalSellAmountLong'] = 0
    tradeStats['totalBuyAmountLong'] = 0
    tradeStats['NumberOfOpenBuy'] = 0
    tradeStats['NumberOfCloseSell'] = 0
    tradeStats['NumberOfOpenSell'] = 0
    tradeStats['NumberOfCloseBuy'] = 0
    tradeStats['currentPositionShort'] = 0
    tradeStats['currentPositionLong'] = 0
    noPredictionForThisRow = 0
    currentSellPredictedValue = 0
    currentBuyPredictedValue = 0
    currentIndex = 0
    l_previous_obj = None
    l_obj = None
    print("Processing the data file for trades :")
    attribute.initList()
    print("EntryLevels", entryCL1, entryCL2, entryCL3, entryCL4, exitCL1,
          exitCL2, exitCL3, exitCL4)
    for currentDataRow in dataFile.matrix:

        l_obj = update_obj_list(currentDataRow)
        if (l_previous_obj != None):
            lReturnList = checkIfPreviousDecisionToEnterOrExitTradeWasSuccessful(
                l_obj, l_previous_obj, tradeStats)

            lDummyBidQ0 = lReturnList[0]
            lDummyAskQ0 = lReturnList[1]
            lDummyTTQForBuy = lReturnList[2]
            lDummyTTQForSell = lReturnList[3]
            lBuyTradedPrice = lReturnList[4]
            lBuyTradedQty = lReturnList[5]
            lSellTradedPrice = lReturnList[6]
            lSellTradedQty = lReturnList[7]
            if currentIndex > 0:
                if (g_bestqty_list_for_buy != {}):
                    l_best_bidq = g_bestqty_list_for_buy['qty']
                    l_best_bidp = g_bestqty_list_for_buy['price']
                else:
                    l_best_bidq = 0
                    l_best_bidp = 0
                if (g_bestqty_list_for_sell != {}):
                    l_best_askq = g_bestqty_list_for_sell['qty']
                    l_best_askp = g_bestqty_list_for_sell['price']
                else:
                    l_best_askq = 0
                    l_best_askp = 0
                attribute.aList[currentIndex - 1][0] = l_obj.currentTimeStamp
                attribute.aList[currentIndex -
                                1][1] = tradeStats['currentPositionLong']
                attribute.aList[currentIndex -
                                1][2] = tradeStats['currentPositionShort']
                listOfStringsToPrint = [ str(l_previous_obj.BidQ) , str(l_previous_obj.BidP) , str(l_previous_obj.AskP) , \
                                        str(l_previous_obj.AskQ) , str(l_previous_obj.TTQ) , str(l_previous_obj.LTP) ,\
                                        str(currentSellPredictedValue) , str(l_previous_obj.EnterTradeShort) ,l_previous_obj.ReasonForTradingOrNotTradingShort , str(currentBuyPredictedValue) ,\
                                        str(l_previous_obj.EnterTradeLong) ,l_previous_obj.ReasonForTradingOrNotTradingLong , str(tradeStats['NumberOfCloseBuy']),\
                                        str(tradeStats['NumberOfOpenBuy']),str(tradeStats['NumberOfOpenSell']),\
                                        str(tradeStats['NumberOfCloseBuy']),str(lDummyBidQ0),str(lDummyAskQ0),\
                                        str(lDummyTTQForBuy),str(lDummyTTQForSell),str(l_best_bidq),str(l_best_bidp),str(l_best_askp),\
                                        str(l_best_askq) ,str(lBuyTradedPrice), str(lBuyTradedQty), str(lSellTradedPrice),str(lSellTradedQty)]
                attribute.aList[currentIndex -
                                1][3] = ";".join(listOfStringsToPrint)
            currentTimeStamp = common.convertTimeStampFromStringToFloat(
                currentDataRow[colNumberOfData.TimeStamp])

            try:
                currentSellPredictedValue = float(
                    predictedValuesDict[l_obj.currentTimeStamp]['sell'])
                currentBuyPredictedValue = float(
                    predictedValuesDict[l_obj.currentTimeStamp]['buy'])
            except:
                noPredictionForThisRow += 1

            #Open sell and Close Buy
            if (currentSellPredictedValue >= entryCL4
                    and tradeStats['currentPositionLong'] == 0):
                g_bestqty_list_for_sell = {}
                l_obj.EnterTradeShort = 4  #For close by hitting
            elif (currentSellPredictedValue >= entryCL3
                  and tradeStats['currentPositionLong'] == 0):
                g_bestqty_list_for_sell = {}
                l_obj.EnterTradeShort = 3  #For standing at Bid +1
            elif (currentSellPredictedValue >= entryCL2
                  and currentBuyPredictedValue < exitCL1
                  and tradeStats['currentPositionLong'] == 0):
                if (l_previous_obj.EnterTradeShort not in [2, 1]):
                    g_bestqty_list_for_sell = {}
                l_obj.EnterTradeShort = 2  #For standing at bid
            elif (l_previous_obj.EnterTradeShort > 0
                  and currentSellPredictedValue >= entryCL1
                  and currentBuyPredictedValue < exitCL1
                  and tradeStats['currentPositionLong'] == 0):
                l_obj.EnterTradeShort = 1  ##For continuing to stand at bid
            elif (currentBuyPredictedValue >= exitCL4
                  and tradeStats['currentPositionShort'] > 0):
                g_bestqty_list_for_buy = {}
                l_obj.EnterTradeShort = -4  #For close by hitting
            elif (currentBuyPredictedValue >= exitCL3
                  and tradeStats['currentPositionShort'] > 0):
                g_bestqty_list_for_buy = {}
                l_obj.EnterTradeShort = -3  #For standing at Bid +1
            elif (currentBuyPredictedValue >= exitCL2
                  and tradeStats['currentPositionShort'] > 0):
                if (l_previous_obj.EnterTradeShort not in [-2, -1]):
                    g_bestqty_list_for_buy = {}
                l_obj.EnterTradeShort = -2  #For standing at bid
            elif (l_previous_obj.EnterTradeShort < 0
                  and currentBuyPredictedValue >= exitCL1
                  and tradeStats['currentPositionShort'] > 0):
                l_obj.EnterTradeShort = -1  ##For continuing to stand at bid
            else:
                l_obj.EnterTradeShort = 0

            #Open Buy
            if (currentBuyPredictedValue >= entryCL4
                    and tradeStats['currentPositionShort'] == 0):
                g_bestqty_list_for_buy = {}
                l_obj.EnterTradeLong = 4  #For close by hitting
            elif (currentBuyPredictedValue >= entryCL3
                  and tradeStats['currentPositionShort'] == 0):
                g_bestqty_list_for_buy = {}
                l_obj.EnterTradeLong = 3  #For standing at Bid +1
            elif (currentBuyPredictedValue >= entryCL2
                  and currentSellPredictedValue < exitCL1
                  and tradeStats['currentPositionShort'] == 0):
                if (l_previous_obj.EnterTradeLong not in [2, 1]):
                    g_bestqty_list_for_buy = {}
                l_obj.EnterTradeLong = 2  #For standing at bid
            elif (l_previous_obj.EnterTradeLong < 0
                  and currentBuyPredictedValue >= entryCL1
                  and currentSellPredictedValue < exitCL1
                  and tradeStats['currentPositionShort'] == 0):
                l_obj.EnterTradeLong = 1  #For continuing to stand at bid
            elif (currentSellPredictedValue >= exitCL4
                  and tradeStats['currentPositionLong'] > 0):
                g_bestqty_list_for_sell = {}
                l_obj.EnterTradeLong = -4  #For close by hitting
            elif (currentSellPredictedValue >= exitCL3
                  and tradeStats['currentPositionLong'] > 0):
                g_bestqty_list_for_sell = {}
                l_obj.EnterTradeLong = -3  #For standing at Bid +1
            elif (currentSellPredictedValue >= exitCL2
                  and tradeStats['currentPositionLong'] > 0):
                if (l_previous_obj.EnterTradeLong not in [-2, -1]):
                    g_bestqty_list_for_sell = {}
                l_obj.EnterTradeLong = -2  #For standing at bid
            elif (l_previous_obj.EnterTradeLong > 0
                  and currentSellPredictedValue >= exitCL1
                  and tradeStats['currentPositionLong'] > 0):
                l_obj.EnterTradeLong = -1  ##For continuing to stand at bid
            else:
                l_obj.EnterTradeLong = 0

        l_previous_obj = l_obj
        currentIndex = currentIndex + 1

# Squaring off if some open position there
    if tradeStats['currentPositionLong'] > 0:
        tradeStats['NumberOfCloseSell'] += tradeStats['currentPositionLong']
        tradeStats['totalSellAmountLong'] += tradeStats[
            'currentPositionLong'] * (l_previous_obj.BidP)
        tradeStats['currentPositionLong'] = 0
        l_obj.ReasonForTradingOrNotTradingLong = 'CloseSell(Hitting)'
    if tradeStats['currentPositionShort'] > 0:
        tradeStats['NumberOfCloseBuy'] += tradeStats['currentPositionShort']
        tradeStats['totalBuyAmountShort'] += tradeStats[
            'currentPositionShort'] * (l_previous_obj.AskP)
        tradeStats['currentPositionShort'] = 0
        l_obj.ReasonForTradingOrNotTradingLong = 'CloseBuy(Hitting)'

    attribute.aList[currentIndex - 1][0] = currentTimeStamp
    attribute.aList[currentIndex - 1][1] = tradeStats['currentPositionLong']
    attribute.aList[currentIndex - 1][2] = tradeStats['currentPositionShort']
    if (g_bestqty_list_for_buy != {}):
        l_best_bidq = g_bestqty_list_for_buy['qty']
        l_best_bidp = g_bestqty_list_for_buy['price']
    else:
        l_best_bidq = 0
        l_best_bidp = 0
    if (g_bestqty_list_for_sell != {}):
        l_best_askq = g_bestqty_list_for_sell['qty']
        l_best_askp = g_bestqty_list_for_sell['price']
    else:
        l_best_askq = 0
        l_best_askp = 0
    listOfStringsToPrint = [ str(l_previous_obj.BidQ) , str(l_previous_obj.BidP) , str(l_previous_obj.AskP) , \
                            str(l_previous_obj.AskQ) , str(l_previous_obj.TTQ) , str(l_previous_obj.LTP) ,\
                            str(currentSellPredictedValue) , str(l_previous_obj.EnterTradeShort) ,l_previous_obj.ReasonForTradingOrNotTradingShort , str(currentBuyPredictedValue) ,\
                            str(l_previous_obj.EnterTradeLong) ,l_previous_obj.ReasonForTradingOrNotTradingLong , str(tradeStats['NumberOfCloseBuy']),\
                            str(tradeStats['NumberOfOpenBuy']),str(tradeStats['NumberOfOpenSell']),\
                            str(tradeStats['NumberOfCloseBuy']),str(lDummyBidQ0),str(lDummyAskQ0),\
                            str(lDummyTTQForBuy),str(lDummyTTQForSell),str(l_best_bidq),str(l_best_bidp),str(l_best_askp), str(l_best_askq) , "0;0;0;0"]
    attribute.aList[currentIndex - 1][3] = ";".join(listOfStringsToPrint)

    dirName = args.pd.replace('/ro/', '/rs/')
    tradeLogMainDirName = dirName + "/t/"
    if not os.path.exists(tradeLogMainDirName):
        os.mkdir(tradeLogMainDirName)
    tradeLogSubDirectoryName = tradeLogMainDirName + mainExperimentName + "/"
    if not os.path.exists(tradeLogSubDirectoryName):
        os.mkdir(tradeLogSubDirectoryName)

    fileName = tradeLogSubDirectoryName + pFileName + ".trade"
    lHeaderColumnNamesList  = ['TimeStamp','CurrentPositionLong','CurrentPositionShort','BidQ0','BidP0','AskP0','AskQ0','TTQ','LTP','CurPredValueShort',\
                               'EnterTradeShort','ReasonForTradingOrNotTradingShort','CurPredValueLong','EnterTradeLong','ReasonForTradingOrNotTradingLong',\
                               'totalBuyTradeShort','totalBuyLong','totalSellShort','totalSellLong','DummyBidQ0','DummyAskQ0','DummyTTQChangeForSell','DummyTTQChangeForBuy' \
                               ,'BestBidQ','BestBidP','BestAskP','BestAskQ','BuyTradedPrcie','BuyTradedQty','SellTradedPrice','SellTradedQty']

    #attribute.writeToFile(fileName , lHeaderColumnNamesList)

    tradeResultMainDirName = dirName + "/r/"
    if not os.path.exists(tradeResultMainDirName):
        os.mkdir(tradeResultMainDirName)
    tradeResultSubDirectoryName = tradeResultMainDirName + mainExperimentName + "/"
    if not os.path.exists(tradeResultSubDirectoryName):
        os.mkdir(tradeResultSubDirectoryName)
    fileName = tradeResultSubDirectoryName + pFileName + ".result"
    outputFile = open(fileName, "w")

    #changed file write to modify it to Short Long version
    print("Starting to write: " + fileName)
    print("The gross profit for Short are: " +
          str(tradeStats['totalSellAmountShort'] -
              tradeStats['totalBuyAmountShort']),
          file=outputFile)
    print("The gross profit for Long are: " +
          str(tradeStats['totalSellAmountLong'] -
              tradeStats['totalBuyAmountLong']),
          file=outputFile)
    print("The total open sell amount is: " +
          str(tradeStats['totalSellAmountShort']),
          file=outputFile)
    print("The total close sell amount is: " +
          str(tradeStats['totalSellAmountLong']),
          file=outputFile)
    print("The total close buy amount is: " +
          str(tradeStats['totalBuyAmountShort']),
          file=outputFile)
    print("The total open buy amount is: " +
          str(tradeStats['totalBuyAmountLong']),
          file=outputFile)
    print("Number open sell trade happened: " +
          str(tradeStats['NumberOfOpenSell']),
          file=outputFile)
    print("Number close sell trade happened: " +
          str(tradeStats['NumberOfCloseSell']),
          file=outputFile)
    print("Number close buy trade happened: " +
          str(tradeStats['NumberOfCloseBuy']),
          file=outputFile)
    print("Number open buy trade happened: " +
          str(tradeStats['NumberOfOpenBuy']),
          file=outputFile)

    try:
        averageOpenSellPrice = tradeStats['totalSellAmountShort'] / tradeStats[
            'NumberOfOpenSell']
        averageCloseBuyPrice = tradeStats['totalBuyAmountShort'] / tradeStats[
            'NumberOfCloseBuy']
    except:
        averageOpenSellPrice = 0
        averageCloseBuyPrice = 0
    try:
        averageCloseSellPrice = tradeStats['totalSellAmountLong'] / tradeStats[
            'NumberOfCloseSell']
        averageOpenBuyPrice = tradeStats['totalBuyAmountLong'] / tradeStats[
            'NumberOfOpenBuy']
    except:
        averageCloseSellPrice = 0
        averageOpenBuyPrice = 0

    print("Average open sell price per unit is: " + str(averageOpenSellPrice),
          file=outputFile)
    print("Average close sell price per unit is: " +
          str(averageCloseSellPrice),
          file=outputFile)
    print("Average open buy price per unit is: " + str(averageOpenBuyPrice),
          file=outputFile)
    print("Average close buy price per unit is: " + str(averageCloseBuyPrice),
          file=outputFile)
    print("The current position Short: " +
          str(tradeStats['currentPositionShort']),
          file=outputFile)
    print("The current position Long: " +
          str(tradeStats['currentPositionLong']),
          file=outputFile)
    print("Profit or loss per Qty traded Short is: " +
          str(averageOpenSellPrice - averageCloseBuyPrice),
          file=outputFile)
    print("Profit or loss per Qty traded Long is: " +
          str(averageCloseSellPrice - averageOpenBuyPrice),
          file=outputFile)
    pLPerLotShort = (averageOpenSellPrice - averageCloseBuyPrice) * 1000
    pLPerLotLong = (averageCloseSellPrice - averageOpenBuyPrice) * 1000
    print("1 lot has 1000 qty's so P/L Short per lot is: " +
          str(pLPerLotShort),
          file=outputFile)
    print("1 lot has 1000 qty's so P/L Long per lot is: " + str(pLPerLotLong),
          file=outputFile)
    print("P/L for Short trading 10 lots is: " + str(pLPerLotShort * 10),
          file=outputFile)
    print("P/L for Long trading 10 lots is: " + str(pLPerLotLong * 10),
          file=outputFile)
def extractAttributeFromDataMatrix(args):
    if args.n == None:
        N = 5
    else:
        N = int(args.n)

    try:
        args.c
    except:
        print "Since -c has not been specified I cannot proceed"
        os._exit()

    colNumberOfTimeStamp = colNumberOfData.TimeStamp
    colNumberOfExchangeStamp = colNumberOfData.ExchangeTS
    l_first_time_elapsed = False
    numberOfRowsInLastNSecs = 0
    queueOfValuesInLastNSecs = deque()
    totalOfRowsInLastNSecs = 0.0
    timeOfOldestRow = common.convertTimeStampFromStringToFloat(
        dataFile.matrix[0][colNumberOfExchangeStamp], "synthetic")
    currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
    lengthOfDataMatrix = len(dataFile.matrix)
    while (currentRowNumberForWhichFeatureValueIsBeingCalculated <
           lengthOfDataMatrix):
        if currentRowNumberForWhichFeatureValueIsBeingCalculated != 0:
            lPreviousDataRow = dataFile.matrix[
                currentRowNumberForWhichFeatureValueIsBeingCalculated - 1]
        lCurrentDataRow = dataFile.matrix[
            currentRowNumberForWhichFeatureValueIsBeingCalculated]
        lPreviousRowData = dataFile.matrix[
            currentRowNumberForWhichFeatureValueIsBeingCalculated - 1]
        timeOfCurrentRow = common.convertTimeStampFromStringToFloat(
            lCurrentDataRow[colNumberOfExchangeStamp], "synthetic")
        timeElapsed = timeOfCurrentRow - timeOfOldestRow
        if (timeElapsed < N):
            lMsgCode = lCurrentDataRow[colNumberOfData.MsgCode]
            lOrderType = lCurrentDataRow[colNumberOfData.OrderType]
            lNewPrice = float(lCurrentDataRow[colNumberOfData.NewP])
            lNewQty = int(lCurrentDataRow[colNumberOfData.NewQ])
            if lMsgCode == "M":
                lOldPrice = float(lCurrentDataRow[colNumberOfData.OldP])
                lOldQty = int(lCurrentDataRow[colNumberOfData.OldQ])
            if args.c.lower() == "ask":
                lColCQtyList = [
                    lCurrentDataRow[colNumberOfData.AskQ0],
                    lCurrentDataRow[colNumberOfData.AskQ1],
                    lCurrentDataRow[colNumberOfData.AskQ2],
                    lCurrentDataRow[colNumberOfData.AskQ3],
                    lCurrentDataRow[colNumberOfData.AskQ4]
                ]
            else:
                lColCQtyList = [
                    lCurrentDataRow[colNumberOfData.BidQ0],
                    lCurrentDataRow[colNumberOfData.BidQ1],
                    lCurrentDataRow[colNumberOfData.BidQ2],
                    lCurrentDataRow[colNumberOfData.BidQ3],
                    lCurrentDataRow[colNumberOfData.BidQ4]
                ]
            lColCPrevPriceList = []
            lColCPriceList = []
            if args.c.lower() == "ask":
                lColCPriceList = [
                    lCurrentDataRow[colNumberOfData.AskP0],
                    lCurrentDataRow[colNumberOfData.AskP1],
                    lCurrentDataRow[colNumberOfData.AskP2],
                    lCurrentDataRow[colNumberOfData.AskP3],
                    lCurrentDataRow[colNumberOfData.AskP4]
                ]
                if currentRowNumberForWhichFeatureValueIsBeingCalculated != 0:
                    lColCPrevPriceList = [
                        lPreviousDataRow[colNumberOfData.AskP0],
                        lPreviousDataRow[colNumberOfData.AskP1],
                        lPreviousDataRow[colNumberOfData.AskP2],
                        lPreviousDataRow[colNumberOfData.AskP3],
                        lPreviousDataRow[colNumberOfData.AskP4]
                    ]
            else:
                lColCPriceList = [
                    lCurrentDataRow[colNumberOfData.BidP0],
                    lCurrentDataRow[colNumberOfData.BidP1],
                    lCurrentDataRow[colNumberOfData.BidP2],
                    lCurrentDataRow[colNumberOfData.BidP3],
                    lCurrentDataRow[colNumberOfData.BidP4]
                ]
                if currentRowNumberForWhichFeatureValueIsBeingCalculated != 0:
                    lColCPrevPriceList = [
                        lPreviousDataRow[colNumberOfData.BidP0],
                        lPreviousDataRow[colNumberOfData.BidP1],
                        lPreviousDataRow[colNumberOfData.BidP2],
                        lPreviousDataRow[colNumberOfData.BidP3],
                        lPreviousDataRow[colNumberOfData.BidP4]
                    ]

            cellValue = 0
            if (lMsgCode == "N" and args.c.lower() == "ask"
                    and lOrderType == "S") or (lMsgCode == "N"
                                               and args.c.lower() == "bid"
                                               and lOrderType == "B"):
                cellValue = lNewQty
            elif (lMsgCode == "X" and args.c.lower() == "ask"
                  and lOrderType == "S") or (lMsgCode == "X"
                                             and args.c.lower() == "bid"
                                             and lOrderType == "B"):
                cellValue = -1 * lNewQty
            elif (lMsgCode == "M" and args.c.lower() == "ask"
                  and lOrderType == "S") or (lMsgCode == "M"
                                             and args.c.lower() == "bid"
                                             and lOrderType == "B"):
                if len(lColCPrevPriceList) == 0:
                    cellValue = lNewQty
            #Case 1------------------------------------------------------------------------------------------------------------
                if (lNewPrice
                        in lColCPriceList) and (lOldPrice
                                                not in lColCPrevPriceList):
                    cellValue = lNewQty
            #Case 2------------------------------------------------------------------------------------------------------------
                elif (lNewPrice
                      not in lColCPriceList) and (lOldPrice
                                                  in lColCPrevPriceList):
                    cellValue = -1 * lOldQty
                else:
                    cellValue = lNewQty - lOldQty
            elif (len(lColCPrevPriceList) != 0) and (
                    lMsgCode == "T" and lNewPrice == lColCPrevPriceList[0]):
                cellValue = -1 * lNewQty
            totalOfRowsInLastNSecs = totalOfRowsInLastNSecs + cellValue
            lAvgValue = 0
            if timeElapsed != 0:
                if l_first_time_elapsed == False:
                    lAvgValue = float(totalOfRowsInLastNSecs) / (
                        numberOfRowsInLastNSecs + 1)
                else:
                    lAvgValue = float(totalOfRowsInLastNSecs) / N
            else:
                lAvgValue = 1
            attribute.aList[
                currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    0] = common.convertTimeStampFromStringToDecimal(
                        lCurrentDataRow[colNumberOfTimeStamp])
            attribute.aList[
                currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    1] = lAvgValue  # in 1st iteration currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
            attribute.aList[
                currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    2] = str(cellValue) + ";" + str(timeElapsed)
            queueOfValuesInLastNSecs.append([cellValue, timeOfCurrentRow])
            numberOfRowsInLastNSecs += 1  # Every append gets a +1
            currentRowNumberForWhichFeatureValueIsBeingCalculated += 1
            continue  # Since we are going back 1 row from current we cannot get data from current row

        else:
            l_first_time_elapsed = True
            # We need to reset the timeOfOldestRow since timeElapsed has exceeded N seconds
            while (timeElapsed >= N):
                if (len(queueOfValuesInLastNSecs) == 0):
                    timeOfOldestRow = timeOfCurrentRow
                    timeElapsed = 0
                    if (numberOfRowsInLastNSecs != 0):
                        print "Sanity check: This condition is not possible logically. numberOfRowsInLastNSecs should been 0. There has been an unknown error"
                        sys.exit(-1)
                    if (totalOfRowsInLastNSecs != 0):
                        print "Sanity check: This condition is not possible logically. totalOfRowsInLastNSecs should have been 0. There has been an unknown error"
                        sys.exit(-1)
                else:
                    oldestElementInQueue = queueOfValuesInLastNSecs.popleft()
                    colValueInOldestElementInQueue = oldestElementInQueue[0]
                    totalOfRowsInLastNSecs -= colValueInOldestElementInQueue
                    if len(queueOfValuesInLastNSecs) == 0:
                        timeElapsed = 0
                        timeOfOldestRow = timeOfCurrentRow
                    else:
                        timeOfOldestRow = queueOfValuesInLastNSecs[0][1]
                    numberOfRowsInLastNSecs -= 1  # every pop from the queue gets a -1
                    timeElapsed = timeOfCurrentRow - timeOfOldestRow
                    if (len(queueOfValuesInLastNSecs) !=
                            numberOfRowsInLastNSecs):
                        print "Sanity check: This condition is not possible logically. There has been an unknown error"
                        sys.exit(-1)

        print "Processed row number " + str(
            currentRowNumberForWhichFeatureValueIsBeingCalculated)

    lNameOfFeaturePrinted = "fMovSumMsgQtyOverCol" + args.c + "InLast" + str(
        args.n) + "Secs"
    return [
        "TimeStamp", lNameOfFeaturePrinted, "QtyAddedOrSubtracted",
        "TimeElapsed"
    ]
Exemple #21
0
def getDataFileAndPredictionsIntoObjectList(dataFileObject,pFileObjectList,lMinOfExitCl):
    lObjectList = []
    lCurrentDataRowCount = 0
    lPrevObj = None
    fileHasHeader = 1
    headerSkipped = 0
    dataFileSep = ";"
    predictFileSep = ","
    lListOfBidP = []
    lListOfAskP = []
    l_data_row_list =  list(islice(dataFileObject,10000)) 
    lFeatureFileRowList = {}
    for index in xrange(len(pFileObjectList)):
        if dd.gTreeVariablesPresent[index].lower() != "j" and dd.gTreeVariablesPresent[index].lower() != "k":  
            lFeatureFileRowList[ dd.gTreeVariablesPresent[index] ] = (list(islice(pFileObjectList[index],10000)))
    while True:
        lDataFileRowsList = list(islice(dataFileObject,dd.gNoOfLineReadPerChunk))
        lFeatureFileRowList = []
        index = 0
        lFeatureFileRowList = {}
        for index in xrange(len(pFileObjectList)):
            lFeatureFileRowList[ dd.gTreeVariablesPresent[index] ] = (list(islice(pFileObjectList[index],dd.gNoOfLineReadPerChunk)))
        if not lDataFileRowsList:
            print("Finished reading file")
            lObjectList.append(lPrevObj)    
            lPrevObj = None          
            break
        lengthOfDataList = len(lDataFileRowsList)
        for features in lFeatureFileRowList:
            if lengthOfDataList != len(lFeatureFileRowList[features]):
                print("Length of data file and feature file are not same ")
                os._exit(-1)                
        for currentRowIndex in range(lengthOfDataList):
            if(fileHasHeader == 1 and headerSkipped != 1):
                headerSkipped = 1 
                continue
            lDataRow = lDataFileRowsList[currentRowIndex].rstrip().split(dataFileSep)
            '''
            if((args.e).find("nsefut") >= 0):
                lAskP = float(lDataRow[colNumberOfData.BestAskP])
                lBidP = float(lDataRow[colNumberOfData.BestBidP])
                lAskQ = int(lDataRow[colNumberOfData.BestAskQ])
                lBidQ = int(lDataRow[colNumberOfData.BestBidQ])
            else:
            '''
            lAskP = float(lDataRow[colNumberOfData.AskP0])
            lBidP = float(lDataRow[colNumberOfData.BidP0])
            lAskQ = int(lDataRow[colNumberOfData.AskQ0])
            lBidQ = int(lDataRow[colNumberOfData.BidQ0])
            lTTQ = int(lDataRow[colNumberOfData.TTQ])
            lLTP = float(lDataRow[colNumberOfData.LTP]) 
            lCurrentDataRowTimeStamp = common.convertTimeStampFromStringToFloat(lDataRow[colNumberOfData.TimeStamp])
            lFeatureValueDict = {}
            for feature in lFeatureFileRowList:
                if feature.lower() == "j" or feature.lower() == "k":
                    lSep = ","
                    lTimeStampIndex = 1
                    lFeatureIndex = 2
                else:
                    lSep = ";"
                    lTimeStampIndex = 0
                    lFeatureIndex = 1
                #print("Varible name" , feature)
                lFeatureFileRow = lFeatureFileRowList[feature][currentRowIndex].rstrip().split(lSep)
                lFeatureFileTimeStamp = float(lFeatureFileRow[lTimeStampIndex])
                lFeatureFileValue = float(lFeatureFileRow[lFeatureIndex])
                if lCurrentDataRowTimeStamp != lFeatureFileTimeStamp:
                    print('Time stamp of data row with feature value is not matching .\n Data row time stamp :- ' , lCurrentDataRowTimeStamp,'Feature value Time Stamp :- ' , lFeatureFileTimeStamp)
                    os._exit(-1)
                lFeatureValueDict[feature]=lFeatureFileValue
            lObj = dd.Tick(lCurrentDataRowTimeStamp,lAskP,lBidP,lAskQ,lBidQ,lLTP,lTTQ,lFeatureValueDict)
            if lPrevObj!=None:
                lPrevObj.TTQChange  = lObj.TTQ - lPrevObj.TTQ
                lPrevObj.NextLTP = lObj.LTP
                if ( lPrevObj.AskP -lPrevObj.BidP > dd.gTickSize ) and ( lPrevObj.TTQChange == 0 ):
                    if lPrevObj.BidP not in lListOfBidP:
                        lListOfBidP.append(lPrevObj.BidP)
                    if lPrevObj.AskP not in lListOfAskP:
                        lListOfAskP.append(lPrevObj.AskP)
                    pass
                else:
                    if (1):
                        if len(lListOfBidP) > 1:
                            lPrevObj.bidPChangedInBetweenLastTickAndCurrentTick = 1
                        if len(lListOfAskP) > 1:
                            lPrevObj.askPChangedInBetweenLastTickAndCurrentTick = 1
                        lObjectList.append(lPrevObj)  
                        lListOfBidP = [lPrevObj.BidP]
                        lListOfAskP = [lPrevObj.AskP]
                    else:
                        if lPrevObj.BidP not in lListOfBidP:
                            lListOfBidP.append(lPrevObj.BidP)
                        if lPrevObj.AskP not in lListOfAskP:
                            lListOfAskP.append(lPrevObj.AskP)
            lPrevObj = lObj
            if lCurrentDataRowCount%50000 ==0:
                print("Completed reading ",lCurrentDataRowCount)
            lCurrentDataRowCount = lCurrentDataRowCount + 1 
    return lObjectList
def extractAttributeFromDataMatrix(args):
    N = 5
    if args.n == None:
        N = 5
    else:
        N = int(args.n) 
    
    try:
        args.c
    except:
        print "Since -c has not been specified I cannot proceed"
        os._exit()

    try:
        args.o
    except:
        print "Since -o has not been specified I cannot proceed"
        os._exit()       

    colNumberOfTimeStamp = colNumberOfData.TimeStamp
    colNumberOfExchangeStamp = colNumberOfData.ExchangeTS
    numberOfRowsInLastNSecs = 0
    l_first_time_elapsed = False   
    queueOfValuesInLastNSecs = deque()
    timeOfOldestRow = common.convertTimeStampFromStringToFloat(dataFile.matrix[0][colNumberOfExchangeStamp],"synthetic")
    currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
    lengthOfDataMatrix = len(dataFile.matrix)
    totalTradedQty = 0
    lPreviousTickObject = None
    priceIndex = eval('colNumberOfData.'+args.c+'P0')
    qtyIndex = [ eval('colNumberOfData.'+args.c+'Q0') , eval('colNumberOfData.'+args.c+'Q1') , eval('colNumberOfData.'+args.c+'Q2') , eval('colNumberOfData.'+args.c+'Q3') , eval('colNumberOfData.'+args.c+'Q4') ]
    if args.c == 'Ask':
        orderTypeToBeTracked = 'S'
    else:
        orderTypeToBeTracked = 'B'
    while (currentRowNumberForWhichFeatureValueIsBeingCalculated < lengthOfDataMatrix):

        lCurrentTickObject = ticks_values_to_be_stored()
        lCurrentDataRow = dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated]
        timeOfCurrentRow = common.convertTimeStampFromStringToFloat(lCurrentDataRow[colNumberOfExchangeStamp],"synthetic")
        timeElapsed = timeOfCurrentRow - timeOfOldestRow
        if (timeElapsed < N):
            lCurrentTickObject.MsgCode = lCurrentDataRow[colNumberOfData.MsgCode]
            lCurrentTickObject.OrderType = lCurrentDataRow[colNumberOfData.OrderType]
            lCurrentTickObject.NewP = float(lCurrentDataRow[colNumberOfData.NewP])
            lCurrentTickObject.NewQ = int(lCurrentDataRow[colNumberOfData.NewQ])
            lCurrentTickObject.Price = float( lCurrentDataRow[priceIndex] )
            lCurrentTickObject.QtyList = [ int(lCurrentDataRow[qtyIndex[0]]) , int(lCurrentDataRow[qtyIndex[1]]), int(lCurrentDataRow[qtyIndex[2]]),int(lCurrentDataRow[qtyIndex[3]]),int(lCurrentDataRow[qtyIndex[4]]) ]

            totalTradedQty = updateCurrentTickAdditionToQueue(lCurrentTickObject, lPreviousTickObject , totalTradedQty , timeElapsed , l_first_time_elapsed , N , orderTypeToBeTracked , args.o , numberOfRowsInLastNSecs)

            attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][0] = common.convertTimeStampFromStringToDecimal(lCurrentDataRow[colNumberOfTimeStamp])
            attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][1] = str(lCurrentTickObject.IntensityValue) 
            
            attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][2] = ";".join( map( str , [ lCurrentDataRow[colNumberOfExchangeStamp] , timeElapsed , totalTradedQty , lCurrentTickObject.MsgCode ,\
                                                                                                          lCurrentTickObject.OrderType , lCurrentTickObject.NewP , lCurrentTickObject.NewQ ] ) )
            attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][3] = str(lCurrentTickObject.Price)
            queueOfValuesInLastNSecs.append([lCurrentTickObject,timeOfCurrentRow])
            lPreviousTickObject = lCurrentTickObject
            numberOfRowsInLastNSecs += 1   # Every append gets a +1 
            currentRowNumberForWhichFeatureValueIsBeingCalculated += 1
            continue     # Since we are going back 1 row from current we cannot get data from current row
        
        else:
            l_first_time_elapsed = True
            # We need to reset the timeOfOldestRow since timeElapsed has exceeded N seconds
            while(timeElapsed >= N):
                if(len(queueOfValuesInLastNSecs) == 0):
                    timeOfOldestRow = timeOfCurrentRow
                    timeElapsed = 0
                    if(numberOfRowsInLastNSecs != 0):
                        print "Sanity check: This condition is not possible logically. numberOfRowsInLastNSecs should been 0. There has been an unknown error"
                        sys.exit(-1)
                else:   
                    oldestElementInQueue = queueOfValuesInLastNSecs.popleft()
                    colValueInOldestElementInQueue = oldestElementInQueue[0]
                    totalTradedQty = updateTickDeletionFromQueue(colValueInOldestElementInQueue , totalTradedQty )
                    if len(queueOfValuesInLastNSecs) == 0:
                        timeElapsed = 0
                        timeOfOldestRow = timeOfCurrentRow
                    else:
                        timeOfOldestRow = queueOfValuesInLastNSecs[0][1]
                    numberOfRowsInLastNSecs -= 1 # every pop from the queue gets a -1
                    timeElapsed = timeOfCurrentRow - timeOfOldestRow
                    if(len(queueOfValuesInLastNSecs) != numberOfRowsInLastNSecs):
                        print "Sanity check: This condition is not possible logically. There has been an unknown error"
                        sys.exit(-1)
        
        print "Processed row number " + str(currentRowNumberForWhichFeatureValueIsBeingCalculated)

    lNameOfFeaturePrinted = "fIntensityOfCol" + str(args.c) + "InOrder" + str(args.o) + "InLast" + str(args.n) + "Secs" 
    return [ "TimeStamp", lNameOfFeaturePrinted , "ExTimeStamp","TimeElapsed" ,"totalQty", "MsgCode" , "Ordertype" ,"NewP","NewQ" ,"Price"
            ]
Exemple #23
0
def extractAttributeFromDataMatrix(args):
    if args.n == None:
        N = 5
    else:
        N = int(args.n)
    try:
        #Value of argument c can be taken as BidP0 and AskP0
        args.c
    except:
        print "Since -c has not been specified I cannot proceed"
        os._exit()
    try:
        M = int(args.m)
    except:
        print "-m wrong!!"
    colNumberOfAttribute = eval("colNumberOfData." + args.c)
    colNumberOfOppositeAttribute = eval(
        "colNumberOfData.BestBidP") if "ask" in args.c else eval(
            "colNumberOfData.BestAskP")
    colNumberOfTimeStamp = colNumberOfData.TimeStamp
    if "ask" in args.c.lower():
        colAttributeRowNo = [
            colNumberOfData.AskP0, colNumberOfData.AskP1
        ]  #, colNumberOfData.AskP2, colNumberOfData.AskP3, colNumberOfData.AskP4]
    else:
        colAttributeRowNo = [
            colNumberOfData.BidP0, colNumberOfData.BidP1
        ]  #, colNumberOfData.BidP2, colNumberOfData.BidP3, colNumberOfData.BidP4]
    numberOfRowsInLastNSecs = 0
    queueOfValuesInLastNSecsN = []
    queueOfValuesInLastNSecsX = []
    totalOfRowsInLastNSecsX = 0.0
    totalOfRowsInLastNSecsN = 0.0
    totalOfQtyInLastNSecsX = 0.0
    totalOfQtyInLastNSecsN = 0.0

    timeOfOldestRow = common.convertTimeStampFromStringToFloat(
        dataFile.matrix[0][colNumberOfTimeStamp], args.cType)
    currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
    lengthOfDataMatrix = len(dataFile.matrix)
    while (currentRowNumberForWhichFeatureValueIsBeingCalculated <
           lengthOfDataMatrix):
        cellValueQtyN, cellValueQtyX, cellValueTotalN, cellValueTotalX = [
            0, 0, 0, 0
        ]
        colAttributeRow = map(float, [
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][i]
            for i in colAttributeRowNo
        ])
        timeOfCurrentRow = common.convertTimeStampFromStringToFloat(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated]
            [colNumberOfTimeStamp], args.cType)
        if currentRowNumberForWhichFeatureValueIsBeingCalculated != 0:
            previousRowPrice = float(dataFile.matrix[
                currentRowNumberForWhichFeatureValueIsBeingCalculated -
                1][colNumberOfAttribute])
            previousRowOppPrice = float(dataFile.matrix[
                currentRowNumberForWhichFeatureValueIsBeingCalculated -
                1][colNumberOfOppositeAttribute])
        else:
            previousRowPrice = 0
            previousRowOppPrice = 0
        currentPrice = float(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated]
            [colNumberOfAttribute])
        currentLTP = float(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                colNumberOfData.LTP])
        currentMsgCode = dataFile.matrix[
            currentRowNumberForWhichFeatureValueIsBeingCalculated][
                colNumberOfData.MsgCode]
        newQty = int(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                colNumberOfData.NewQ])
        newPrice = int(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                colNumberOfData.NewP])
        oldQty = int(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                colNumberOfData.OldQ])
        oldPrice = int(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                colNumberOfData.OldP])
        if (currentMsgCode == 'T' and (
            (previousRowOppPrice <= currentLTP) if "bid" in args.c.lower() else
            (previousRowOppPrice >= currentLTP))):
            totalOfRowsInLastNSecsN += newQty * currentLTP
            cellValueTotalN = newQty * currentLTP
            totalOfQtyInLastNSecsN += newQty
            cellValueQtyN = newQty
            queueOfValuesInLastNSecsN.append([
                cellValueTotalN, colAttributeRow[-1], cellValueQtyN,
                currentLTP, timeOfCurrentRow
            ])

        elif currentMsgCode == 'M':
            if ("ask" in args.c.lower() and newPrice <= colAttributeRow[-1]
                    and newPrice >= currentPrice) or (
                        "bid" in args.c.lower()
                        and newPrice >= colAttributeRow[-1]
                        and newPrice <= currentPrice):
                check = True
            else:
                check = False
            totalOfRowsInLastNSecsN += (newPrice * newQty) if check else 0
            cellValueTotalN = newPrice * newQty if check else 0
            totalOfQtyInLastNSecsN += newQty if check else 0
            cellValueQtyN = newQty if check else 0
            if check:
                queueOfValuesInLastNSecsN.append([
                    cellValueTotalN, colAttributeRow[-1], cellValueQtyN,
                    newPrice, timeOfCurrentRow
                ])

            if ("ask" in args.c.lower() and oldPrice <= colAttributeRow[-1]
                    and oldPrice >= previousRowPrice) or (
                        "bid" in args.c.lower()
                        and oldPrice >= colAttributeRow[-1]
                        and oldPrice <= previousRowPrice):
                check = True
            else:
                check = False
            totalOfRowsInLastNSecsX += oldPrice * oldQty if check else 0
            cellValueTotalX = oldPrice * oldQty if check else 0
            totalOfQtyInLastNSecsX += oldQty if check else 0
            cellValueQtyX = oldQty if check else 0
            if check:
                queueOfValuesInLastNSecsX.append([
                    cellValueTotalX, colAttributeRow[-1], cellValueQtyX,
                    oldPrice, timeOfCurrentRow
                ])

        elif (currentMsgCode == 'N'):
            if ("ask" in args.c.lower() and newPrice <= colAttributeRow[-1]
                    and newPrice >= currentPrice) or (
                        "bid" in args.c.lower()
                        and newPrice >= colAttributeRow[-1]
                        and newPrice <= currentPrice):
                check = True
            else:
                check = False
            totalOfRowsInLastNSecsN += newPrice * newQty if check else 0
            cellValueTotalN = newPrice * newQty if check else 0
            totalOfQtyInLastNSecsN += newQty if check else 0
            cellValueQtyN = newQty if check else 0
            if check:
                queueOfValuesInLastNSecsN.append([
                    cellValueTotalN, colAttributeRow[-1], cellValueQtyN,
                    newPrice, timeOfCurrentRow
                ])

        elif (currentMsgCode == 'X'):
            if ("ask" in args.c.lower() and newPrice <= colAttributeRow[-1]
                    and newPrice >= previousRowPrice) or (
                        "bid" in args.c.lower()
                        and newPrice >= colAttributeRow[-1]
                        and newPrice <= previousRowPrice):
                check = True
            else:
                check = False
            totalOfRowsInLastNSecsX += newPrice * newQty if check else 0
            cellValueTotalX = newPrice * newQty if check else 0
            totalOfQtyInLastNSecsX += newQty if check else 0
            cellValueQtyX = newQty if check else 0
            if check:
                queueOfValuesInLastNSecsX.append([
                    cellValueTotalX, colAttributeRow[-1], cellValueQtyX,
                    newPrice, timeOfCurrentRow
                ])

        if (totalOfQtyInLastNSecsN) > N:
            while totalOfQtyInLastNSecsN > N:
                excess = totalOfQtyInLastNSecsN - N
                if queueOfValuesInLastNSecsN[0][2] <= excess:
                    totalOfQtyInLastNSecsN -= queueOfValuesInLastNSecsN[0][2]
                    totalOfRowsInLastNSecsN -= queueOfValuesInLastNSecsN[0][0]
                    queueOfValuesInLastNSecsN = queueOfValuesInLastNSecsN[1:]
                else:
                    totalOfQtyInLastNSecsN -= excess
                    totalOfRowsInLastNSecsN -= excess * float(
                        queueOfValuesInLastNSecsN[0]
                        [0]) / queueOfValuesInLastNSecsN[0][2]
                    queueOfValuesInLastNSecsN[0][2] -= excess
                    queueOfValuesInLastNSecsN[0][
                        0] -= excess * queueOfValuesInLastNSecsN[0][3]

        if (totalOfQtyInLastNSecsX) > N:
            while totalOfQtyInLastNSecsX > N:
                excess = totalOfQtyInLastNSecsX - N
                if queueOfValuesInLastNSecsX[0][2] <= excess:
                    totalOfQtyInLastNSecsX -= queueOfValuesInLastNSecsX[0][2]
                    totalOfRowsInLastNSecsX -= queueOfValuesInLastNSecsX[0][0]
                    queueOfValuesInLastNSecsX = queueOfValuesInLastNSecsX[1:]
                else:
                    totalOfQtyInLastNSecsX -= excess
                    totalOfRowsInLastNSecsX -= excess * float(
                        queueOfValuesInLastNSecsX[0]
                        [0]) / queueOfValuesInLastNSecsX[0][2]
                    queueOfValuesInLastNSecsX[0][2] -= excess
                    queueOfValuesInLastNSecsX[0][
                        0] -= excess * queueOfValuesInLastNSecsX[0][3]

        if M == 1:
            exPrice = colAttributeRow[-1]
        elif M == 2:
            try:
                exPrice = queueOfValuesInLastNSecsN[0][
                    1] if queueOfValuesInLastNSecsN[0][
                        -1] < queueOfValuesInLastNSecsX[0][
                            -1] else queueOfValuesInLastNSecsX[0][1]
            except:
                print("Warning!!!!")
                exPrice = colAttributeRow[-1]
        elif M == 3:
            try:
                exPrice = totalOfRowsInLastNSecsN / float(
                    totalOfQtyInLastNSecsN)
            except:
                print("Warning!!!!")
                exPrice = colAttributeRow[-1]
        elif M == 4:
            try:
                exPrice = totalOfRowsInLastNSecsX / float(
                    totalOfQtyInLastNSecsX)
            except:
                print("Warning!!!!")
                exPrice = colAttributeRow[-1]

        newSide = (float(totalOfRowsInLastNSecsN +
                         (N - totalOfQtyInLastNSecsN) * exPrice) / N)
        cancelSide = (float(totalOfRowsInLastNSecsX +
                            (N - totalOfQtyInLastNSecsX) * exPrice) / N)
        attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][
            0] = common.convertTimeStampFromStringToDecimal(
                dataFile.
                matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated]
                [colNumberOfTimeStamp], args.cType)
        attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][
            1] = (
                float(newSide) - cancelSide
            )  # in 1st iteration currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
        attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][
            2] = str(currentPrice) + ";" + str(cellValueQtyN) + ";" + str(
                cellValueQtyX) + ";" + str(cellValueTotalN) + ";" + str(
                    cellValueTotalX) + ";" + str(currentMsgCode)
        numberOfRowsInLastNSecs += 1  # Every append gets a +1
        #      if currentRowNumberForWhichFeatureValueIsBeingCalculated == 164:
        #          import pdb
        #          pdb.set_trace()
        currentRowNumberForWhichFeatureValueIsBeingCalculated += 1
        continue  # Since we are going back 1 row from current we cannot get data from current row
        print "Processed row number " + str(
            currentRowNumberForWhichFeatureValueIsBeingCalculated)

    lNameOfFeaturePrinted = "fMarketModNewSumOfCol" + args.c + "InLast" + str(
        args.n) + "Secs"
    return [
        "TimeStamp", lNameOfFeaturePrinted, args.c, "QtyForN", "QtyForX",
        "AmountN", "AmountX", "MsgCode"
    ]
def extractAttributeFromDataMatrix(args):
    if args.n == None:
        N = 5
    else:
        N = float(args.n)
    try:
        args.c
    except:
        print "Since -c has not been specified I cannot proceed"
        os._exit()

    if (args.cType == "synthetic"):
        colNumberOfAttribute = colNumberOfData.SysFeature
    else:
        colNumberOfAttribute = eval("colNumberOfData." + args.c)

    colNumberOfTimeStamp = colNumberOfData.TimeStamp
    colNumberOfExchangeStamp = colNumberOfData.ExchangeTS
    numberOfRowsInLastNSecs = 0
    queueOfValuesInLastNSecs = deque()
    totalOfRowsInLastNSecs = 0.0
    timeOfOldestRow = common.convertTimeStampFromStringToFloat(
        dataFile.matrix[0][colNumberOfExchangeStamp], "synthetic")
    currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
    lengthOfDataMatrix = len(dataFile.matrix)
    print "lengthOfDataMatrix", lengthOfDataMatrix
    while (currentRowNumberForWhichFeatureValueIsBeingCalculated <
           lengthOfDataMatrix):
        timeOfCurrentRow = common.convertTimeStampFromStringToFloat(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated]
            [colNumberOfExchangeStamp], "synthetic")
        timeElapsed = timeOfCurrentRow - timeOfOldestRow
        if (timeElapsed < N):
            cellValue = float(
                dataFile.
                matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated]
                [colNumberOfAttribute])
            totalOfRowsInLastNSecs += cellValue
            attribute.aList[
                currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    0] = common.convertTimeStampFromStringToDecimal(
                        dataFile.matrix[
                            currentRowNumberForWhichFeatureValueIsBeingCalculated]
                        [colNumberOfTimeStamp])
            attribute.aList[
                currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    1] = totalOfRowsInLastNSecs / (
                        numberOfRowsInLastNSecs + 1
                    )  # in 1st iteration currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
            attribute.aList[
                currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    2] = str(totalOfRowsInLastNSecs)
            attribute.aList[
                currentRowNumberForWhichFeatureValueIsBeingCalculated][
                    3] = str(numberOfRowsInLastNSecs) + ";" + str(
                        timeElapsed) + ";" + str(timeOfCurrentRow)
            queueOfValuesInLastNSecs.append([cellValue, timeOfCurrentRow])
            numberOfRowsInLastNSecs += 1  # Every append gets a +1
            currentRowNumberForWhichFeatureValueIsBeingCalculated += 1
            continue  # Since we are going back 1 row from current we cannot get data from current row
        else:
            # We need to reset the timeOfOldestRow since timeElapsed has exceeded N seconds
            while (timeElapsed >= N):
                if (len(queueOfValuesInLastNSecs) == 0):
                    timeOfOldestRow = timeOfCurrentRow
                    timeElapsed = 0
                    if (numberOfRowsInLastNSecs != 0):
                        print "Sanity check: This condition is not possible logically. numberOfRowsInLastNSecs should been 0. There has been an unknown error"
                        sys.exit(-1)
                    if (totalOfRowsInLastNSecs != 0):
                        print "Sanity check: This condition is not possible logically. totalOfRowsInLastNSecs should have been 0. There has been an unknown error"
                        sys.exit(-1)
                else:
                    oldestElementInQueue = queueOfValuesInLastNSecs.popleft()
                    colValueInOldestElementInQueue = oldestElementInQueue[0]
                    totalOfRowsInLastNSecs -= colValueInOldestElementInQueue
                    if len(queueOfValuesInLastNSecs) == 0:
                        timeElapsed = 0
                        timeOfOldestRow = timeOfCurrentRow
                    else:
                        timeOfOldestRow = queueOfValuesInLastNSecs[0][1]
                    numberOfRowsInLastNSecs -= 1  # every pop from the queue gets a -1
                    timeElapsed = timeOfCurrentRow - timeOfOldestRow
                    if (len(queueOfValuesInLastNSecs) !=
                            numberOfRowsInLastNSecs):
                        print "Sanity check: This condition is not possible logically. There has been an unknown error"
                        sys.exit(-1)

        print "Processed row number " + str(
            currentRowNumberForWhichFeatureValueIsBeingCalculated)

    lNameOfFeaturePrinted = "fMovingAverageOfCol" + args.c + "InLast" + str(
        args.n) + "Secs"
    return [
        "TimeStamp", lNameOfFeaturePrinted, "TotalOfRowsInLastNSecs",
        "NumberOfRowsInLastNSecs", "TimeElapsed", "ExTimeStamp"
    ]
Exemple #25
0
def readOnceAndWrite(pFileName, pIndexOfEntryOrExitCL, predictedValuesDict):
    global g_bestqty_list_for_sell, g_bestqty_list_for_buy
    attribute.initList()
    tradeStats = dict()
    tradeStats['totalSellAmountShort'] = 0
    tradeStats['totalBuyAmountShort'] = 0
    tradeStats['totalSellAmountLong'] = 0
    tradeStats['totalBuyAmountLong'] = 0
    tradeStats['NumberOfOpenBuy'] = 0
    tradeStats['NumberOfCloseSell'] = 0
    tradeStats['NumberOfOpenSell'] = 0
    tradeStats['NumberOfCloseBuy'] = 0
    tradeStats['currentPositionShort'] = 0
    tradeStats['currentPositionLong'] = 0
    noPredictionForThisRow = 0
    currentSellPredictedValue = 0
    currentBuyPredictedValue = 0
    entryCL = float(totalEntryCL[pIndexOfEntryOrExitCL]) / 100
    exitCL = float(totalExitCL[pIndexOfEntryOrExitCL]) / 100
    entryCL1 = float(totalEntryCL1[pIndexOfEntryOrExitCL]) / 100
    entryCL2 = float(totalEntryCL2[pIndexOfEntryOrExitCL]) / 100
    print(entryCL, exitCL, entryCL1, entryCL2)
    #  entryCLCutoff = float(args.entryCLCutoff)
    #  exitCLCutoff = float(args.exitCLCutoff)
    currentIndex = 0
    l_previous_obj = None
    l_obj = None
    print("Processing the data file for trades :")
    attribute.initList()
    for currentDataRow in dataFile.matrix:

        l_obj = update_obj_list(currentDataRow)
        lReturnList = checkIfPreviousDecisionToEnterOrExitTradeWasSuccessful(
            l_obj, l_previous_obj, tradeStats)

        lDummyBidQ0 = lReturnList[2]
        lDummyAskQ0 = lReturnList[3]
        lDummyTTQForBuy = lReturnList[4]
        lDummyTTQForSell = lReturnList[5]
        if currentIndex > 0:
            attribute.aList[currentIndex - 1][0] = l_obj.currentTimeStamp
            attribute.aList[currentIndex -
                            1][1] = tradeStats['currentPositionLong']
            attribute.aList[currentIndex -
                            1][2] = tradeStats['currentPositionShort']
            listOfStringsToPrint = [ str(l_previous_obj.BidQ) , str(l_previous_obj.BidP) , str(l_previous_obj.AskP0) , \
                                    str(l_previous_obj.AskQ0) , str(l_previous_obj.TTQ) , str(l_previous_obj.LTP) ,\
                                    str(currentSellPredictedValue) , str(l_previous_obj.enterTradeShort) ,l_previous_obj.ReasonForTradingOrNotTradingShort , str(currentBuyPredictedValue) ,\
                                    str(l_previous_obj.enterTradeLong) ,l_previous_obj.ReasonForTradingOrNotTradingLong , str(tradeStats['NumberOfCloseBuy']),\
                                    str(tradeStats['NumberOfOpenBuy']),str(tradeStats['NumberOfOpenSell']),\
                                    str(tradeStats['NumberOfCloseBuy']),str(lDummyBidQ0),str(lDummyAskQ0),\
                                    str(lDummyTTQForBuy),str(lDummyTTQForSell)]
            attribute.aList[currentIndex -
                            1][3] = ";".join(listOfStringsToPrint)
        currentTimeStamp = common.convertTimeStampFromStringToFloat(
            currentDataRow[colNumberOfData.TimeStamp])

        try:
            currentSellPredictedValue = float(
                predictedValuesDict[l_obj.currentTimeStamp]['sell'])
            currentBuyPredictedValue = float(
                predictedValuesDict[l_obj.currentTimeStamp]['buy'])
        except:
            noPredictionForThisRow += 1

        #short decisions
        if (currentSellPredictedValue >= entryCL):
            l_obj.standingAtAsk = 0
            l_obj.enterTradeShort = 1
        elif (currentSellPredictedValue >= entryCL1):
            if (l_previous_obj.standingAtAsk == 0):
                g_bestqty_list_for_sell = {}
            l_obj.standingAtAsk = 1
            l_obj.enterTradeShort = 1
        elif (l_previous_obj.standingAtAsk == 1
              and currentSellPredictedValue >= entryCL2):
            l_obj.standingAtAsk = 1
            l_obj.enterTradeShort = 1
        else:
            l_obj.standingAtAsk = 0
            l_obj.enterTradeShort = 0  # Implies make no change
        if ((currentBuyPredictedValue >= exitCL)
                and tradeStats['currentPositionShort'] > 0):
            l_obj.enterTradeShort = -1  # Implies to exit the trade
        else:
            l_obj.enterTradeShort = 0  # Implies make no change

        #       if enterTradeShort == 1:
        #           if currentPredictedValue <= entryCLCutoff:
        #               enterTrade = 0
        #       if enterTrade == -1:
        #           if currentPredictedValue >= exitCLCutoff:
        #               enterTrade = 0

        #long decisions
        if (l_obj.currentBuyPredictedValue >= entryCL):
            l_obj.standingAtBid = 0
            l_obj.enterTradeLong = 1
        elif (currentBuyPredictedValue >= entryCL1):
            if (l_previous_obj.standingAtBid == 0):
                g_bestqty_list_for_buy = {}
            l_obj.standingAtBid = 1
            l_obj.enterTradeLong = 1
        elif (l_previous_obj.standingAtBid == 1
              and currentBuyPredictedValue >= entryCL2):
            l_obj.standingAtBid = 1
            l_obj.enterTradeLong = 1
        else:
            l_obj.standingAtBid = 0
            l_obj.enterTradeLong = 0  # Implies make no change

        if ((currentSellPredictedValue >= exitCL)
                and tradeStats['currentPositionLong'] > 0):
            l_obj.enterTradeLong = -1  # Implies to exit the trade
        else:
            l_obj.enterTradeLong = 0  # Implies make no change

        l_previous_obj = l_obj
        currentIndex = currentIndex + 1


# Squaring off if some open position there
    if tradeStats['currentPositionLong'] > 0:
        tradeStats['NumberOfCloseSell'] += tradeStats['currentPositionLong']
        tradeStats['totalSellAmountLong'] += tradeStats[
            'currentPositionLong'] * (l_previous_obj.BidP)
        tradeStats['currentPositionLong'] = 0
        l_obj.ReasonForTradingOrNotTradingLong = 'CloseSell(Hitting)'
    if tradeStats['currentPositionShort'] > 0:
        tradeStats['NumberOfCloseBuy'] += tradeStats['currentPositionShort']
        tradeStats['totalBuyAmountShort'] += tradeStats[
            'currentPositionShort'] * (l_previous_obj.AskP)
        tradeStats['currentPositionShort'] = 0
        l_obj.ReasonForTradingOrNotTradingLong = 'CloseBuy(Hitting)'

    attribute.aList[currentIndex - 1][0] = currentTimeStamp
    attribute.aList[currentIndex - 1][1] = tradeStats['currentPositionLong']
    attribute.aList[currentIndex - 1][2] = tradeStats['currentPositionShort']
    listOfStringsToPrint = [ str(l_previous_obj.BidQ) , str(l_previous_obj.BidP) , str(l_previous_obj.AskP0) , \
                            str(l_previous_obj.AskQ0) , str(l_previous_obj.TTQ) , str(l_previous_obj.LTP) ,\
                            str(currentSellPredictedValue) , str(l_previous_obj.enterTradeShort) ,l_previous_obj.ReasonForTradingOrNotTradingShort , str(currentBuyPredictedValue) ,\
                            str(l_previous_obj.enterTradeLong) ,l_previous_obj.ReasonForTradingOrNotTradingLong , str(tradeStats['NumberOfCloseBuy']),\
                            str(tradeStats['NumberOfOpenBuy']),str(tradeStats['NumberOfOpenSell']),\
                            str(tradeStats['NumberOfCloseBuy']),str(lDummyBidQ0),str(lDummyAskQ0),\
                            str(lDummyTTQForBuy),str(lDummyTTQForSell)]
    attribute.aList[currentIndex - 1][3] = ";".join(listOfStringsToPrint)

    dirName = args.pd.replace('/ro/', '/rs/')
    tradeLogMainDirName = dirName + "/t/"
    if not os.path.exists(tradeLogMainDirName):
        os.mkdir(tradeLogMainDirName)
    tradeLogSubDirectoryName = tradeLogMainDirName + mainExperimentName + "/"
    if not os.path.exists(tradeLogSubDirectoryName):
        os.mkdir(tradeLogSubDirectoryName)

    fileName = tradeLogSubDirectoryName + pFileName + ".trade"
    lHeaderColumnNamesList = [
        'TimeStamp', 'CurrentPositionLong', 'CurrentPositionShort', 'BidQ0',
        'BidP0', 'AskP0', 'AskQ0', 'TTQ', 'LTP', 'CurPredValueShort',
        'EnterTradeShort', 'ReasonForTradingOrNotTradingShort',
        'CurPredValueLong', 'EnterTradeLong',
        'ReasonForTradingOrNotTradingLong', 'totalBuyTradeShort',
        'totalBuyLong', 'totalSellShort', 'totalSellLong', 'DummyBidQ0',
        'DummyAskQ0', 'DummyTTQChangeForSell', 'DummyTTQChangeForBuy'
    ]
    attribute.writeToFile(fileName, lHeaderColumnNamesList)

    tradeResultMainDirName = dirName + "/r/"
    if not os.path.exists(tradeResultMainDirName):
        os.mkdir(tradeResultMainDirName)
    tradeResultSubDirectoryName = tradeResultMainDirName + mainExperimentName + "/"
    if not os.path.exists(tradeResultSubDirectoryName):
        os.mkdir(tradeResultSubDirectoryName)
    fileName = tradeResultSubDirectoryName + pFileName + ".result"
    outputFile = open(fileName, "w")

    #changed file write to modify it to Short Long version
    print("Starting to write: " + fileName)
    print("The gross profit for Short are: " +
          str(tradeStats['totalSellAmountShort'] -
              tradeStats['totalBuyAmountShort']),
          file=outputFile)
    print("The gross profit for Long are: " +
          str(tradeStats['totalSellAmountLong'] -
              tradeStats['totalBuyAmountLong']),
          file=outputFile)
    print("The total open sell amount is: " +
          str(tradeStats['totalSellAmountShort']),
          file=outputFile)
    print("The total close sell amount is: " +
          str(tradeStats['totalSellAmountLong']),
          file=outputFile)
    print("The total close buy amount is: " +
          str(tradeStats['totalBuyAmountShort']),
          file=outputFile)
    print("The total open buy amount is: " +
          str(tradeStats['totalBuyAmountLong']),
          file=outputFile)
    print("Number open sell trade happened: " +
          str(tradeStats['NumberOfOpenSell']),
          file=outputFile)
    print("Number close sell trade happened: " +
          str(tradeStats['NumberOfCloseSell']),
          file=outputFile)
    print("Number close buy trade happened: " +
          str(tradeStats['NumberOfCloseBuy']),
          file=outputFile)
    print("Number open buy trade happened: " +
          str(tradeStats['NumberOfOpenBuy']),
          file=outputFile)

    try:
        averageOpenSellPrice = tradeStats['totalSellAmountShort'] / tradeStats[
            'NumberOfOpenSell']
        averageCloseBuyPrice = tradeStats['totalBuyAmountShort'] / tradeStats[
            'NumberOfCloseBuy']
    except:
        averageOpenSellPrice = 0
        averageCloseBuyPrice = 0
    try:
        averageCloseSellPrice = tradeStats['totalSellAmountLong'] / tradeStats[
            'NumberOfCloseSell']
        averageOpenBuyPrice = tradeStats['totalBuyAmountLong'] / tradeStats[
            'NumberOfOpenBuy']
    except:
        averageCloseSellPrice = 0
        averageOpenBuyPrice = 0

    print("Average open sell price per unit is: " + str(averageOpenSellPrice),
          file=outputFile)
    print("Average close sell price per unit is: " +
          str(averageCloseSellPrice),
          file=outputFile)
    print("Average open buy price per unit is: " + str(averageOpenBuyPrice),
          file=outputFile)
    print("Average close buy price per unit is: " + str(averageCloseBuyPrice),
          file=outputFile)
    print("The current position Short: " +
          str(tradeStats['currentPositionShort']),
          file=outputFile)
    print("The current position Long: " +
          str(tradeStats['currentPositionLong']),
          file=outputFile)
    print("Profit or loss per Qty traded Short is: " +
          str(averageOpenSellPrice - averageCloseBuyPrice),
          file=outputFile)
    print("Profit or loss per Qty traded Long is: " +
          str(averageCloseSellPrice - averageOpenBuyPrice),
          file=outputFile)
    pLPerLotShort = (averageOpenSellPrice - averageCloseBuyPrice) * 1000
    pLPerLotLong = (averageCloseSellPrice - averageOpenBuyPrice) * 1000
    print("1 lot has 1000 qty's so P/L Short per lot is: " +
          str(pLPerLotShort),
          file=outputFile)
    print("1 lot has 1000 qty's so P/L Long per lot is: " + str(pLPerLotLong),
          file=outputFile)
    print("P/L for Short trading 10 lots is: " + str(pLPerLotShort * 10),
          file=outputFile)
    print("P/L for Long trading 10 lots is: " + str(pLPerLotLong * 10),
          file=outputFile)
Exemple #26
0
def getDataFileAndPredictionsIntoObjectList(dataFileObject, fA5, fB5, fC5, fD5,
                                            fE5, fA6, fB6, fC6, fD6, fE6,
                                            lMinOfExitCl):
    global gNoOfLineReadPerChunk, gTickSize
    lObjectList = []
    lCurrentDataRowCount = 0
    lPrevObj = None
    fileHasHeader = 1
    headerSkipped = 0
    dataFileSep = ";"
    featureFileSep = ";"
    lListOfBidP = []
    lListOfAskP = []
    lDataFileRowsList = list(islice(dataFileObject, 10000))
    lFeatureAFileRowListFor5Level = list(islice(fA5, 10000))
    lFeatureBFileRowListFor5Level = list(islice(fB5, 10000))
    lFeatureCFileRowListFor5Level = list(islice(fC5, 10000))
    lFeatureDFileRowListFor5Level = list(islice(fD5, 10000))
    lFeatureEFileRowListFor5Level = list(islice(fE5, 10000))

    lFeatureAFileRowListFor6Level = list(islice(fA6, 10000))
    lFeatureBFileRowListFor6Level = list(islice(fB6, 10000))
    lFeatureCFileRowListFor6Level = list(islice(fC6, 10000))
    lFeatureDFileRowListFor6Level = list(islice(fD6, 10000))
    lFeatureEFileRowListFor6Level = list(islice(fE6, 10000))
    while True:
        lDataFileRowsList = list(islice(dataFileObject, gNoOfLineReadPerChunk))
        lFeatureAFileRowListFor5Level = list(islice(fA5,
                                                    gNoOfLineReadPerChunk))
        lFeatureBFileRowListFor5Level = list(islice(fB5,
                                                    gNoOfLineReadPerChunk))
        lFeatureCFileRowListFor5Level = list(islice(fC5,
                                                    gNoOfLineReadPerChunk))
        lFeatureDFileRowListFor5Level = list(islice(fD5,
                                                    gNoOfLineReadPerChunk))
        lFeatureEFileRowListFor5Level = list(islice(fE5,
                                                    gNoOfLineReadPerChunk))

        lFeatureAFileRowListFor6Level = list(islice(fA6,
                                                    gNoOfLineReadPerChunk))
        lFeatureBFileRowListFor6Level = list(islice(fB6,
                                                    gNoOfLineReadPerChunk))
        lFeatureCFileRowListFor6Level = list(islice(fC6,
                                                    gNoOfLineReadPerChunk))
        lFeatureDFileRowListFor6Level = list(islice(fD6,
                                                    gNoOfLineReadPerChunk))
        lFeatureEFileRowListFor6Level = list(islice(fE6,
                                                    gNoOfLineReadPerChunk))

        if not lDataFileRowsList:
            print("Finished reading file")
            lObjectList.append(lPrevObj)
            lPrevObj = None
            break
        lengthOfDataList = len(lDataFileRowsList)
        lengthOfFeatureAListFor5Level = len(lFeatureAFileRowListFor5Level)
        lengthOfFeatureBListFor5Level = len(lFeatureBFileRowListFor5Level)

        if lengthOfDataList != lengthOfFeatureAListFor5Level or lengthOfFeatureAListFor5Level != lengthOfFeatureBListFor5Level:
            print(
                "Length of data file and predicted buy and sell values file are not same "
            )
            os._exit(-1)
        for currentRowIndex in range(lengthOfDataList):
            if (fileHasHeader == 1 and headerSkipped != 1):
                headerSkipped = 1
                continue
            lDataRow = lDataFileRowsList[currentRowIndex].rstrip().split(
                dataFileSep)
            if 'nsefut' not in args.e:
                lAskP = float(lDataRow[colNumberOfData.AskP0])
                lBidP = float(lDataRow[colNumberOfData.BidP0])
                lAskQ = int(lDataRow[colNumberOfData.AskQ0])
                lBidQ = int(lDataRow[colNumberOfData.BidQ0])
                lAskP1 = float(lDataRow[colNumberOfData.AskP1])
                lBidP1 = float(lDataRow[colNumberOfData.BidP1])
                lAskQ1 = int(lDataRow[colNumberOfData.AskQ1])
                lBidQ1 = int(lDataRow[colNumberOfData.BidQ1])
            else:
                lAskP = float(lDataRow[colNumberOfData.BestAskP])
                lBidP = float(lDataRow[colNumberOfData.BestBidP])
                lAskQ = int(lDataRow[colNumberOfData.BestAskQ])
                lBidQ = int(lDataRow[colNumberOfData.BestBidQ])
                lAskP1 = float(lDataRow[colNumberOfData.BestAskP1])
                lBidP1 = float(lDataRow[colNumberOfData.BestBidP1])
                lAskQ1 = int(lDataRow[colNumberOfData.BestAskQ1])
                lBidQ1 = int(lDataRow[colNumberOfData.BestBidQ1])
            lTTQ = int(lDataRow[colNumberOfData.TTQ])
            lLTP = float(lDataRow[colNumberOfData.LTP])
            lCurrentDataRowTimeStamp = common.convertTimeStampFromStringToFloat(
                lDataRow[colNumberOfData.TimeStamp])

            lFeatureARow = lFeatureAFileRowListFor5Level[
                currentRowIndex].rstrip().split(featureFileSep)
            lFeatureATimeStamp = float(lFeatureARow[0])
            lFeatureA = float(lFeatureARow[1])

            lFeatureBRow = lFeatureBFileRowListFor5Level[
                currentRowIndex].rstrip().split(featureFileSep)
            lFeatureBTimeStamp = float(lFeatureBRow[0])
            lFeatureB = float(lFeatureBRow[1])

            lFeatureCRow = lFeatureCFileRowListFor5Level[
                currentRowIndex].rstrip().split(featureFileSep)
            lFeatureC = float(lFeatureCRow[1])

            lFeatureDRow = lFeatureDFileRowListFor5Level[
                currentRowIndex].rstrip().split(featureFileSep)
            lFeatureD = float(lFeatureDRow[1])

            lFeatureERow = lFeatureEFileRowListFor5Level[
                currentRowIndex].rstrip().split(featureFileSep)
            lFeatureE = float(lFeatureERow[1])

            lFeatureA6Row = lFeatureAFileRowListFor6Level[
                currentRowIndex].rstrip().split(featureFileSep)
            lFeatureA6 = float(lFeatureA6Row[1])

            lFeatureB6Row = lFeatureBFileRowListFor6Level[
                currentRowIndex].rstrip().split(featureFileSep)
            lFeatureB6 = float(lFeatureB6Row[1])

            lFeatureC6Row = lFeatureCFileRowListFor6Level[
                currentRowIndex].rstrip().split(featureFileSep)
            lFeatureC6 = float(lFeatureC6Row[1])

            lFeatureD6Row = lFeatureDFileRowListFor6Level[
                currentRowIndex].rstrip().split(featureFileSep)
            lFeatureD6 = float(lFeatureD6Row[1])

            lFeatureE6Row = lFeatureEFileRowListFor6Level[
                currentRowIndex].rstrip().split(featureFileSep)
            lFeatureE6 = float(lFeatureE6Row[1])

            l_buy_SOP_of_alphas_for_5_level = exp(
                ((813.280878130412 * lFeatureA) +
                 (-546.820253147887 * lFeatureB) +
                 (-498.096438026184 * lFeatureC) +
                 (-1008.9535016807 * lFeatureD) + 1237.93033895547))
            l_sell_SOP_of_alphas_for_5_level = exp(
                ((366.62364204055 * lFeatureA) +
                 (-881.958964774577 * lFeatureB) +
                 (619.51957804397 * lFeatureC) +
                 (1003.99686893559 * lFeatureE) - 1110.76386909796))

            l_buy_SOP_of_alphas_for_6_level = exp(
                ((813.280878130412 * lFeatureA6) +
                 (-546.820253147887 * lFeatureB6) +
                 (-498.096438026184 * lFeatureC6) +
                 (-1008.9535016807 * lFeatureD6) + 1237.93033895547))
            l_sell_SOP_of_alphas_for_6_level = exp(
                ((366.62364204055 * lFeatureA6) +
                 (-881.958964774577 * lFeatureB6) +
                 (619.51957804397 * lFeatureC6) +
                 (1003.99686893559 * lFeatureE6) - 1110.76386909796))

            lBuyPredictedValueFor5Levels = l_buy_SOP_of_alphas_for_5_level / (
                1 + l_buy_SOP_of_alphas_for_5_level)
            lSellPredictedValueFor5Levels = l_sell_SOP_of_alphas_for_5_level / (
                1 + l_sell_SOP_of_alphas_for_5_level)

            lBuyPredictedValueFor6Levels = l_buy_SOP_of_alphas_for_6_level / (
                1 + l_buy_SOP_of_alphas_for_6_level)
            lSellPredictedValueFor6Levels = l_sell_SOP_of_alphas_for_6_level / (
                1 + l_sell_SOP_of_alphas_for_6_level)

            if lCurrentDataRowTimeStamp != lFeatureATimeStamp or lFeatureATimeStamp != lFeatureBTimeStamp:
                print('Time stamp of data row with predicted value is not matching .\n Data row time stamp :- ' , lCurrentDataRowTimeStamp,'BuyPredicted Time Stamp :- ' , lFeatureATimeStamp\
                      ,"SellPredicted Time Stamp :- ",lFeatureBTimeStamp)
                os._exit(-1)
            else:
                lObj = Tick(lCurrentDataRowTimeStamp, lAskP, lBidP, lAskQ,
                            lBidQ, lAskP1, lBidP1, lAskQ1, lBidQ1, lLTP, lTTQ,
                            lBuyPredictedValueFor5Levels,
                            lSellPredictedValueFor5Levels,
                            lBuyPredictedValueFor6Levels,
                            lSellPredictedValueFor6Levels, lFeatureA,
                            lFeatureB, lFeatureC, lFeatureD, lFeatureE,
                            lFeatureA6, lFeatureB6, lFeatureC6, lFeatureD6,
                            lFeatureE6)
                if lPrevObj != None:
                    lPrevObj.TTQChange = lObj.TTQ - lPrevObj.TTQ
                    lPrevObj.NextLTP = lObj.LTP
                    if (lPrevObj.AskP - lPrevObj.BidP >
                            gTickSize) and (lPrevObj.TTQChange == 0):
                        if lPrevObj.BidP not in lListOfBidP:
                            lListOfBidP.append(lPrevObj.BidP)
                        if lPrevObj.AskP not in lListOfAskP:
                            lListOfAskP.append(lPrevObj.AskP)
                        pass
                    else:
                        if lPrevObj.currentBuyPredictedValue1 >= lMinOfExitCl or lPrevObj.currentSellPredictedValue1 >= lMinOfExitCl:
                            if len(lListOfBidP) > 1:
                                lPrevObj.bidPChangedInBetweenLastTickAndCurrentTick = 1
                            if len(lListOfAskP) > 1:
                                lPrevObj.askPChangedInBetweenLastTickAndCurrentTick = 1
                            lObjectList.append(lPrevObj)
                            lListOfBidP = [lPrevObj.BidP]
                            lListOfAskP = [lPrevObj.AskP]
                        else:
                            if lPrevObj.BidP not in lListOfBidP:
                                lListOfBidP.append(lPrevObj.BidP)
                            if lPrevObj.AskP not in lListOfAskP:
                                lListOfAskP.append(lPrevObj.AskP)
                lPrevObj = lObj
            if lCurrentDataRowCount % 50000 == 0:
                print("Completed reading ", lCurrentDataRowCount)
            lCurrentDataRowCount = lCurrentDataRowCount + 1
    return lObjectList
Exemple #27
0
def readOnceAndWrite(pFileName, entryCL , exitCL , predictedValuesDict):
    global g_bestqty_list_for_sell, g_bestqty_list_for_buy 
    global transactionCost , currencyDivisor
    attribute.initList()
    tradeStats = dict()
    tradeStats['totalSellAmountShort'] = 0
    tradeStats['totalBuyAmountShort'] = 0
    tradeStats['totalSellAmountLong'] = 0
    tradeStats['totalBuyAmountLong'] = 0
    tradeStats['NumberOfOpenBuy'] = 0
    tradeStats['NumberOfCloseSell'] = 0
    tradeStats['NumberOfOpenSell'] = 0
    tradeStats['NumberOfCloseBuy'] = 0
    tradeStats['currentPositionShort'] = 0
    tradeStats['currentPositionLong'] = 0
    noPredictionForThisRow = 0
    currentSellPredictedValue = 0
    currentBuyPredictedValue = 0
    currentIndex = 0
    l_previous_obj = None
    l_obj= None
    print("Processing the data file for trades :")
    attribute.initList()
    print("EntryLevels",entryCL , exitCL)
    for currentDataRow in dataFile.matrix:
        
        l_obj = update_obj_list(currentDataRow)
        if(l_previous_obj != None):
            lReturnList = checkIfPreviousDecisionToEnterOrExitTradeWasSuccessful( l_obj , l_previous_obj , tradeStats )
            
            lDummyBidQ0 = lReturnList[0]
            lDummyAskQ0 = lReturnList[1]
            lDummyTTQForBuy = lReturnList[2]
            lDummyTTQForSell = lReturnList[3]
            lBuyTradedPrice = lReturnList[4]
            lBuyTradedQty = lReturnList[5]
            lSellTradedPrice = lReturnList[6]
            lSellTradedQty = lReturnList[7]
            if currentIndex > 0:
                if(g_bestqty_list_for_buy != {}):
                    l_best_bidq = g_bestqty_list_for_buy['qty']
                    l_best_bidp = g_bestqty_list_for_buy['price']
                else:
                    l_best_bidq = 0
                    l_best_bidp = 0
                if(g_bestqty_list_for_sell != {}):
                    l_best_askq = g_bestqty_list_for_sell['qty']
                    l_best_askp = g_bestqty_list_for_sell['price'] 
                else:
                    l_best_askq = 0
                    l_best_askp = 0
                attribute.aList[currentIndex-1][0] = l_obj.currentTimeStamp
                attribute.aList[currentIndex-1][1] = tradeStats['currentPositionLong']
                attribute.aList[currentIndex-1][2] = tradeStats['currentPositionShort']
                listOfStringsToPrint = [ str(l_previous_obj.BidQ[0]) , str(l_previous_obj.BidP[0]),str(l_previous_obj.BidQ[1]) , str(l_previous_obj.BidP[1]),str(l_previous_obj.BidQ[2]) , str(l_previous_obj.BidP[2]),str(l_previous_obj.BidQ[3]) ,str(l_previous_obj.BidP[3]),\
                                         str(l_previous_obj.BidP[4]),str(l_previous_obj.BidQ[4]) , str(l_previous_obj.AskQ[0]) , str(l_previous_obj.AskP[0]) , str(l_previous_obj.AskQ[1]) , str(l_previous_obj.AskP[1]) ,str(l_previous_obj.AskQ[2]) , str(l_previous_obj.AskP[2]) ,\
                                         str(l_previous_obj.AskQ[3]) , str(l_previous_obj.AskP[3]) ,str(l_previous_obj.AskQ[4]) , str(l_previous_obj.AskP[4]) ,\
                                         str(l_previous_obj.TTQ) , str(l_previous_obj.LTP) ,\
                                        str(currentBuyPredictedValue) , str(currentSellPredictedValue) , str(l_previous_obj.OpenBuy) ,l_previous_obj.ReasonForTradingOrNotTradingOpenBuy ,str(l_previous_obj.CloseBuy) ,l_previous_obj.ReasonForTradingOrNotTradingCloseBuy , \
                                        str(l_previous_obj.OpenSell) ,l_previous_obj.ReasonForTradingOrNotTradingOpenSell ,str(l_previous_obj.CloseSell) ,l_previous_obj.ReasonForTradingOrNotTradingCloseSell  , \
                                        str(tradeStats['NumberOfOpenBuy']),\
                                        str(tradeStats['NumberOfCloseBuy']),str(tradeStats['NumberOfOpenSell']),\
                                        str(tradeStats['NumberOfCloseSell']),str(lDummyBidQ0),str(lDummyAskQ0),\
                                        str(lDummyTTQForBuy),str(lDummyTTQForSell),str(l_best_bidq),str(l_best_bidp),str(l_best_askp),\
                                        str(l_best_askq) ,str(lBuyTradedPrice), str(lBuyTradedQty), str(lSellTradedPrice),str(lSellTradedQty)]
                attribute.aList[currentIndex-1][3] =  ";".join(listOfStringsToPrint)
            currentTimeStamp = common.convertTimeStampFromStringToFloat(currentDataRow[colNumberOfData.TimeStamp])
            
            try:
                currentSellPredictedValue = float(predictedValuesDict[l_obj.currentTimeStamp]['sell']) 
                currentBuyPredictedValue  = float(predictedValuesDict[l_obj.currentTimeStamp]['buy'])
            except:
                noPredictionForThisRow += 1

            
            #Open Sell and Close Buy
            if(currentBuyPredictedValue >= exitCL and tradeStats['currentPositionShort'] > 0):
                g_bestqty_list_for_buy = {}
                l_obj.CloseBuy = -1       #For close by hitting
                

            if(currentSellPredictedValue >= entryCL and tradeStats['currentPositionLong'] == 0):
                g_bestqty_list_for_sell = {}
                l_obj.OpenSell = 1	#For open buy
                
            if(tradeStats['currentPositionShort'] > 0) and (l_obj.CloseBuy != -1) :
                l_obj.CloseBuy = -2
                
            
            
            
            #Open Buy and Close Sell

            if(currentSellPredictedValue >= exitCL and tradeStats['currentPositionLong'] > 0):
                g_bestqty_list_for_sell = {}
                l_obj.CloseSell = -1       #For close by hitting
                
            if(currentBuyPredictedValue >= entryCL and tradeStats['currentPositionShort'] == 0) :
                g_bestqty_list_for_buy = {}
                l_obj.OpenBuy = 1       #For close by hitting
                 
            if(tradeStats['currentPositionLong'] > 0) and (l_obj.CloseSell != -1)  :
                l_obj.CloseSell = -2              
                
        
        l_previous_obj = l_obj
        currentIndex = currentIndex + 1

# Squaring off if some open position there   
    if tradeStats['currentPositionLong'] > 0:
        tradeStats['NumberOfCloseSell'] += tradeStats['currentPositionLong']
        tradeStats['totalSellAmountLong'] += tradeStats['currentPositionLong'] * (l_previous_obj.BidP[0])
        tradeStats['currentPositionLong'] = 0
        l_obj.ReasonForTradingOrNotTradingCloseSell = 'CloseSell(Hitting)'
    if tradeStats['currentPositionShort'] > 0:
        tradeStats['NumberOfCloseBuy'] += tradeStats['currentPositionShort']
        tradeStats['totalBuyAmountShort'] += tradeStats['currentPositionShort'] * (l_previous_obj.AskP[0])
        tradeStats['currentPositionShort'] = 0
        l_obj.ReasonForTradingOrNotTradingCloseBuy = 'CloseBuy(Hitting)'
    
    attribute.aList[currentIndex-1][0] = currentTimeStamp
    attribute.aList[currentIndex-1][1] = tradeStats['currentPositionLong']
    attribute.aList[currentIndex-1][2] = tradeStats['currentPositionShort']
    if(g_bestqty_list_for_buy != {}):
        l_best_bidq = g_bestqty_list_for_buy['qty']
        l_best_bidp = g_bestqty_list_for_buy['price'] 
    else:
        l_best_bidq = 0
        l_best_bidp = 0
    if(g_bestqty_list_for_sell != {}):
        l_best_askq = g_bestqty_list_for_sell['qty']
        l_best_askp = g_bestqty_list_for_sell['price'] 
    else:
        l_best_askq = 0
        l_best_askp = 0
    listOfStringsToPrint = [  str(l_previous_obj.BidQ[0]) , str(l_previous_obj.BidP[0]),str(l_previous_obj.BidQ[1]) , str(l_previous_obj.BidP[1]),str(l_previous_obj.BidQ[2]) , str(l_previous_obj.BidP[2]),str(l_previous_obj.BidQ[3]) ,str(l_previous_obj.BidP[3]),\
                                        str(l_previous_obj.BidP[4]),str(l_previous_obj.BidQ[4]) , str(l_previous_obj.AskQ[0]) , str(l_previous_obj.AskP[0]) , str(l_previous_obj.AskQ[1]) , str(l_previous_obj.AskP[1]) ,str(l_previous_obj.AskQ[2]) , str(l_previous_obj.AskP[2]) ,\
                                        str(l_previous_obj.AskQ[3]) , str(l_previous_obj.AskP[3]) ,str(l_previous_obj.AskQ[4]) , str(l_previous_obj.AskP[4]) ,\
                                        str(l_previous_obj.TTQ) , str(l_previous_obj.LTP) ,\
                                        str(currentBuyPredictedValue) , str(currentSellPredictedValue) , str(l_previous_obj.OpenBuy) ,l_previous_obj.ReasonForTradingOrNotTradingOpenBuy ,str(l_previous_obj.CloseBuy) ,l_previous_obj.ReasonForTradingOrNotTradingCloseBuy , \
                                        str(l_previous_obj.OpenSell) ,l_previous_obj.ReasonForTradingOrNotTradingOpenSell ,str(l_previous_obj.CloseSell) ,l_previous_obj.ReasonForTradingOrNotTradingCloseSell  , str(tradeStats['NumberOfCloseBuy']),\
                                       str(tradeStats['NumberOfOpenBuy']),\
                                        str(tradeStats['NumberOfCloseBuy']),str(tradeStats['NumberOfOpenSell']),\
                                        str(tradeStats['NumberOfCloseSell']),str(lDummyBidQ0),str(lDummyAskQ0),\
                                        str(lDummyTTQForBuy),str(lDummyTTQForSell),str(l_best_bidq),str(l_best_bidp),str(l_best_askp), str(l_best_askq) , "0;0;0;0"]
    attribute.aList[currentIndex-1][3] =  ";".join(listOfStringsToPrint) 
    
    dirName = args.pd.replace('/ro/','/rs/')
    tradeLogMainDirName = dirName+"/t/"
    if not os.path.exists(tradeLogMainDirName):
        os.mkdir(tradeLogMainDirName)
    tradeLogSubDirectoryName =  tradeLogMainDirName + mainExperimentName+"/"
    if not os.path.exists(tradeLogSubDirectoryName):
        os.mkdir(tradeLogSubDirectoryName)
    
    fileName = tradeLogSubDirectoryName + pFileName + ".trade" 
    lHeaderColumnNamesList  = ['TimeStamp','CurrentPositionLong','CurrentPositionShort','BidQ0;BidP0;BidQ1;BidP1;BidQ2;BidP2;BidQ3;BidP3;BidQ4;BidP4','AskQ0;AskP0;AskQ1;AskP1;AskQ2;AskP2;AskQ3;AskP3;AskQ4;AskP4','TTQ','LTP','CurBuyPredValue','CurrentSellPredValue',\
                               'EnterTradeOpenBuy','ReasonForTradingOrNotTradingOpenBuy','EnterTradeCloseBuy','ReasonForTradingOrNotTradingCloseBuy','EnterTradeOpenSell','ReasonForTradingOrNotTradingOpenSell','EnterTradeCloseSell','ReasonForTradingOrNotTradingCloseSell',\
                               'NumberOfOpenBuy','NumOfCloseBuy','NumberOfOpenSell','NumberOfCloseSell','DummyBidQ0','DummyAskQ0','DummyTTQChangeForSell','DummyTTQChangeForBuy' \
                               ,'BestBidQ','BestBidP','BestAskP','BestAskQ','BuyTradedPrcie','BuyTradedQty','SellTradedPrice','SellTradedQty']
    
#     attribute.writeToFile(fileName , lHeaderColumnNamesList)

    
    tradeResultMainDirName = dirName+"/r/"
    if not os.path.exists(tradeResultMainDirName):
        os.mkdir(tradeResultMainDirName)
    tradeResultSubDirectoryName =  tradeResultMainDirName + mainExperimentName+"/"
    if not os.path.exists(tradeResultSubDirectoryName):
        os.mkdir(tradeResultSubDirectoryName)
    fileName = tradeResultSubDirectoryName+pFileName+".result" 
    outputFile = open(fileName,"w")
    gross_short_profit = tradeStats['totalSellAmountShort'] - tradeStats['totalBuyAmountShort']
    gross_long_profit = tradeStats['totalSellAmountLong'] - tradeStats['totalBuyAmountLong']
    gross_profit = gross_short_profit + gross_long_profit
    net_short_profit = gross_short_profit - ( transactionCost * ( tradeStats['totalSellAmountShort'] + tradeStats['totalBuyAmountShort'] ) ) 
    net_long_profit = gross_long_profit - ( transactionCost * (tradeStats['totalSellAmountLong'] + tradeStats['totalBuyAmountLong'] ) )
    net_profit = net_short_profit + net_long_profit
    
    gross_short_profit_in_dollars = gross_profit / (currencyDivisor * 60)
    net_profit_in_dollars = net_profit / (currencyDivisor * 60 )
    #changed file write to modify it to Short Long version
    print("Starting to write: "+pFileName)
    print("The gross results for Short are: %.6f" %gross_short_profit, file = outputFile)
    print("The gross results for Long are: %.6f" %gross_long_profit, file = outputFile)
    print("Number of rows for which there is no prediction: " + str(0), file = outputFile)    
    print("Number of times asked to enter trade Short: " + str(0), file = outputFile)    
    print("Number of times asked to enter trade Long: " + str(0), file = outputFile)    
    print("Number of times asked to exit trade Short: " + str(0), file = outputFile)
    print("Number of times asked to exit trade Long: " + str(0), file = outputFile)
    print("The net results for Short are: %.6f" %net_short_profit, file = outputFile)
    print("The net results for Long are: %.6f" %net_long_profit, file = outputFile)
    print("Gross Results in Dollars: %.6f" %gross_short_profit_in_dollars, file = outputFile)
    print("Net Results in Dollars: %.6f" %net_profit_in_dollars, file = outputFile)
    print("Number of times Close buy trade happened: " + str(tradeStats['NumberOfCloseBuy']), file = outputFile)
    print("Number of times open buy trade happened: " + str(tradeStats['NumberOfOpenBuy']), file = outputFile)
    print("Assumed open sell trade did not happen since volume did not increase: " + str(0), file = outputFile)
    print("Assumed close sell trade did not happen since volume did not increase: " + str(0), file = outputFile)
    print("Assumed open sell trade did not happen since bidP0 not same as LTP: " + str(0), file = outputFile)
    print("Assumed close sell trade did not happen since bidP0 not same as LTP: " + str(0), file = outputFile)
    print("Number of Open sell trade happened: " + str(tradeStats['NumberOfOpenSell']), file = outputFile)
    print("Number of Close sell trade happened: " + str(tradeStats['NumberOfCloseSell']), file = outputFile)
    print("The total open sell value is: " + str(tradeStats['totalSellAmountShort']), file = outputFile)
    print("The total close sell value is: " + str(tradeStats['totalSellAmountLong']), file = outputFile)
    print("The total close buy value is: " + str(tradeStats['totalBuyAmountShort']), file = outputFile)
    print("The total open buy value is: " + str(tradeStats['totalBuyAmountLong']), file = outputFile)

    try:
        averageOpenSellPrice = tradeStats['totalSellAmountShort']/tradeStats['NumberOfOpenSell'] 
        averageCloseBuyPrice = tradeStats['totalBuyAmountShort']/tradeStats['NumberOfCloseBuy'] 
    except:
        averageOpenSellPrice = 0 
        averageCloseBuyPrice = 0
    try:
        averageCloseSellPrice = tradeStats['totalSellAmountLong']/tradeStats['NumberOfCloseSell'] 
        averageOpenBuyPrice = tradeStats['totalBuyAmountLong']/tradeStats['NumberOfOpenBuy'] 
    except:
        averageCloseSellPrice = 0
        averageOpenBuyPrice = 0 
    
    print("Average open sell price per unit is: " + str(averageOpenSellPrice), file = outputFile)
    print("Average close sell price per unit is: " + str(averageCloseSellPrice), file = outputFile)
    print("Average open buy price per unit is: " + str(averageOpenBuyPrice), file = outputFile)
    print("Average close buy price per unit is: " + str(averageCloseBuyPrice), file = outputFile)
    print("The current position Short: " + str(tradeStats['currentPositionShort']), file = outputFile)
    print("The current position Long: " + str(tradeStats['currentPositionLong']), file = outputFile)
    print("Profit or loss per Qty traded Short is: " + str(averageOpenSellPrice - averageCloseBuyPrice), file = outputFile)
    print("Profit or loss per Qty traded Long is: " + str(averageCloseSellPrice - averageOpenBuyPrice), file = outputFile)
    pLPerLotShort=(averageOpenSellPrice - averageCloseBuyPrice)* 1000
    pLPerLotLong=(averageCloseSellPrice - averageOpenBuyPrice)* 1000
    print("1 lot has 1000 qty's so P/L Short per lot is: " + str(pLPerLotShort), file = outputFile)
    print("1 lot has 1000 qty's so P/L Long per lot is: " + str(pLPerLotLong), file = outputFile)
    print("P/L for Short trading 10 lots is: " + str(pLPerLotShort * 10), file = outputFile)
    print("P/L for Long trading 10 lots is: " + str(pLPerLotLong * 10), file = outputFile)
def extractAttributeFromDataMatrix(args):
    if args.n == None:
        N = 5
    else:
        N = int(args.n)
    try:
        #Value of argument c can be taken as BidP0 and AskP0
        args.c
    except:
        print "Since -c has not been specified I cannot proceed"
        os._exit()
    if args.m == None:
        M = 1
    else:
        M = int(args.m.split(".")[0])
        M1 = int(args.m.split(".")[1])
        print "Values of N and N1 are ", M, M1

    PIP_SIZE = 5

    colNumberOfAttribute = eval("colNumberOfData." + args.c)
    colNumberOfOppositeAttribute = eval(
        "colNumberOfData.BestBidP") if "ask" in args.c else eval(
            "colNumberOfData.BestAskP")
    colNumberOfTimeStamp = colNumberOfData.TimeStamp
    if "ask" in args.c.lower():
        colAttributeRowNo = [
            colNumberOfData.BestAskP1, colNumberOfData.BestAskP2
        ]  #, colNumberOfData.BestAskP2, colNumberOfData.BestAskP3, colNumberOfData.BestAskP4]
        colAttributeRowNoBand = [colNumberOfData.AskP1, colNumberOfData.AskP2]
    else:
        colAttributeRowNo = [
            colNumberOfData.BestBidP1, colNumberOfData.BestBidP2
        ]  #, colNumberOfData.BestBidP2, colNumberOfData.BestBidP3, colNumberOfData.BestBidP4]
        colAttributeRowNoBand = [colNumberOfData.BidP1, colNumberOfData.BidP2]
    numberOfRowsInLastNSecs = 0
    queueOfValuesInLastNQty = []
    lenQ = 0
    totalOfQty = 0.0
    currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
    lengthOfDataMatrix = len(dataFile.matrix)
    while (currentRowNumberForWhichFeatureValueIsBeingCalculated <
           lengthOfDataMatrix):
        totalOfRowsInLastNQty = 0.0
        totalOfQtyInLastNQty = 0.0
        cellValueTotal = 0
        colAttributeRow = map(float, [
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][i]
            for i in colAttributeRowNo
        ])
        colAttributeRowBand = map(float, [
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][i]
            for i in colAttributeRowNoBand
        ])
        timeOfCurrentRow = common.convertTimeStampFromStringToFloat(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated]
            [colNumberOfTimeStamp], args.cType)
        oppositeAttribute = float(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated]
            [colNumberOfOppositeAttribute])
        if currentRowNumberForWhichFeatureValueIsBeingCalculated != 0:
            previousRowPrice = float(dataFile.matrix[
                currentRowNumberForWhichFeatureValueIsBeingCalculated -
                1][colNumberOfAttribute])
        else:
            previousRowPrice = 0
        currentPrice = float(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated]
            [colNumberOfAttribute])
        currentLTP = float(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                colNumberOfData.LTP])
        currentMsgCode = dataFile.matrix[
            currentRowNumberForWhichFeatureValueIsBeingCalculated][
                colNumberOfData.MsgCode]
        newQty = int(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                colNumberOfData.NewQ])
        newPrice = int(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                colNumberOfData.NewP])
        oldQty = int(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                colNumberOfData.OldQ])
        oldPrice = int(
            dataFile.
            matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][
                colNumberOfData.OldP])
        if (currentMsgCode == 'M' and (newPrice in colAttributeRow)
                and ((newPrice > oldPrice) if "bid" in args.c.lower() else
                     (newPrice < oldPrice))):
            cellValueTotal = newPrice * newQty
            totalOfQty += newQty
            cellValueQty = newQty
            queueOfValuesInLastNQty.append(
                [cellValueTotal, cellValueQty, colAttributeRowBand[0]])
            lenQ += 1
        elif (currentMsgCode == 'N' and (newPrice in colAttributeRow)):
            cellValueTotal = newPrice * newQty
            totalOfQty += newQty
            cellValueQty = newQty
            queueOfValuesInLastNQty.append(
                [cellValueTotal, cellValueQty, colAttributeRowBand[0]])
            lenQ += 1

        if totalOfQty > N:
            while totalOfQty > N:
                excess = totalOfQty - N
                if queueOfValuesInLastNQty[0][1] < excess:
                    totalOfQty -= queueOfValuesInLastNQty[0][1]
                    queueOfValuesInLastNQty = queueOfValuesInLastNQty[1:]
                    lenQ -= 1
                else:
                    totalOfQty -= excess
                    price = float(queueOfValuesInLastNQty[0]
                                  [0]) / queueOfValuesInLastNQty[0][1]
                    queueOfValuesInLastNQty[0][0] = price * (
                        queueOfValuesInLastNQty[0][1] - excess)
                    queueOfValuesInLastNQty[0][1] -= excess

        cell = lenQ - 1
        if cell >= 0:
            while ((queueOfValuesInLastNQty[cell][2] > colAttributeRowBand[0] -
                    M * PIP_SIZE and colAttributeRowBand[0] >
                    queueOfValuesInLastNQty[cell][2] - M1 * PIP_SIZE) and
                   ("bid" in args.c.lower())) or (
                       (queueOfValuesInLastNQty[cell][2] <
                        colAttributeRowBand[0] + M * PIP_SIZE
                        and colAttributeRowBand[0] <
                        queueOfValuesInLastNQty[cell][2] + M1 * PIP_SIZE) and
                       ("ask" in args.c.lower())):
                totalOfRowsInLastNQty += queueOfValuesInLastNQty[cell][0]
                totalOfQtyInLastNQty += queueOfValuesInLastNQty[cell][1]
                cell -= 1
                if cell < 0:
                    break

        exPrice = colAttributeRowBand[
            0] - M * PIP_SIZE if "bid" in args.c.lower(
            ) else colAttributeRowBand[0] + M * PIP_SIZE
        exQty = N - totalOfQtyInLastNQty
        wtdAvg = float(totalOfRowsInLastNQty + exQty * exPrice) / N

        attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][
            0] = common.convertTimeStampFromStringToDecimal(
                dataFile.
                matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated]
                [colNumberOfTimeStamp], args.cType)
        attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][
            1] = wtdAvg  # in 1st iteration currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
        attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][
            2] = str(currentPrice) + ";" + str(
                colAttributeRowBand[0]) + ";" + str(
                    cellValueTotal) + ";" + str(currentMsgCode) + ";" + str(
                        totalOfQtyInLastNQty) + ";" + str(
                            totalOfRowsInLastNQty)
        currentRowNumberForWhichFeatureValueIsBeingCalculated += 1
        continue  # Since we are going back 1 row from current we cannot get data from current row
        print "Processed row number " + str(
            currentRowNumberForWhichFeatureValueIsBeingCalculated)

    lNameOfFeaturePrinted = "fMarketMNTSumOfCol" + args.c + "InLast" + str(
        args.n) + "Qty" + "WithDiff" + str(args.m) + "Pip"
    return [
        "TimeStamp", lNameOfFeaturePrinted, args.c, "LTP",
        "TradedQtyWhichIs" + args.c, "MsgCode", "totalQty", "totalCellValue"
    ]
Exemple #29
0
def main():
   attribute.initList()
   dataFile.getDataIntoMatrix(args.pd)
   predictedValuesDict = dict()
   getPredictedValuesIntoDict(predictedValuesDict)
   enterTradeShort = 0
   enterTradeLong = 0
   ttqAtTimeOfPreviousDataRow = 0
   askP0AtTimeOfPreviousDataRow = 0
   bidP0AtTimeOfPreviousDataRow = 0
   askQ0AtTimeOfPreviousDataRow = 0
   bidQ0AtTimeOfPreviousDataRow = 0
   tradeStats = dict()
   tradeStats['totalSellValueShort'] = 0
   tradeStats['totalBuyValueShort'] = 0
   tradeStats['currentPositionShort'] = 0
   tradeStats['totalSellValueLong'] = 0
   tradeStats['totalBuyValueLong'] = 0
   tradeStats['currentPositionLong'] = 0
   noPredictionForThisRow = 0
   currentPredictedValueShort = 0
   currentPredictedValueLong = 0
   entryCL = float(args.entryCL)/100
   exitCL = float(args.exitCL)/100
 #  entryCLCutoff = float(args.entryCLCutoff)
 #  exitCLCutoff = float(args.exitCLCutoff)
   numberOfTimesAskedToEnterTradeShort = 0
   numberOfTimesAskedToEnterTradeLong = 0
   numberOfTimesAskedToExitTradeShort = 0
   numberOfTimesAskedToExitTradeLong = 0
   reasonForTrade = dict()
   reasonForTrade['LTPDoesNotEqualBidP0Short'] = 0
   reasonForTrade['LTPDoesNotEqualAskP0Short'] = 0
   reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptShort'] = 0
   reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptLong'] = 0
   reasonForTrade['AssumingBuyTradeHappenedShort'] = 0
   reasonForTrade['AssumingBuyTradeHappenedLong'] = 0
   reasonForTrade['LTPDoesNotEqualAskP0Long'] = 0
   reasonForTrade['LTPDoesNotEqualBidP0Long'] = 0
   reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptShort'] = 0
   reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptLong'] = 0
   reasonForTrade['AssumingSellTradeHappenedShort'] = 0
   reasonForTrade['AssumingSellTradeHappenedLong'] = 0
   currentIndex = 0
   print("Processing the data file for trades :")
   attribute.initList()
   for currentDataRow in dataFile.matrix:
       
       lReturnList = checkIfPreviousDecisionToEnterOrExitTradeWasSuccessful(currentDataRow,ttqAtTimeOfPreviousDataRow,askP0AtTimeOfPreviousDataRow,bidP0AtTimeOfPreviousDataRow,askQ0AtTimeOfPreviousDataRow , bidQ0AtTimeOfPreviousDataRow , enterTradeShort,enterTradeLong,tradeStats,reasonForTrade )

       lReasonForTradingOrNotTradingShort = lReturnList[0]
       lReasonForTradingOrNotTradingLong = lReturnList[1] 
       lDummyBidQ0 = lReturnList[2]
       lDummyAskQ0 = lReturnList[3]
       lDummyTTQForBuy = lReturnList[4]
       lDummyTTQForSell = lReturnList[5]
       if currentIndex > 0:
           attribute.aList[currentIndex-1][0] = currentTimeStamp
           attribute.aList[currentIndex-1][1] = tradeStats['currentPositionLong']
           attribute.aList[currentIndex-1][2] = tradeStats['currentPositionShort']
           listOfStringsToPrint = [ str(bidQ0AtTimeOfPreviousDataRow) , str(bidP0AtTimeOfPreviousDataRow) , str(askP0AtTimeOfPreviousDataRow) , str(askQ0AtTimeOfPreviousDataRow) , str(ttqAtTimeOfPreviousDataRow) , str(ltpAtTimeOfPreviousDataRow) , str(currentPredictedValueShort) , str(enterTradeShort) ,lReasonForTradingOrNotTradingShort , str(currentPredictedValueLong) , str(enterTradeLong) ,lReasonForTradingOrNotTradingLong , str(reasonForTrade['AssumingBuyTradeHappenedShort']),str(reasonForTrade['AssumingBuyTradeHappenedLong']),str(reasonForTrade['AssumingSellTradeHappenedShort']),str(reasonForTrade['AssumingSellTradeHappenedLong']),str(lDummyBidQ0),str(lDummyAskQ0),str(lDummyTTQForBuy),str(lDummyTTQForSell)]
           attribute.aList[currentIndex-1][3] =  ";".join(listOfStringsToPrint)
       currentTimeStamp = common.convertTimeStampFromStringToFloat(currentDataRow[colNumberOfData.TimeStamp])

       try:
           currentPredictedValueShort = (float(predictedValuesDict[currentTimeStamp][-2]) + float(predictedValuesDict[currentTimeStamp][-1])) 
           currentPredictedValueLong = (float(predictedValuesDict[currentTimeStamp][2]) + float(predictedValuesDict[currentTimeStamp][1])) 
       except:
           noPredictionForThisRow += 1

       #short decisions
       if(currentPredictedValueShort > entryCL):
           enterTradeShort = 1
           numberOfTimesAskedToEnterTradeShort += 1
       elif(currentPredictedValueShort < exitCL and tradeStats['currentPositionShort'] > 0):
           numberOfTimesAskedToExitTradeShort += 1
           enterTradeShort = -1  # Implies to exit the trade
       else:
           enterTradeShort = 0  # Implies make no change
           
#       if enterTradeShort == 1:
#           if currentPredictedValue <= entryCLCutoff:
#               enterTrade = 0
#       if enterTrade == -1:
#           if currentPredictedValue >= exitCLCutoff:
#               enterTrade = 0
               
       #long decisions
       if(currentPredictedValueLong > entryCL):
           enterTradeLong = 1
           numberOfTimesAskedToEnterTradeLong += 1
       elif(currentPredictedValueLong < exitCL and tradeStats['currentPositionLong'] > 0):
           numberOfTimesAskedToExitTradeLong += 1
           enterTradeLong = -1  # Implies to exit the trade
       else:
           enterTradeLong = 0  # Implies make no change
       
       ttqAtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.TTQ]) 
       askP0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.AskP0])
       bidP0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.BidP0])
       askQ0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.AskQ0])
       bidQ0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.BidQ0])
       ltpAtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.LTP])
       currentIndex = currentIndex + 1

# Squaring off if some open position there   
   if tradeStats['currentPositionLong'] > 0:
       reasonForTrade['AssumingSellTradeHappenedLong'] += tradeStats['currentPositionLong']
       tradeStats['totalSellValueLong'] += tradeStats['currentPositionLong'] * (bidP0AtTimeOfPreviousDataRow)
       tradeStats['currentPositionLong'] = 0
       lReasonForTradingOrNotTradingLong = 'CloseSell(Hitting)'
   elif tradeStats['currentPositionShort'] > 0:
       reasonForTrade['AssumingBuyTradeHappenedShort'] += tradeStats['currentPositionShort']
       tradeStats['totalBuyValueShort'] += tradeStats['currentPositionShort'] * (askP0AtTimeOfPreviousDataRow)
       tradeStats['currentPositionShort'] = 0
       lReasonForTradingOrNotTradingLong = 'CloseBuy(Hitting)'

   attribute.aList[currentIndex-1][0] = currentTimeStamp
   attribute.aList[currentIndex-1][1] = tradeStats['currentPositionLong']
   attribute.aList[currentIndex-1][2] = tradeStats['currentPositionShort']
   listOfStringsToPrint = [ str(bidQ0AtTimeOfPreviousDataRow) , str(bidP0AtTimeOfPreviousDataRow) , str(askP0AtTimeOfPreviousDataRow) , str(askQ0AtTimeOfPreviousDataRow) , str(ttqAtTimeOfPreviousDataRow) , str(ltpAtTimeOfPreviousDataRow) , str(currentPredictedValueShort) , str(enterTradeShort) , "" , str(currentPredictedValueLong) , str(enterTradeLong) ,"" , str(reasonForTrade['AssumingBuyTradeHappenedShort']),str(reasonForTrade['AssumingBuyTradeHappenedLong']),str(reasonForTrade['AssumingSellTradeHappenedShort']),str(reasonForTrade['AssumingSellTradeHappenedLong']),str(lDummyBidQ0),str(lDummyAskQ0),str(lDummyTTQForBuy),str(lDummyTTQForSell)]
   attribute.aList[currentIndex-1][3] =  ";".join(listOfStringsToPrint) 
   dirName = args.pd.replace('/ro/','/rs/')
   tradeLogMainDirName = dirName+"/t/"
   if not os.path.exists(tradeLogMainDirName):
        os.mkdir(tradeLogMainDirName)
   tradeLogSubDirectoryName =  tradeLogMainDirName + mainExperimentName+"/"
   if not os.path.exists(tradeLogSubDirectoryName):
        os.mkdir(tradeLogSubDirectoryName)
   fileName = tradeLogSubDirectoryName+experimentName+initialFileName+".trade" 
   lHeaderColumnNamesList  = ['TimeStamp','CurrentPositionLong','CurrentPositionShort','BidQ0','BidP0','AskP0','AskQ0','TTQ','LTP','CurPredValueShort','EnterTradeShort','ReasonForTradingOrNotTradingShort','CurPredValueLong','EnterTradeLong','ReasonForTradingOrNotTradingLong','totalBuyTradeShort','totalBuyLong','totalSellShort','totalSellLong','DummyBidQ0','DummyAskQ0','DummyTTQChangeForSell','DummyTTQChangeForBuy']
   attribute.writeToFile(fileName , lHeaderColumnNamesList)

   tradeResultMainDirName = dirName+"/r/"
   if not os.path.exists(tradeResultMainDirName):
        os.mkdir(tradeResultMainDirName)
   tradeResultSubDirectoryName =  tradeResultMainDirName + mainExperimentName+"/"
   if not os.path.exists(tradeResultSubDirectoryName):
        os.mkdir(tradeResultSubDirectoryName)
   fileName = tradeResultSubDirectoryName+experimentName+initialFileName+".result" 
   outputFile = open(fileName,"w")
 
   #changed file write to modify it to Short Long version
   print("Starting to write: "+fileName)
   print("The net results for Short are: " + str(tradeStats['totalSellValueShort'] - tradeStats['totalBuyValueShort']), file = outputFile)
   print("The net results for Long are: " + str(tradeStats['totalSellValueLong'] - tradeStats['totalBuyValueLong']), file = outputFile)
   print("Number of rows for which there is no prediction: " + str(noPredictionForThisRow), file = outputFile)    
   print("Number of times asked to enter trade Short: " + str(numberOfTimesAskedToEnterTradeShort), file = outputFile)    
   print("Number of times asked to enter trade Long: " + str(numberOfTimesAskedToEnterTradeLong), file = outputFile)    
   print("Number of times asked to exit trade Short: " + str(numberOfTimesAskedToExitTradeShort), file = outputFile)
   print("Number of times asked to exit trade Long: " + str(numberOfTimesAskedToExitTradeLong), file = outputFile)
   print("Assumed close buy trade did not happen since volume did not increase: " + str(reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptShort']), file = outputFile)
   print("Assumed open buy trade did not happen since volume did not increase: " + str(reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptLong']), file = outputFile)
   print("Assumed close buy trade did not happen since bidP0 not same as LTP: " + str(reasonForTrade['LTPDoesNotEqualBidP0Short']), file = outputFile)
   print("Assumed open buy trade did not happen since bidP0 not same as LTP: " + str(reasonForTrade['LTPDoesNotEqualBidP0Long']), file = outputFile)
   print("Assumed close buy trade happened: " + str(reasonForTrade['AssumingBuyTradeHappenedShort']), file = outputFile)
   print("Assumed open buy trade happened: " + str(reasonForTrade['AssumingBuyTradeHappenedLong']), file = outputFile)
   print("Assumed open sell trade did not happen since volume did not increase: " + str(reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptShort']), file = outputFile)
   print("Assumed close sell trade did not happen since volume did not increase: " + str(reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptLong']), file = outputFile)
   print("Assumed open sell trade did not happen since bidP0 not same as LTP: " + str(reasonForTrade['LTPDoesNotEqualAskP0Short']), file = outputFile)
   print("Assumed close sell trade did not happen since bidP0 not same as LTP: " + str(reasonForTrade['LTPDoesNotEqualAskP0Long']), file = outputFile)
   print("Assumed open sell trade happened: " + str(reasonForTrade['AssumingSellTradeHappenedShort']), file = outputFile)
   print("Assumed close sell trade happened: " + str(reasonForTrade['AssumingSellTradeHappenedLong']), file = outputFile)
   print("The total open sell value is: " + str(tradeStats['totalSellValueShort']), file = outputFile)
   print("The total close sell value is: " + str(tradeStats['totalSellValueLong']), file = outputFile)
   print("The total close buy value is: " + str(tradeStats['totalBuyValueShort']), file = outputFile)
   print("The total open buy value is: " + str(tradeStats['totalBuyValueLong']), file = outputFile)

   try:
       averageOpenSellPrice = tradeStats['totalSellValueShort']/reasonForTrade['AssumingSellTradeHappenedShort']
       averageCloseBuyPrice = tradeStats['totalBuyValueShort']/reasonForTrade['AssumingBuyTradeHappenedShort']
   except:
       averageOpenSellPrice = 0 
       averageCloseBuyPrice = 0
   try:
       averageCloseSellPrice = tradeStats['totalSellValueLong']/reasonForTrade['AssumingSellTradeHappenedLong']
       averageOpenBuyPrice = tradeStats['totalBuyValueLong']/reasonForTrade['AssumingBuyTradeHappenedLong']
   except:
       averageCloseSellPrice = 0
       averageOpenBuyPrice = 0 

   print("Average open sell price per unit is: " + str(averageOpenSellPrice), file = outputFile)
   print("Average close sell price per unit is: " + str(averageCloseSellPrice), file = outputFile)
   print("Average open buy price per unit is: " + str(averageOpenBuyPrice), file = outputFile)
   print("Average close buy price per unit is: " + str(averageCloseBuyPrice), file = outputFile)
   print("The current position Short: " + str(tradeStats['currentPositionShort']), file = outputFile)
   print("The current position Long: " + str(tradeStats['currentPositionLong']), file = outputFile)
   print("Profit or loss per Qty traded Short is: " + str(averageOpenSellPrice - averageCloseBuyPrice), file = outputFile)
   print("Profit or loss per Qty traded Long is: " + str(averageCloseSellPrice - averageOpenBuyPrice), file = outputFile)
   pLPerLotShort=(averageOpenSellPrice - averageCloseBuyPrice)* 1000
   pLPerLotLong=(averageCloseSellPrice - averageOpenBuyPrice)* 1000
   print("1 lot has 1000 qty's so P/L Short per lot is: " + str(pLPerLotShort), file = outputFile)
   print("1 lot has 1000 qty's so P/L Long per lot is: " + str(pLPerLotLong), file = outputFile)
   print("P/L for Short trading 10 lots is: " + str(pLPerLotShort * 10), file = outputFile)
   print("P/L for Long trading 10 lots is: " + str(pLPerLotLong * 10), file = outputFile)
Exemple #30
0
def main():
   dataFile.getDataIntoMatrix(args.pd)
   predictedValuesDict = dict()
   getPredictedValuesIntoDict(predictedValuesDict)
   enterTrade = 0
   ttqAtTimeOfPreviousDataRow = 0
   askP0AtTimeOfPreviousDataRow = 0
   bidP0AtTimeOfPreviousDataRow = 0
   tradeStats = dict()
   tradeStats['totalSellValue'] = 0
   tradeStats['totalBuyValue'] = 0
   tradeStats['currentPosition'] = 0
   noPredictionForThisRow = 0
   currentPredictedValue = 0
   entryCL = float(args.entryCL)/100
   exitCL = float(args.exitCL)/100
   numberOfTimesAskedToEnterTrade = 0
   numberOfTimesAskedToExitTrade = 0
   reasonForTrade = dict()
   reasonForTrade['LTPDoesNotEqualBidP0'] = 0
   reasonForTrade['VolumeDidNotIncreaseDuringBuyAttempt'] = 0
   reasonForTrade['AssumingBuyTradeHappened'] = 0
   reasonForTrade['LTPDoesNotEqualAskP0'] = 0
   reasonForTrade['VolumeDidNotIncreaseDuringSellAttempt'] = 0
   reasonForTrade['AssumingSellTradeHappened'] = 0


   print("Processing the data file for trades :")

   for currentDataRow in dataFile.matrix:
       checkIfPreviousDecisionToEnterOrExitTradeWasSuccessful(currentDataRow,ttqAtTimeOfPreviousDataRow,askP0AtTimeOfPreviousDataRow,bidP0AtTimeOfPreviousDataRow,enterTrade,tradeStats,reasonForTrade)
       currentTimeStamp = common.convertTimeStampFromStringToFloat(currentDataRow[colNumberOfData.TimeStamp])

       try:
           currentPredictedValue = float(predictedValuesDict[currentTimeStamp])
       except:
           noPredictionForThisRow += 1

       if(currentPredictedValue > entryCL):
           enterTrade = 1
           numberOfTimesAskedToEnterTrade += 1
       elif(currentPredictedValue < exitCL and tradeStats['currentPosition'] > 0):
           numberOfTimesAskedToExitTrade += 1
           enterTrade = -1  # Implies to exit the trade
       else:
           enterTrade = 0  # Implies make no change

       ttqAtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.TTQ]) 
       askP0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.AskP0])
       bidP0AtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.BidP0])

   dirName = args.pd.replace('/ro/','/rs/')  
   tradeResultMainDirName = dirName+"/r/"
   if not os.path.exists(tradeResultMainDirName):
        os.mkdir(tradeResultMainDirName)
   tradeResultSubDirectoryName =  tradeResultMainDirName + mainExperimentName+"/"
   if not os.path.exists(tradeResultSubDirectoryName):
        os.mkdir(tradeResultSubDirectoryName)
   fileName = tradeResultSubDirectoryName+experimentName+initialFileName+".result" 
   outputFile = open(fileName,"w")

   print("Starting to write: "+fileName)
   print("The net results are: " + str(tradeStats['totalSellValue'] - tradeStats['totalBuyValue']), file = outputFile)    
   print("Number of rows for which there is no prediction: " + str(noPredictionForThisRow), file = outputFile)    
   print("Number of times asked to enter trade: " + str(numberOfTimesAskedToEnterTrade), file = outputFile)    
   print("Number of times asked to exit trade: " + str(numberOfTimesAskedToExitTrade), file = outputFile)    
   print("Assumed buy trade did not happen since volume did not increase: " + str(reasonForTrade['VolumeDidNotIncreaseDuringBuyAttempt']), file = outputFile)
   print("Assumed buy trade did not happen since bidP0 not same as LTP: " + str(reasonForTrade['LTPDoesNotEqualBidP0']), file = outputFile)
   print("Assumed buy trade happened: " + str(reasonForTrade['AssumingBuyTradeHappened']), file = outputFile)
   print("Assumed sell trade did not happen since volume did not increase: " + str(reasonForTrade['VolumeDidNotIncreaseDuringSellAttempt']), file = outputFile)
   print("Assumed sell trade did not happen since bidP0 not same as LTP: " + str(reasonForTrade['LTPDoesNotEqualAskP0']), file = outputFile)
   print("Assumed sell trade happened: " + str(reasonForTrade['AssumingSellTradeHappened']), file = outputFile)
   print("The total sell value is: " + str(tradeStats['totalSellValue']), file = outputFile)
   print("The total buy value is: " + str(tradeStats['totalBuyValue']), file = outputFile)
   try:
       averageSellPrice = tradeStats['totalSellValue']/reasonForTrade['AssumingSellTradeHappened']
       averageBuyPrice = tradeStats['totalBuyValue']/reasonForTrade['AssumingBuyTradeHappened']
   except:
       averageSellPrice = 0 
       averageBuyPrice = 0
   print("Average sell price per unit is: " + str(averageSellPrice), file = outputFile)
   print("Average buy price per unit is: " + str(averageBuyPrice), file = outputFile)
   print("The current position: " + str(tradeStats['currentPosition']), file = outputFile)
   print("Profit or loss per Qty traded is: " + str(averageSellPrice - averageBuyPrice), file = outputFile)
   pLPerLot=(averageSellPrice - averageBuyPrice)* 1000
   print("1 lot has 1000 qty's so P/L per lot is: " + str(pLPerLot), file = outputFile)
   print("P/L for trading 10 lots is: " + str(pLPerLot * 10), file = outputFile)
def extractAttributeFromDataMatrix(args):
    if args.n == None:
        N = 5
    else:
        N = int(args.n) 
    try:
        args.c
    except:
        print "Since -c has not been specified I cannot proceed"
        os._exit()
    if(args.cType == "synthetic"):
        colNumberOfAttribute = colNumberOfData.SysFeature
    else:
        colNumberOfAttribute = eval("colNumberOfData."+ args.c )
    
    colNumberOfTimeStamp = colNumberOfData.TimeStamp
    colNumberOfExchangeStamp = colNumberOfData.ExchangeTS
    numberOfRowsInLastNSecs = 0
    queueOfValuesInLastNSecs = deque()
    totalOfRowsInLastNSecs = 0.0
    totalOfSquareOfRowsInLastNSecs = 0.0
    timeOfOldestRow = common.convertTimeStampFromStringToFloat(dataFile.matrix[0][colNumberOfExchangeStamp],"synthetic")
    currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
    lengthOfDataMatrix = len(dataFile.matrix)
    while (currentRowNumberForWhichFeatureValueIsBeingCalculated < lengthOfDataMatrix):
        timeOfCurrentRow = common.convertTimeStampFromStringToFloat(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfExchangeStamp],"synthetic")
        timeElapsed = timeOfCurrentRow - timeOfOldestRow
        if (timeElapsed < N):
            cellValue = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfAttribute])
            totalOfRowsInLastNSecs += cellValue
            totalOfSquareOfRowsInLastNSecs += ( cellValue * cellValue )
            
            meanOfRows = totalOfRowsInLastNSecs/(numberOfRowsInLastNSecs+1) 
            
            meanOfSquareOfRows = totalOfSquareOfRowsInLastNSecs/( numberOfRowsInLastNSecs+1 )
            
            variance = meanOfSquareOfRows - ( meanOfRows * meanOfRows )

            if variance < 0:
                variance = 0
                totalOfSquareOfRowsInLastNSecs = ( meanOfRows * meanOfRows ) * ( numberOfRowsInLastNSecs+1 )
                
            attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][0] = common.convertTimeStampFromStringToDecimal(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfTimeStamp])
            attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][1] = variance # in 1st iteration currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
            attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][2] = str(timeElapsed)
            attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][3] = str(totalOfRowsInLastNSecs)+ ";"  + str(totalOfSquareOfRowsInLastNSecs) + ";" +  str(numberOfRowsInLastNSecs+1) 
            queueOfValuesInLastNSecs.append([cellValue,timeOfCurrentRow])
            numberOfRowsInLastNSecs += 1   # Every append gets a +1 
            currentRowNumberForWhichFeatureValueIsBeingCalculated += 1
            continue     # Since we are going back 1 row from current we cannot get data from current row
        else:
            # We need to reset the timeOfOldestRow since timeElapsed has exceeded N seconds
            while(timeElapsed >= N):
                if(len(queueOfValuesInLastNSecs) == 0):
                    timeOfOldestRow = timeOfCurrentRow
                    timeElapsed = 0
                    if(numberOfRowsInLastNSecs != 0):
                        print "Sanity check: This condition is not possible logically. numberOfRowsInLastNSecs should been 0. There has been an unknown error"
                        sys.exit(-1)
                    if(totalOfRowsInLastNSecs != 0):
                        print "Sanity check: This condition is not possible logically. totalOfRowsInLastNSecs should have been 0. There has been an unknown error"
                        sys.exit(-1)   
                else:   
                    oldestElementInQueue = queueOfValuesInLastNSecs.popleft()
                    colValueInOldestElementInQueue = oldestElementInQueue[0]
                    if len(queueOfValuesInLastNSecs) == 0:
                        timeElapsed = 0
                        timeOfOldestRow = timeOfCurrentRow
                    else:
                        timeOfOldestRow = queueOfValuesInLastNSecs[0][1]
                    totalOfRowsInLastNSecs -= colValueInOldestElementInQueue
                    totalOfSquareOfRowsInLastNSecs -= ( colValueInOldestElementInQueue * colValueInOldestElementInQueue )
                    numberOfRowsInLastNSecs -= 1 # every pop from the queue gets a -1
                    timeElapsed = timeOfCurrentRow - timeOfOldestRow
                    if(len(queueOfValuesInLastNSecs) != numberOfRowsInLastNSecs):
                        print "Sanity check: This condition is not possible logically. There has been an unknown error"
                        sys.exit(-1)
    
        print "Processed row number " + str(currentRowNumberForWhichFeatureValueIsBeingCalculated)
    
    lNameOfFeaturePrinted = "fVarianceOfCol" + args.c + "InLast" + str(args.n) + "Secs"
    return [ "TimeStamp", lNameOfFeaturePrinted , "TimeElapsed","TotalOfRowsInLastNSecs" , "TotalOfSquareOfRowsInLastNSecs" , "NumberOfRowsInLastNSecs" , ]
Exemple #32
0
def main():
    dataFile.getDataIntoMatrix(args.pd)
    predictedValuesDict = dict()
    getPredictedValuesIntoDict(predictedValuesDict)
    enterTrade = 0
    ttqAtTimeOfPreviousDataRow = 0
    askP0AtTimeOfPreviousDataRow = 0
    bidP0AtTimeOfPreviousDataRow = 0
    tradeStats = dict()
    tradeStats['totalSellValue'] = 0
    tradeStats['totalBuyValue'] = 0
    tradeStats['currentPosition'] = 0
    noPredictionForThisRow = 0
    currentPredictedValue = 0
    entryCL = float(args.entryCL) / 100
    exitCL = float(args.exitCL) / 100
    numberOfTimesAskedToEnterTrade = 0
    numberOfTimesAskedToExitTrade = 0
    reasonForTrade = dict()
    reasonForTrade['LTPDoesNotEqualBidP0'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringBuyAttempt'] = 0
    reasonForTrade['AssumingBuyTradeHappened'] = 0
    reasonForTrade['LTPDoesNotEqualAskP0'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringSellAttempt'] = 0
    reasonForTrade['AssumingSellTradeHappened'] = 0

    print("Processing the data file for trades :")

    for currentDataRow in dataFile.matrix:
        checkIfPreviousDecisionToEnterOrExitTradeWasSuccessful(
            currentDataRow, ttqAtTimeOfPreviousDataRow,
            askP0AtTimeOfPreviousDataRow, bidP0AtTimeOfPreviousDataRow,
            enterTrade, tradeStats, reasonForTrade)
        currentTimeStamp = common.convertTimeStampFromStringToFloat(
            currentDataRow[colNumberOfData.TimeStamp])

        try:
            currentPredictedValue = float(
                predictedValuesDict[currentTimeStamp])
        except:
            noPredictionForThisRow += 1

        if (currentPredictedValue > entryCL):
            enterTrade = 1
            numberOfTimesAskedToEnterTrade += 1
        elif (currentPredictedValue < exitCL
              and tradeStats['currentPosition'] > 0):
            numberOfTimesAskedToExitTrade += 1
            enterTrade = -1  # Implies to exit the trade
        else:
            enterTrade = 0  # Implies make no change

        ttqAtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.TTQ])
        askP0AtTimeOfPreviousDataRow = float(
            currentDataRow[colNumberOfData.AskP0])
        bidP0AtTimeOfPreviousDataRow = float(
            currentDataRow[colNumberOfData.BidP0])

    dirName = args.pd.replace('/ro/', '/rs/')
    tradeResultMainDirName = dirName + "/r/"
    if not os.path.exists(tradeResultMainDirName):
        os.mkdir(tradeResultMainDirName)
    tradeResultSubDirectoryName = tradeResultMainDirName + mainExperimentName + "/"
    if not os.path.exists(tradeResultSubDirectoryName):
        os.mkdir(tradeResultSubDirectoryName)
    fileName = tradeResultSubDirectoryName + experimentName + initialFileName + ".result"
    outputFile = open(fileName, "w")

    print("Starting to write: " + fileName)
    print("The net results are: " +
          str(tradeStats['totalSellValue'] - tradeStats['totalBuyValue']),
          file=outputFile)
    print("Number of rows for which there is no prediction: " +
          str(noPredictionForThisRow),
          file=outputFile)
    print("Number of times asked to enter trade: " +
          str(numberOfTimesAskedToEnterTrade),
          file=outputFile)
    print("Number of times asked to exit trade: " +
          str(numberOfTimesAskedToExitTrade),
          file=outputFile)
    print("Assumed buy trade did not happen since volume did not increase: " +
          str(reasonForTrade['VolumeDidNotIncreaseDuringBuyAttempt']),
          file=outputFile)
    print("Assumed buy trade did not happen since bidP0 not same as LTP: " +
          str(reasonForTrade['LTPDoesNotEqualBidP0']),
          file=outputFile)
    print("Assumed buy trade happened: " +
          str(reasonForTrade['AssumingBuyTradeHappened']),
          file=outputFile)
    print("Assumed sell trade did not happen since volume did not increase: " +
          str(reasonForTrade['VolumeDidNotIncreaseDuringSellAttempt']),
          file=outputFile)
    print("Assumed sell trade did not happen since bidP0 not same as LTP: " +
          str(reasonForTrade['LTPDoesNotEqualAskP0']),
          file=outputFile)
    print("Assumed sell trade happened: " +
          str(reasonForTrade['AssumingSellTradeHappened']),
          file=outputFile)
    print("The total sell value is: " + str(tradeStats['totalSellValue']),
          file=outputFile)
    print("The total buy value is: " + str(tradeStats['totalBuyValue']),
          file=outputFile)
    try:
        averageSellPrice = tradeStats['totalSellValue'] / reasonForTrade[
            'AssumingSellTradeHappened']
        averageBuyPrice = tradeStats['totalBuyValue'] / reasonForTrade[
            'AssumingBuyTradeHappened']
    except:
        averageSellPrice = 0
        averageBuyPrice = 0
    print("Average sell price per unit is: " + str(averageSellPrice),
          file=outputFile)
    print("Average buy price per unit is: " + str(averageBuyPrice),
          file=outputFile)
    print("The current position: " + str(tradeStats['currentPosition']),
          file=outputFile)
    print("Profit or loss per Qty traded is: " +
          str(averageSellPrice - averageBuyPrice),
          file=outputFile)
    pLPerLot = (averageSellPrice - averageBuyPrice) * 1000
    print("1 lot has 1000 qty's so P/L per lot is: " + str(pLPerLot),
          file=outputFile)
    print("P/L for trading 10 lots is: " + str(pLPerLot * 10), file=outputFile)
Exemple #33
0
def getDataFileAndPredictionsIntoObjectList(dataFileObject, fA5 , fB5 , fC5 , fD5 , fE5 , fA6 , fB6 , fC6 , fD6 , fE6 ,lMinOfExitCl):
    global gNoOfLineReadPerChunk,gTickSize
    lObjectList = []
    lCurrentDataRowCount = 0
    lPrevObj = None
    fileHasHeader = 1
    headerSkipped = 0
    dataFileSep = ";"
    featureFileSep = ";"
    lListOfBidP = []
    lListOfAskP = []
    lDataFileRowsList = list(islice(dataFileObject,10000))
    lFeatureAFileRowListFor5Level = list(islice(fA5,10000))
    lFeatureBFileRowListFor5Level = list(islice( fB5,10000 ))
    lFeatureCFileRowListFor5Level = list(islice(fC5,10000))
    lFeatureDFileRowListFor5Level = list(islice( fD5,10000 ))
    lFeatureEFileRowListFor5Level = list(islice(fE5,10000))
    
    lFeatureAFileRowListFor6Level = list(islice( fA6,10000 ))
    lFeatureBFileRowListFor6Level = list(islice(fB6,10000))
    lFeatureCFileRowListFor6Level = list(islice( fC6,10000 ))
    lFeatureDFileRowListFor6Level = list(islice(fD6,10000))
    lFeatureEFileRowListFor6Level = list(islice( fE6,10000 ))
    while True:
        lDataFileRowsList = list(islice(dataFileObject,gNoOfLineReadPerChunk))
        lFeatureAFileRowListFor5Level = list(islice(fA5,gNoOfLineReadPerChunk))
        lFeatureBFileRowListFor5Level = list(islice( fB5,gNoOfLineReadPerChunk ))
        lFeatureCFileRowListFor5Level = list(islice(fC5,gNoOfLineReadPerChunk))
        lFeatureDFileRowListFor5Level = list(islice( fD5,gNoOfLineReadPerChunk ))
        lFeatureEFileRowListFor5Level = list(islice(fE5,gNoOfLineReadPerChunk))
        
        lFeatureAFileRowListFor6Level = list(islice( fA6,gNoOfLineReadPerChunk ))
        lFeatureBFileRowListFor6Level = list(islice(fB6,gNoOfLineReadPerChunk))
        lFeatureCFileRowListFor6Level = list(islice( fC6,gNoOfLineReadPerChunk ))
        lFeatureDFileRowListFor6Level = list(islice(fD6,gNoOfLineReadPerChunk))
        lFeatureEFileRowListFor6Level = list(islice( fE6,gNoOfLineReadPerChunk ))
        
        if not lDataFileRowsList :
            print("Finished reading file")
            lObjectList.append(lPrevObj)    
            lPrevObj = None          
            break
        lengthOfDataList = len(lDataFileRowsList)
        lengthOfFeatureAListFor5Level = len(lFeatureAFileRowListFor5Level)
        lengthOfFeatureBListFor5Level = len(lFeatureBFileRowListFor5Level)
        
        if lengthOfDataList!=lengthOfFeatureAListFor5Level or lengthOfFeatureAListFor5Level!=lengthOfFeatureBListFor5Level:
            print("Length of data file and predicted buy and sell values file are not same ")
            os._exit(-1)
        for currentRowIndex in range(lengthOfDataList):
            if(fileHasHeader == 1 and headerSkipped != 1):
                headerSkipped = 1 
                continue
            lDataRow = lDataFileRowsList[currentRowIndex].rstrip().split(dataFileSep)
            if 'nsefut' not in args.e:
                lAskP = float(lDataRow[colNumberOfData.AskP0])
                lBidP = float(lDataRow[colNumberOfData.BidP0])
                lAskQ = int(lDataRow[colNumberOfData.AskQ0])
                lBidQ = int(lDataRow[colNumberOfData.BidQ0])
                lAskP1 = float(lDataRow[colNumberOfData.AskP1])
                lBidP1 = float(lDataRow[colNumberOfData.BidP1])
                lAskQ1 = int(lDataRow[colNumberOfData.AskQ1])
                lBidQ1 = int(lDataRow[colNumberOfData.BidQ1])
            else:
                lAskP = float(lDataRow[colNumberOfData.BestAskP])
                lBidP = float(lDataRow[colNumberOfData.BestBidP])
                lAskQ = int(lDataRow[colNumberOfData.BestAskQ])
                lBidQ = int(lDataRow[colNumberOfData.BestBidQ])
                lAskP1 = float(lDataRow[colNumberOfData.BestAskP1])
                lBidP1 = float(lDataRow[colNumberOfData.BestBidP1])
                lAskQ1 = int(lDataRow[colNumberOfData.BestAskQ1])
                lBidQ1 = int(lDataRow[colNumberOfData.BestBidQ1])            
            lTTQ = int(lDataRow[colNumberOfData.TTQ])
            lLTP = float(lDataRow[colNumberOfData.LTP]) 
            lCurrentDataRowTimeStamp = common.convertTimeStampFromStringToFloat(lDataRow[colNumberOfData.TimeStamp])

            lFeatureARow = lFeatureAFileRowListFor5Level[currentRowIndex].rstrip().split(featureFileSep)
            lFeatureATimeStamp = float(lFeatureARow[0])
            lFeatureA = float(lFeatureARow[1])
            
            lFeatureBRow = lFeatureBFileRowListFor5Level[currentRowIndex].rstrip().split(featureFileSep)
            lFeatureBTimeStamp = float(lFeatureBRow[0])
            lFeatureB = float(lFeatureBRow[1])

            lFeatureCRow = lFeatureCFileRowListFor5Level[currentRowIndex].rstrip().split(featureFileSep)
            lFeatureC = float(lFeatureCRow[1])
            
            lFeatureDRow = lFeatureDFileRowListFor5Level[currentRowIndex].rstrip().split(featureFileSep)
            lFeatureD = float(lFeatureDRow[1])
            
            lFeatureERow = lFeatureEFileRowListFor5Level[currentRowIndex].rstrip().split(featureFileSep)
            lFeatureE = float(lFeatureERow[1])
            
            lFeatureA6Row = lFeatureAFileRowListFor6Level[currentRowIndex].rstrip().split(featureFileSep)
            lFeatureA6 = float(lFeatureA6Row[1])
            
            lFeatureB6Row = lFeatureBFileRowListFor6Level[currentRowIndex].rstrip().split(featureFileSep)
            lFeatureB6 = float(lFeatureB6Row[1])

            lFeatureC6Row = lFeatureCFileRowListFor6Level[currentRowIndex].rstrip().split(featureFileSep)
            lFeatureC6 = float(lFeatureC6Row[1])
            
            lFeatureD6Row = lFeatureDFileRowListFor6Level[currentRowIndex].rstrip().split(featureFileSep)
            lFeatureD6 = float(lFeatureD6Row[1])
            
            lFeatureE6Row = lFeatureEFileRowListFor6Level[currentRowIndex].rstrip().split(featureFileSep)
            lFeatureE6 = float(lFeatureE6Row[1])
                        
            l_buy_SOP_of_alphas_for_5_level = exp ( ((813.280878130412 * lFeatureA ) + ( -546.820253147887 * lFeatureB) + ( -498.096438026184 * lFeatureC) + ( -1008.9535016807 * lFeatureD) + 1237.93033895547) )
            l_sell_SOP_of_alphas_for_5_level = exp( (( 366.62364204055 * lFeatureA  ) + ( -881.958964774577 * lFeatureB ) + ( 619.51957804397 * lFeatureC ) + ( 1003.99686893559 * lFeatureE ) - 1110.76386909796) )

            l_buy_SOP_of_alphas_for_6_level = exp ( ((813.280878130412 * lFeatureA6 ) + ( -546.820253147887 * lFeatureB6) + ( -498.096438026184 * lFeatureC6) + ( -1008.9535016807 * lFeatureD6) + 1237.93033895547) )
            l_sell_SOP_of_alphas_for_6_level = exp( (( 366.62364204055 * lFeatureA6  ) + ( -881.958964774577 * lFeatureB6 ) + ( 619.51957804397 * lFeatureC6 ) + ( 1003.99686893559* lFeatureE6 ) - 1110.76386909796) )            
            
            lBuyPredictedValueFor5Levels = l_buy_SOP_of_alphas_for_5_level / ( 1 + l_buy_SOP_of_alphas_for_5_level )
            lSellPredictedValueFor5Levels = l_sell_SOP_of_alphas_for_5_level / ( 1 + l_sell_SOP_of_alphas_for_5_level )
            
            lBuyPredictedValueFor6Levels = l_buy_SOP_of_alphas_for_6_level / ( 1 + l_buy_SOP_of_alphas_for_6_level )
            lSellPredictedValueFor6Levels = l_sell_SOP_of_alphas_for_6_level / ( 1 + l_sell_SOP_of_alphas_for_6_level )
                        
            if lCurrentDataRowTimeStamp != lFeatureATimeStamp or lFeatureATimeStamp!=lFeatureBTimeStamp:
                print('Time stamp of data row with predicted value is not matching .\n Data row time stamp :- ' , lCurrentDataRowTimeStamp,'BuyPredicted Time Stamp :- ' , lFeatureATimeStamp\
                      ,"SellPredicted Time Stamp :- ",lFeatureBTimeStamp)
                os._exit(-1)
            else:
                lObj = Tick(lCurrentDataRowTimeStamp,lAskP,lBidP,lAskQ,lBidQ,lAskP1,lBidP1,lAskQ1,lBidQ1,lLTP,lTTQ,lBuyPredictedValueFor5Levels,lSellPredictedValueFor5Levels,lBuyPredictedValueFor6Levels,lSellPredictedValueFor6Levels,
                            lFeatureA,lFeatureB,lFeatureC,lFeatureD,lFeatureE,lFeatureA6,lFeatureB6,lFeatureC6,lFeatureD6,lFeatureE6)
                if lPrevObj!=None:
                    lPrevObj.TTQChange  = lObj.TTQ - lPrevObj.TTQ
                    lPrevObj.NextLTP = lObj.LTP
                    if ( lPrevObj.AskP -lPrevObj.BidP > gTickSize ) and ( lPrevObj.TTQChange == 0 ):
                        if lPrevObj.BidP not in lListOfBidP:
                            lListOfBidP.append(lPrevObj.BidP)
                        if lPrevObj.AskP not in lListOfAskP:
                            lListOfAskP.append(lPrevObj.AskP)
                        pass
                    else:
                        if lPrevObj.currentBuyPredictedValue1 >= lMinOfExitCl or lPrevObj.currentSellPredictedValue1>=lMinOfExitCl:
                            if len(lListOfBidP) > 1:
                                lPrevObj.bidPChangedInBetweenLastTickAndCurrentTick = 1
                            if len(lListOfAskP) > 1:
                                lPrevObj.askPChangedInBetweenLastTickAndCurrentTick = 1
                            lObjectList.append(lPrevObj)  
                            lListOfBidP = [lPrevObj.BidP]
                            lListOfAskP = [lPrevObj.AskP]
                        else:
                            if lPrevObj.BidP not in lListOfBidP:
                                lListOfBidP.append(lPrevObj.BidP)
                            if lPrevObj.AskP not in lListOfAskP:
                                lListOfAskP.append(lPrevObj.AskP)
                lPrevObj = lObj
            if lCurrentDataRowCount%50000 ==0:
                print("Completed reading ",lCurrentDataRowCount)
            lCurrentDataRowCount = lCurrentDataRowCount + 1 
    return lObjectList
Exemple #34
0
def readOnceAndWrite(pFileName, targetValuesDict):
    global gOpenBuyPrice, gCloseSellPrice, gOpenSellPrice, gCloseBuyPrice
    attribute.initList()
    enterTradeShort = 0
    enterTradeLong = 0
    ltpAtTimeOfPreviousDataRow = 0
    ttqAtTimeOfPreviousDataRow = 0
    askP0AtTimeOfPreviousDataRow = 0
    bidP0AtTimeOfPreviousDataRow = 0
    askQ0AtTimeOfPreviousDataRow = 0
    bidQ0AtTimeOfPreviousDataRow = 0
    exchangeTimeStamp = 0
    currentTimeStamp = 0
    tradeStats = dict()
    tradeStats['totalSellValueShort'] = 0
    tradeStats['totalBuyValueShort'] = 0
    tradeStats['currentPositionShort'] = 0
    tradeStats['totalSellValueLong'] = 0
    tradeStats['totalBuyValueLong'] = 0
    tradeStats['currentPositionLong'] = 0
    noTargetForThisRow = 0
    currentSellTargetValue = 0
    currentBuyTargetValue = 0
    #  entryCLCutoff = float(args.entryCLCutoff)
    #  exitCLCutoff = float(args.exitCLCutoff)
    numberOfTimesAskedToEnterTradeShort = 0
    numberOfTimesAskedToEnterTradeLong = 0
    numberOfTimesAskedToExitTradeShort = 0
    numberOfTimesAskedToExitTradeLong = 0
    reasonForTrade = dict()
    reasonForTrade['LTPDoesNotEqualBidP0Short'] = 0
    reasonForTrade['LTPDoesNotEqualAskP0Short'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptShort'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptLong'] = 0
    reasonForTrade['CloseBuyTradeHappened'] = 0
    reasonForTrade['OpenBuyTradeHappened'] = 0
    reasonForTrade['LTPDoesNotEqualAskP0Long'] = 0
    reasonForTrade['LTPDoesNotEqualBidP0Long'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptShort'] = 0
    reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptLong'] = 0
    reasonForTrade['OpenSellTradeHappened'] = 0
    reasonForTrade['CloseSellTradeHappened'] = 0
    reasonForTrade['PrevWasOurOrder'] = 0
    lReasonForTradingOrNotTradingShort = ""
    lReasonForTradingOrNotTradingLong = ""
    currentIndex = 0
    lDummyBidQ0 = 0
    lDummyAskQ0 = 0
    lDummyTTQForBuy = 0
    lDummyTTQForSell = 0
    print("Processing the data file for trades :")
    attribute.initList()
    prevBuyTargetValue = 0
    prevSellTargetValue = 0
    exchangeTimeStamp = 0
    lOpenBuyIndex = 0
    lOpenSellIndex = 0
    l_exchange_time_stamp_to_print = ""
    for currentDataRow in dataFile.matrix:

        l_expiry_wrt_1970 = exchangeTimeStamp + 315513000.0

        l_dt = datetime.datetime.fromtimestamp(l_expiry_wrt_1970)
        l_exchange_time_stamp_to_print = str(l_dt.hour) + ":" + str(
            l_dt.minute) + ":" + str(l_dt.second)
        lReturnList = checkIfPreviousDecisionToEnterOrExitTradeWasSuccessful(currentDataRow,ttqAtTimeOfPreviousDataRow,askP0AtTimeOfPreviousDataRow,bidP0AtTimeOfPreviousDataRow,\
                                                                                 askQ0AtTimeOfPreviousDataRow , bidQ0AtTimeOfPreviousDataRow , enterTradeShort,enterTradeLong,tradeStats,reasonForTrade,\
                                                                                 lReasonForTradingOrNotTradingLong,lReasonForTradingOrNotTradingShort )

        lReasonForTradingOrNotTradingShort = lReturnList[0]
        lReasonForTradingOrNotTradingLong = lReturnList[1]
        lDummyBidQ0 = lReturnList[2]
        lDummyAskQ0 = lReturnList[3]
        lDummyTTQForBuy = lReturnList[4]
        lDummyTTQForSell = lReturnList[5]
        if currentIndex > 0:
            lFlag = 0
            if "CloseSell" in lReasonForTradingOrNotTradingLong:
                if gCloseSellPrice - gOpenBuyPrice <= 0:
                    for i in range(lOpenBuyIndex, currentIndex):
                        attribute.aList[i][1] = 0
                else:
                    lFlag = 1
                lOpenBuyIndex = 0
            if "CloseBuy" in lReasonForTradingOrNotTradingShort:
                if gOpenSellPrice - gCloseBuyPrice <= 0:
                    for i in range(lOpenSellIndex, currentIndex):
                        attribute.aList[i][1] = 0
                else:
                    lFlag = 1
                lOpenSellIndex = 0
            if "OpenSell" in lReasonForTradingOrNotTradingShort:
                lOpenSellIndex = currentIndex - 1
            if "OpenBuy" in lReasonForTradingOrNotTradingLong:
                lOpenBuyIndex = currentIndex - 1

            if (lOpenSellIndex != 0 or lOpenBuyIndex != 0):
                lFlag = 1

            attribute.aList[currentIndex - 1][0] = currentTimeStamp
            attribute.aList[currentIndex - 1][1] = lFlag
            attribute.aList[currentIndex -
                            1][2] = tradeStats['currentPositionLong']
            listOfStringsToPrint = [ str(tradeStats['currentPositionShort']) , str(bidQ0AtTimeOfPreviousDataRow) , str(bidP0AtTimeOfPreviousDataRow) , str(askP0AtTimeOfPreviousDataRow) , \
                                    str(askQ0AtTimeOfPreviousDataRow) , str(ttqAtTimeOfPreviousDataRow) , str(ltpAtTimeOfPreviousDataRow) ,\
                                    str(currentSellTargetValue) , str(enterTradeShort) ,lReasonForTradingOrNotTradingShort , str(currentBuyTargetValue) ,\
                                    str(enterTradeLong) ,lReasonForTradingOrNotTradingLong ,l_exchange_time_stamp_to_print, str(reasonForTrade['CloseBuyTradeHappened']),\
                                    str(reasonForTrade['OpenBuyTradeHappened']),str(reasonForTrade['OpenSellTradeHappened']),str(reasonForTrade['CloseSellTradeHappened']),\
                                    str(gCloseSellPrice),str(gOpenBuyPrice),str(gOpenSellPrice),str(gCloseBuyPrice)]
            attribute.aList[currentIndex -
                            1][3] = ";".join(listOfStringsToPrint)
        currentTimeStamp = common.convertTimeStampFromStringToFloat(
            currentDataRow[colNumberOfData.TimeStamp])

        try:
            currentSellTargetValue = float(
                targetValuesDict[currentTimeStamp]['sell'])
            currentBuyTargetValue = float(
                targetValuesDict[currentTimeStamp]['buy'])
        except:
            noTargetForThisRow += 1

        #short decisions
        if (currentSellTargetValue == 1):
            enterTradeShort = 1
            numberOfTimesAskedToEnterTradeShort += 1
        elif (currentSellTargetValue == 0
              and tradeStats['currentPositionShort'] > 0):
            numberOfTimesAskedToExitTradeShort += 1
            enterTradeShort = -1  # Implies to exit the trade
        else:
            enterTradeShort = 0  # Implies make no change

        #long decisions
        if (currentBuyTargetValue == 1):
            enterTradeLong = 1
            numberOfTimesAskedToEnterTradeLong += 1
        elif (currentBuyTargetValue == 0
              and tradeStats['currentPositionLong'] > 0):
            numberOfTimesAskedToExitTradeLong += 1
            enterTradeLong = -1  # Implies to exit the trade
        else:
            enterTradeLong = 0  # Implies make no change

        ttqAtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.TTQ])
        askP0AtTimeOfPreviousDataRow = float(
            currentDataRow[colNumberOfData.AskP0])
        bidP0AtTimeOfPreviousDataRow = float(
            currentDataRow[colNumberOfData.BidP0])
        askQ0AtTimeOfPreviousDataRow = float(
            currentDataRow[colNumberOfData.AskQ0])
        bidQ0AtTimeOfPreviousDataRow = float(
            currentDataRow[colNumberOfData.BidQ0])
        ltpAtTimeOfPreviousDataRow = float(currentDataRow[colNumberOfData.LTP])
        currentIndex = currentIndex + 1
        prevBuyTargetValue = currentBuyTargetValue
        prevSellTargetValue = currentSellTargetValue
        exchangeTimeStamp = float(currentDataRow[colNumberOfData.ExchangeTS])

    # Squaring off if some open position there
    if tradeStats['currentPositionLong'] > 0:
        reasonForTrade['CloseSellTradeHappened'] += tradeStats[
            'currentPositionLong']
        tradeStats['totalSellValueLong'] += tradeStats[
            'currentPositionLong'] * (bidP0AtTimeOfPreviousDataRow)
        tradeStats['currentPositionLong'] = 0
        lReasonForTradingOrNotTradingLong = 'CloseSell(Hitting)'
        gCloseSellPrice = bidP0AtTimeOfPreviousDataRow
        if gCloseSellPrice - gOpenBuyPrice < 0:
            for i in range(lOpenBuyIndex, currentIndex):
                attribute.aList[i][1] = 0

    if tradeStats['currentPositionShort'] > 0:
        reasonForTrade['CloseBuyTradeHappened'] += tradeStats[
            'currentPositionShort']
        tradeStats['totalBuyValueShort'] += tradeStats[
            'currentPositionShort'] * (askP0AtTimeOfPreviousDataRow)
        tradeStats['currentPositionShort'] = 0
        lReasonForTradingOrNotTradingLong = 'CloseBuy(Hitting)'
        gCloseBuyPrice = askP0AtTimeOfPreviousDataRow
        if gOpenSellPrice - gCloseBuyPrice < 0:
            for i in range(lOpenSellIndex, currentIndex):
                attribute.aList[i][1] = 0

    attribute.aList[currentIndex - 1][0] = currentTimeStamp
    attribute.aList[currentIndex - 1][1] = attribute.aList[currentIndex - 2][1]
    attribute.aList[currentIndex - 1][2] = tradeStats['currentPositionLong']
    listOfStringsToPrint = [ str(tradeStats['currentPositionShort']),str(bidQ0AtTimeOfPreviousDataRow) , str(bidP0AtTimeOfPreviousDataRow) , str(askP0AtTimeOfPreviousDataRow) ,\
                             str(askQ0AtTimeOfPreviousDataRow) , str(ttqAtTimeOfPreviousDataRow) , str(ltpAtTimeOfPreviousDataRow) , \
                             str(currentSellTargetValue) , str(enterTradeShort) , "" , str(currentBuyTargetValue) , str(enterTradeLong) ,\
                             "" ,l_exchange_time_stamp_to_print, str(reasonForTrade['CloseBuyTradeHappened']),str(reasonForTrade['OpenBuyTradeHappened']),str(reasonForTrade['OpenSellTradeHappened']),\
                             str(reasonForTrade['CloseSellTradeHappened']),str(gCloseSellPrice),str(gOpenBuyPrice),str(gOpenSellPrice),str(gCloseBuyPrice)]
    attribute.aList[currentIndex - 1][3] = ";".join(listOfStringsToPrint)

    dirName = args.d.replace('/ro/', '/wf/')
    tradeLogMainDirName = dirName + "/tr/"
    if not os.path.exists(tradeLogMainDirName):
        os.mkdir(tradeLogMainDirName)
    tradeLogSubDirectoryName = tradeLogMainDirName + mainExperimentName + "/"
    if not os.path.exists(tradeLogSubDirectoryName):
        os.mkdir(tradeLogSubDirectoryName)

    fileName = tradeLogSubDirectoryName + pFileName + ".targetTrade"
    lHeaderColumnNamesList  = ['TimeStamp','targetValue','CurrentPositionLong','CurrentPositionShort','BidQ0','BidP0','AskP0','AskQ0','TTQ','LTP',\
                               'CurTargetValueShort','EnterTradeShort','ReasonForTradingOrNotTradingShort','CurTargetValueLong','EnterTradeLong',\
                               'ReasonForTradingOrNotTradingLong','Exchange_TS','totalBuyShort','totalBuyLong','totalSellShort','totalSellLong','CloseSellPrice',\
                               'OpenBuyPrice','OpenSellPrice','CloseBuyPrice']
    attribute.writeToFile(fileName, lHeaderColumnNamesList)

    tradeResultMainDirName = dirName + "/r/"
    if not os.path.exists(tradeResultMainDirName):
        os.mkdir(tradeResultMainDirName)
    tradeResultSubDirectoryName = tradeResultMainDirName + mainExperimentName + "/"
    if not os.path.exists(tradeResultSubDirectoryName):
        os.mkdir(tradeResultSubDirectoryName)
    fileName = tradeResultSubDirectoryName + pFileName + ".targetResult"
    outputFile = open(fileName, "w")

    #changed file write to modify it to Short Long version
    print("Starting to write: " + fileName)
    print("The net results for Short are: " +
          str(tradeStats['totalSellValueShort'] -
              tradeStats['totalBuyValueShort']),
          file=outputFile)
    print("The net results for Long are: " +
          str(tradeStats['totalSellValueLong'] -
              tradeStats['totalBuyValueLong']),
          file=outputFile)
    print("Number of rows for which there is no target: " +
          str(noTargetForThisRow),
          file=outputFile)
    print("Number of times asked to enter trade Short: " +
          str(numberOfTimesAskedToEnterTradeShort),
          file=outputFile)
    print("Number of times asked to enter trade Long: " +
          str(numberOfTimesAskedToEnterTradeLong),
          file=outputFile)
    print("Number of times asked to exit trade Short: " +
          str(numberOfTimesAskedToExitTradeShort),
          file=outputFile)
    print("Number of times asked to exit trade Long: " +
          str(numberOfTimesAskedToExitTradeLong),
          file=outputFile)
    print(
        "Assumed close buy trade did not happen since volume did not increase: "
        + str(reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptShort']),
        file=outputFile)
    print(
        "Assumed open buy trade did not happen since volume did not increase: "
        + str(reasonForTrade['VolumeDidNotIncreaseDuringBuyAttemptLong']),
        file=outputFile)
    print(
        "Assumed close buy trade did not happen since bidP0 not same as LTP: "
        + str(reasonForTrade['LTPDoesNotEqualBidP0Short']),
        file=outputFile)
    print(
        "Assumed open buy trade did not happen since bidP0 not same as LTP: " +
        str(reasonForTrade['LTPDoesNotEqualBidP0Long']),
        file=outputFile)
    print("Assumed close buy trade happened: " +
          str(reasonForTrade['CloseBuyTradeHappened']),
          file=outputFile)
    print("Assumed open buy trade happened: " +
          str(reasonForTrade['OpenBuyTradeHappened']),
          file=outputFile)
    print(
        "Assumed open sell trade did not happen since volume did not increase: "
        + str(reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptShort']),
        file=outputFile)
    print(
        "Assumed close sell trade did not happen since volume did not increase: "
        + str(reasonForTrade['VolumeDidNotIncreaseDuringSellAttemptLong']),
        file=outputFile)
    print(
        "Assumed open sell trade did not happen since bidP0 not same as LTP: "
        + str(reasonForTrade['LTPDoesNotEqualAskP0Short']),
        file=outputFile)
    print(
        "Assumed close sell trade did not happen since bidP0 not same as LTP: "
        + str(reasonForTrade['LTPDoesNotEqualAskP0Long']),
        file=outputFile)
    print("Assumed open sell trade happened: " +
          str(reasonForTrade['OpenSellTradeHappened']),
          file=outputFile)
    print("Assumed close sell trade happened: " +
          str(reasonForTrade['CloseSellTradeHappened']),
          file=outputFile)
    print("The total open sell value is: " +
          str(tradeStats['totalSellValueShort']),
          file=outputFile)
    print("The total close sell value is: " +
          str(tradeStats['totalSellValueLong']),
          file=outputFile)
    print("The total close buy value is: " +
          str(tradeStats['totalBuyValueShort']),
          file=outputFile)
    print("The total open buy value is: " +
          str(tradeStats['totalBuyValueLong']),
          file=outputFile)

    try:
        averageOpenSellPrice = tradeStats[
            'totalSellValueShort'] / reasonForTrade['OpenSellTradeHappened']
        averageCloseBuyPrice = tradeStats[
            'totalBuyValueShort'] / reasonForTrade['CloseBuyTradeHappened']
    except:
        averageOpenSellPrice = 0
        averageCloseBuyPrice = 0
    try:
        averageCloseSellPrice = tradeStats[
            'totalSellValueLong'] / reasonForTrade['CloseSellTradeHappened']
        averageOpenBuyPrice = tradeStats['totalBuyValueLong'] / reasonForTrade[
            'OpenBuyTradeHappened']
    except:
        averageCloseSellPrice = 0
        averageOpenBuyPrice = 0

    print("Average open sell price per unit is: " + str(averageOpenSellPrice),
          file=outputFile)
    print("Average close sell price per unit is: " +
          str(averageCloseSellPrice),
          file=outputFile)
    print("Average open buy price per unit is: " + str(averageOpenBuyPrice),
          file=outputFile)
    print("Average close buy price per unit is: " + str(averageCloseBuyPrice),
          file=outputFile)
    print("The current position Short: " +
          str(tradeStats['currentPositionShort']),
          file=outputFile)
    print("The current position Long: " +
          str(tradeStats['currentPositionLong']),
          file=outputFile)
    print("Profit or loss per Qty traded Short is: " +
          str(averageOpenSellPrice - averageCloseBuyPrice),
          file=outputFile)
    print("Profit or loss per Qty traded Long is: " +
          str(averageCloseSellPrice - averageOpenBuyPrice),
          file=outputFile)
    pLPerLotShort = (averageOpenSellPrice - averageCloseBuyPrice) * 1000
    pLPerLotLong = (averageCloseSellPrice - averageOpenBuyPrice) * 1000
    print("1 lot has 1000 qty's so P/L Short per lot is: " +
          str(pLPerLotShort),
          file=outputFile)
    print("1 lot has 1000 qty's so P/L Long per lot is: " + str(pLPerLotLong),
          file=outputFile)
    print("P/L for Short trading 10 lots is: " + str(pLPerLotShort * 10),
          file=outputFile)
    print("P/L for Long trading 10 lots is: " + str(pLPerLotLong * 10),
          file=outputFile)
def extractAttributeFromDataMatrix(args):
   if args.n == None:
      N = 5
   else:
      N = int(args.n) 
   try:
      #Value of argument c can be taken as BidP0 and AskP0 
      args.c
   except:
      print "Since -c has not been specified I cannot proceed"
      os._exit()
   colNumberOfAttribute = eval("colNumberOfData."+ args.c )
   colNumberOfOppositeAttribute = eval("colNumberOfData.BestBidP") if "ask" in args.c else eval("colNumberOfData.BestAskP")
   colNumberOfTimeStamp = colNumberOfData.TimeStamp
   if "ask" in args.c.lower():
      colAttributeRowNo = [colNumberOfData.BestAskP, colNumberOfData.BestAskP1, colNumberOfData.BestAskP2, colNumberOfData.BestAskP3, colNumberOfData.BestAskP4] 
   else:
      colAttributeRowNo = [colNumberOfData.BestBidP, colNumberOfData.BestBidP1, colNumberOfData.BestBidP2, colNumberOfData.BestBidP3, colNumberOfData.BestBidP4]
   numberOfRowsInLastNSecs = 0
   queueOfValuesInLastNSecs = deque()
   totalOfRowsInLastNSecsX = 0.0
   totalOfRowsInLastNSecsN = 0.0
   totalOfQtyInLastNSecsX = 0.0
   totalOfQtyInLastNSecsN = 0.0   
   timeOfOldestRow = common.convertTimeStampFromStringToFloat(dataFile.matrix[0][colNumberOfTimeStamp],args.cType)
   currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
   lengthOfDataMatrix = len(dataFile.matrix)
   while (currentRowNumberForWhichFeatureValueIsBeingCalculated < lengthOfDataMatrix):
      colAttributeRow = map(float, [dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][i] for i in colAttributeRowNo])
      timeOfCurrentRow = common.convertTimeStampFromStringToFloat(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfTimeStamp],args.cType)
      timeElapsed = timeOfCurrentRow - timeOfOldestRow
      if (timeElapsed < N):
         if  currentRowNumberForWhichFeatureValueIsBeingCalculated != 0:
             previousRowPrice = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated - 1][colNumberOfAttribute])
         else:
             previousRowPrice = 0
         currentPrice = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfAttribute])
         currentLTP = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.LTP])
         currentMsgCode = dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.MsgCode]
         newQty = int(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.NewQ])
         newPrice = int(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.NewP])
         oldQty = int(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.OldQ])
         oldPrice = int(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfData.OldP])
         if ( currentMsgCode =='T' and (( colNumberOfOppositeAttribute <= currentLTP ) if "bid" in args.c.lower() else (colNumberOfOppositeAttribute >= currentLTP))):
             totalOfRowsInLastNSecsX += newQty * currentLTP
             cellValueTotalX = newQty * currentLTP
             totalOfQtyInLastNSecsX += newQty
             cellValueQtyX = newQty
             cellValueTotalN = 0
             cellValueQtyN = 0
             
         elif currentMsgCode == 'M':
             totalOfRowsInLastNSecsN += (newPrice * newQty) if ( newPrice in colAttributeRow ) else 0 
             cellValueTotalN = newPrice * newQty if ( newPrice in colAttributeRow ) else 0
             totalOfQtyInLastNSecsN += newQty if ( newPrice in colAttributeRow ) else 0
             cellValueQtyN = newQty if ( newPrice in colAttributeRow ) else 0
             totalOfRowsInLastNSecsX += oldPrice * oldQty if ( oldPrice in colAttributeRow ) else 0
             cellValueTotalX = oldPrice * oldQty if ( oldPrice in colAttributeRow ) else 0
             totalOfQtyInLastNSecsX += oldQty if ( oldPrice in colAttributeRow ) else 0
             cellValueQtyX = oldQty if ( oldPrice in colAttributeRow ) else 0
         
         elif ( currentMsgCode == 'N' and ( newPrice in colAttributeRow )):
             totalOfRowsInLastNSecsN += newPrice * newQty
             cellValueTotalN = newPrice * newQty
             totalOfQtyInLastNSecsN += newQty
             cellValueQtyN = newQty
             cellValueTotalX = 0
             cellValueQtyX = 0
             
         elif ( currentMsgCode == 'X' and ( newPrice in colAttributeRow )):
             totalOfRowsInLastNSecsX += newPrice * newQty
             cellValueTotalX = newPrice * newQty
             totalOfQtyInLastNSecsX += newQty
             cellValueQtyX = newQty
             cellValueTotalN = 0
             cellValueQtyN = 0

         else:
             cellValueTotalN = 0
             cellValueQtyN = 0
             cellValueTotalX = 0
             cellValueQtyX = 0
             
         maxQty = max(totalOfQtyInLastNSecsN, totalOfQtyInLastNSecsX)
         newSide = (float(totalOfRowsInLastNSecsN + (maxQty - totalOfQtyInLastNSecsN) * colAttributeRow[4]) / maxQty) if maxQty > 0 else currentPrice
         cancelSide = (float(totalOfRowsInLastNSecsX + (maxQty - totalOfQtyInLastNSecsX) * colAttributeRow[4]) / maxQty) if maxQty > 0 else currentPrice
         attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][0] = common.convertTimeStampFromStringToDecimal(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfTimeStamp],args.cType)
         attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][1] = (float(newSide) / cancelSide) # in 1st iteration currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
         attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][2] = str(currentPrice) + ";" + str(cellValueQtyN) + ";" + str(cellValueQtyX) + ";" + str(cellValueTotalN) + ";" + str(cellValueTotalX) + ";" + str(currentMsgCode) + ";" + str(timeElapsed)
         queueOfValuesInLastNSecs.append([cellValueTotalN,timeOfCurrentRow,cellValueQtyN,cellValueTotalX,cellValueQtyX])
         numberOfRowsInLastNSecs += 1   # Every append gets a +1 
         currentRowNumberForWhichFeatureValueIsBeingCalculated += 1
         continue     # Since we are going back 1 row from current we cannot get data from current row
      else:
         # We need to reset the timeOfOldestRow since timeElapsed has exceeded N seconds
         while(timeElapsed >= N):
            if(len(queueOfValuesInLastNSecs) == 0):
               timeOfOldestRow = timeOfCurrentRow
               timeElapsed = 0
               if(numberOfRowsInLastNSecs != 0):
                  print "Sanity check: This condition is not possible logically. numberOfRowsInLastNSecs should been 0. There has been an unknown error"
                  sys.exit(-1)
               if(totalOfRowsInLastNSecs != 0):
                  print "Sanity check: This condition is not possible logically. totalOfRowsInLastNSecs should have been 0. There has been an unknown error"
                  sys.exit(-1)   
            else:   
               oldestElementInQueue = queueOfValuesInLastNSecs.popleft()
               colValueInOldestElementInQueueN = oldestElementInQueue[0]
               colQtyInOldestElementInQueueN = oldestElementInQueue[2]
               colValueInOldestElementInQueueX = oldestElementInQueue[3]
               colQtyInOldestElementInQueueX = oldestElementInQueue[4]
               totalOfRowsInLastNSecsN -= colValueInOldestElementInQueueN
               totalOfQtyInLastNSecsN -= colQtyInOldestElementInQueueN
               totalOfRowsInLastNSecsX -= colValueInOldestElementInQueueX
               totalOfQtyInLastNSecsX -= colQtyInOldestElementInQueueX

               if len(queueOfValuesInLastNSecs) == 0:
                    timeElapsed = 0
                    timeOfOldestRow = timeOfCurrentRow
               else:
                    timeOfOldestRow = queueOfValuesInLastNSecs[0][1]
               numberOfRowsInLastNSecs -= 1 # every pop from the queue gets a -1
               timeElapsed = timeOfCurrentRow - timeOfOldestRow
               if(len(queueOfValuesInLastNSecs) != numberOfRowsInLastNSecs):
                  print "Sanity check: This condition is not possible logically. There has been an unknown error"
                  sys.exit(-1)
 
      print "Processed row number " + str(currentRowNumberForWhichFeatureValueIsBeingCalculated)
   
   lNameOfFeaturePrinted = "fMarketTradedQtySumOfCol" + args.c + "InLast" + str(args.n) + "Secs"
   return [ "TimeStamp", lNameOfFeaturePrinted , args.c , "QtyForN" , "QtyForX", "AmountN" , "AmountX", "MsgCode" , "TimeElapsed"]
Exemple #36
0
def extractAttributeFromDataMatrix(args):
    if args.n == None:
        N = 5
    else:
        N = float(args.n) 
    try:
        args.c
    except:
        print "Since -c has not been specified I cannot proceed"
        os._exit()
    colNumberOfLTP = colNumberOfData.NiftyLTP
    colNumberOfTTQ = colNumberOfData.NiftyTTQ
    
    colNumberOfTimeStamp = colNumberOfData.TimeStamp
    colNumberOfExchangeStamp = colNumberOfData.ExchangeTS
    numberOfRowsInLastNSecs = 0
    queueOfValuesInLastNSecs = deque()
    totalOfRowsInLastNSecs = 0.0
    timeOfOldestRow = common.convertTimeStampFromStringToFloat(dataFile.matrix[0][colNumberOfExchangeStamp],"synthetic")
    currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
    l_sum_LtQ = 0
    prevTTQ = 0
    lengthOfDataMatrix = len(dataFile.matrix)
    l_prev_feature_val = 0
    print "lengthOfDataMatrix",lengthOfDataMatrix
    while (currentRowNumberForWhichFeatureValueIsBeingCalculated < lengthOfDataMatrix):
        timeOfCurrentRow = common.convertTimeStampFromStringToFloat(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfExchangeStamp],"synthetic")
        timeElapsed = timeOfCurrentRow - timeOfOldestRow
        TtqOfCurrentRow = int(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfTTQ])
        LtpCurrentRow = float(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfLTP])
        if (timeElapsed < N):
            l_diff_TTQ = TtqOfCurrentRow-prevTTQ
            cellValue = LtpCurrentRow * l_diff_TTQ
            totalOfRowsInLastNSecs += cellValue
            l_sum_LtQ += l_diff_TTQ
            attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][0] = common.convertTimeStampFromStringToDecimal(dataFile.matrix[currentRowNumberForWhichFeatureValueIsBeingCalculated][colNumberOfTimeStamp])
            if l_sum_LtQ != 0:
                attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][1] = totalOfRowsInLastNSecs/(l_sum_LtQ) # in 1st iteration currentRowNumberForWhichFeatureValueIsBeingCalculated = 0
                l_prev_feature_val =  totalOfRowsInLastNSecs/(l_sum_LtQ)
            else:
                attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][1] = l_prev_feature_val
            attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][2] = str(totalOfRowsInLastNSecs)
            attribute.aList[currentRowNumberForWhichFeatureValueIsBeingCalculated][3] = str(l_sum_LtQ) + ";" + str(TtqOfCurrentRow) + ";"+str(LtpCurrentRow) + ";" + str(timeElapsed) + ";" + str(timeOfCurrentRow)
            queueOfValuesInLastNSecs.append([l_diff_TTQ,cellValue,timeOfCurrentRow])
            prevTTQ = TtqOfCurrentRow
            numberOfRowsInLastNSecs +=1
            currentRowNumberForWhichFeatureValueIsBeingCalculated += 1
            continue     # Since we are going back 1 row from current we cannot get data from current row
        else:
            # We need to reset the timeOfOldestRow since timeElapsed has exceeded N seconds
            while(timeElapsed >= N):
                if(len(queueOfValuesInLastNSecs) == 0):
                    timeOfOldestRow = timeOfCurrentRow
                    timeElapsed = 0
                    if(numberOfRowsInLastNSecs != 0):
                        print "Sanity check: This condition is not possible logically. numberOfRowsInLastNSecs should been 0. There has been an unknown error"
                        sys.exit(-1)
                    if(totalOfRowsInLastNSecs != 0):
                        print "Sanity check: This condition is not possible logically. totalOfRowsInLastNSecs should have been 0. There has been an unknown error"
                        sys.exit(-1)   
                else:   
                    oldestElementInQueue = queueOfValuesInLastNSecs.popleft()
                    LtqValueInOldRow =  oldestElementInQueue[0]
                    colValueInOldestElementInQueue = oldestElementInQueue[1]
                    totalOfRowsInLastNSecs -= colValueInOldestElementInQueue
                    l_sum_LtQ -= LtqValueInOldRow
                    if len(queueOfValuesInLastNSecs) == 0:
                        timeElapsed = 0
                        timeOfOldestRow = timeOfCurrentRow
                    else:
                        timeOfOldestRow = queueOfValuesInLastNSecs[0][2]
                    numberOfRowsInLastNSecs -= 1 # every pop from the queue gets a -1
                    timeElapsed = timeOfCurrentRow - timeOfOldestRow
                    if(len(queueOfValuesInLastNSecs) != numberOfRowsInLastNSecs):
                        print "Sanity check: This condition is not possible logically. There has been an unknown error"
                        sys.exit(-1)
        
        print "Processed row number " + str(currentRowNumberForWhichFeatureValueIsBeingCalculated)
    
    lNameOfFeaturePrinted = "fWALTPInLast" + str(args.n) + "Secs"
    return [ "TimeStamp", lNameOfFeaturePrinted , "totalValueOfRowsInLastNSecs" , "LtqSum" ,"TTQ", "LTP", "TimeElapsed" ,"ExTimeStamp"]
Exemple #37
0
def main():

    dirName = args.pd.replace('/ro/', '/rs/')
    tradeResultMainDirName = dirName + "/r/"
    if not os.path.exists(tradeResultMainDirName):
        os.mkdir(tradeResultMainDirName)
    tradeResultSubDirectoryName = tradeResultMainDirName + mainExperimentName + "/"
    if not os.path.exists(tradeResultSubDirectoryName):
        os.mkdir(tradeResultSubDirectoryName)
    outputFileName = tradeResultSubDirectoryName + experimentName + initialFileName + ".result"

    #if os.path.isfile(outputFileName):
    #    print("The results file already exisits. Delete it to run the program again" + outputFileName)
    #    return 1
    dataFile.getDataIntoMatrix(args.pd)
    predictedValuesDict = dict()
    getPredictedValuesIntoDict(predictedValuesDict)
    enterTrade = 0
    ttqAtTimeOfPreviousDataRow = 0
    askP0AtTimeOfPreviousDataRow = 0
    bidP0AtTimeOfPreviousDataRow = 0
    tradeStats = dict()
    tradeStats['totalSellValue'] = 0
    tradeStats['totalBuyValue'] = 0
    tradeStats['currentPosition'] = 0
    noPredictionForThisRow = 0
    currentPredictedValue = 0
    entryCL = float(args.entryCL) / 100
    exitCL = float(args.exitCL) / 100
    numberOfTimesAskedToEnterTrade = 0
    numberOfTimesAskedToExitTrade = 0
    reasonForTrade = dict()
    reasonForTrade['LTPDoesNotEqualBidP0'] = 0
    reasonForTrade['VolumeDidNotIncreaseSufficientlyDuringBuyAttempt'] = 0
    reasonForTrade['AssumingBuyTradeHappened'] = 0
    reasonForTrade['LTPDoesNotEqualAskP0'] = 0
    reasonForTrade['VolumeDidNotIncreaseSufficientlyDuringSellAttempt'] = 0
    reasonForTrade['AssumingSellTradeHappened'] = 0
    varsAtTimeOfTradeDecision = dict()
    varsAtTimeOfTradeDecision['decision'] = 0
    varsAtTimeOfTradeDecision['bidP0'] = 0
    varsAtTimeOfTradeDecision['qtyTradedAtBidP0'] = 0
    varsAtTimeOfTradeDecision['ttq'] = 0
    varsAtTimeOfTradeDecision['askP0'] = 0
    varsAtTimeOfTradeDecision['qtyTradedAtAskP0'] = 0

    print("Processing the data file for trades :")

    for currentDataRow in dataFile.matrix:  #currentDataRow has the new tick
        if (enterTrade == varsAtTimeOfTradeDecision['decision']
                and enterTrade == 1 and varsAtTimeOfTradeDecision['bidP0'] >=
                float(currentDataRow[colNumberOfData.BidP0])):
            print("Position not lost in the queue")
            if (varsAtTimeOfTradeDecision['bidP0'] == float(
                    currentDataRow[colNumberOfData.LTP])
                    and float(currentDataRow[colNumberOfData.TTQ]) >
                    varsAtTimeOfTradeDecision['ttq']):
                varsAtTimeOfTradeDecision['qtyTradedAtBidP0'] += float(
                    currentDataRow[colNumberOfData.
                                   TTQ]) - varsAtTimeOfTradeDecision['ttq']
        elif (enterTrade == varsAtTimeOfTradeDecision['decision']
              and enterTrade == -1 and varsAtTimeOfTradeDecision['askP0'] <=
              float(currentDataRow[colNumberOfData.AskP0])):
            print("Position not lost in the queue")
            if (varsAtTimeOfTradeDecision['askP0'] == float(
                    currentDataRow[colNumberOfData.LTP])
                    and float(currentDataRow[colNumberOfData.TTQ]) >
                    varsAtTimeOfTradeDecision['ttq']):
                varsAtTimeOfTradeDecision['qtyTradedAtAskP0'] += float(
                    currentDataRow[colNumberOfData.
                                   TTQ]) - varsAtTimeOfTradeDecision['ttq']
        else:
            positionLostInQueue = True

        checkIfPreviousDecisionToEnterOrExitTradeWasSuccessful(
            currentDataRow, varsAtTimeOfTradeDecision, enterTrade, tradeStats,
            reasonForTrade)

        currentTimeStamp = common.convertTimeStampFromStringToFloat(
            currentDataRow[colNumberOfData.TimeStamp])

        try:
            currentPredictedValue = float(
                predictedValuesDict[currentTimeStamp])
        except:
            noPredictionForThisRow += 1

        if (currentPredictedValue > entryCL):
            enterTrade = 1
            numberOfTimesAskedToEnterTrade += 1
        elif (currentPredictedValue < exitCL
              and tradeStats['currentPosition'] > 0):
            numberOfTimesAskedToExitTrade += 1
            enterTrade = -1  # Implies to exit the trade
        else:
            enterTrade = 0  # Implies make no change

        if (positionLostInQueue == True):
            varsAtTimeOfTradeDecision = updateVarsAtTimeOfTradeDecision(
                currentDataRow, enterTrade
            )  # This implies that the position in the queue is lost.

    outputFile = open(outputFileName, "w")
    print("Starting to write: " + outputFileName)
    print("The net results are: " +
          str(tradeStats['totalSellValue'] - tradeStats['totalBuyValue']),
          file=outputFile)
    print("Number of rows for which there is no prediction: " +
          str(noPredictionForThisRow),
          file=outputFile)
    print("Number of times asked to enter trade: " +
          str(numberOfTimesAskedToEnterTrade),
          file=outputFile)
    print("Number of times asked to exit trade: " +
          str(numberOfTimesAskedToExitTrade),
          file=outputFile)
    print(
        "Assumed buy trade did not happen since volume did not increase: " +
        str(reasonForTrade['VolumeDidNotIncreaseSufficientlyDuringBuyAttempt']
            ),
        file=outputFile)
    print("Assumed buy trade did not happen since bidP0 not same as LTP: " +
          str(reasonForTrade['LTPDoesNotEqualBidP0']),
          file=outputFile)
    print("Assumed buy trade happened: " +
          str(reasonForTrade['AssumingBuyTradeHappened']),
          file=outputFile)
    print(
        "Assumed sell trade did not happen since volume did not increase: " +
        str(reasonForTrade['VolumeDidNotIncreaseSufficientlyDuringSellAttempt']
            ),
        file=outputFile)
    print("Assumed sell trade did not happen since bidP0 not same as LTP: " +
          str(reasonForTrade['LTPDoesNotEqualAskP0']),
          file=outputFile)
    print("Assumed sell trade happened: " +
          str(reasonForTrade['AssumingSellTradeHappened']),
          file=outputFile)
    print("The total sell value is: " + str(tradeStats['totalSellValue']),
          file=outputFile)
    print("The total buy value is: " + str(tradeStats['totalBuyValue']),
          file=outputFile)
    try:
        averageSellPrice = tradeStats['totalSellValue'] / reasonForTrade[
            'AssumingSellTradeHappened']
        averageBuyPrice = tradeStats['totalBuyValue'] / reasonForTrade[
            'AssumingBuyTradeHappened']
    except:
        averageSellPrice = 0
        averageBuyPrice = 0
    print("Average sell price per unit is: " + str(averageSellPrice),
          file=outputFile)
    print("Average buy price per unit is: " + str(averageBuyPrice),
          file=outputFile)
    print("The current position: " + str(tradeStats['currentPosition']),
          file=outputFile)
    print("Profit or loss per Qty traded is: " +
          str(averageSellPrice - averageBuyPrice),
          file=outputFile)
    pLPerLot = (averageSellPrice - averageBuyPrice) * 1000
    print("1 lot has 1000 qty's so P/L per lot is: " + str(pLPerLot),
          file=outputFile)
    print("P/L for trading 10 lots is: " + str(pLPerLot * 10), file=outputFile)