Why "HasCrossedAbove/Bellow" didn't returned true for this picture.
Why "HasCrossedAbove/Bellow" didn't returned true for this picture.
29 Nov 2018, 02:32
Hello there and thanks in advance.
I have been trying to find and explanation for HasCrossedAbove/Below not returning true for this picture.
The black curve (_ma1) is my main moving average: Time Series, closed, 15 periods.
The blue curve (_ma2) is my second moving averafe: Triangular, closed, 15 periods.
I made a simple trend bot for placing a long position when _ma1 crosses above _ma2 and a short position when _ma1 crosses below _ma2. Such crossings also close the any opposite positions.
My bot parameters match the ones used on the above picture: EURUSD m15.
My code for handling positions is given as such:
protected override void OnBar() { // Result.Last(0) is far more profitable than Result in backtesting. if (_ma1.Result.HasCrossedAbove(_ma2.Result.Last(0), 0)) { ClosePositions(TradeType.Sell); OpenPosition(TradeType.Buy); } if (_ma1.Result.HasCrossedBelow(_ma2.Result.Last(0), 0)) { ClosePositions(TradeType.Buy); OpenPosition(TradeType.Sell); } } private double _volume { get { return Symbol.NormalizeVolumeInUnits(Quantity * Account.FreeMargin * Account.PreciseLeverage / 100); } } public void ClosePositions(TradeType tradeType) { foreach (Position position in Positions.FindAll(_label, Symbol, tradeType)) ClosePosition(position); } public void OpenPosition(TradeType tradeType) { if (Positions.FindAll(_label, Symbol, tradeType).Length != 0) return; double? sl = GetStopLoss(); ExecuteMarketOrder(tradeType, Symbol, _volume, _label, sl, null, null); } public double? GetStopLoss() { if (SL != 0) return SL; else return null; } }
I have read this page https://ctrader.com/api/reference/functions/hascrossedabove. It kind of touches the question, but no entirely.
Again, thanks.