Bad Volume and No Money Errors

Created at 03 Mar 2023, 14:51
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!
PA

paulhowell92

Joined 01.02.2023

Bad Volume and No Money Errors
03 Mar 2023, 14:51


Hi All,

I am new to this so might be a simple fix that I have not spotted but I am getting combinations of  'BAD VOLUME' or 'NO MONEY' errors when running my cBot.

I am still using a demo account for learning and testing but the bot works well backtesting etc but when I start it on live trading I am getting these errors when trying to execute trades.

The bot is a MACD Cross and coded to execute two trades; one with a take profit and one without. If the take profit is hit the position of the second trade is modified to Breakeven/Trailing Stop with the position being closed if the MACD Lines cross again.

Like I said I am new so any advice/tips would be appreciated.

Thanks In Advance

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 MacDAUDGBP : Robot
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }
        
        [Parameter("Risk %", DefaultValue = 0.02)]
        public double RiskPct { get; set; }
        
        [Parameter("LongCycle", DefaultValue = 26)]
        public int LongCycle { get; set; }
        
        [Parameter("ShortCycle", DefaultValue = 12)]
        public int ShortCycle { get; set; }
        
        [Parameter("SignalPeriods", DefaultValue = 9)]
        public int SignalPeriods { get; set; }
        
        [Parameter("MacDShift", DefaultValue = 0)]
        public int macdShift { get; set; }
        
        //Create indicator variables
        private AverageTrueRange atr;
        private MacdCrossOver macd;
        private PriceVolumeTrend _priceVolumeTrend;
        public IndicatorDataSeries MACDdata { get; set; }
        

//-------------------------------------------------------------------------------------------------------------------------


        protected override void OnStart()
        {
            Print("Hello Paul");
            
            //Load indicators on start up
            atr = Indicators.AverageTrueRange(14, MovingAverageType.Exponential);
            macd = Indicators.MacdCrossOver(LongCycle, ShortCycle, SignalPeriods);
            _priceVolumeTrend = Indicators.PriceVolumeTrend(Bars.ClosePrices);
            
            //Calculate Trade Amount based on ATR
            var PrevATR = Math.Round(atr.Result.Last(1)/Symbol.PipSize);
            var TradeAmount = (Account.Equity*RiskPct ) / (1.5 * PrevATR*Symbol.PipValue);
            TradeAmount = Symbol.NormalizeVolumeInUnits(TradeAmount, RoundingMode.Down);
            var pricePrev = Bars.ClosePrices.Last(0);
            
            //Breakeven close Posistion event
            Positions.Closed += PositionsOnClosed;
        }
        

//-------------------------------------------------------------------------------------------------------------------------

        protected override void OnBar()
        {
            
            //Two Line Cross 
            var MACDLine = macd.MACD.Last(1);
            var PrevMACDLine = macd.MACD.Last(2);
            var Signal = macd.Signal.Last(1);
            var PrevSignal = macd.Signal.Last(2);
            
            //Check for Entry Signal
            if (MACDLine > Signal & PrevMACDLine < PrevSignal & MACDLine < 0 & Signal < 0)
            
                Open(TradeType.Buy, "MACD");
                //Close(TradeType.Sell, "MACD");
            
            if (MACDLine < Signal & PrevMACDLine > PrevSignal & MACDLine > 0 & Signal > 0)
            
                Open(TradeType.Sell, "MACD");
                //Close(TradeType.Buy, "MACD");
                
            //Check for Exit Signal
            if (MACDLine > Signal)
                Close(TradeType.Sell, "MACD");
            else if (MACDLine < Signal)
                Close(TradeType.Buy, "MACD");
                
         }
         
//-------------------------------------------------------------------------------------------------------------------------        
        
         private void Open(TradeType TradeDirection, string Label)
        {
            //Calculate Trade Amount based on ATR
            var ATR = atr.Result.Last(0) / Symbol.PipSize;
            var PrevATR = Math.Round(atr.Result.Last(1)/Symbol.PipSize);
            var TradeAmount = (Account.Equity*RiskPct ) / (1.5 * PrevATR*Symbol.PipValue);
            TradeAmount = Symbol.NormalizeVolumeInUnits(TradeAmount, RoundingMode.Down);
                    {
                    ExecuteMarketOrder(TradeDirection, SymbolName, TradeAmount/2, Label, 1.5*ATR, ATR);
                    ExecuteMarketOrder(TradeDirection, SymbolName, TradeAmount/2, Label, 1.5*ATR, null);
                    }
        }  
        
        
//-------------------------------------------------------------------------------------------------------------------------


         private void Close(TradeType TradeDirection, string Label)
            {
            foreach (var position in Positions.FindAll(Label, SymbolName, TradeDirection))
                ClosePosition(position);
            }   
         

//-------------------------------------------------------------------------------------------------------------------------         
         
            //Function for after a position is Closed      
         private void PositionsOnClosed(PositionClosedEventArgs args)
        
        {
            //Check if take profit was hit
            var ATR = atr.Result.Last(0) / Symbol.PipSize;
            var PrevATR = Math.Round(atr.Result.Last(1)/Symbol.PipSize);
            var pricePrev = Bars.ClosePrices.Last(0);
            
            if (args.Reason == PositionCloseReason.TakeProfit)
            {
                var position = Positions.Find("MACD");
                ModifyPosition(position, position.EntryPrice, null, true);
            }
        }   
        

//-------------------------------------------------------------------------------------------------------------------------


        protected override void OnStop()
        {
            
        }
        
        
    }
 }


@paulhowell92