fermeture de toute les position à 20 heure

Created at 17 Nov 2018, 16:24
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!
FR

fringou.bfe

Joined 17.11.2018

fermeture de toute les position à 20 heure
17 Nov 2018, 16:24


bonjour, je cherche une solution pour cloturer toutes mes position à 18heure, j'ai essayer les codes cité dans le forum mais rien ne marche... voici le code de mon cbot.

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 BotUSNDAQ100m1 : Robot
    {

        private DateTime _startTime;
        private DateTime _stopTime;

        [Parameter("Start Hour", DefaultValue = 10.0)]
        public double StartTime { get; set; }
        [Parameter("Stop Hour", DefaultValue = 18.0)]
        public double StopTime { get; set; }

        [Parameter()]
        public DataSeries Prix { get; set; }
        [Parameter(DefaultValue = 32)]
        public int Period { get; set; }
        private ExponentialMovingAverage Ema;
        [Parameter(DefaultValue = 39)]
        public int Period2 { get; set; }
        private ExponentialMovingAverage Ema2;
        [Parameter(DefaultValue = 50)]
        public int Period3 { get; set; }
        private ExponentialMovingAverage Ema3;
        [Parameter(DefaultValue = 75)]
        public int Period4 { get; set; }
        private ExponentialMovingAverage Ema4;
        [Parameter(DefaultValue = 100)]
        public int Period5 { get; set; }
        private ExponentialMovingAverage Ema5;

        [Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 1)]
        public int StopLossInPips { get; set; }
        [Parameter("Take Profit (pips)", DefaultValue = 60, MinValue = 1)]
        public int TakeProfitInPips { get; set; }
        public double Quantity;
        private const string Label = "";
        private MacdCrossOver MyMacd;
//====================== Pivot ==================================//
        private int GetStartDayIndex(int index)
        {
            var startDayIndex = index;
            while (startDayIndex > 0 && MarketSeries.OpenTime[startDayIndex].Date == MarketSeries.OpenTime[index].Date)
                startDayIndex--;
            return ++startDayIndex;
        }
        private double GetHigh(int fromIndex, int toIndex)
        {
            var result = MarketSeries.High[fromIndex];
            for (var i = fromIndex; i <= toIndex; i++)
                result = Math.Max(result, MarketSeries.High[i]);
            return result;
        }
        private double GetLow(int fromIndex, int toIndex)
        {
            var result = MarketSeries.Low[fromIndex];
            for (var i = fromIndex; i <= toIndex; i++)
                result = Math.Min(result, MarketSeries.Low[i]);
            return result;
        }
//====================== Calculs des paramètres =================//
        protected override void OnStart()
        {

            _startTime = Server.Time.Date.AddHours(StartTime);
            _stopTime = Server.Time.Date.AddHours(StopTime);

            Ema = Indicators.ExponentialMovingAverage(MarketSeries.Close, Period);
            Ema2 = Indicators.ExponentialMovingAverage(MarketSeries.Close, Period2);
            Ema3 = Indicators.ExponentialMovingAverage(MarketSeries.Close, Period3);
            Ema4 = Indicators.ExponentialMovingAverage(MarketSeries.Close, Period4);
            Ema5 = Indicators.ExponentialMovingAverage(MarketSeries.Close, Period5);
            MyMacd = Indicators.MacdCrossOver(5, 11, 3);
        }
        protected override void OnBar()
        {
//========================= Horaire de trading =========================//

            var currentHours = Server.Time.TimeOfDay.TotalHours;
            bool tradeTime = StartTime < StopTime ? currentHours > StartTime && currentHours < StopTime : currentHours < StopTime || currentHours > StartTime;
            if (!tradeTime)
                return;
            if (Positions.Count != 0)
                return;

//========================= Moyennes mobiles =========================//
            var currentprix = MarketSeries.Close.Last(0);
            var currentema = Ema.Result.Last(0);
            var currentema2 = Ema2.Result.Last(0);
            var currentema3 = Ema3.Result.Last(0);
            var currentema4 = Ema4.Result.Last(0);
            var currentema5 = Ema5.Result.Last(0);
//========================= Macd =========================//            
            var signal = MyMacd.Signal[0];
            var macd = MyMacd.MACD[0];
//===================== pivot =====================================//
            if (TimeFrame == TimeFrame.Daily)
                return;
            var todayStartIndex = GetStartDayIndex(0);
            if (todayStartIndex == 0)
                return;
            var yesterdayStartIndex = GetStartDayIndex(todayStartIndex - 1);
            var yHigh = GetHigh(yesterdayStartIndex, todayStartIndex - 1);
            var yLow = GetLow(yesterdayStartIndex, todayStartIndex - 1);
            var yClose = MarketSeries.Close[todayStartIndex - 1];
            var yHigh2 = GetHigh(yesterdayStartIndex - 1, todayStartIndex - 2);
            var yLow2 = GetLow(yesterdayStartIndex - 1, todayStartIndex - 2);
            var yClose2 = MarketSeries.Close[todayStartIndex - 2];
            var pivot = (yHigh + yLow + yClose) / 3;
            var pivot2 = (yHigh2 + yLow2 + yClose2) / 3;
            var s1 = (pivot * 2) - yHigh;
            var ms1 = ((s1 - pivot) / 2 + pivot);
            var r1 = (pivot * 2) - yLow;
            var mr1 = ((r1 - pivot) / 2 + pivot);
//====================== Trade Type =================================//   
            var longPosition = Positions.Find(Label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(Label, Symbol, TradeType.Sell);

            if (Account.Equity <= 1500)
                Quantity = 1;
            if ((Account.Equity > 1500) && (Account.Equity <= 3000))
                Quantity = 2;
            if ((Account.Equity > 3000) && (Account.Equity <= 4500))
                Quantity = 3;
            if ((Account.Equity > 4500) && (Account.Equity <= 6000))
                Quantity = 4;
            if ((Account.Equity > 6000) && (Account.Equity <= 7500))
                Quantity = 5;
            if ((Account.Equity > 7500) && (Account.Equity <= 9000))
                Quantity = 6;
            if ((Account.Equity > 9000) && (Account.Equity <= 10500))
                Quantity = 7;
            if ((Account.Equity > 10500) && (Account.Equity <= 12000))
                Quantity = 8;
            if ((Account.Equity > 12000) && (Account.Equity <= 13500))
                Quantity = 9;
            if ((Account.Equity > 13500) && (Account.Equity <= 15000))
                Quantity = 10;
            if ((Account.Equity > 15000) && (Account.Equity <= 16500))
                Quantity = 11;
            if ((Account.Equity > 16500) && (Account.Equity <= 18000))
                Quantity = 12;
            if ((Account.Equity > 18000) && (Account.Equity <= 19500))
                Quantity = 13;
            if ((Account.Equity > 19500) && (Account.Equity <= 21000))
                Quantity = 14;
            if ((Account.Equity > 21000) && (Account.Equity <= 22500))
                Quantity = 15;
            if ((Account.Equity > 22500) && (Account.Equity <= 24000))
                Quantity = 16;
            if ((Account.Equity > 24000) && (Account.Equity <= 25500))
                Quantity = 17;
            if ((Account.Equity > 25500) && (Account.Equity <= 27000))
                Quantity = 18;
            if ((Account.Equity > 27000) && (Account.Equity <= 30000))
                Quantity = 19;
            if (Account.Equity > 30000)
                Quantity = 20;
//======================= Condition d'achat ===========================================//
            if ((currentprix < currentema) && (currentprix > currentema2) && (currentema2 > currentema3) && (currentema3 > currentema4) && (currentema4 > currentema5) && (longPosition == null) && (shortPosition == null) && (MyMacd.Signal.HasCrossedAbove(MyMacd.MACD, 0)))
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Quantity, Label, StopLossInPips, TakeProfitInPips);
            }
//======================= Condition de vente ===========================================//
            if ((currentprix > currentema) && (currentprix < currentema2) && (currentema2 < currentema3) && (currentema3 < currentema4) && (currentema4 < currentema5) && (shortPosition == null) && (longPosition == null) && (MyMacd.Signal.HasCrossedBelow(MyMacd.MACD, 0)))
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, Quantity, Label, StopLossInPips, TakeProfitInPips);
            }

            if (DateTime.UtcNow.Hour == 18)
            {
                ClosePosition(longPosition);
                ClosePosition(shortPosition);
            }

        }
    }
}

 


@fringou.bfe
Replies

tradermatrix
18 Nov 2018, 18:15

salut
il devrait mieux fonctionner ainsi...
attention il faut tenir compte de utc heure d hivers / heures d ete ...
par exemple en france si tu regles stoptime a 18 H le robot coupera ta commande a 19 H ...regarde le backtesting
@+

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 BotUSNDAQ100m1 : Robot
    {

        private DateTime _startTime;
        private DateTime _stopTime;

        [Parameter("Start Hour", DefaultValue = 10.0)]
        public double StartTime { get; set; }
        [Parameter("Stop Hour", DefaultValue = 18.0)]
        public double StopTime { get; set; }

        [Parameter()]
        public DataSeries Prix { get; set; }
        [Parameter(DefaultValue = 32)]
        public int Period { get; set; }
        private ExponentialMovingAverage Ema;
        [Parameter(DefaultValue = 39)]
        public int Period2 { get; set; }
        private ExponentialMovingAverage Ema2;
        [Parameter(DefaultValue = 50)]
        public int Period3 { get; set; }
        private ExponentialMovingAverage Ema3;
        [Parameter(DefaultValue = 75)]
        public int Period4 { get; set; }
        private ExponentialMovingAverage Ema4;
        [Parameter(DefaultValue = 100)]
        public int Period5 { get; set; }
        private ExponentialMovingAverage Ema5;

        [Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 1)]
        public int StopLossInPips { get; set; }
        [Parameter("Take Profit (pips)", DefaultValue = 60, MinValue = 1)]
        public int TakeProfitInPips { get; set; }
        public double Quantity;
        private const string Label = "";
        private MacdCrossOver MyMacd;
//====================== Pivot ==================================//
        private int GetStartDayIndex(int index)
        {
            var startDayIndex = index;
            while (startDayIndex > 0 && MarketSeries.OpenTime[startDayIndex].Date == MarketSeries.OpenTime[index].Date)
                startDayIndex--;
            return ++startDayIndex;
        }
        private double GetHigh(int fromIndex, int toIndex)
        {
            var result = MarketSeries.High[fromIndex];
            for (var i = fromIndex; i <= toIndex; i++)
                result = Math.Max(result, MarketSeries.High[i]);
            return result;
        }
        private double GetLow(int fromIndex, int toIndex)
        {
            var result = MarketSeries.Low[fromIndex];
            for (var i = fromIndex; i <= toIndex; i++)
                result = Math.Min(result, MarketSeries.Low[i]);
            return result;
        }
//====================== Calculs des paramètres =================//
        protected override void OnStart()
        {

            _startTime = Server.Time.Date.AddHours(StartTime);
            _stopTime = Server.Time.Date.AddHours(StopTime);

            if (Account.Equity <= 1500)
                Quantity = 10000;
            if ((Account.Equity > 1500) && (Account.Equity <= 3000))
                Quantity = 20000;
            if ((Account.Equity > 3000) && (Account.Equity <= 4500))
                Quantity = 30000;
            if ((Account.Equity > 4500) && (Account.Equity <= 6000))
                Quantity = 40000;
            if ((Account.Equity > 6000) && (Account.Equity <= 7500))
                Quantity = 50000;
            if ((Account.Equity > 7500) && (Account.Equity <= 9000))
                Quantity = 60000;
            if ((Account.Equity > 9000) && (Account.Equity <= 10500))
                Quantity = 70000;
            if ((Account.Equity > 10500) && (Account.Equity <= 12000))
                Quantity = 80000;
            if ((Account.Equity > 12000) && (Account.Equity <= 13500))
                Quantity = 90000;
            if ((Account.Equity > 13500) && (Account.Equity <= 15000))
                Quantity = 100000;
            if ((Account.Equity > 15000) && (Account.Equity <= 16500))
                Quantity = 110000;
            if ((Account.Equity > 16500) && (Account.Equity <= 18000))
                Quantity = 120000;
            if ((Account.Equity > 18000) && (Account.Equity <= 19500))
                Quantity = 130000;
            if ((Account.Equity > 19500) && (Account.Equity <= 21000))
                Quantity = 140000;
            if ((Account.Equity > 21000) && (Account.Equity <= 22500))
                Quantity = 150000;
            if ((Account.Equity > 22500) && (Account.Equity <= 24000))
                Quantity = 160000;
            if ((Account.Equity > 24000) && (Account.Equity <= 25500))
                Quantity = 170000;
            if ((Account.Equity > 25500) && (Account.Equity <= 27000))
                Quantity = 180000;
            if ((Account.Equity > 27000) && (Account.Equity <= 30000))
                Quantity = 190000;
            if (Account.Equity > 30000)
                Quantity = 200000;

            Ema = Indicators.ExponentialMovingAverage(MarketSeries.Close, Period);
            Ema2 = Indicators.ExponentialMovingAverage(MarketSeries.Close, Period2);
            Ema3 = Indicators.ExponentialMovingAverage(MarketSeries.Close, Period3);
            Ema4 = Indicators.ExponentialMovingAverage(MarketSeries.Close, Period4);
            Ema5 = Indicators.ExponentialMovingAverage(MarketSeries.Close, Period5);
            MyMacd = Indicators.MacdCrossOver(5, 11, 3);
        }

        protected override void OnTick()
        {
            var currentHours = Server.Time.TimeOfDay.TotalHours;
            bool tradeTime = StartTime < StopTime ? currentHours > StartTime && currentHours < StopTime : currentHours < StopTime || currentHours > StartTime;
            if (currentHours >= StopTime)
            {
                Close(TradeType.Sell);
                Close(TradeType.Buy);
            }
        }

        protected override void OnBar()
        {



//========================= Horaire de trading =========================//


            var currentHours = Server.Time.TimeOfDay.TotalHours;
            bool tradeTime = StartTime < StopTime ? currentHours > StartTime && currentHours < StopTime : currentHours < StopTime || currentHours > StartTime;
            if (!tradeTime)
                return;

            if (Positions.Count != 0)
                return;

//========================= Moyennes mobiles =========================//
            var currentprix = MarketSeries.Close.Last(0);
            var currentema = Ema.Result.Last(0);
            var currentema2 = Ema2.Result.Last(0);
            var currentema3 = Ema3.Result.Last(0);
            var currentema4 = Ema4.Result.Last(0);
            var currentema5 = Ema5.Result.Last(0);
//========================= Macd =========================//            
            var signal = MyMacd.Signal[0];
            var macd = MyMacd.MACD[0];
//===================== pivot =====================================//
            if (TimeFrame == TimeFrame.Daily)
                return;
            var todayStartIndex = GetStartDayIndex(0);
            if (todayStartIndex == 0)
                return;
            var yesterdayStartIndex = GetStartDayIndex(todayStartIndex - 1);
            var yHigh = GetHigh(yesterdayStartIndex, todayStartIndex - 1);
            var yLow = GetLow(yesterdayStartIndex, todayStartIndex - 1);
            var yClose = MarketSeries.Close[todayStartIndex - 1];
            var yHigh2 = GetHigh(yesterdayStartIndex - 1, todayStartIndex - 2);
            var yLow2 = GetLow(yesterdayStartIndex - 1, todayStartIndex - 2);
            var yClose2 = MarketSeries.Close[todayStartIndex - 2];
            var pivot = (yHigh + yLow + yClose) / 3;
            var pivot2 = (yHigh2 + yLow2 + yClose2) / 3;
            var s1 = (pivot * 2) - yHigh;
            var ms1 = ((s1 - pivot) / 2 + pivot);
            var r1 = (pivot * 2) - yLow;
            var mr1 = ((r1 - pivot) / 2 + pivot);
//====================== Trade Type =================================//   
            var longPosition = Positions.Find(Label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(Label, Symbol, TradeType.Sell);


//======================= Condition d'achat ===========================================//
            if ((currentprix < currentema) && (currentprix > currentema2) && (currentema2 > currentema3) && (currentema3 > currentema4) && (currentema4 > currentema5) && (longPosition == null) && (shortPosition == null) && (MyMacd.Signal.HasCrossedAbove(MyMacd.MACD, 0)))
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Quantity, Label, StopLossInPips, TakeProfitInPips);
            }
//======================= Condition de vente ===========================================//
            if ((currentprix > currentema) && (currentprix < currentema2) && (currentema2 < currentema3) && (currentema3 < currentema4) && (currentema4 < currentema5) && (shortPosition == null) && (longPosition == null) && (MyMacd.Signal.HasCrossedBelow(MyMacd.MACD, 0)))
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, Quantity, Label, StopLossInPips, TakeProfitInPips);

            }
        }

        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll(Label, Symbol, tradeType))
                ClosePosition(position);
        }

    }
}

 


@tradermatrix

fringou.bfe
19 Nov 2018, 14:30

un grand merci

je te remercie, ca marche nikel :)


@fringou.bfe