Back

How to attempt a limit order, then cancel and make a market order

So I wrote these functions that I thought might be useful to someone, so I'm pasting them here.

They attempt to place a limit order first, and then on the next loop, if the limit order is outstanding, they will cancel the limti order and place a market order.

My idea was to try to avoid paying fees when trading on GDAX this way. In my back testing though, you actually loose more money in the lost profit, than you save in the fees. So it seems you are better off doing market orders always (my stratergy is based around day trading, so perhaps with a longer holding time these would work for you),
 

def GoLong(self, symbol, ratio):

# Make sure we can trade this
security = self.Securities[symbol]
if not security.IsTradable:
self.Debug("{} is not tradable.".format(symbol))
return

# Setup vars
orderQuantity = self.CalculateOrderQuantity(symbol, ratio)
limit = 1.001 # +0.1% Limit Order above current price
limitPrice = round( security.Price * d.Decimal(limit), 2 )
openOrders = self.Transactions.GetOpenOrders(symbol)

if(openOrders):
# Cancel all outstanding orders, then do a Market Order
self.Transactions.CancelOpenOrders(symbol)
self.SetHoldings(symbol, ratio)
self.Log('Going long, canceled all open orders, did a Market Order')

else:
# No open orders, try a limit order then
self.LimitOrder(symbol, orderQuantity, limitPrice)
self.Log( str(security.Price) )
self.Log('Going long. Placed a limit order at ' + str(limitPrice))


def CloseLong(self, symbol):

# Make sure we can trade this
security = self.Securities[symbol]
if not security.IsTradable:
self.Debug("{} is not tradable.".format(symbol))
return

# Setup vars
quantity = security.Holdings.Quantity
limit = 1.001 # +0.1% Limit Order below current price
limitPrice = round( security.Price / d.Decimal(limit), 2 )
openOrders = self.Transactions.GetOpenOrders(symbol)

if(openOrders):
# Liquidate (which also cancels all outstanding orders)
self.Liquidate(symbol)
self.Log('Closing long, liquidated')

else:
# Try a limit order
self.LimitOrder(symbol, -quantity, limitPrice)
self.Log( str(security.Price) )
self.Log('Closing long. Placed a limit order at ' + str(limitPrice))

Update Backtest








Thanks mate, saved me some time :).  It probably nets you some loss since you don't check to see if the limit when through until the next GoLong call.

You could probably try (though not sure if its worth the time like you say with intra-day) experimenting with checking for % of loss and either continuing to update the limitorder until it fills or then doing the market order. 

I doubt this works, more just psuedocodish.

if(openOrders):

# Check open order
if self.Securities[symbol].Price < round((self.Portfolio[symbol].HoldingsCost/self.Portfolio[self.target_crypto].Quantity),2)*.90:
# try to update existing order if exists for symbol being bought if not cancel any open order
try:
self.getOpenOrders(symbol).updateOrderFields.Update(limitPrice)
except:
self.Transactions.CancelOpenOrders(symbol)
0

Actually, just realized looking back at this, your closing limit sell order is set to security price / 1.001. So you are trying to set a limit sell order below the current price.  I'm not sure how that method is translated for GDAX, but I'm pretty sure GDAX wouldn't except that order.  I changed it in my code to be like .999 so it trys set a limit sell order .1% above the current price, seems to work better in testing. 

1

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed