Morning star reversal not working

Created at 24 Nov 2024, 03:39
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!
AI

airsakuragi

Joined 24.11.2024

Morning star reversal not working
24 Nov 2024, 03:39


hi, i tried to write a bot that execute a buy when there is morning star signal touching a previous day low, but it seems not working.

 

could someone please help me? im not good in programing

thank you

 

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

namespace cAlgo.Robots
{
   [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
   public class MorningStarBot : Robot
   {
       [Parameter("Volume", DefaultValue = 1000)]
       public int Volume { get; set; }

       [Parameter("Stop Loss (pips)", DefaultValue = 20)]
       public int StopLoss { get; set; }

       [Parameter("Take Profit (pips)", DefaultValue = 50)]
       public int TakeProfit { get; set; }

       protected override void OnStart()
       {
           // Subscribe to the bar updates
           Bars.LastBarUpdated += OnBarUpdated;
       }

       private void OnBarUpdated(BarsUpdatedEventArgs e)
       {
           // Ensure there are enough bars to check the pattern
           if (Bars.Count < 3)
               return;

           // Get the last three candles
           var lastCandle = Bars.Last(0);
           var middleCandle = Bars.Last(1);
           var firstCandle = Bars.Last(2);

           // Get the previous day's low
           var previousDayLow = Bars.Where(b => b.OpenTime.Date == lastCandle.OpenTime.Date.AddDays(-1))
                                     .Select(b => b.Low)
                                     .DefaultIfEmpty(double.MaxValue)
                                     .Min();

           // Check if there is a Morning Star pattern
           if (IsMorningStar(firstCandle, middleCandle, lastCandle, previousDayLow))
           {
               // Execute a buy order
               ExecuteMarketOrder(TradeType.Buy, Symbol.Name, Volume, "MorningStarBot", StopLoss, TakeProfit);
           }
       }

       private bool IsMorningStar(Bar firstCandle, Bar middleCandle, Bar lastCandle, double previousDayLow)
       {
           // Morning Star pattern logic:
           // 1. First candle is bearish (Close < Open)
           // 2. Middle candle touches the previous day's low
           // 3. Last candle is bullish (Close > Open) and closes above the midpoint of the first candle

           bool isFirstBearish = firstCandle.Close < firstCandle.Open;
           bool isMiddleTouchesLow = middleCandle.Low <= previousDayLow;
           bool isLastBullish = lastCandle.Close > lastCandle.Open;
           bool isLastAboveMidpoint = lastCandle.Close > (firstCandle.Open + firstCandle.Close) / 2;

           return isFirstBearish && isMiddleTouchesLow && isLastBullish && isLastAboveMidpoint;
       }

       protected override void OnStop()
       {
           // Unsubscribe from the bar updates
           Bars.LastBarUpdated -= OnBarUpdated;
       }
   }
}
 


@airsakuragi
Replies

PanagiotisCharalampous
25 Nov 2024, 06:45

Hi there,

Did you write the code or ChatGPT :) ?

Best regards,

Panagiotis


@PanagiotisCharalampous