在t2.micro上运行python的性能很差,我认为I/O操作需要越来越长的时间
因此,我在AmazonEC2(t2.micro)实例上运行了这个python脚本,我注意到性能非常差,我认为这可能是因为我在其中执行I/O,但它“非常差”。随着时间的推移,完成pollingPriceForTickerSymbol函数的时间越来越长;读取数据功能;平均分函数;LETSSEOWWEDID函数 我已经尝试过插入print语句来查看发生了什么,当我检查CPU使用率百分比时,它是99%。我想知道有没有更好的方法来编写这段代码?和/或指出我在这里做错了什么在t2.micro上运行python的性能很差,我认为I/O操作需要越来越长的时间,python,amazon-web-services,amazon-ec2,Python,Amazon Web Services,Amazon Ec2,因此,我在AmazonEC2(t2.micro)实例上运行了这个python脚本,我注意到性能非常差,我认为这可能是因为我在其中执行I/O,但它“非常差”。随着时间的推移,完成pollingPriceForTickerSymbol函数的时间越来越长;读取数据功能;平均分函数;LETSSEOWWEDID函数 我已经尝试过插入print语句来查看发生了什么,当我检查CPU使用率百分比时,它是99%。我想知道有没有更好的方法来编写这段代码?和/或指出我在这里做错了什么 from datetime im
from datetime import datetime
import smtplib
import time
import imaplib
import email
import time
import robin_stocks as r
import shutil
from datetime import timedelta
# -------------------------------------------------
#
#
# ------------------------------------------------
running = False
counter = 0
currOpen = 0.00
currHigh = 0.00
currLow = 0.00
currClose = 0.00
#shorterVal = 5 # for daily data seems to work fine
#longerVal = 10 # for daily data seems to work fine
shorterVal = 10
longerVal = 20
shorterAvg = []
longerAvg = []
dohlcACv = []
cursor = -1
inPosition = False
boughtPrice = 0.00
soldPrice = 0.00
profit = 0.00
alreadyExperiencedDeathCross = False
numOfLosses = 0
numOfWins = 0
tickerSymb = "aapl"
# -----------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------
def getLoggedIntoRH():
try:
#
#
login = r.login("jimom","2") ###############
#
except Exception as e:
print(str(e))
# -----------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------
def pollingPriceForTickerSymbol(i):
try:
#
global tickerSymb
#
resp2 = r.stocks.get_stock_historicals(tickerSymb,"5minute", "week")
#
f = open("4reel.txt", "w")
for x in range(len(resp2)):
tempString = ""
for key in resp2[x]:
if "begins" in key:
tempString2 = str(resp2[x][key]) + ","
if "open" in key:
tempString3 = str(resp2[x][key]) + ","
if "high" in key:
tempString4 = str(resp2[x][key]) + ","
if "low" in key:
tempString5 = str(resp2[x][key]) + ","
if "close" in key:
tempString6 = str(resp2[x][key]) + "," + str(resp2[x][key]) + ","
if "volume" in key:
tempString7 = str(resp2[x][key]) + "\n"
tempString = tempString2 + tempString3 + tempString4 + tempString5 + tempString6 + tempString7
f.write(tempString)
#print(key, '->', resp2[x][key])
f.close()
#
return resp2
#
except Exception as e:
print(str(e))
# -----------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------
def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
# -----------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------
def readData():
#
try:
file1 = open('4reel.txt', 'r')
Lines = file1.readlines()
for line in Lines:
dohlcACv.append(line.strip())
#
except Exception as e:
print(str(e))
# -----------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------
def averagesDoIt():
#
try:
#
for x in range(len(dohlcACv)):
if x < longerVal:
longerAvg.append(0.00)
else:
sum = 0.00
for y in range(x):
temp = dohlcACv[y].split(",")
#print(temp[0])
#print(temp[1])
#print(temp[2])
#print(temp[4])
if y in range(x-longerVal, x):
sum = round(sum + round(float(temp[4]), 2), 2)
add = round(sum / longerVal, 2)
longerAvg.append(add)
#
#
#
for x in range(len(dohlcACv)):
if x < shorterVal:
shorterAvg.append(0.00)
else:
sum = 0.00
for y in range(x):
temp = dohlcACv[y].split(",")
#print(temp[0])
#print(temp[1])
#print(temp[2])
#print(temp[4])
if y in range(x-shorterVal, x):
sum = round(sum + round(float(temp[4]), 2), 2)
add = round(sum / shorterVal, 2)
shorterAvg.append(add)
#
except Exception as e:
print(str(e))
# -----------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------
def isThereACrossOver():
#
try:
for z in range(len(dohlcACv)):
if shorterAvg[z] == 0.00 or longerAvg[z] == 0.00:
pass
else:
if shorterAvg[z] > longerAvg[z]:
print("golden crossover matey")
#
except Exception as e:
print(str(e))
# -----------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------
def letsSeeHowWeDid(z):
#
try:
global inPosition, profit, alreadyExperiencedDeathCross, numOfLosses, numOfWins, boughtPrice, soldPrice
#
if shorterAvg[z] == 0.00 or longerAvg[z] == 0.00:
pass
else:
if inPosition:
#temp = dohlcACv[z].split(",")
#currPrice = round(float(temp[1]), 2)
currPrice = r.stocks.get_latest_price(tickerSymb, "bid_price", True)
currPrice = round(float(currPrice[0]), 2)
if currPrice > ((boughtPrice * 0.015) + boughtPrice):
inPosition = False
numOfWins = numOfWins + 1
soldPrice = currPrice
print("sold @ " + str(soldPrice) + " " + str(datetime.now()))
#print("z = " + str(z))
profit = profit + (soldPrice - boughtPrice)
elif currPrice < (boughtPrice - (boughtPrice * 0.010)):
inPosition = False
numOfLosses = numOfLosses + 1
soldPrice = currPrice
print("sold @ " + str(soldPrice) + " " + str(datetime.now()))
#print("z = " + str(z))
profit = profit + (soldPrice - boughtPrice)
else:
pass
if shorterAvg[z] > longerAvg[z] and not inPosition and alreadyExperiencedDeathCross:
#print("golden crossover matey")
inPosition = True
alreadyExperiencedDeathCross = False
#temp = dohlcACv[z].split(",")
#boughtPrice = round(float(temp[1]), 2)
boughtPrice = r.stocks.get_latest_price(tickerSymb, "ask_price", True)
boughtPrice = round(float(boughtPrice[0]), 2)
print("buy @ " + str(boughtPrice) + " " + str(datetime.now()))
#print("z = " + str(z))
if shorterAvg[z] < longerAvg[z]:
#print("golden crossover matey")
alreadyExperiencedDeathCross = True
#
except Exception as e:
print(str(e))
# -----------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------
while True:
if not running:
getLoggedIntoRH()
running = True
#
else:
time.sleep(1.0)
counter = counter + 1
if counter == 5:
pollingPriceForTickerSymbol("ssss")
#
readData()
#
averagesDoIt()
#
letsSeeHowWeDid(len(dohlcACv) - 1)
#
counter = 0
try:
f2 = open("status.txt", "w")
f2.write("\n")
f2.write("\n")
f2.write("PnL = " + str(profit))
f2.write("\n")
f2.write("\n")
f2.write("number of wins = " + str(numOfWins))
f2.write("\n")
f2.write("number of losses = " + str(numOfLosses))
f2.write("\n")
f2.write("total number of trades = " + str(numOfLosses + numOfWins))
f2.write("\n")
f2.write("\n")
if (numOfLosses + numOfWins) == 0:
f2.write("Wins percentage = N/A ")
f2.write("\n")
f2.write("\n")
else:
f2.write("Wins percentage = " + str(numOfWins / (numOfLosses + numOfWins)))
f2.write("\n")
f2.write("\n")
if inPosition:
f2.write("We are currently in a position")
else:
f2.write("no open positions")
f2.write("\n")
f2.close()
except Exception as e:
print(str(e))
从日期时间导入日期时间
导入smtplib
导入时间
导入imaplib
导入电子邮件
导入时间
将robin_股票作为r进口
进口舒蒂尔
从日期时间导入时间增量
# -------------------------------------------------
#
#
# ------------------------------------------------
运行=错误
计数器=0
currOpen=0.00
电流高=0.00
电流=0.00
currClose=0.00
#shorterVal=5#对于日常数据来说似乎效果不错
#longerVal=10#对于日常数据来说似乎效果不错
短距离=10
longerVal=20
shorterAvg=[]
longerAvg=[]
dohlcACv=[]
游标=-1
位置=错误
价格=0.00
溶解度=0.00
利润=0.00
AlreadyExperiencedAthCross=False
numofloss=0
numOfWins=0
tickerSymb=“aapl”
# -----------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------
def getLoggedIntoRH():
尝试:
#
#
login=r.login(“jimom”,“2”)###############
#
例外情况除外,如e:
打印(str(e))
# -----------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------
def pollingPriceForTickerSymbol(一):
尝试:
#
全球股票SYMB
#
resp2=r.stocks.get_stock_historicals(股票代码,“5分钟”、“一周”)
#
f=打开(“4reel.txt”,“w”)
对于范围内的x(len(resp2)):
tempString=“”
对于resp2[x]中的键:
如果在键中“开始”:
tempString2=str(resp2[x][key])+“,”
如果钥匙处于“打开”状态:
tempString3=str(resp2[x][key])+“,”
如果键为“高”:
tempString4=str(resp2[x][key])+“,”
如果键为“低”:
tempString5=str(resp2[x][key])+“,”
如果在键中“关闭”:
tempString6=str(resp2[x][key])+,“+str(resp2[x][key])+,”
如果键中有“音量”:
tempString7=str(resp2[x][key])+“\n”
tempString=tempString2+tempString3+tempString4+tempString5+tempString6+tempString7
f、 写入(临时字符串)
#打印(键“->”,resp2[x][key])
f、 关闭()
#
返回响应2
#
例外情况除外,如e:
打印(str(e))
# -----------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------
def文件(fname):
将open(fname)作为f:
对于枚举(f)中的i,l:
通过
返回i+1
# -----------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------
def readData():
#
尝试:
file1=open('4reel.txt','r')
Lines=file1.readlines()
对于行中的行:
dohlcACv.append(line.strip())
#
例外情况除外,如e:
打印(str(e))
# -----------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------
def averagesDoIt():
#
尝试:
#
对于范围内的x(len(dohlcACv)):
如果xclear()