def load(self): """If the config file exists, try to load it""" if (os.path.exists(self._filename)): cfgp = getConfigParser(self._filename) for input in self._inputs: input.readValue(cfgp)
def load(self): """If the config file exists, try to load it""" if(os.path.exists(self._filename)): cfgp = getConfigParser(self._filename) for input in self._inputs: input.readValue(cfgp)
def selectStocks(year): parser = getConfigParser() BASED_DIR = parser.get('happy', 'based_dir') SOURCE_LOCATION = BASED_DIR + parser.get('happy', 'source_location') SELECTED_STOCK_LOCATION = BASED_DIR + parser.get( 'happy', 'selected_stock_location') NUMBER_OF_DAYS = int(parser.get('happy', 'active_days')) MIN_VOLUME = int(parser.get('happy', 'min_volume')) MIN_OPEN_PRICE = int(parser.get('happy', 'min_open_price')) MAX_OPEN_PRICE = int(parser.get('happy', 'max_open_price')) if len(getCsvFiles(SELECTED_STOCK_LOCATION)) > 0: sh.rm(glob.glob(SELECTED_STOCK_LOCATION + "*")) ic("Started selecting stocks...") csvFiles = getCsvFiles(SOURCE_LOCATION) # csvFiles = ["AAA.csv","APG.csv"] selected = [] for file in csvFiles: df = readFile((SOURCE_LOCATION + "{}").format(file)) df = df.loc[year] if (len(df) > NUMBER_OF_DAYS) and df.Volume.mean() > MIN_VOLUME and ( df.Open.mean() > MIN_OPEN_PRICE and df.Open.mean() < MAX_OPEN_PRICE): df["Short"] = False df["Long"] = False df["Categories"] = "" df["Recommendation"] = "" df["Action"] = "" df.to_csv((SELECTED_STOCK_LOCATION + "{}").format(file)) ic("Completed selecting stocks!")
def isOverProfit(df, position, stock, portfolio): c = df.iloc[position] if stock in portfolio.index: parser = getConfigParser() overcomeProfitRate = float(parser.get('happy', 'over_profit_rate')) boughtPrice = portfolio.loc[stock].Price profitThreshold = boughtPrice * (1 + overcomeProfitRate); # ic("---isOverProfit {} {} {} {}".format(boughtPrice, profitThreshold, c.Open, c.Close)) if min(c.Open, c.Close) < profitThreshold and profitThreshold < max(c.Open, c.Close): return True; if isDoji(df, position) and min(c.Open, c.Close) > profitThreshold: return True; return False;
def isCutLoss(df, position, stock, portfolio): c = df.iloc[position] if stock in portfolio.index: parser = getConfigParser() cutLossRate = float(parser.get('happy', 'cut_loss_rate')) boughtPrice = portfolio.loc[stock].Price cutLossThreshold = boughtPrice * (1 - cutLossRate) # ic("---isCutLoss {} {} {} {}".format(boughtPrice, cutLossThreshold, c.Open, c.Close)) if min(c.Open, c.Close) < cutLossThreshold and cutLossThreshold < max(c.Open, c.Close): return True; if isDoji(df, position) and max(c.Open, c.Close) < cutLossThreshold: return True; return False;
def getTab(filename, *args): """Return the correct type EditTab for the specified config file Argument filename -- path and name of a config file of a Varberg display args -- arguments to the EditTab constructor that are not used by getTab """ cfgp = getConfigParser(filename) if(cfgp.has_section('BottomArea')): return EditTab3(filename, *args) elif(cfgp.has_section('TopLeftText')): return EditTab2(filename, *args) else: return EditTab1(filename, *args)
def getTab(filename, *args): """Return the correct type EditTab for the specified config file Argument filename -- path and name of a config file of a Varberg display args -- arguments to the EditTab constructor that are not used by getTab """ cfgp = getConfigParser(filename) if (cfgp.has_section('BottomArea')): return EditTab3(filename, *args) elif (cfgp.has_section('TopLeftText')): return EditTab2(filename, *args) else: return EditTab1(filename, *args)
def updateRec(dailyDf, stock): parser = getConfigParser() BASED_DIR = parser.get('happy', 'based_dir') REC_FULL_LOCATION = BASED_DIR + parser.get('happy', 'rec_full_location') REC_ACTIONS_LOCATION = BASED_DIR + parser.get('happy', 'rec_actions_location') try: # ic("Update rec file for ", stock) df = readFile(REC_FULL_LOCATION + stock + "_rec.csv") df.loc[dailyDf.index[0]] = dailyDf.iloc[0] df.to_csv(REC_FULL_LOCATION + stock + "_rec.csv") df = df[df.Action.isin(["Sold", "Bought"])] df.to_csv(REC_ACTIONS_LOCATION + stock + "_rec_actions.csv") except Exception as ex: ic(ex) # ic("Create rec file for ", stock) dailyDf.to_csv(REC_FULL_LOCATION + stock + "_rec.csv") dailyDf = dailyDf[dailyDf.Action.isin(["Sold", "Bought"])] dailyDf.to_csv(REC_ACTIONS_LOCATION + stock + "_rec_actions.csv")
def main(): """Load the inifile and start the program.""" inidir = os.path.dirname(os.path.abspath(sys.argv[0])) download = False if(sys.argv[1].upper() == _displaynames[0]): unit = varberg1 ininame = _ininames[0] elif(sys.argv[1].upper() == _displaynames[1]): unit = varberg2 ininame = _ininames[1] elif(sys.argv[1].upper() == _displaynames[2]): unit = varberg3 ininame = _ininames[2] if(len(sys.argv) > 2): if(sys.argv[2].upper().startswith(_cache)): amount = sys.argv[2][len(_cache):] download = True elif(sys.argv[1].upper() == _displaynames[3]): unit = hylte4 ininame = _ininames[3] else: print('Please provide a valid display number') return inifile = os.path.join(inidir, ininame) parser = getConfigParser(inifile) #Workaround for unicode in exefiles if hasattr(sys,"setdefaultencoding"): sys.setdefaultencoding("utf-8") if(parser is not None): if(download): if(unit == varberg3): download3(inidir, parser, amount) else: controller = unit.Controller(inidir, parser) controller.gui.root.mainloop()
def main(): """Load the inifile and start the program.""" inidir = os.path.dirname(os.path.abspath(sys.argv[0])) download = False if (sys.argv[1].upper() == _displaynames[0]): unit = varberg1 ininame = _ininames[0] elif (sys.argv[1].upper() == _displaynames[1]): unit = varberg2 ininame = _ininames[1] elif (sys.argv[1].upper() == _displaynames[2]): unit = varberg3 ininame = _ininames[2] if (len(sys.argv) > 2): if (sys.argv[2].upper().startswith(_cache)): amount = sys.argv[2][len(_cache):] download = True elif (sys.argv[1].upper() == _displaynames[3]): unit = hylte4 ininame = _ininames[3] else: print('Please provide a valid display number') return inifile = os.path.join(inidir, ininame) parser = getConfigParser(inifile) #Workaround for unicode in exefiles if hasattr(sys, "setdefaultencoding"): sys.setdefaultencoding("utf-8") if (parser is not None): if (download): if (unit == varberg3): download3(inidir, parser, amount) else: controller = unit.Controller(inidir, parser) controller.gui.root.mainloop()
def analyzeAll(date, files, dailyReports, dailyDf, portfolio, investingMoney, investingAmount): for file in files: parser = getConfigParser() BASED_DIR = parser.get('happy', 'based_dir') SELECTED_STOCK_LOCATION = BASED_DIR + parser.get( 'happy', 'selected_stock_location') REPORT_LOCATION = BASED_DIR + parser.get('happy', 'report_location') df = readFile((SELECTED_STOCK_LOCATION + "{}").format(file)) df['Action'] = df.Action.astype(str) df['Categories'] = df.Categories.astype(str) df['Recommendation'] = df.Recommendation.astype(str) (dailyDf, portfolio, investingMoney, investingAmount) = analyzePattern(df, date, file[0:3], dailyDf, portfolio, investingMoney, investingAmount) df.to_csv(SELECTED_STOCK_LOCATION + "{}".format(file)) if len(dailyDf) > 0: finalDf = functools.reduce(lambda a, b: a.append(b), dailyDf) dailyReports = dailyReports.append(finalDf) dailyReports.to_csv(REPORT_LOCATION + "reports.csv", index=True) portfolio.to_csv(REPORT_LOCATION + "portfolio.csv", index=True) return (dailyReports, dailyDf, portfolio, investingMoney, investingAmount)
def getRate(): parser = getConfigParser() return int(parser.get('happy', 'candle_rates'))
from datetime import datetime, timedelta import common client = common.getTransmissionClient('cleanup') print('Running cleanup') torrents = client.get_torrents() delay = common.getConfigParser().getint('cleanup', 'delay') for torrent in torrents: if torrent.isFinished and not torrent.error and datetime.fromtimestamp( torrent.doneDate) + timedelta(days=delay) < datetime.now(): print('%s is finished and will be removed' % (torrent.name)) client.remove(torrent.hashString, delete_data=True)
def analyzePattern(df, date, stock, dailyDf, portfolio, investingMoney, investingAmount): if not df.loc[str(date)].empty: positions = df.index.get_loc(str(date)) if len(positions) > 0 and positions[0] < len(df) - 1: ic(date) parser = getConfigParser() MAX_VOLUME = int(parser.get('happy', 'max_volume')) TRADE_RATE = float(parser.get('happy', 'trade_rate')) TRADE_STRATEGY = parser.get('happy', 'trade_strategy') position = positions[0] df.Short.iloc[position] = df.iloc[position].Change < 1 df.Long.iloc[position] = df.iloc[position].Change >= 5 categorizeCandle(df, position) recommendDaily(df, position, stock, portfolio) # ic(df.loc[[df.index[position]]]) updateRec(df.loc[[df.index[position]]], stock) # Sold the stock on portfolio if (stock in portfolio.index) and "Sold" in df.iloc[position].Action: p = df.iloc[position + 1] stockVolume = portfolio.loc[stock].Volume # currentDate = datetime.strptime(date, '%Y-%m-%d') boughtDate = datetime.strptime(portfolio.loc[stock].Date, '%Y-%m-%d') if (date - boughtDate).days < 3: ic("Waiting...{} {} {}".format(date, portfolio.loc[stock].Date, (date - boughtDate).days)) df.Action.iloc[position] = df.Action.iloc[ position] + "| Will Sell as Waiting for T3" else: if "Cut Loss" in p.Recommendation: price = round(df.iloc[position].Close / 2 + df.iloc[position].Open / 2) ic("Sold as Cut Loss {}".format(date)) elif "Overcome Profit" in p.Recommendation: price = round(df.iloc[position].Close / 2 + df.iloc[position].Open / 2) ic("Sold as Overcome Profit {}".format(date)) else: price = getPrice(df.iloc[position], TRADE_STRATEGY) investingMoney = investingMoney - stockVolume * price investingAmount = investingAmount + stockVolume * price - getTradeFee( stockVolume * price, TRADE_RATE) profit = (price - portfolio.loc[stock].Price) * stockVolume report = { "ID": [str(date)[0:10] + "-" + stock], "Date": [str(date)[0:10]], "Stock": [stock], "Action": ["Sell"], "Volume": [stockVolume], "Price": [price], "Value": [stockVolume * price], "Profit": [profit], "investingMoney": [investingMoney], "investingAmount": [investingAmount] } stockReportDf = pd.DataFrame.from_dict(report) stockReportDf.set_index("ID", inplace=True) dailyDf.append(stockReportDf) # When allow multiple buys on one stock, the drop statement need to be updated: drop by ID instead of Stock portfolio.drop(stock, inplace=True) ic("Sold", stock, stockVolume, price, str(date)[0:10]) # Check stock existing on portfolio without Sold Recommendation: ## 1. Will SELL if overprofit ## 2. Will SELL if if (stock in portfolio.index) and ("Sold" not in df.iloc[position].Action): if "Cut Loss" in df.iloc[position].Action: ic("Sell as Cut Loss") if "Overcome Profit" in df.iloc[position].Action: ic("Sell as Overcome Profit") if "Bought" in df.iloc[position].Action and ( stock not in portfolio.index): # price = df.iloc[position].Open price = getPrice(df.iloc[position], TRADE_STRATEGY) stockVolume = getInvestingVolume(price, investingMoney, investingAmount, MAX_VOLUME) if stockVolume > 0: investingMoney = investingMoney + stockVolume * price investingAmount = investingAmount - stockVolume * price - getTradeFee( stockVolume * price, TRADE_RATE) report = { "ID": [str(date)[0:10] + "-" + stock], "Date": [str(date)[0:10]], "Stock": [stock], "Action": ["Buy"], "Volume": [stockVolume], "Price": [price], "Value": [stockVolume * price], "Profit": [0], "investingMoney": [investingMoney], "investingAmount": [investingAmount] } stockReportDf = pd.DataFrame.from_dict(report) stockReportDf.set_index("ID", inplace=True) dailyDf.append(stockReportDf) newStock = pd.DataFrame.from_dict({ "ID": [str(date)[0:10] + "-" + stock], "Date": [str(date)[0:10]], "Stock": [stock], "Price": [price], "Volume": [stockVolume], "Value": [stockVolume * price] }) newStock.set_index("Stock", inplace=True) portfolio = portfolio.append(newStock) ic("Bought", stock, stockVolume, price, str(date)[0:10]) return (dailyDf, portfolio, investingMoney, investingAmount)
import pandas as pd from common import getCsvFiles, readRawFile, readFile, getConfigParser, clearFileContent, clearReports, clearRecFolders from utils import getInvestingVolume, getTradeFee, selectStocks from analysis import analyzeAll from icecream import ic import logging from configparser import ConfigParser parser = getConfigParser() pd.options.mode.chained_assignment = None BASED_DIR = parser.get('happy', 'based_dir') SELECTED_STOCK_LOCATION = BASED_DIR + parser.get('happy', 'selected_stock_location') REPORT_LOCATION = BASED_DIR + parser.get('happy', 'report_location') LOG_FILENAME = BASED_DIR + parser.get('happy', 'log_filename') YEAR = parser.get('happy', 'year') logging.basicConfig(filename=LOG_FILENAME, level=logging.INFO) DATE_LIST = pd.date_range(start=parser.get('happy', 'start_date'),end=parser.get('happy', 'end_date')) totalMoney = 100000000 def simulateDaily(): investingMoney = 0 investingAmount = 300000000 for d in DATE_LIST: portfolio = pd.read_csv(REPORT_LOCATION + "portfolio.csv", index_col="Stock") dailyReports = pd.read_csv(REPORT_LOCATION + "reports.csv", index_col="ID") dailyDf = [] # (dailyReports, dailyDf, portfolio, investingMoney, investingAmount) = analyzeAll(d, getCsvFiles(SELECTED_STOCK_LOCATION), dailyReports, dailyDf, portfolio, investingMoney, investingAmount); # stockList = ['MBG.csv','HDC.csv','DRC.csv','TNG.csv','SBT.csv','APG.csv','CII.csv','AAA.csv','DXG.csv','PDR.csv','DPM.csv'] stockList = ['CII.csv']