help my bot stop working

Created at 01 Sep 2020, 18:13
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!
DD

dddman1000

Joined 25.08.2018

help my bot stop working
01 Sep 2020, 18:13


sorry I am new in writing C#

after open an instance of GBPUSD 1 Minute and run for a while (1-2 hour) bot stop working. I want to run this 7-24

How could I fix this ?


using System;
using System.IO;
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.FileSystem)]


    public class algoFollowSignal : Robot
    {

        [Parameter("trade name", DefaultValue = "algo1")]
        public string tradeName { get; set; }

        [Parameter(DefaultValue = 200000)]
        public double limitLotSize { get; set; }


        // ------------------------ PRE-DEFINE -----------------------------


        public double tradeSignal;
        private string[][] param = new string[5][];


        // ------------------------ STRUCTURE -----------------------------

        protected override void OnStart()
        {
            tradeSignal = 0;
        }

        protected override void OnTick()
        {
            getSignal();
            int currentPosi = getTradeTypePosi();
            double vol1 = (Math.Floor((Account.Balance * 1.0) * (10000 / 200) / 1000)) * 1000;
            if (vol1 > limitLotSize)
                vol1 = limitLotSize;
            vol1 = 10000;

            if (tradeSignal == 0)
            {
                if (!NoOrders())
                {
                    closeOrder();
                }
            }
            if (tradeSignal == 1)
            {
                currentPosi = getTradeTypePosi();
                if (currentPosi == -1)
                    closeOrder();
                if (currentPosi == 0)
                    ExecuteMarketOrder(TradeType.Buy, Symbol.Name, vol1, tradeName);
            }
            if (tradeSignal == -1)
            {

                if (currentPosi == 1)
                    closeOrder();
                if (currentPosi == 0)
                    ExecuteMarketOrder(TradeType.Sell, Symbol.Name, vol1, tradeName);
            }

        }

        protected override void OnBar()
        {

        }


        protected void closeOrder()
        {

            foreach (var pos in Positions)
            {
                if (pos.Label == tradeName)
                    ClosePosition(pos);
            }
        }
        protected override void OnStop()
        {

        }


        // ------------------------ FUNCTION -----------------------------


        protected void getSignal()
        {
            using (var reader = new StreamReader("d:\\ctdnParameters/signal.csv"))
            {
                int i = 0;
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    var values = line.Split(',');
                    param[i] = values;
                    i++;
                }
            }
            tradeSignal = double.Parse(param[0][0]);
        }

        int getTradeTypePosi()
        {

            foreach (Position pos in Positions)
            {
                if (pos.Label == tradeName && pos.TradeType == TradeType.Buy)
                    return 1;
                if (pos.Label == tradeName && pos.TradeType == TradeType.Sell)
                    return -1;
            }

            return 0;
        }


        bool NoOrders()
        {
            foreach (Position pos in Positions)
            {
                if (pos.Label == tradeName)
                    return false;
            }

            foreach (PendingOrder po in PendingOrders)
            {
                if (po.Label == tradeName)
                    return false;
            }

            return true;
        }


        //end
    }
}

 


@dddman1000
Replies

PanagiotisCharalampous
02 Sep 2020, 07:56

Hi dddman1000,

There are only two possibilities that could make a cBot stop 1) You stopped it manually 2) There was an issue in the code e.g. an exception, and the cBot stopped. If you did not stop it manually, then check the cBot log and you will probably see the reason.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous