def order(self, numShares): #averages out buyPrice if numShares > 0: self.buyPrice = (self.buyPrice * self.numShares + float(data.get_last_trade_price(self.ticker)) * numShares) / (self.numShares + numShares) if self.numShares + numShares < 0: numShares = -self.numShares variables.balance = variables.balance - numShares * float(data.get_last_trade_price(self.ticker)) - variables.tradePrice self.numShares = self.numShares + numShares if self.numShares == 0 and self.inFolio() == True: variables.portfolio.remove(self) if self.numShares > 0 and self.inFolio() == False: variables.portfolio.append(self) if numShares > 0: print("BUYSTOCK %s %s:") % (variables.day, time.strftime("%H:%M")) elif numShares < 0: print("SELLSTOCK %s %s:") % (variables.day, time.strftime("%H:%M")) self.printQuote() if variables.SENDMAIL: self.emailQuote()
def order(self, numShares): #averages out buyPrice if numShares > 0: self.buyPrice = (self.buyPrice * self.numShares + float(data.get_last_trade_price(self.ticker)) * numShares) / (self.numShares + numShares) if self.numShares + numShares < 0: numShares = -self.numShares variables.balance = variables.balance - numShares * float( data.get_last_trade_price(self.ticker)) - variables.tradePrice self.numShares = self.numShares + numShares if self.numShares == 0 and self.inFolio() == True: variables.portfolio.remove(self) if self.numShares > 0 and self.inFolio() == False: variables.portfolio.append(self) if numShares > 0: print("BUYSTOCK %s %s:") % (variables.day, time.strftime("%H:%M")) elif numShares < 0: print("SELLSTOCK %s %s:") % (variables.day, time.strftime("%H:%M")) self.printQuote() if variables.SENDMAIL: self.emailQuote()
def sellStock(order): #conditions for selling a stock #total margin you are willing to lose on an investment lossMargin = float(0.10) #selling conditions if (variables.forceSell or float(data.get_last_trade_price( order.ticker)) > data.get_50_sma(order.ticker) or float(data.get_last_trade_price(order.ticker)) < float(order.buyPrice) * float(1 - lossMargin)): order.order(-order.numShares)
def buyStock(order): #conditions for buying a stock from the initial finviz criteria #total amt of balance you are willing to invest in one stock maxMargin = float(0.10) #cap on no. of investments in your portfolio portfolioCap = int(10) #cap on volume volumeCap = int(500000) #buying conditions if ( variables.forceBuy or order.inFolio() == False and len(variables.portfolio) < portfolioCap ): #calculations needed to buy stock maxPay = float(variables.balance * maxMargin) numShares = int(maxPay / float(data.get_last_trade_price(order.ticker))) if numShares > int(data.get_volume(order.ticker)): numShares = int(data.get_volume(order.ticker)) #proceeds to buy after conditions and calculations order.order(numShares)
def sellStock(order): #conditions for selling a stock #total margin you are willing to lose on an investment lossMargin = float(0.05) #selling conditions if (variables.forceSell or float(data.get_last_trade_price( order.ticker)) > float(data.get_50_sma(order.ticker)) or float(data.get_last_trade_price(order.ticker)) < float(order.buyPrice) * float(1 - lossMargin)): #calculations needed to sell stock #proceeds to buy after conditions and calculations order.order(-order.numShares)
def emailQuote(self): msg = ''' Ticker: %s price: %s numShares: %s BALANCE: %s ''' % (self.ticker, data.get_last_trade_price(self.ticker), self.numShares, variables.balance) sendMail.sendMail(msg)
def emailQuote(self): msg = ''' Ticker: %s price: %s numShares: %s BALANCE: %s ''' % (self.ticker, data.get_last_trade_price( self.ticker), self.numShares, variables.balance) sendMail.sendMail(msg)
def sellStock(order): #conditions for selling a stock #total margin you are willing to lose on an investment lossMargin = float(0.05) #selling conditions if ( variables.forceSell or float(data.get_last_trade_price(order.ticker)) > float(data.get_50_sma(order.ticker)) or float(data.get_last_trade_price(order.ticker)) < float(order.buyPrice) * float(1 - lossMargin) ): #calculations needed to sell stock #proceeds to buy after conditions and calculations order.order(-order.numShares)
def sellStock(order): #conditions for selling a stock #total margin you are willing to lose on an investment lossMargin = float(0.10) #selling conditions if ( variables.forceSell or float(data.get_last_trade_price(order.ticker)) > float(data.get_200_sma(order.ticker)) ): #print(data.get_last_trade_price(order.ticker)) #print(data.get_200_sma(order.ticker)) order.order(-order.numShares)
def printQuote(self): print("Ticker: %s" % (self.ticker)) print("price: %s" % (data.get_last_trade_price(self.ticker))) print("numShares: %s " % (self.numShares)) print ""