Zerodha风筝的Python代码连接api

时间:2017-02-07 10:02:19

标签: python api

我正在使用风筝api来自动化交易,例如买入,卖出等。我从csv文件中提取数据并将它们放入变量中。 以下是excel文件的快照.. enter image description here 这是我的代码..

import csv
from kiteconnect import KiteConnect
import urllib.request
import webbrowser
csvfile = open('../desktop/venkat.csv','r')
csvFileArray = []
for row in csv.reader(csvfile, delimiter = '\t'):
        csvFileArray.append(row)

header =csvFileArray[0][0].split(',')

kite = KiteConnect(api_key="pgxi3s1pltmrjlkw")
#print(kite.login_url())
data = kite.request_access_token("lqa7c5i65srjnb2wgnfdzoy8dhal4wx0",secret="i345wx7pto3yx2gtb999q531qb2ffkzf")
kite.set_access_token(data["access_token"])
#print(kite.access_token)
for each in csvFileArray[1:]:
        data= each[0].split(',')
        for i in range(len(data)):
            if i==0:
                    exchange=str(data[i])
            elif i==1:
                    tradingsymbol=str(data[i])
            elif i==2:  
                    transaction_type=str(data[i])
            elif i==3:
                    quantity=int(data[i])
            elif i==4:
                    price=int(data[i])
            elif i==5:  
                    product=str(data[i])
            elif i==6:    
                    order_type=str(data[i])
            elif i==7:
                    validity=str(data[i])
            elif i==8:  
                    squareoff_value=int(data[i])
            elif i==9:  
                    stoploss_value=int(data[i])
            elif i==10:  
                    trailing_stoploss=int(data[i])
            else:  
                    variety=str(data[i]) # this is the additional line
        #print(exchange)
        disclosed_quantity=None
        trigger_price=None
        tag=''
        try:
            order_id = kite.order_place(exchange,tradingsymbol,transaction_type,quantity,price,product,order_type,validity,squareoff_value,stoploss_value,trailing_stoploss,variety,disclosed_quantity,trigger_price,tag)
            #print(exchange, tradingsymbol, transaction_type, quantity, price, product, order_type, validity, disclosed_quantity, trigger_price, squareoff_value, stoploss_value, trailing_stoploss, variety, tag)
            #print("Order Placed")
            #print("Order placed. ID is", order_id)
        except Exception as e:
            print("Order placement failed", e.message)

它给出了一个我无法解决的错误。这是终端的屏幕截图。

enter image description here

此外,这是文档中的代码..

def order_place(    self, exchange, tradingsymbol, transaction_type, quantity, price=None, product=None, order_type=None, validity=None, disclosed_quantity=None, trigger_price=None, squareoff_value=None, stoploss_value=None, trailing_stoploss=None, variety='regular', tag='') 

我在调用order_place()函数时遇到错误。 请看看并帮助我。谢谢!

1 个答案:

答案 0 :(得分:0)

调用函数的方式有误。在这里发布帖子api_keyapi_secretrequest_token并不是所有人的好主意。

这就是文档所说的:

def order_place(self, exchange, tradingsymbol, transaction_type, quantity, price=None, product=None, order_type=None, validity=None, disclosed_quantity=None, trigger_price=None, squareoff_value=None, stoploss_value=None, trailing_stoploss=None, variety='regular', tag='') 

这是您调用函数的方式:

order_id = kite.order_place(exchange,tradingsymbol,transaction_type,quantity,price,product,order_type,validity,squareoff_value,stoploss_value,trailing_stoploss,variety,disclosed_quantity,trigger_price,tag)

该函数按顺序取值。您最后通过了shared_quantity和trigger_price,它们应在有效性之后通过。

尝试使用此:

order_id = kite.order_place(exchange,tradingsymbol,transaction_type,quantity,price,product,order_type,validity,disclosed_quantity,trigger_price,squareoff_value,stoploss_value,trailing_stoploss,variety,tag)

我不确定如果没有办法重现错误并自行测试已更正的函数调用,是否可以解决错误。

现在kiteconnect API已更新为版本3。因此,下订单的方式现在略有不同。