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);
}
}
}
}