Im new to quant connect and wanted to implement my first little algorithm... it should simply determine the trend by comparing the last and the current candle... if it noticies a up trend, it should place a buy order and if it detects a down trend it should place a short order...
My code is pretty simply currently... but it never ever made any profit at all... it simply only makes losses... i tested multiple other currencys with the same result.
Wheres my mistake ? Did i forgot something ? Or is this kind of algorithm simply not working in praxis ?
namespace QuantConnect.Algorithm.CSharp
{
public class CalibratedVentralCompensator : QCAlgorithm
{
public string forex = "EURUSD";
private bool betOnUp = false;
private RollingWindow<decimal> close;
public override void Initialize()
{
SetEndDate(2019, 12, 1);
SetStartDate(2018, 12, 1);
SetCash(200);
var eud = AddForex(forex, Resolution.Hour, Market.Oanda);
eud.SetDataNormalizationMode(DataNormalizationMode.Raw);
SetBrokerageModel(BrokerageName.OandaBrokerage);
close = new RollingWindow<decimal>(4);
}
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// Slice object keyed by symbol containing the stock data
public override void OnData(Slice data)
{
close.Add(data[forex].Close);
if(close.Count <= 1) return;
var currentPrice = Securities[forex].Price;
var amountToBuy = Portfolio.Cash/currentPrice;
var upTrend = close[1] < close[0];
Debug("Last Close : "+close[1]+", Current Close : "+close[0]+", Profit since last trade : "+Portfolio[forex].UnrealizedProfit);
if(betOnUp != upTrend){
Liquidate(forex);
Debug("Liquidated position, holding "+Securities[forex].Invested);
}
if(upTrend){
MarketOrder(forex, amountToBuy);
betOnUp = true;
Debug("Long with "+(amountToBuy)+" Units, holding "+Securities[forex].Invested);
}
else{
MarketOrder(forex, -amountToBuy);
betOnUp = false;
Debug("Short with "+(-amountToBuy)+" Units, holding "+Securities[forex].Invested);
}
}
}
}
Mark Reeve
I dont know C# so I can't check your code exactly but any strategy that buys on an UP candle and sells on a DOWN candle will lose over time, due to transaction costs... the shorter the time frame the greater the losses and then more rapidly they will compound. I see you are using Hourly data backtesting over a year long period so those losses could be right!
Lars Matthäus
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!