Back

One Market Order Per Day Maximum

Does anyone know how to prevent your algorithm from making more than one trade per day? My algorithm (in Python) is based off a rolling window/consolidating indicator, and I am wondering if it is possible to prevent my algorithm from making more than one market order per day. I know that my Stop Loss/Profit Taking mechanism is just not working and I still don't understand why, but at the moment I would like to limit my algorithm to making one market order per day at the maximum.

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.


In addition, in the OnData area, I want to allow that section of the code to say, if you haven't invested today, and everything there is true, place the market order. Does anyone know how to do this?

1

Hi Nicholas,

If I've read your code correctly, I think this is roughly what it will do on any given day once the market order condition is hit:

  1. Market order submitted (I'm assuming here that the 100 shares order is submitted and not the 101 shares order. Did you really mean to have two different quantities?)
  2. OnOrderEvent runs for submission of market order (this will return early as fill quantity == 0)
  3. Market order filled
  4. OnOrderEvent runs for fill of market order, submitting 1 stop market order and 1 limit order
  5. OnOrderEvent runs for the submission of the 1 stop market order (this will return early as fill quantity == 0)
  6. OnOrderEvent runs for the submission of the 1 limit order (this will return early as fill quantity == 0)
  7. [...] Time passes, assume now we hit the Take Profit price
  8. Limit order filled for 50 shares
  9. OnOrderEvent runs for the fill of the limit order. As fill quantity != 0, the function does not return early and instead creates 1 new stop market order and 1 new limit order. The reference price used for these two is the fill price of the limit order.
  10. OnOrderEvent is run once for each of the submissions of the order, returns each time as fill quantity == 0
  11. [...] Time passes, and the cycle repeats

 

At point #9, you have a really weird situation:

  • Long 50 shares at the initial price from #3. Let's assume it's $100.
  • 1 remaining stop order (from #5) for -100 shares at $95.
  • 1 new limit order for -50 shares at $110 * 1.1 = $121.
  • 1 new stop order for -100 shares at $110 * 0.95 = $104.5.

 

What happens if the price now drops below $104.5? You only have 50 shares and you're trying to sell 100 - that's not going to work. What happens if the price goes up to $121? You will hit your limit order and sell the remaining shares, but you'll still have your 2 stop orders alive. I hope this illustrates why the algorithm is not behaving as you expect it to. There are a myriad of ways to solve these issues but you'll have to think about the best way. General tips:

  • Be really careful and check what type of order the OnOrderEvent refers to by getting the order by its ID (as you've done already) and then checking its .Type property
  • Make sure you cancel any outstanding limit or stop orders when they are no longer needed
  • Think carefully about how a -50 share take profit works and ask yourself if you're better off either selling all 100 shares or submitting two take profits for the same amount, one slightly above the other, so that you have a chance to sell off your entire position at some point
  • Python doesn't need semicolons, get rid of them
  • When submitting algorithms to the forums, save people some time by removing the stuff that isn't crucial to how the algorithm operates (such as logs, comments and non-essential functionality)

 

 

Now onto your second question. How do you prevent the algorithm from trading more than once per day? This can be achieved in many different ways - one of the easiest ones is to simply store the date of the last trade somewhere in your code and check the current date against that saved date. If they're identical, then you don't trade.

1

Thank you very much for the help Douglas. I'll be working on all that right now.

0

I also would like to make it so that the algorithm only trades once when the conditions are met. For my algorithm, if I don't write self.Portfolio Not Invested, then my algorithm will trade more than 100 times a day. Any tips on how to accomplish this?

0

Nicholas I gave you tips on that at the end of my last reply.

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