Topics
Replies

PTrader
06 Jun 2016, 12:09

RE:

Thank you Lucian, but not work.

When I click play the bot start, but at the stop loss or take profit the first position, it don't restart.

lucian said:

Try backtest with:

 



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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PTrader : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

        [Parameter("Stop Loss", DefaultValue = 40)]
        public int StopLoss { get; set; }
        [Parameter("Take Profit", DefaultValue = 40)]
        public int TakeProfit { get; set; }

        [Parameter("Start hour", DefaultValue = 11)]
        public int _start_hour { get; set; }
        [Parameter("End Hour", DefaultValue = 18)]
        public int _end_hour { get; set; }

        public bool trigger;
        private Random random = new Random();

        protected override void OnStart()
        {
            trigger = false;
            Positions.Closed += OnPositionsClosed;

        }

        protected override void OnTick()
        {


            if ((Server.Time.Hour == _start_hour) && (Positions.Count == 0) && (!trigger))
            {
                ExecuteOrder(InitialVolume, GetRandomTradeType());
                trigger = true;
            }
        }

        private void ExecuteOrder(long volume, TradeType tradeType)
        {

            var result = ExecuteMarketOrder(tradeType, Symbol, volume, "Martingale", StopLoss, TakeProfit);

            if (result.Error == ErrorCode.NoMoney)
                Stop();
        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            if (Server.Time.Hour > _start_hour)
            {
                trigger = false;
            }
            var position = args.Position;

            if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code)
                return;


            if ((Server.Time.Hour >= _start_hour) && (Server.Time.Hour < _end_hour))
            {
                if (position.GrossProfit > 0)
                {
                    ExecuteOrder(InitialVolume, GetRandomTradeType());
                }
                else
                {
                    ExecuteOrder((int)position.Volume * 2, position.TradeType);
                }
            }

        }


        private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
        }
    }
}

 

 


@PTrader

PTrader
03 Jun 2016, 12:11 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Sorry, the bot end but not auto restart. I stop and restart manual. :-(

lucian said:

The _end_hour must be higher then _start_hour. 

and

   must be between _start_hour and _end_hour.

 


@PTrader

PTrader
02 Jun 2016, 20:12 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Thank you very much, it's ok in real time, but not in backtesting. But it's fine!

Have a great week end, Lucian!

lucian said:

The _end_hour must be higher then _start_hour. 

and

   must be between _start_hour and _end_hour.

 


@PTrader

PTrader
02 Jun 2016, 14:14

RE:

Thank you. I tested cbot with changes, but don't start.

lucian said:

Sorry.

 

Change ( both OnStart and  OnPositionsClosed)

 if ((Server.Time.Hour > _start_hour) && (Server.Time.Hour < _end_hour))

 

with:

 

 if ((Server.Time.Hour >= _start_hour) && (Server.Time.Hour < _end_hour))

 


@PTrader

PTrader
02 Jun 2016, 13:03

RE:

Thank you Lucian for your reply, but the cbot don't start.

lucian said:

Try something like this:

 



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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PTrader : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

        [Parameter("Stop Loss", DefaultValue = 40)]
        public int StopLoss { get; set; }
        [Parameter("Take Profit", DefaultValue = 40)]
        public int TakeProfit { get; set; }

        [Parameter("Start hour", DefaultValue = 11)]
        public int _start_hour { get; set; }
        [Parameter("End Hour", DefaultValue = 18)]
        public int _end_hour { get; set; }


        private Random random = new Random();

        protected override void OnStart()
        {
            if ((Server.Time.Hour > _start_hour) && (Server.Time.Hour < _end_hour))
            {
                Positions.Closed += OnPositionsClosed;

                ExecuteOrder(InitialVolume, GetRandomTradeType());
            }
        }

        private void ExecuteOrder(long volume, TradeType tradeType)
        {
            var result = ExecuteMarketOrder(tradeType, Symbol, volume, "Martingale", StopLoss, TakeProfit);

            if (result.Error == ErrorCode.NoMoney)
                Stop();
        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            Print("Closed");
            var position = args.Position;

            if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code)
                return;

            if ((Server.Time.Hour > _start_hour) && (Server.Time.Hour < _end_hour))
            {
                if (position.GrossProfit > 0)
                {
                    ExecuteOrder(InitialVolume, GetRandomTradeType());
                }
                else
                {
                    ExecuteOrder((int)position.Volume * 2, position.TradeType);
                }
            }
            else
            {
                OnStart();
            }
        }

        private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
        }
    }
}

 

 


@PTrader