Back

Little coding help please

Im trying to teach myself programming through this site and its been very slow going. It would be great if I had something small to mess around with the variables while I'm learning to build something more complex . Could anyone help me out with some code for this?

1. When a price increase or decrease of X % occurs at the end of a trading day, trigger a buy or short on open of next day (bonus if I could look at buying at the very end of the same day)
2. Sell stock at end of following trading day
Update Backtest








One idea is that there are two order types OrderType.MarketOnOpen and OrderType.MarketOnClose. There is also an event you can put into your algorithm. OnEndOfDay(string symbol) It works like OnData except that it occurs at 3:50PM. For example, see the CustomChartingAlgorithm.cs in Lean.

public override void OnEndOfDay()
{
//Log the end of day prices:
Plot("Trade Plot", "Price", lastPrice);
}


In there you can check the x% and then place an MarketOnOpen. The platform also converts a Market order placed during hours when the market is closed to a MarketOnOpen order when the algorithm is live. I am not sure how to do that.

I wanted to reset a couple of indicators during the last bar of the day, which is at 4:00PM or 16:00, so in my OnData event handler, I wrote:

if (time.Hour == 16)
{
trend.Reset();
trendHistory.Reset();
barcount = 0;
Plot("Strategy Equity", "Portfolio", Portfolio.TotalPortfolioValue);
}


Or you can simply write when you want it to happen. Here is my code to liquidate at 10 min before the end of day. Since it is in the OnData event, it will happen 10 times before 4:00 to make sure I am liquidated.

public bool SellOutEndOfDay(string symbol, int quantity)
{
if (shouldSellOutAtEod)
{
if (this.Time.Hour == 15 && this.Time.Minute > 49 || this.Time.Hour == 16)
{
if (Portfolio[symbol].IsLong)
{
Sell(symbol, Portfolio[symbol].AbsoluteQuantity);
}
if (Portfolio[symbol].IsShort)
{
Buy(symbol, Portfolio[symbol].AbsoluteQuantity);
}
// do some other cute stuff like
Tickets[symbol].Add(MarketOnOpenOrder(symbol, quantity, "Open next day"));
return false;
}
}
return true;
}


See QCAlgorithm.Tradeing.CS for the implementation of MarketOnOrderOpen.
2

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