Back

Simple algorithm not working

Hi guys,

I am trying to write a simple algorithm that buys a stock if it falls a certain amount, and sells it if the price goes above the price at which it was bought by a certain amount. unfortunately cannot work out how to keep purchase price as a constant variable over time. I would be extremely grateful if someone could show me how to do this!

 

Best

Hugo

namespace QuantConnect.Algorithm.CSharp
{
public class buythedip : QCAlgorithm
{
//Member variables
private static Symbol _google = QuantConnect.Symbol.Create("GOOGL", SecurityType.Equity, Market.USA);

private int ForwardLookingPeriod = 25;
private int BackwardLookingPeriod = 25;
private decimal fallAmount = Convert.ToDecimal(0.1);
private decimal riseAmount = Convert.ToDecimal(0.05);
private int minutesToExecute = 10;
private decimal relevantPrice;


private RollingWindow<decimal> _close_window;
public Boolean cash;
public Boolean buyTrigger;
public Boolean sellTrigger;


public override void Initialize() {
SetStartDate(2013,01,01);
SetEndDate(2018,02,01);
SetCash(30000);

AddEquity(_google, Resolution.Daily);

_close_window = new RollingWindow<decimal>(BackwardLookingPeriod);

IEnumerable<TradeBar> slices = History(_google, BackwardLookingPeriod);

foreach (TradeBar bar in slices) {
_close_window.Add(bar.Close);




}
Debug(_close_window[0]);
Console.WriteLine("Hugo Lu");

Schedule.On(DateRules.EveryDay(_google),
TimeRules.AfterMarketOpen(_google,minutesToExecute), //Execute the function at the start of the day
// x minutes in....
EveryDayOnMarketOpen);





}//Initialize end

public void EveryDayOnMarketOpen(){ //Define the bad boy

//Sanity Check; are there open orders?
if (Transactions.GetOpenOrders().Count > 0) {
return;
}
//Get information about however many periods ago
IEnumerable<TradeBar> slices = History(_google,BackwardLookingPeriod) ;
TradeBar BarOfInterest = slices.First();

decimal relevantClose = BarOfInterest.Close;
decimal yestClose = slices.Last().Close;

decimal fallPercent = 1 - (yestClose/relevantClose);


//Is it time to buy?

if(fallPercent > fallAmount && Portfolio.Cash > 100 ) {
buyTrigger = true;
relevantPrice = yestClose;

} else {
buyTrigger = false;
relevantPrice = 0;

};

if(buyTrigger)
{SetHoldings(_google,1);

}





//Is it time to sell?

if( ((yestClose / relevantPrice)-1)> riseAmount) {
sellTrigger = true;
} else { sellTrigger = false;};

if(sellTrigger) {

Liquidate();

};





}

}
}















 

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.


Hi Hugo,

The issue arises during the buyTrigger and when the relevantPrice is assigned to 0. When this occurs, you divide by 0 during the sellTrigger. I am unable to solve this issue efficiently because I am not sure what relevantPrice should be if we do not enter the loop: 

//Is it time to buy?
if(fallPercent > fallAmount && Portfolio.Cash > 100 ) {
buyTrigger = true;
relevantPrice = yestClose;
} else {
buyTrigger = false;
relevantPrice = 0;
};

The backtest below solves this issue by assigning relevantPrice to yestClose outside of the loop and removing the line: relevantPrice=0. But this makes it so that we never ented the sellTrigger loop. Also, you mention maintain a "purchase price," however you never purchase using a price in the algorithm. Hope this helps!

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