cAlgo works on backtest, but fails to run on demo or live account

Created at 03 Apr 2017, 12:09
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!
JI

jivy.int

Joined 08.09.2016

cAlgo works on backtest, but fails to run on demo or live account
03 Apr 2017, 12:09


Hi,

Im pretty sure i made a post about this earlier but it might have been deleted or I simply forgot to create the thread. If this is the wrong place to ask the question, kindly let me know.

 

Anyways, someone made a cAlgo bot for me the other day, but it simply wont run on a demo or live account although it works when I backtest it. Im not the best at coding but if someone can direct me to where the issue is, i'd be quite happy. ive not even sure its due to coding but I thought i should probably ask first. I've bolded where I think the issue might be down below, but im not entirely sure. So before i play around with it, I wanted someone with more experience to possibly point me in the right direction.

 

Thanks

Code:

--------

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

namespace cAlgo
{
    [Robot(TimeZone = 

TimeZones.UTC, AccessRights = AccessRights.None)]
    public class JivyycBotV1b : Robot
    {
        //+------------------------------------------------------------------+

        [Parameter("ID", DefaultValue = "100")]
        public string ID { get; set; }


        [Parameter("Strategy 1", DefaultValue = "~~~~~~~~~")]
        public string str1 { get; set; }

        [Parameter("Pips", DefaultValue = 30.0)]
        public double pip1 { get; set; }

        [Parameter("Leverage", DefaultValue = 20.0)]
        public double lvrg1 { get; set; }

        [Parameter("Minutes", DefaultValue = 60, MinValue = 1)]
        public int mnt1 { get; set; }


        [Parameter("Strategy 2", DefaultValue = "~~~~~~~~~")]
        public string str2 { get; set; }

        [Parameter("Pips", DefaultValue = 300.0)]
        public double pip2 { get; set; }

        [Parameter("Leverage", DefaultValue = 5.0)]
        public double lvrg2 { get; set; }

        [Parameter("Minutes", DefaultValue = 60, MinValue = 1)]
        public int mnt2 { get; set; }


        [Parameter("Strategy 3", DefaultValue = "~~~~~~~~~")]
        public string str3 { get; set; }

        [Parameter("Pips", DefaultValue = 300.0)]
        public double pip3 { get; set; }

        [Parameter("Leverage", DefaultValue = 0.0)]
        public double lvrg3 { get; set; }

        [Parameter("Minutes", DefaultValue = 360, MinValue = 1)]
        public int mnt3 { get; set; }


        [Parameter("Strategy 4", DefaultValue = "~~~~~~~~~")]
        public string str4 { get; set; }

        [Parameter("Pips", DefaultValue = 300.0)]
        public double pip4 { get; set; }

        [Parameter("Leverage", DefaultValue = 12.0)]
        public double lvrg4 { get; set; }

        [Parameter("Minutes", DefaultValue = 90, MinValue = 1)]
        public int mnt4 { get; set; }


        [Parameter("Strategy 5", DefaultValue = "~~~~~~~~~")]
        public string str5 { get; set; }

        [Parameter("Pips", DefaultValue = 300.0)]
        public double pip5 { get; set; }

        [Parameter("Leverage", DefaultValue = 0.0)]
        public double lvrg5 { get; set; }

        [Parameter("Minutes", DefaultValue = 1200, MinValue = 1)]
        public int mnt5 { get; set; }

        //+------------------------------------------------------------------+

        protected override void OnStart()
        {
            if (Time.Year >= 2016 && Time.Month >= 3 && Time.Day > 2)
            {
                ChartObjects.DrawText("Demo_Expired", "DEMO EXPIRED!!!\nDEMO EXPIRED!!!\nDEMO EXPIRED!!!\nDEMO EXPIRED!!!\nDEMO EXPIRED!!!\n", StaticPosition.TopLeft, Colors.Red);
                OnStop();
            }
        }


        //+------------------------------------------------------------------+

        protected override void OnTick()
        {

            if (Time.Year >= 2016 && Time.Month >= 3 && Time.Day > 1)
                return;

            bool CondBuy = false;
            bool CondSell = false;
            long vol = 0;
            int nBuy = 0;
            int nSell = 0;
            int nAll = 0;
            string cmt = "";

            CountOrder(ref nBuy, ref nSell, ref nAll);

            if (nAll <= 0)
            {
                CheckSignal(ref CondBuy, ref CondSell, ref vol, ref cmt);
                if (CondBuy)
                    ExecuteMarketOrder(TradeType.Buy, Symbol, vol, ID, 0, 0, 0, cmt);
                if (CondSell)
                    ExecuteMarketOrder(TradeType.Sell, Symbol, vol, ID, 0, 0, 0, cmt);
            }
            else
                CheckToClose();
        }

        //+------------------------------------------------------------------+

        private void CheckSignal(ref bool CondBuy, ref bool CondSell, ref long vol, ref string cmt)
        {
            if ((MarketData.GetSeries(TimeFrame.Minute15).Close.Last(1) - MarketData.GetSeries(TimeFrame.Minute15).Open.Last(1)) / Symbol.PipSize >= pip1)
            {
                CondBuy = true;
                cmt = mnt1.ToString();
                vol = (long)Account.Balance;
                vol = Symbol.NormalizeVolume(lvrg1 * vol, RoundingMode.Down);
                if (vol < Symbol.VolumeMin)
                    vol = Symbol.VolumeMin;
            }
            else if ((MarketData.GetSeries(TimeFrame.Minute15).Open.Last(1) - MarketData.GetSeries(TimeFrame.Minute15).Close.Last(1)) / Symbol.PipSize >= pip2)
            {
                CondSell = true;
                cmt = mnt2.ToString();
                vol = (long)Account.Balance;
                vol = Symbol.NormalizeVolume(lvrg2 * vol, RoundingMode.Down);
                if (vol < Symbol.VolumeMin)
                    vol = Symbol.VolumeMin;
            }
            else if ((MarketData.GetSeries(TimeFrame.Minute15).Close.Last(1) - MarketData.GetSeries(TimeFrame.Minute15).Low.Last(1)) / Symbol.PipSize >= pip3)
            {
                CondBuy = true;
                cmt = mnt3.ToString();
                vol = (long)Account.Balance;
                vol = Symbol.NormalizeVolume(lvrg3 * vol, RoundingMode.Down);
                if (vol < Symbol.VolumeMin)
                    vol = Symbol.VolumeMin;
            }
            else if ((MarketData.GetSeries(TimeFrame.Minute15).High.Last(1) - MarketData.GetSeries(TimeFrame.Minute15).Close.Last(1)) / Symbol.PipSize >= pip4)
            {
                CondSell = true;
                cmt = mnt4.ToString();
                vol = (long)Account.Balance;
                vol = Symbol.NormalizeVolume(lvrg4 * vol, RoundingMode.Down);
                if (vol < Symbol.VolumeMin)
                    vol = Symbol.VolumeMin;
            }
            else if ((MarketData.GetSeries(TimeFrame.Minute15).High.Last(1) - MarketData.GetSeries(TimeFrame.Minute15).Low.Last(1)) / Symbol.PipSize >= pip5)
            {
                CondBuy = true;
                cmt = mnt5.ToString();
                vol = (long)Account.Balance;
                vol = Symbol.NormalizeVolume(lvrg5 * vol, RoundingMode.Down);
                if (vol < Symbol.VolumeMin)
                    vol = Symbol.VolumeMin;
            }
        }

        //+------------------------------------------------------------------+

        private void CountOrder(ref int nBuy, ref int nSell, ref int nAll)
        {
            nBuy = nSell = nAll = 0;

            nBuy = Positions.FindAll(ID, Symbol, TradeType.Buy).Length;
            nSell = Positions.FindAll(ID, Symbol, TradeType.Sell).Length;
            nAll = nBuy + nSell;
        }

        //+------------------------------------------------------------------+

        private void CheckToClose()
        {
            string cmt = "";
            DateTime OpenTime;
            DateTime CloseTime;

            foreach (var position in Positions.FindAll(ID, Symbol))
            {
                cmt = position.Comment;
                OpenTime = position.EntryTime;
                CloseTime = OpenTime.AddMinutes(int.Parse(cmt));
                if (Time >= CloseTime)
                    ClosePosition(position);
            }
        }

        //+------------------------------------------------------------------+

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }

        //+------------------------------------------------------------------+
    }
}


@jivy.int
Replies

Jiri
03 Apr 2017, 13:52

Hi, just remove or comment following lines.

if (Time.Year >= 2016 && Time.Month >= 3 && Time.Day > 2)
{
    ChartObjects.DrawText("Demo_Expired", "DEMO EXPIRED!!!\nDEMO EXPIRED!!!\nDEMO EXPIRED!!!\nDEMO EXPIRED!!!\nDEMO EXPIRED!!!\n", StaticPosition.TopLeft, Colors.Red);
    OnStop();
}
if (Time.Year >= 2016 && Time.Month >= 3 && Time.Day > 1)
    return;

 


@Jiri