Back

Using Limit Orders

My latest project in learning to write algorithms is figuring out how to use limit orders. Simple enough I thought, just put the word "Limit" in front of the order line of code. When I ran a backtest, it ordered the specified quantity once every minute (Minute Resolution), until I ran out of money then gave me an insufficient funds run error.  Oops - not as simple as I thought!  Apparently, limit orders do not cancel themselves automatically when they fill.  Is that correct?

My search for examples of shared code using limit orders has left me even more confused, so I am asking for help.  Can someone please explain how to use these orders correctly?  Start with the basics and assume that I know nothing - which is not a bad assumption :(  If someone knows of some simple code somewhere that uses limit orders, a link would be appreciated.  Thank you in advance to anyone willing to help out a constant newbie!

Update Backtest








Hi Oran,

Could you provide an algorithm that generates this behaviour so that I can debug it?
 

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.


Alexandre,

I don't know that I can, I have probably changed it 50 tiimes since that first failure. I would change something and it would fail, so I change something else and it would fail. I'm running out of ideas of what to change. Let me see if I can recreate the original flop and I'll share it so everyone can see what NOT to do :)

Oran

0

OK, here was my original attempt that flopped big time.  Don't laugh at the strategy, it was never intended to be do anything but enter a limit order at a price lower than where the stock was when it placed the order, sell at a profit, and enter a new limit order to buy again at the lower price and sell again if the profit exit was met.  Looking at a 1 minute chart for SPY on 08/22/2016, it should have placed a limit order, bought, sold, bought again, sold again, and set out the day since the price never again fell to the buy point.  Instead, it buys multiple times at crazy times until it runs out of money, sells as it should with the market order, then immediately starts on a buying frenzy again when the price is nowhere near $218.10.

In other words - this doesn't work anything like expected. So, I am a DA with no clue of what I'm doing :)  I already knew that; that is why I am asking for help! I know there is a way to make limit orders work, it is just not as simple as I had hoped for. Surprise - it never is!

0


After spending the day reading and working on this project, I am starting to think that limit orders require working with order tickets. Am I on the right path? The problem with that is that I don't know anything about that one either. So, I guess I am going to have to learn about tickets first, before I can tackle limit orders. Anybody out there willing to play Professor?

0

Hi Oran, please checkout the QCU example "How do I use Limit Orders?"

_limitOrder = LimitOrder(_symbol, quantity, (_price * 0.95m));

This saves the order ticket, which you can use to update or cancel the order later.

_limitOrder.Update(new UpdateOrderFields{LimitPrice = newLimitPrice});

You can check on the status of the order too:

if (_limitOrder.Status != OrderStatus.Filled)
{
Plot("Limit Plot", "Limit", _limitOrder.Get(OrderField.LimitPrice));
}

 

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.


Hi Oran, 

OrderTickets are the object result from a LimitOrder, MarketOrder, etc.... from the OrderTicket, you can access its Status (Filled, canceled, etc...) and with it, you can also Update or Cancel orders (providing the ticket id).
Have a look in this thread about the OrderTickets feature: 
https://www.quantconnect.com/forum/discussion/623/feature-ordertickets-and-updating-orders/p1
 

0

Continuing the quest, I checked out (again) the QCU examples for "Using Limit Orders" and "Using Order Tickets" trying, as best my feeble brain could, to follow them.  I thought I was getting there, so I updated my algo as attached.  I get a run error in the backtest, and I can't figure out why.  Obviously, I am not "there" yet.  Can somebody shed some light on why this does not work? Pretty Please :)  Thanks.

0


Oran,
I have an algorithm which uses limit orders in which I worked out the flow of events.  I am attaching the back test for the algorithm.  The returns are pretty bad, but it compiles and runs the back test.  I even ran a similar algorithm live for a while and it performed well insofar as the event handling is concerned. 

Clone the algorithm and run a back test.  Then download the code and put it into your Lean on your local machine.  Then dive into the code and look at the way I handle the events and blocking of new orders until the existing order has resolved either through a complete fill or a cancel.  It also handles partial fills gracefully as well.

Essentially here is the logic.  Make limit order -> block further orders and wait for a fill -> if it has not filled after x number of bars, cancel whatever is left of the order and unblock further orders.

Nick

 

0


Thanks Nick,

I cloned yours and will dig into it. That will take some time, your algo is a BUNCH more complex than mine. You are doing things I don't know about or understand so, like I said, it's going to take some time.  That's okay though, I'll learn a few things along the way, and that's the name of the game!

In the meantime, since you are obviously way above me in coding skills, can you see where my simple algo blows up? It's bugging me why it doesn't work. Learning why something doesn't work is as important to me as learning how to do it right. Just stubborn I guess :)

Thanks again,

Oran

0

Although I'm not a programmer, I've been slowly teaching myself by taking online courses and analyzing scripts posted by others.

Currently I'm working on the script posted above by Nicholas. I wanted to add three things: 1. Add the option to use EMA rather than SMA, 2. Allow for fixed number of shares and 3. Have the indicators use 15 min bars. 

I believe that I resolved the EMA addition, although I ended up inserting    

    private bool UseEMA = true 

in both the Main.cs and MultiSymbolStrategy.cs files.  Ideally I would like to only have to declare the variable in one file if this is possible. 

I was able to add code to use a fixed number of shares. 

The main issue that I can't resolve is adding a 15 minute consolidator.After reading:

  https://www.quantconnect.com/docs/algorithm-reference/consolidating-data#Consolidating-Data
   and
   https://www.quantconnect.com/forum/discussion/663/complex-indicators-on-15-minute-time-frame 

I inserted 15 minute consolidation code into Main.cs but I'm stumped as to how to tie it into MultiSymbolStrategy. 

I've tried a number of things, including
  adding fifteenMinute to the priceIdentity variable,
  using FastEMA in MultiSymbolStrategy
  attaching the 15 minute event handler
 
But nothing I tried thus far worked.  I've attached my code and should be greatful if someone could help me and either explain or show me what additional steps I need to take. 

Thanks,
Richard

0


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