Back

Setting Algo to buy at preivous high

Hey, I'm trying to figure out how I could get my Algo to buy at the previous day or week high when the price is met. Does anyone think they could help?

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.


I'm super new myself. And I haven't actually done any coding in quant connect yet (although I've built several python programs on my desktop) However, I've watched all the videos on youtube regarding quantconnect, gone through the boot camp, been read the documentation, and about to start looking at sample code.

The impression I'm getting is you're going to want to pass the data slice to a class that you define in a seperate file as: "Class PreviousHigh(slice):"

Then initialize internal variables to store daily high: self.currentDayHigh, self.previousDayHigh, self.currentDayDate self.currentWeekHigh, self.previousWeekHigh, self.previousWeekDate. Include getter methods to retrieve the previous values.

Then initialize an instance of the class such as spyPreviousHigh in the algorithm init section, and literally pass the slices into the instance when the data events occur.

You will likely need to import the datetime library.

Initially the class should set all these values to those for the first price it receives if they are set to 0 or something. Then it should check the dates. When a newly dated slice appears we know a new day has occured, and we should set self.previousDayHigh to self.currentDayHigh and update self.currentDayDate. Likewise if the timedelta of the datetime objects is greater than 7 days we should update the weekly information and set the self.previousWeekDate to the day starting the previous week (use timedelta again from current day). So anyway it will take a OHLC bar passed to it, can be minute data or whatever, and it will check the date on these. If it's the current date (or less than 7 days from the start of the previous week), check to see whether the HIGH price is higher than what's recorded as current day and current week, updating if required. If not, do nothing.

Your algorithm can simply compare the current high of the OHLC bar to the value you get from spyPreviousHigh.getDay() and if the current high is higher AND  you don't already have a position, you can send an order to buy.

 

 

0

Hi Ez,

There are a few things we need for your design:

If we are using daily resolution, we could store data[our_symbol].High to a variable (i.e. self.previousHigh) to use on the next day. However, if we are using higher resolution, we need to use data consolidator, and update self.previousHigh in the DataConsolidated event handler.

To place an order that triggers when certain price is reached, we do StopMarketOrder.

Here is my backtest with an example. Hope it helps!

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.


Link Liang great help, but I can't seem to figure out how the " self. previoushigh" part of your Algo gets the high of the previous tick unless self.preioushigh is a statement in itself for the previous high. Also could you please provide an email or point of contact where I could reach you by directly for any possible further questions.

0

Hi Ez,

Here is how it works:

Since we are subscribing minute level resolution data, we get a bar every minute and those bars are handled by OnData() method. With our one-day data consolidator, we combine all minute bars into one bar at the end of the day. The combined bar is handled by OneDayBarHandler() method, since we added it to DataConsolidated event. Then we store the high price of today into self.previousHigh, and use that information for tomorrow's trading.

Here you could find more information about bars, and here is more information and example for data consolicator.

You are welcome to join our slack channel to get more help from the community. Thanks for your support!

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.


Link Liang, thank you. Just one last thing. On another project, I am using the data consolidator to make longer tradebars like week bars etc. When I backtest with a debug, in the logs I can see that the tradebars start on whatever day that I am backtesting. So how could I get the weeks to always start on Mondays and for the months to always start on the 1st?

I was thinking maybe I could try a scheduling function I'm not sure how to approach it, but I would like to know what you think are the best ways for me to solve this problem. Also, a backtest attachment would be very useful. Thank you.

0

Link Liang , thank you. Just one last thing. On another project, I am using the data consolidator to make longer tradebars like week bars etc. When I backtest with a debug, in the logs I can see that the tradebars start on whatever day that I am backtesting. So how could I get the weeks to always start on Mondays and for the months to always start on the 1st?

I was thinking maybe I could try a scheduling function I'm not sure how to approach it, but I would like to know what you think are the best ways for me to solve this problem. Also, a backtest attachment would be very useful. Thank you.

0

Hi Ez Buckets,

It really depends on your usage to choose between data consolidator and scheduled event, or even both. If you want to discuss your project in detail, please share your main strategy so that we could decide what we should use.

Meanwhile, there are plenty of posts with similar questions in the community. Our forum offers a search function which is usually helpful, and you could also use Google for your question. Here are a few recent related posts that I found:

Here is an example of scheduled universe selection every Monday.

Here we talked about universe selection in every 7days or a month.

Here we talked about data consolidation of number of bars and time range.

Please let us know if you have further questions!

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 Link Liang , my bad I forgot to attach it but here it is.

In the logs, the start dates to the weekly tradebars are all over the place. What do you think is the best way to get the tradebars to always begin Monday open and end Friday close. 

0


Hi Ez Buckets,

I would recommend to use scheduled events in this case, because it will ensure your event to fire at the expected time. Here is what I would do:

1. Schedule the event on Saturday 00:01. The reason is the daily bar of Friday arrives on Saturday 00:00.

2. Request the data for the past week.

3. Manually build a weekly bar based on historical data of last week.

4. Apply your strategy base on this weekly bar!

Hope it helps.

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.


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