Seeking Assistance for Simple Moving Average Crossover Bot

Created at 26 Oct 2023, 22:28
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
CR

creatorn906

Joined 26.10.2023

Seeking Assistance for Simple Moving Average Crossover Bot
26 Oct 2023, 22:28


Hello everyone,

I'm working on a relatively simple trading bot using cAlgo, and I'm encountering an issue with the strategy implementation. I want the bot to perform short trades whenever the 33-period moving averages (both fast and slow) cross below the 144-period moving averages. Additionally, I'd like the bot to re-enter a short position each time the price retraces to the 33-period moving average within this downtrend. The stop loss should be above the 144-period moving average.

I'm facing difficulties as the positions are not being opened as expected. The closure of all positions should occur when the 33-period moving averages cross above the 144-period moving averages, signaling the start of an uptrend.

I would appreciate any insights or suggestions to help resolve this issue. Thanks in advance for your assistance!


@creatorn906
Replies

PanagiotisChar
27 Oct 2023, 06:03

Hi there,

In order to get assistance, you should share your cBot code first. Then explain how can somebody reproduce your problem

 


@PanagiotisChar

creatorn906
27 Oct 2023, 17:03 ( Updated at: 21 Dec 2023, 09:23 )

RE: Seeking Assistance for Simple Moving Average Crossover Bot

PanagiotisChar said: 

Hi there,

In order to get assistance, you should share your cBot code first. Then explain how can somebody reproduce your problem

Sorry, here is the code, the positions are not opening according to the assumption that if the blue average, as in the screenshot, crosses the red one from top to bottom, it is short and every time the blue average 33 is below the red, and the price returns and touches the blue 33, it is short again. A SL crossing red 144

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MovingAverageCrossEA : Robot
    {
        [Parameter("WWS Period 33", DefaultValue = 33)]
        public int Period33 { get; set; }

        [Parameter("WWS Period 144", DefaultValue = 144)]
        public int Period144 { get; set; }

        [Parameter("Stop Loss Multiplier", DefaultValue = 2.0)]
        public double StopLossMultiplier { get; set; }

        private WeightedMovingAverage wwsHigh33;
        private WeightedMovingAverage wwsLow33;
        private WeightedMovingAverage wwsHigh144;
        private WeightedMovingAverage wwsLow144;

        private double stopLossLevel;

        protected override void OnStart()
        {
            wwsHigh33 = Indicators.WeightedMovingAverage(MarketSeries.High, Period33);
            wwsLow33 = Indicators.WeightedMovingAverage(MarketSeries.Low, Period33);
            wwsHigh144 = Indicators.WeightedMovingAverage(MarketSeries.High, Period144);
            wwsLow144 = Indicators.WeightedMovingAverage(MarketSeries.Low, Period144);
        }

        protected override void OnBar()
        {
            // Oblicz poziom dynamicznego stop lossa
            stopLossLevel = Math.Max(wwsLow144.Result.Last(1), stopLossLevel);

            // Sprawdź, czy doszło do przecięcia średnich kroczących
            bool crossedAbove = wwsHigh33.Result.Last(1) > wwsLow33.Result.Last(1) && wwsHigh144.Result.Last(1) > wwsLow144.Result.Last(1);
            bool crossedBelow = wwsHigh33.Result.Last(1) < wwsLow33.Result.Last(1) && wwsHigh144.Result.Last(1) < wwsLow144.Result.Last(1);

            if (crossedAbove)
            {
                // Otwórz pozycję short z dynamicznym stop loss
                ExecuteMarketOrder(TradeType.Sell, Symbol, 1, "Przecięcie WWS Short", 0, 0, stopLossLevel, "Opening short position on WWS crossover");
            }
            else if (crossedBelow)
            {
                // Otwórz pozycję long z dynamicznym stop loss
                ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "Przecięcie WWS Long", 0, 0, stopLossLevel, "Opening long position on WWS crossover");
            }
        }
    }
}


@creatorn906

PanagiotisChar
28 Oct 2023, 05:55

Hi there,

Your code does not check for crosses anywhere. It just checks if one average is above the other. If you want to check for crosses e.g. wwsHigh33 crosses above wwsHigh144, your condition should look like this

bool crossedAbove = wwsHigh33.Result.Last(1) > wwsHigh144.Result.Last(1) && wwsHigh33.Result.Last(2) < wwsHigh144.Result.Last(2)


@PanagiotisChar