📄 margin.py
字号:
#coding:cp936
import urllib, sys, csv
def quot(focus, sortID=2, rev = True, amendment=0.0010, volume=5000):
#quto_url = r'quto.txt'
#quto_ini = r'quto-ini.txt'
quto_url = r'http://222.73.103.206/data/quto.txt'
quto_ini = r'http://222.73.103.206/quote/quto-ini.txt'
page = urllib.urlopen(quto_url)
forex = {}
for line in page.readlines():
parts = line.split()
#print len(parts), '--', parts
forex[parts[0]] = (parts[1], parts[2], parts[3], parts[4], parts[6], parts[7], parts[8], parts[10][3:])
page.close()
page = urllib.urlopen(quto_ini)
lines = page.readlines()
parts = lines[1].split(',')
format = '%-14s%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s'
currencyList = []
print format %('货币', '当前价', '昨收', '涨跌', '百分比', '买价', '卖价', '最高', '最低', '振幅', '波幅', '更新时间')
for pos in range(0, len(parts[4:]), 4):
name = parts[4:][pos]
num = parts[4:][pos + 1]
closePrice, x, todayLow, todayHigh, bit, offer, priceDiff, updatetime = forex[num]
currentPrice = round(float(closePrice) + float(priceDiff), 4)
change = round(float(priceDiff) / float(closePrice) * 100, 3)
highLowRatio = abs(round((float(todayHigh) - float(todayLow))/min(float(todayHigh),float(todayLow)) * 100, 3))
scope = abs(round((float(todayHigh) - float(todayLow))/float(closePrice) * 100, 3))
currencyList.append((name, currentPrice, change, highLowRatio, scope, forex[num]))
if sortID != None:
currencyList.sort(key=lambda x:x[sortID], reverse=rev)
reader = csv.reader(open("margin-alert.csv", "r"))
preSet = {}
for row in reader:
preSet[row[0]] = row
cost_reader = csv.reader(open("forex-repository.csv", "r"))
costSet = {}
for row in cost_reader:
costSet[row[0]] = row
costText = None
showSet = []
for forex in currencyList:
name, currentPrice, change, highLowRatio, scope, (closePrice, x, todayLow, todayHigh, bit, offer, priceDiff, updatetime) = forex
if name in preSet.keys():
set = preSet[name]
_text = '<<%s>>的价位已经%s设定的%s的价位:%s, 当前的%s是: %s'
text = None
'''
o(offer):表示银行卖出价(实盘交易高10点,保证金交易高20点)
b(bit): 表示银行买入价(实盘交易低20点,保证金交易低10点)
'''
if 'o' == set[1].strip():
if 'g' == set[2].strip():
if float(offer) > float(set[3]):
text = _text %(name, '卖出', '大于', set[3], '卖出', offer)
elif 'l' == set[2].strip():
if float(offer) < float(set[3]):
text = _text %(name, '卖出', '小于', set[3], '卖出', offer)
if 'b' == set[1].strip():
if 'g' == set[1].strip():
if float(bit) > float(set[3]):
text = _text %(name, '买入', '大于', set[3], '买入', bit)
elif 'l' == set[1].strip():
if float(bit) < float(set[3]):
text = _text %(name, '买入', '小于', set[3], '买入', bit)
if text != None:
showSet.append(text)
if name in costSet.keys():
set = costSet[name]
if '美元/日元' == name:
amendment = 0.1
volume = 50
marginfxOffer = float(bit) + amendment
marginfxBit = float(offer) - amendment
__text = '卖美元: %-2.4f, 买美元: %-2.4f, 成本汇率: %-2.4f, 当前汇率: %-2.4f, 浮亏: %-2.2f'
'''
b(buy):表示买入银行的卖出价(offer),做多这个货币对
s(sell):表示卖出银行的买入价(bit), 做空这个货币对
'''
if 'b' == set[1].strip():
earing = (marginfxOffer - float(set[2])) * volume
costText = __text % (marginfxOffer, marginfxBit, float(set[2]), marginfxOffer, earing)
elif 's' == set[1].strip():
earing = (float(set[2]) - marginfxBit) * volume
costText = __text % (marginfxOffer, marginfxBit, float(set[2]), marginfxBit, earing)
if name in focus:
name += '★'
print
text = format %(name, str(currentPrice), closePrice, priceDiff, str(change) + '%', bit, offer, todayLow, todayHigh, str(scope) + '%', str(highLowRatio) + '%', updatetime)
print text
page.close()
print '\n' + '-' * 50
for show in showSet:
print show
if costText != None:
print '\n保证金情况: ' + costText
def usage():
print '''
python quot.py -h ==> 显示帮助信息
python quot.py -s排序 ==> 排序显示价牌
排序的值可以是:
2 => 比昨收价的涨跌百分比
3 => 最高最低价差的百分比
4 => 振幅百分比
例如: python quot.py -s3
'''
if __name__ == '__main__':
import getopt
focus = '美元/瑞朗', '美元/日元', '澳元/美元', '美元/加元'
opts, args = getopt.getopt(sys.argv[1:], '?hs:', ['--help', '--sort'])
for op, arg in opts:
if op in ('-h', '--help', '-?'):
usage()
elif op in ('-s', '--sort'):
quot(focus, int(arg))
if len(opts) == 0:
quot(focus)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -