CBot MacD Market timer help spot error free

by shane.scott.pub in category Other at 22/01/2023
Description

I downloaded a simple Macd crossover algo that was free because I'm trying to learn. Everything seems fine but what do I know right? It compiles but places no trades.

Please help. Code below.  I also cut out the trading hours thing, just to be sure it wasn't somehow interfering.  There was also a reference to Trade.Executing or something similar that was deprecated and had to be removed. The original file sans changes is attached.

Thanks a million

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)]
    public class MACDMarketTimerV2 : Robot
    {
    
     [Parameter("Sentiment: Buy", DefaultValue = true)]
        public bool Buy { get; set; }

        [Parameter("Sentiment: Sell", DefaultValue = true)]
        public bool Sell { get; set; }
    
        [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);
        
        //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 && default==Sell) && (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 && default== Buy)&(i_MA_fast.Result.LastValue < i_MA_slow.Result.LastValue))
                            {
                   
                              var result = ExecuteMarketOrder( TradeType.Sell ,SymbolName,volumeInUnits, " MACDMarketTimerV2,RSI,MACD",StopLoss,TakeProfit);
                  
                               if (result.Error == ErrorCode.NoMoney)
                               Stop();
                            }
             }
         
           } 
           
         
                   
       
           protected override void OnStop()
        {
        }
                  
        }
    }
     

 

Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
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)]
    public class MACDMarketTimerV2 : Robot
    {
    
     [Parameter("Sentiment: Buy", DefaultValue = true)]
        public bool Buy { get; set; }

        [Parameter("Sentiment: Sell", DefaultValue = true)]
        public bool Sell { get; set; }
    
        [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 & default==Sell) & (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 && default== Buy)&(i_MA_fast.Result.LastValue < i_MA_slow.Result.LastValue))
                            {
                   
                     ExecuteMarketOrder( TradeType.Sell ,SymbolName,volumeInUnits, " MACDMarketTimerV2,RSI,MACD",StopLoss,TakeProfit);
                  
                            }
             }
         
           } 
           
         }
                   
       
           protected override void OnStop()
        {
        }
                  
        }
    }
     


        
   
    
    

Comments

Host - January 23, 2023 @ 13:32

I am also learning. I started a few days ago, but I'm very focused.

If you don't mind, I made some changes, like the organization in the code structure, which is something important, and the option to decide how many operations can be performed in a row.

But, answering your question, I believe that what you did wrong is having mentioned Default==Sell and Default==Buy. In this case, it is only necessary to mention only the variant, which, if true, will follow the code.

Host - January 23, 2023 @ 13:32

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
 
namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class MACDMarketTimerV2 : Robot
    {
        [Parameter("Label", Group = "Label", DefaultValue = " MACDMarketTimerV2,RSI,MACD")]
        public string Label { get; set; }

        [Parameter("Sentiment: Buy", Group = "Basic Setup", DefaultValue = true)]
        public bool Buy { get; set; }
 
        [Parameter("Sentiment: Sell", Group = "Basic Setup", DefaultValue = true)]
        public bool Sell { get; set; }

        [Parameter("Max Trades", Group = "Basic Setup", DefaultValue = 1, MinValue = 1)]
        public int TradeCount { get; set; }

        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Stop Loss ", Group = "Protection", DefaultValue = 100)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", Group = "Protection", DefaultValue = 100)]
        public int TakeProfit { get; set; }

        [Parameter("Start Hour", Group = "Market Time", DefaultValue = 10.0)]
        public double StartTime { get; set; }

        [Parameter("Stop Hour", Group = "Market Time", DefaultValue = 12.0)]
        public double StopTime { get; set; }

        [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(" 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; }
                             
        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);    
        }

        protected override void OnBar()
        {
            if (!MarketTime())
                return;
                
            if (Positions.FindAll(Label, SymbolName).Length <= TradeCount)
            {
                SendTrade();
            }
        }
        
        private void SendTrade()
        {
            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 (rsi.Result.LastValue > 25 && rsi.Result.LastValue < 70)
            {
                if (MACDLine > Signal && PrevMACDLine < PrevSignal && Buy)
                {
                    if (i_MA_fast.Result.LastValue > i_MA_slow.Result.LastValue)
                    {
                        ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, Label, StopLoss, TakeProfit);
                    }
                }

                else if (MACDLine < Signal && PrevMACDLine > PrevSignal && Sell)
                {
                    if (i_MA_fast.Result.LastValue < i_MA_slow.Result.LastValue)
                    {
                        ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, Label, StopLoss, TakeProfit);
                    }
                }
            }
        }

        private bool MarketTime()
        {
            var CorrentHour = Server.Time.TimeOfDay.TotalHours;

                if (CorrentHour > StartTime && CorrentHour < StopTime)
                    return true;
                else
                    return false;
        }

        protected override void OnError(Error result)
        {
            if (result.Code == ErrorCode.NoMoney)
                Stop();
        }
    }
}

carneiroads - January 24, 2023 @ 05:40

Thanks for the contribution, the idea is to make mistakes and learn.  We helped each other, and we came up with something better.

carneiroads - January 24, 2023 @ 05:58

but the idea of ‚Äč‚Äčthis bot is just to choose one side of the trend if you select sell and buy it will not open orders.

anthonyclerk23 - February 02, 2023 @ 13:21

We provide premium full grain leather since it adds to the durability and long life of the garment and is only possible with the best materials and craftsmanship. Consequently, you can choose from a variety of superb leather jackets made of calfskin, goatskin, cowhide, sheepskin and lambskin. Our Website The Designer Jackets is an outstanding selection of leather bombers and biker jackets,

Visit Here:designer jackets

arraibqureshi10 - February 09, 2023 @ 10:20

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info.daman games

0