I want to receive the average cost of a single position.
I am using the IB-insync API and using reqPositions(). The ouput is:
[Position(account='DU1675421', contract=Stock(conId=29622888, symbol='HEIA', exchange='AEB', currency='EUR', localSymbol='HEIA', tradingClass='HEIA'), position=100.0, avgCost=90.97088),
Position(account='DU1675421', contract=Future(conId=176791153, symbol='N225M', lastTradeDateOrContractMonth='20191212', multiplier='100', currency='JPY', localSymbol='164120019', tradingClass='NK225M'), position=1.0, avgCost=2284540.0)]
I would like to have the avgcost of 1 position. How would I do this?
b = ib.reqPositions()
while ib.sleep(0.5):
plb = b
print (plb)
b.avgCost() doesn't work.
It looks like reqPositions is returning a list of namedtuples.
To access an element in the namedtuple you would need to iterate over the list, e.g.
for position in b:
print(position.avgCost)
Related
I have been trying to figure out what the issue is and can't seem to figure it out.
valueRange = [j.value for i in copyRange for j in i]
vrCounter = 0
vrStep = 7
x.field_names = ["Jurisdiction","SPOC-Name", "Lines of Business","Market Area", "Consultant Personal Lines", "Consultant Business Lines", " ROC-Name"]
for i in range(0,len(valueRange)):
x.add_row(valueRange[i], valueRange[i+1], valueRange[i+2], valueRange[i+3], valueRange[i+4], valueRange[i+5], valueRange[i+6])
print(x)
I ran the code without the x.add_row() function and it printed the values correctly. The valueRange list is just a bunch of keywords that match the field_names.
As shown in the docs, add_row() takes a list of values as an argument.
Enclose all the values in a list and it must work.
x.add_row([valueRange[i], valueRange[i+1], valueRange[i+2], valueRange[i+3], valueRange[i+4], valueRange[i+5], valueRange[i+6]])
Turn out the issue was the for i in range(0,len(valueRange)): portion. So it wasn't possible to answer this question without complete information about that valueRange list. This list contained 28 (0-27) string values. The loop was stepping through the 0-27 range 1 at a time while trying to apply an i+number value to each one. This would quickly go out of bounds unless the step size matches the valueRange list.
The final solution looks like:
for i in range(0,len(valueRange),7):
x.add_row(valueRange[i:i+7])
I have this dictionary and I need it to be formatted in a table like manner to a text file.
dictionary_lines = {'1-2': (69.18217255912117, 182.95794152905918), '2-3': (35.825144800822954, 175.40503498180715), '3-4': (37.34332738254673, 97.30771061242511), '4-5': (57.026590289091914, 97.33437880141743), '5-6': (57.23912298419586, 14.32271997820363), '6-7': (55.61382561917492, 351.4794228420951), '7-8': (41.21551406933976, 275.1365340619268), '8-1': (57.83213034291623, 272.6560961904868)}
As of right now I'm working on printing them out first before working on writing a new file. I'm stuck on how to properly format them. Here's what I got so far:
for item in dictionary_lines:
print ("{l}\t{d:<10.3f}\t{a:<10.3f}".format(l= ,d =,a = ))
I want it to be printed like this:
Lines[tab] Distances [tab] Azimuth from the South
Key 0 value1 in tuple0 Value2 in tuple0
You don't use the data from the dictionary in the format().
Also, consider using items() to iterate over key-value pairs of a dictionary as:
for key, value in dictionary_lines.items():
print ("l={}\td={:<10.3f}\ta={:<10.3f}".format(key, value[0], value[1]))
l=1-2 d=69.182 a=182.958
l=2-3 d=35.825 a=175.405
l=3-4 d=37.343 a=97.308
l=4-5 d=57.027 a=97.334
l=5-6 d=57.239 a=14.323
l=6-7 d=55.614 a=351.479
l=7-8 d=41.216 a=275.137
l=8-1 d=57.832 a=272.656
I have a list adImageList of dictionary items in following form:
[{'Image_thumb_100x75': 'https://cache.domain.com/mmo/7/295/170/227_174707044_thumb.jpg',
'Image_hoved_400x300': 'https://cache.domain.com/mmo/7/295/170/227_174707044_hoved.jpg',
'Image_full_800x600': 'https://cache.domain.com/mmo/7/295/170/227_174707044.jpg'},
{'Image_thumb_100x75': 'https://cache.domain.com/mmo/7/295/170/227_1136648194_thumb.jpg',
'Image_hoved_400x300': 'https://cache.domain.com/mmo/7/295/170/227_1136648194_hoved.jpg',
'Image_full_800x600': 'https://cache.domain.com/mmo/7/295/170/227_1136648194.jpg'},
{'Image_thumb_100x75': 'https://cache.domain.com/mmo/7/295/170/227_400613427_thumb.jpg',
'Image_hoved_400x300': 'https://cache.domain.com/mmo/7/295/170/227_400613427_hoved.jpg',
'Image_full_800x600': 'https://cache.domain.com/mmo/7/295/170/227_400613427.jpg'}]
I have iterator which suppose to add local URL under each image record after fetching it from web (fetching part works ok). So I'm using following code to append local URL to existing dictionary items:
for i, d in enumerate(adImageList):
file_name_thumb = '0{}_{}_{}'.format(i, page_title,'_thumb_100x75.jpg')
urllib.request.urlretrieve(d['Image_thumb_100x75'], file_name_thumb)
local_path_thumb = dir_path+file_name_thumb
adImageList.insert[i](1,{'Image_thumb_100x75_local_path_thumb':local_path_thumb}) # not working
file_name_hoved = '0{}_{}_{}'.format(i, page_title,'_hoved_400x300.jpg')
urllib.request.urlretrieve(d['Image_hoved_400x300'], file_name_hoved)
local_path_hoved = dir_path+file_name_hoved
adImageList.insert[i](3,{'Image_hoved_400x300_local_path_hoved':local_path_hoved}) # not working
file_name_full = '0{}_{}_{}'.format(i, page_title,'_full_800x600.jpg')
urllib.request.urlretrieve(d['Image_full_800x600'], file_name_full)
local_path_full = dir_path+file_name_full
adImageList.insert[i](5,{'Image_full_800x600_local_path_full':local_path_full}) # not working
Idea is to extend dict items in following manner which also explains numbers 1,3 and 5 in my code
{'Image_thumb_100x75': 'https://cache.domain.com/mmo/7/295/170/227_174707044_thumb.jpg',
'Image_thumb_100x75_local_path_thumb':local_path_thumb #1,
'Image_hoved_400x300': 'https://cache.domain.com/mmo/7/295/170/227_174707044_hoved.jpg',
'Image_hoved_400x300_local_path_hoved':local_path_hoved #3
'Image_full_800x600': 'https://cache.domain.com/mmo/7/295/170/227_174707044.jpg',
'Image_full_800x600_local_path_full':local_path_full #5}
But it's giving me error:
TypeError: 'builtin_function_or_method' object is not subscriptable
Most likely here's what you had in mind:
adImageList[i]['Image_thumb_100x75_local_path_thumb']=local_path_thumb
This adds key 'Image_thumb_100x75_local_path_thumb' to the ith dictionary on the list and sets its value to local_path_thumb. The purpose of 1,3,5 is still unclear.
python stack traces give line numbers for a reason, but my guess is this line:
adImageList.insert[i]
insert is a method
I have a problem with this
def randomrecipes():
print (random.sample(listofrecipes, 1))
mainmenu()
listofrecipes = [monday, tuesday, wednsday, thursday, friday, saturday, sunday]
the recipes is formatted like this (just ignore the Norwegian gibberish).
monday"""
1 l kjøttkraft /buljong eller lettsaltet vann
750 g rå potet
300 g kokt potet
3 dl byggmel
2 ss hvetemel
1 ts salt
"""
When I just print it, it comes out with the whitespace preserved, but from randomrecipes I get this:
['\n1 l kjøttkraft /buljong eller lettsaltet vann\n750 g rå potet\n300 g kokt potet\n3 dl byggmel\n2 ss hvetemel\n1 ts salt\n']
Im trying to learn by myself and is just getting started, so sorry if this is a dumb question. Thanks.
You're using random.sample(iterable, k) which always returns a list of length k or in your case 1. So the object you're printing is a list of a string.
You could change the line
print (random.sample(listofrecipes, 1))
to something like
print(random.sample(listofrecipes,1)[0]) # note the [0]
That will return the first element in the list (which happens to be the only one).
A cleaner solution might be instead to use random.choice(iterable). It always returns a single choice, not a list. It is effectively equivalent as the line above, but it's a little clearer.
random.sample returns a list, which is exactly what you're getting. This output is just how lists are formatted. If you want to extract the string from this list, do it:
random.sample(listofrecipes, 1)[0]
The output of random.sample is a list of a certain length, 1 in this instance. When you print this you are seeing the output that is generated when displaying a list, including newline \n characters.
print(random.sample(listofrecipes, 1)[0])
would print out the string, the first element of the list, which would reinstate the original display across several lines.
for line in open('transactions.dat','r'):
item=line.rstrip('\n')
item=item.split(',')
custid=item[2]
amt=item[4]
if custid in cust1:
a=cust1[custid]
b=amt
c=(a)+(b)
print(cust1[custid]+" : "+a+" :"+b+":"+c)
break
else:
cust1[custid]=amt
Output:
85.91 : 85.91 :85.91:85.9185.91
Well above is my code what I want is
when I read from a file I want to add the customer amount with same
id.
Secondly there should not be repetition of customer id in my
dictionary.
so I am trying to add customer amount which is c but it gives me appended string instead of adding the two. You can see in the last part of my output which is value of c. So how do I add the values.
Sample transaction data:
109400182,2016-09-10,119257029,1094,40.29
109400183,2016-09-10,119257029,1094,9.99
377700146,2016-09-10,119257029,3777,49.37
276900142,2016-09-10,135127654,2769,23.31
276900143,2016-09-10,135127654,2769,25.58
You reading strings, instead of floats, from the file. Use this amt=float(item[4]) to convert strings representing numbers to floats, and then print(str(cust1[custid])+" : "+str(a)+" :"+str(b)+":"+str(c)) to print out.
Your code may need lots of refactor, but in a nutshell and if I understand what you are trying to do you could do
c = float(a) + float(b)
and that should work.