コード例 #1
0
ファイル: PlayGround.py プロジェクト: yokotate/bitcoin
 def __init__(self):
     self.name = os.path.splitext(os.path.basename(__file__))[0]
     # 0 何もしない
     # 1 購入
     # 2 売却
     self.Action = [0, 1, 2]
     self.enable_actions = np.arange(3)
     self.env = DA.DataAccess()
     self.reset()
コード例 #2
0
 def __init__(self, listOfStocks):
     self.listOfStocks= listOfStocks
     self.DAY=86400
     dataItemsList=list()
     dataItemsList.append("alphaValue")
     self.alphaData= DataAccess.DataAccess(False, "AAPLonlybollingerBandsAlphaVals.h5", "/alphaData", "alphaData", True, listOfStocks, None, None, None, dataItemsList)
     print "Timestamps are: " 
     for ts in self.alphaData.timestamps:
         print ts
コード例 #3
0
def check_user(user, password):
    db = DataAccess()
    if len(user) == 0 or len(password) == 0:
        return jsonify("Bad request"), 400
    result = db.check_user(user, password)
    if not result:
        return jsonify("Authorization failed"), 401
    elif result:
        resp = make_response(jsonify(result["guid"]), 200)
        return resp
コード例 #4
0
 def __init__(self, listOfStocks):
     self.listOfStocks = listOfStocks
     self.DAY = 86400
     dataItemsList = list()
     dataItemsList.append("alphaValue")
     self.alphaData = DataAccess.DataAccess(
         False, "curveFittingAlphaVals_Jan_85_to_2010.h5", "/alphaData",
         "alphaData", True, listOfStocks, None, None, None, dataItemsList)
     print("Timestamps are: ")
     for ts in self.alphaData.timestamps:
         print(ts)
コード例 #5
0
    def InsBTC_VALUE(self):
        res = requests.get('https://coincheck.com/api/ticker')
        jsonData = res.json()
        # last 直近売買
        # bid 買値
        # ask 売値
        # high  24時間での最高取引価格
        # low  24時間での最安取引価格
        # volume 24時間での取引量
        last = jsonData["last"]
        bid = jsonData["bid"]
        ask = jsonData["ask"]
        high = jsonData["high"]
        low = jsonData["low"]
        volume = jsonData["volume"]

        # 取得したデータを保存用プログラムに投げる
        env = da.DataAccess()
        env.InsertToBTC_VALUE(last, bid, ask, high, low, volume)
コード例 #6
0
#readStocksFromFile done


#Main begins
noOfDaysToUse = 5
daysAhead = 5
folderList = list()
folderList.append("C:\\tempoutput\\")
listOfStocks = getStocks(folderList)

beginTS = 473490000  #2 Jan 1985 0500 hrs GMT
endTS = 1262235600  #31 DEc 2009 0500 hrs GMT

print "list of stocks is: " + str(listOfStocks)
dataAccess = DataAccess.DataAccess(True, folderList, "/StrategyData",
                                   "StrategyData", True, listOfStocks, beginTS,
                                   endTS)
timestamps = list(dataAccess.getTimestampArray())

print "Printing all timestamps: "
for ts in timestamps:
    print ts
print "Printing ts done"

#alpha= alphaGenerator.AlphaDataModel.AlphaDataModelClass()
adm.openFile("curveFittingAlphaVals_Jan_85_to_2010.h5")
daysArray = numpy.zeros((noOfDaysToUse + 1), dtype=float)

ctr = 0
while (ctr <= noOfDaysToUse):  #because we get back noOfDaysToUse+1 rows
    daysArray[ctr] = ctr
コード例 #7
0
def main():
    '''
    @summary: Calculates Bollinger bands
    '''

    folderList = list()
    folderList.append("C:\\tempoutput\\")
    listOfStocks = list()
    #    listOfStocks.append("ACY")
    listOfStocks.append("AAPL")
    #listOfStocks= getStocks(folderList)

    dataAccess = DataAccess.DataAccess(True, folderList, "/StrategyData",
                                       "StrategyData", True, listOfStocks)
    timestamps = list(dataAccess.getTimestampArray())
    adm.openFile("AAPLonlybollingerBandsAlphaVals.h5")

    period = 10
    stdMultiplier = 2
    noOfDays = len(timestamps)  #400

    centerband = numpy.zeros(
        noOfDays, dtype=float
    )  #len(timestamps)- period + 1 #Just to make it the same length as the adj_close to make it easier to plot
    upperBand = numpy.zeros(noOfDays, dtype=float)
    lowerBand = numpy.zeros(noOfDays, dtype=float)
    x = numpy.zeros(noOfDays, dtype=float)

    ctr = 0
    while (ctr < noOfDays):
        x[ctr] = ctr
        ctr += 1
        #end while

    for stock in listOfStocks:
        print "Processing: " + str(stock)
        #adj_close= dataAccess.getStockDataList(str(stock), 'adj_close')
        adj_close = dataAccess.getStockDataList(stock, 'adj_close',
                                                timestamps[0],
                                                timestamps[noOfDays - 1])

        adj_close = removeNaNs(adj_close)  #nan's removed, unless all are nans

        #Now calculating bollinger bands
        for ctr in range(period, noOfDays):

            try:
                centerband[ctr] = numpy.average(adj_close[ctr - period:ctr])
                stdDev = numpy.std(adj_close[ctr - period:ctr])
                upperBand[ctr] = centerband[ctr] + (stdMultiplier * stdDev)
                lowerBand[ctr] = centerband[ctr] - (stdMultiplier * stdDev)
            except IndexError:
                print "ctr is: " + str(ctr)

        #writing alpha values to file
        for ctr in range(0, noOfDays):
            if (upperBand[ctr] == lowerBand[ctr]) or (adj_close[ctr]
                                                      == centerband[ctr]):
                adm.addRow(str(stock), "blah", 0.0, timestamps[ctr])
            elif (adj_close[ctr] < centerband[ctr]):
                alphaValue = lowerBand[ctr] / adj_close[ctr]
                adm.addRow(str(stock), "blah", alphaValue, timestamps[ctr])
            else:
                alphaValue = -adj_close[ctr] / upperBand[ctr]
                adm.addRow(str(stock), "blah", alphaValue, timestamps[ctr])
            #done writing alpha values of this stock to file

            #calculating bollinger bands done!

#        fig = Figure()
#        canvas = FigureCanvas(fig)
#        ax = fig.add_subplot(111)
#        ax.plot(centerband)
#        ax.plot (lowerBand)
#        ax.plot (upperBand)
#        ax.plot (adj_close)
#
#        ax.set_title(str(stock)+' Bollinger bands')
#        ax.grid(True)
#        ax.set_xlabel('time')
#        ax.set_ylabel('')
#        canvas.print_figure(str(listOfStocks.index(stock)))
#for stock in listOfStocks: done

    adm.closeFile()
コード例 #8
0
def ckeck_token():
    token = request.headers.get("Authorization")
    db = DataAccess()
    sessions = db.get_all_sessions()
    g.isAdmin = token in sessions
コード例 #9
0
def add_location_type():
    ensure_admin()
    location_type = json.loads(request.stream.read())
    loc_DB = DataAccess()
    loc_handler = LocationHandler(loc_DB)
    return jsonify(loc_handler.add_location_type(location_type))
コード例 #10
0
def delete_location_type():
    ensure_admin()
    data = json.loads(request.stream.read())
    db = DataAccess()
    loc_info_handler = LocationHandler(db)
    return jsonify(loc_info_handler.delete_location_type(data))
コード例 #11
0
def get_location_types():
    db = DataAccess()
    loc_info_handler = LocationHandler(db)
    return jsonify(loc_info_handler.get_all_locations_types())
コード例 #12
0
def get_all_event_types():
    db = DataAccess()
    return jsonify(db.get_all_event_types())
コード例 #13
0
def get_all_image_types():
    db = DataAccess()
    return jsonify(db.get_all_image_types())
コード例 #14
0
ファイル: train.py プロジェクト: yokotate/bitcoin
logger.addHandler(sh)
formatter = logging.Formatter(
    '%(asctime)s:%(lineno)d:%(levelname)s:%(message)s')
fh.setFormatter(formatter)
sh.setFormatter(formatter)

if __name__ == "__main__":
    # トレーニング回数
    n_epochs = 10000
    # 受け渡しデータ数
    DataNum = 14
    # トレーニングデータ数(現在未使用)
    # TestDataNum = 200

    pgenv = pg.PlayGround()
    daenv = da.DataAccess()
    data = daenv.SelectBTC_VALUE()
    agent = DQNAgent(pgenv.enable_actions, pgenv.name, DataNum)

    averagelist = [10000]

    # 決められた回数実行する
    for e in range(n_epochs):
        # トレーニングデータの作成
        daenv.TrainDataSet()
        # 初期化
        pgenv.reset()
        i = 0
        count = 0
        length = len(data)
        # 初期金額を保存
コード例 #15
0
def getRandPort( lNum, dtStart=None, dtEnd=None, lsStocks=None,\
 dmPrice=None, dmVolume=None, bFilter=True, fNonNan=0.95,\
 fPriceVolume=100*1000, lSeed=None ):
    """
    @summary Returns a random portfolio based on certain criteria.
    @param lNum: Number of stocks to be included
    @param dtStart: Start date for portfolio
    @param dtEnd: End date for portfolio
    @param lsStocks: Optional list of ticker symbols, if not provided all symbols will be used
    @param bFilter: If False, stocks are not filtered by price or volume data, simply return random Portfolio.
    @param dmPrice: Optional price data, if not provided, data access will be queried
    @param dmVolume: Optional volume data, if not provided, data access will be queried
    @param fNonNan: Optional non-nan percent for filter, default is .95
    @param fPriceVolume: Optional price*volume for filter, default is 100,000
    @warning: Does not work for all sets of optional inputs, e.g. if you don't include dtStart, dtEnd, you need 
              to include dmPrice/dmVolume
    @return list of stocks which meet the criteria
    """

    if (lsStocks is None):
        if (dmPrice is None and dmVolume is None):
            norObj = da.DataAccess('Norgate')
            lsStocks = norObj.get_all_symbols()
        elif (not dmPrice is None):
            lsStocks = list(dmPrice.columns)
        else:
            lsStocks = list(dmVolume.columns)

    if (dmPrice is None and dmVolume is None and bFilter == True):
        norObj = da.DataAccess('Norgate')
        ldtTimestamps = du.getNYSEdays(dtStart, dtEnd, dt.timedelta(hours=16))

    # if dmPrice and dmVol are provided then we don't query it every time """
    bPullPrice = False
    bPullVol = False
    if (dmPrice is None):
        bPullPrice = True
    if (dmVolume is None):
        bPullVol = True

    # Default seed (none) uses system clock """
    rand.seed(lSeed)
    lsRetStocks = []

    # Loop until we have enough randomly selected stocks """
    llRemainingIndexes = list(range(0, len(lsStocks)))
    lsValid = None
    while (len(lsRetStocks) != lNum):

        lsCheckStocks = []
        for i in range(lNum - len(lsRetStocks)):
            lRemaining = len(llRemainingIndexes)
            if (lRemaining == 0):
                print('Error in getRandPort: ran out of stocks')
                return lsRetStocks

            # Pick a stock and remove it from the list of remaining stocks """
            lPicked = rand.randint(0, lRemaining - 1)
            lsCheckStocks.append(lsStocks[llRemainingIndexes.pop(lPicked)])

        # If bFilter is false"""
        # simply return our first list of stocks, don't check prive/vol """
        if (not bFilter):
            return sorted(lsCheckStocks)

        # Get data if needed """
        if (bPullPrice):
            dmPrice = norObj.get_data(ldtTimestamps, lsCheckStocks, 'close')

        # Get data if needed """
        if (bPullVol):
            dmVolume = norObj.get_data(ldtTimestamps, lsCheckStocks, 'volume')

        # Only query this once if data is provided"""
        # else query every time with new data """
        if (lsValid is None or bPullVol or bPullPrice):
            lsValid = stockFilter(dmPrice, dmVolume, fNonNan, fPriceVolume)

        for sAdd in lsValid:
            if sAdd in lsCheckStocks:
                lsRetStocks.append(sAdd)

    return sorted(lsRetStocks)