Category Trend  Published on 18/09/2022

MACD Market Timer

Description

 Robot with MACD signal trend. M30 -H4 GBPUSD. if the market changes the primary trend to high, change the code indicated. I'm working to adjust this 

 

 

 

 

 

 


 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None, AddIndicators = true)]
    public class MACDV1 : Robot
    {
               
       
        [Parameter("MME Slow", Group = "MA", DefaultValue = 16)]
        public int mmeSlow { get; set; }
 
        [Parameter("MME Fast", Group = "MA", DefaultValue = 12)]
        public int mmeFast { get; set; }
       
        [Parameter("Source", Group = "RSI")]
        public DataSeries Source { get; set; }
 
        [Parameter("Periods", Group = "RSI", DefaultValue = 19)]
        public int Periods { get; set; }
 
         
        [Parameter("Start Hour", DefaultValue = 10.0)]
        public double StartTime { get; set; }
 
        [Parameter("Stop Hour", DefaultValue = 12.0)]
        public double StopTime { get; set; }
                      
        [Parameter(" Period", Group="MACD",DefaultValue = 9)]
        public int Period { get; set; }
 
        [Parameter(" Long Cycle",Group="MACD", DefaultValue = 26)]
        public int LongCycle { get; set; }
 
        [Parameter(" Short Cycle",Group="MACD", DefaultValue = 12)]
        public int ShortCycle { get; set; }
    
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }
                       
        [Parameter("Stop Loss ", DefaultValue = 100)]
        public int StopLoss { get; set; }
 
        [Parameter("Take Profit", DefaultValue = 100)]
        public int TakeProfit { get; set; }
                             
        private MovingAverage i_MA_slow;
        private MovingAverage i_MA_fast;
        private RelativeStrengthIndex rsi;
        private DateTime _startTime;
        private DateTime _stopTime;               
        private MacdCrossOver macd;
        private double volumeInUnits;
                 
        protected override void OnStart()
        
        {
         
            i_MA_slow = Indicators.MovingAverage(Bars.ClosePrices, mmeSlow, MovingAverageType.Exponential);
            i_MA_fast = Indicators.MovingAverage(Bars.ClosePrices, mmeFast, MovingAverageType.Exponential);
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
            macd=Indicators.MacdCrossOver(LongCycle, ShortCycle, Period);
            volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
             {
              
             
            _startTime = Server.Time.Date.AddHours(StartTime);
         
            _stopTime = Server.Time.Date.AddHours(StopTime);
 
            Print("Start Time {0},", _startTime);
            Print("Stop Time {0},", _stopTime);
             
             } 
             
        }
                        
        protected override void OnBar()
        {  
         
        var MACDLine  = macd.MACD.Last(1);
        var PrevMACDLine = macd.MACD.Last(2);
        var Signal  = macd.Signal.Last(1);
        var PrevSignal= macd.Signal.Last(2);
         
        if (Trade.IsExecuting) return;
 
            var currentHours = Server.Time.TimeOfDay.TotalHours;
            bool tradeTime = StartTime < StopTime
                ? currentHours > StartTime && currentHours < StopTime
                : currentHours < StopTime || currentHours > StartTime;
 
            if (!tradeTime)
                return;
      
           {
           if (rsi.Result.LastValue > 25 && rsi.Result.LastValue < 70)
            {
                 
               if   ((MACDLine > Signal & PrevMACDLine <PrevSignal ) & (i_MA_fast.Result.LastValue > i_MA_slow.Result.LastValue))
               {           
                     ExecuteMarketOrder( TradeType.Buy  ,SymbolName,volumeInUnits, "MACDMarketTimerV2,RSI,MACD",StopLoss,TakeProfit);
                  }  
                                    
                            else if ( (MACDLine < Signal & PrevMACDLine >PrevSignal )&(i_MA_fast.Result.LastValue < i_MA_slow.Result.LastValue))
                            {
                    
                     ExecuteMarketOrder( TradeType.Sell ,SymbolName,volumeInUnits, " MACDMarketTimerV2,RSI,MACD",StopLoss,TakeProfit);
                   
                            }
             }
          
           } 
            
         }
                    
        
           protected override void OnStop()
        {
        }
                   
        }
    }
      

CA
carneiroads

Joined on 10.09.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: MACD V1_withSourceCode.algo
  • Rating: 0
  • Installs: 25
  • Modified: 29/10/2024 17:15
Comments
Log in to add a comment.
LU
lukaszd · 2 years ago

Thank you for this. Tested on US30 1M, works really well. Then I noticed your comment in the code. Could you please advise what to change to make it trade according to the market trend? Many thanks :)