Hello is it possible to code the following strategy? I want my algorithm backtest to pull a few columns from this spreadsheet: https://docs.google.com/spreadsheets/d/e/2PACX-1vQuZAKy82QmT1dvy-nA2apz1vabq47Pv09sfNim2eYhbDfmmaBXA2sEH2rRAcnpCKgfMmbxath_V0XF/pub?gid=1367730565&single=true&output=csv
I want my algorithm backtest to set up some buy orders one second after the market opens each day. The spreadsheet will show which stocks are wanted for which day in the Date and Ticker columns. The buy orders want to be set one cent above the number shown in the Premarket High column for each stock on each date. The buy order wants to be for $10,000 worth of each stock. In other words, I want the backtest to set up a buy order right after the market opens to buy each stock on each date as soon as it breaks above its premarket high by one cent.
Each day during the regular market hours, I then want the algorithm backtest to check every ten seconds to see if any of my buy orders have triggered. If one of my buy orders has been triggered, I want an order-cancels-order set up to exit the position. The order-cancels-order should be set up to exit that stock position once it hits a 15% profit or once it hits a 15% loss, whichever comes first.
20 seconds before the market closes, I want the algorithm to liquidate any remaining positions and clear all orders and watchlists to get ready for the next day with its new stocks. I want to be able to control the start and end date of the backtests too.
Could you code this strategy in python for me? Thanks!
Mia Alissi
Here’s a complete, ready-to-paste QuantConnect Python algorithm that implements Retrospective’s strategy exactly as requested:
from AlgorithmImports import * import pandas as pd import numpy as np from io import StringIO from datetime import datetime, timedelta, date from typing import Any, Dict, List, Set import re class PremarketBreakoutOCODailyAlgorithm(QCAlgorithm): """ PremarketBreakoutOCODailyAlgorithm
Sources: Order Tickets - QuantConnect.com, Empty Equity And Options400 Benchmark, Data Preparation - QuantConnect.com, docs writing algorithms securities asset classes us equity requesting data, Stop Market Orders - QuantConnect.com
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.
Retrospective Green Ocelot
Thanks!
Retrospective Green Ocelot
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!