2020import exceptions as RH_exception
2121import endpoints
2222
23-
2423class Bounds (Enum ):
2524 """Enum for bounds in `historicals` endpoint """
2625
@@ -113,7 +112,6 @@ def login(self,
113112
114113 if mfa_code :
115114 payload ['mfa_code' ] = mfa_code
116-
117115 try :
118116 res = self .session .post (endpoints .login (), data = payload , timeout = 15 )
119117 res .raise_for_status ()
@@ -317,7 +315,7 @@ def get_quote(self, stock=''):
317315 """Wrapper for quote_data """
318316
319317 data = self .quote_data (stock )
320- return data [ "symbol" ]
318+ return data
321319
322320 def get_historical_quotes (self , stock , interval , span , bounds = Bounds .REGULAR ):
323321 """Fetch historical data for stock
@@ -343,15 +341,10 @@ def get_historical_quotes(self, stock, interval, span, bounds=Bounds.REGULAR):
343341 if isinstance (bounds , str ): # recast to Enum
344342 bounds = Bounds (bounds )
345343
346- data = {
347- 'symbols' : ',' .join (stock ).upper (),
348- 'interval' : interval ,
349- 'span' : span ,
350- 'bounds' : bounds .name .lower ()
351- }
344+ historicals = endpoints .historicals () + "/?symbols=" + ',' .join (stock ).upper () + "&interval=" + interval + "&span=" + span + "&bounds=" + bounds .name .lower ()
352345
353- res = self .session .get (endpoints . historicals (), data = data , timeout = 15 )
354- return res .json ()
346+ res = self .session .get (historicals , timeout = 15 )
347+ return res .json ()[ 'results' ][ 0 ]
355348
356349
357350 def get_news (self , stock ):
@@ -914,15 +907,6 @@ def place_order(self,
914907 else :
915908 payload ['price' ] = float (price )
916909
917- #data = 'account=%s&instrument=%s&price=%f&quxantity=%d&side=%s&symbol=%s#&time_in_force=gfd&trigger=immediate&type=market' % (
918- # self.get_account()['url'],
919- # urllib.parse.unquote(instrument['url']),
920- # float(bid_price),
921- # quantity,
922- # transaction,
923- # instrument['symbol']
924- #)
925-
926910 res = self .session .post (endpoints .orders (), data = payload , timeout = 15 )
927911 res .raise_for_status ()
928912
@@ -1262,6 +1246,11 @@ def submit_order(self,
12621246 (:obj:`requests.request`): result from `orders` put command
12631247 """
12641248
1249+ # Used for default price input
1250+ # Price is required, so we use the current bid price if it is not specified
1251+ current_quote = self .get_quote (symbol )
1252+ current_bid_price = current_quote ['bid_price' ]
1253+
12651254 # Start with some parameter checks. I'm paranoid about $.
12661255 if (instrument_URL is None ):
12671256 if (symbol is None ):
@@ -1313,8 +1302,9 @@ def submit_order(self,
13131302 if (price is not None ):
13141303 if (order_type .lower () == 'market' ):
13151304 raise (ValueError ('Market order has price limit in call to submit_order' ))
1316-
1317- price = float (price )
1305+ price = float (price )
1306+ else :
1307+ price = current_bid_price # default to current bid price
13181308
13191309 if (quantity is None ):
13201310 raise (ValueError ('No quantity specified in call to submit_order' ))
@@ -1327,20 +1317,22 @@ def submit_order(self,
13271317 payload = {}
13281318
13291319 for field , value in [
1330- ('account' , self .get_account ()['url' ]),
1331- ('instrument' , instrument_URL ),
1332- ('symbol' , symbol ),
1333- ('type' , order_type ),
1334- ('time_in_force' , time_in_force ),
1335- ('trigger' , trigger ),
1336- ('price' , price ),
1337- ('stop_price' , stop_price ),
1338- ('quantity' , quantity ),
1339- ('side' , side )
1340- ]:
1320+ ('account' , self .get_account ()['url' ]),
1321+ ('instrument' , instrument_URL ),
1322+ ('symbol' , symbol ),
1323+ ('type' , order_type ),
1324+ ('time_in_force' , time_in_force ),
1325+ ('trigger' , trigger ),
1326+ ('price' , price ),
1327+ ('stop_price' , stop_price ),
1328+ ('quantity' , quantity ),
1329+ ('side' , side )
1330+ ]:
13411331 if (value is not None ):
13421332 payload [field ] = value
13431333
1334+ print (payload )
1335+
13441336 res = self .session .post (endpoints .orders (), data = payload , timeout = 15 )
13451337 res .raise_for_status ()
13461338
0 commit comments