Replies

duccauca
21 Dec 2021, 11:20 ( Updated at: 21 Dec 2021, 11:25 )

Hi amusleh

Thanks you. I will code following your suggest.


@duccauca

duccauca
21 Dec 2021, 04:48 ( Updated at: 21 Dec 2023, 09:22 )

Hi amusleh

Thank for your reply. I'll use your guide to code cBot.

I'd like to ask you how to get the remaining ticks, add to the code above:

 


@duccauca

duccauca
18 Dec 2021, 03:38 ( Updated at: 18 Dec 2021, 09:15 )

Hi amusleh

I try draw Ticks to chart, but my code error. _tickBars.Tick that's the ticks needed for robots?

Please help me to check.

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Toan012speedforumreplyL5tick : Robot
    {
        [Parameter("Position Label", DefaultValue = "BuyLabel")]
        public string BuyLabel { get; set; }

        [Parameter("Position Label", DefaultValue = "SejLabel")]
        public string SejLabel { get; set; }

        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Stop Loss (pips)", Group = "Protection", DefaultValue = 30, MinValue = 1)]
        public int StopLossInPips { get; set; }

        [Parameter("Take Profit (pips)", Group = "Protection", DefaultValue = 60, MinValue = 1)]
        public int TakeProfitInPips { get; set; }

        [Parameter("Candle", DefaultValue = 20, MinValue = 1)]
        public int CandleLeng { get; set; }

        [Parameter("StopDistance", DefaultValue = 3, MinValue = 1)]
        public int StopDistance { get; set; }
        private int BuyL1, SejL1;
        private int BuyId, SejId;

        private Bars _tickBars;

        protected override void OnStart()
        {
            Positions.Opened += PositionsOnOpened;
            Positions.Closed += PositionsOnClosed;
            _tickBars = MarketData.GetBars(TimeFrame.Tick200);
            _tickBars.Tick += _tickBars_Tick;

        }

        private void _tickBars_Tick(BarsTickEventArgs obj)
        {

        }

        private void PositionsOnOpened(PositionOpenedEventArgs args)
        {
            var position = args.Position;

            if (position.Label == BuyLabel)
            {
                BuyId = position.Id;
                Print("Position open= {0}. Id= {1}, waiting for trigger", position.Label, BuyId);
            }
            if (position.Label == SejLabel)
            {
                SejId = position.Id;
                Print("Position open= {0}. Id= {1}, waiting for trigger", position.Label, SejId);
            }
        }

        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            if (position.Label == BuyLabel)
                if (position.Id == BuyId)
                {
                    BuyId = 0;
                    BuyL1 = 0;
                }
            if (position.Label == SejLabel)
                if (position.Id == SejId)
                {
                    SejId = 0;
                    SejL1 = 0;
                }
        }

        protected override void OnTick()
        {

            var stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("_tickBars: " + _tickBars + "  _tickBars.Tick: " + _tickBars.Tick);
            Chart.DrawStaticText("text", stringBuilder.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.Red);

            double HighN0 = Bars.HighPrices.Last(0);
            double LowN0 = Bars.LowPrices.Last(0);
            double BuyDistance = (Symbol.Ask - LowN0) / 1E-05;
            double SejDistance = (HighN0 - Symbol.Bid) / 1E-05;

            //buy stop
            if (BuyDistance > CandleLeng && BuyL1 == 0)
            {
                DateTime ExpirationTime = Server.Time.AddMinutes(10);
                PlaceStopOrder(TradeType.Buy, SymbolName, 100000, Symbol.Ask + StopDistance * Symbol.PipSize, BuyLabel, StopLossInPips, TakeProfitInPips, ExpirationTime);
                BuyL1 = 1;
                Print("Buy stop. LowN0= ", LowN0, "  Ask= ", Symbol.Ask, "  Open= ", Symbol.Ask + StopDistance * Symbol.PipSize, "  Distance=", Symbol.Ask - LowN0);
            }
            //buy stop end

            //sej stop
            if (SejDistance > CandleLeng && SejL1 == 0)
            {
                DateTime ExpirationTime = Server.Time.AddMinutes(10);
                PlaceStopOrder(TradeType.Sell, SymbolName, 100000, Symbol.Bid - StopDistance * Symbol.PipSize, SejLabel, StopLossInPips, TakeProfitInPips, ExpirationTime);
                SejL1 = 1;
                Print("Sej stop. HighN0= ", HighN0, "  Bid= ", Symbol.Bid, "  Open= ", Symbol.Bid - StopDistance * Symbol.PipSize, "  Distance=", HighN0 - Symbol.Bid);
            }
            //sej stop end

        }
    }

}

 


@duccauca

duccauca
16 Dec 2021, 16:37

Hi amusleh

OK. I'm was open new thread.


@duccauca

duccauca
16 Dec 2021, 11:46 ( Updated at: 16 Dec 2021, 11:50 )

Hi amusleh

I forgot not to live trade. So it worked ok. It's a shame that having a back test is too easy to test effective of Robot. I'll try using this data for Robot.

If you have any examples of tick charts for Robot sent to me please. because I look at the tutorial and try it still can't.

Thank you very much


@duccauca

duccauca
16 Dec 2021, 09:15

Hi amusleh

Thanks for your reply. I'm try get data deep market. I write to the chart and all data always = 0:_askVolumeTotal , _bidVolumeTotal ,_askNo ,_bidNo.

So these data I haven't used to make EA calculate. Is it wrong to export data to the chart ?

Please help me to check

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Test : Robot
    {
        private int _askNo;
        private int _bidNo;
        private double _askVolumeTotal;
        private double _bidVolumeTotal;

        private MarketDepth _marketDepth;

        protected override void OnStart()
        {
            _marketDepth = MarketData.GetMarketDepth(SymbolName);

            _marketDepth.Updated += MarketDepth_Updated;
        }

        private void MarketDepth_Updated()
        {
            var askNo = 0;
            var bidNo = 0;
            var askVolumeTotal = 0.0;
            var bidVolumeTotal = 0.0;

            foreach (var entry in _marketDepth.AskEntries)
            {
                askVolumeTotal += entry.VolumeInUnits;
                askNo++;
            }

            foreach (var entry in _marketDepth.BidEntries)
            {
                bidVolumeTotal += entry.VolumeInUnits;
                bidNo++;
            }

            _askVolumeTotal = askVolumeTotal;
            _bidVolumeTotal = bidVolumeTotal;
            _askNo = askNo;
            _bidNo = bidNo;
        }

        protected override void OnTick()
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("_askVolumeTotal: " + _askVolumeTotal + "   _bidVolumeTotal: " + _bidVolumeTotal + "   _askNo: " + _askNo + "   _bidNo: " + _bidNo);
            Chart.DrawStaticText("text", stringBuilder.ToString(), VerticalAlignment.Top, HorizontalAlignment.Left, Color.Red);
        }
    }
}

 


@duccauca

duccauca
15 Dec 2021, 06:39

Thanks amusleh for your reply. I was write text to chart & expire time ok.

 

I read your link for deep market and read carefuly all help for deep market syntax and try write code as following:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Toan008speedpostforumL3 : Robot
    {
        [Parameter("Position Label", DefaultValue = "BuyLabel")]
        public string BuyLabel { get; set; }

        [Parameter("Position Label", DefaultValue = "SejLabel")]
        public string SejLabel { get; set; }

        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Stop Loss (pips)", Group = "Protection", DefaultValue = 30, MinValue = 1)]
        public int StopLossInPips { get; set; }

        [Parameter("Take Profit (pips)", Group = "Protection", DefaultValue = 60, MinValue = 1)]
        public int TakeProfitInPips { get; set; }

        [Parameter("Candle", DefaultValue = 20, MinValue = 1)]
        public int CandleLeng { get; set; }

        [Parameter("StopDistance", DefaultValue = 3, MinValue = 1)]
        public int StopDistance { get; set; }

        private int BuyL1, SejL1;
        private int BuyId, SejId;
        /*
        [Parameter("BidEntries", DefaultValue = 0, MinValue = 0)]
        public IReadonlyList BidEntries { get; }
        */
        //public Ticks Ticks { get; }

        protected override void OnStart()
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("BuyL1: " + BuyL1 + "  SejL1: " + SejL1);
            Chart.DrawStaticText("text", stringBuilder.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.Red);

            Positions.Opened += PositionsOnOpened;
            Positions.Closed += PositionsOnClosed;
        }

        private void PositionsOnOpened(PositionOpenedEventArgs args)
        {
            var position = args.Position;

            if (position.Label == BuyLabel)
            {
                BuyId = position.Id;
                Print("Position open= {0}. Id= {1}, waiting for trigger", position.Label, BuyId);
            }
            if (position.Label == SejLabel)
            {
                SejId = position.Id;
                Print("Position open= {0}. Id= {1}, waiting for trigger", position.Label, SejId);
            }
        }

        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            if (position.Label == BuyLabel)
                if (position.Id == BuyId)
                {
                    BuyId = 0;
                    BuyL1 = 0;
                }
            if (position.Label == SejLabel)
                if (position.Id == SejId)
                {
                    SejId = 0;
                    SejL1 = 0;
                }
        }

        protected override void OnTick()
        {

            double HighN0 = Bars.HighPrices.Last(0);
            double LowN0 = Bars.LowPrices.Last(0);
            double BuyDistance = (Symbol.Ask - LowN0) / 1E-05;
            double SejDistance = (HighN0 - Symbol.Bid) / 1E-05;

            //buy stop
            if (BuyDistance > CandleLeng && BuyL1 == 10)
            {
                DateTime ExpirationTime = Server.Time.AddMinutes(10);
                PlaceStopOrder(TradeType.Buy, SymbolName, 100000, Symbol.Ask + StopDistance * Symbol.PipSize, BuyLabel, StopLossInPips, TakeProfitInPips, ExpirationTime);
                BuyL1 = 1;
                Print("Buy stop. LowN0= ", LowN0, "  Ask= ", Symbol.Ask, "  Open= ", Symbol.Ask + StopDistance * Symbol.PipSize, "  Distance=", Symbol.Ask - LowN0);
            }
            //buy stop end

            //sej stop
            if (SejDistance > CandleLeng && SejL1 == 10)
            {
                DateTime ExpirationTime = Server.Time.AddMinutes(10);
                PlaceStopOrder(TradeType.Sell, SymbolName, 100000, Symbol.Bid - StopDistance * Symbol.PipSize, SejLabel, StopLossInPips, TakeProfitInPips, ExpirationTime);
                SejL1 = 1;
                Print("Sej stop. HighN0= ", HighN0, "  Bid= ", Symbol.Bid, "  Open= ", Symbol.Bid - StopDistance * Symbol.PipSize, "  Distance=", HighN0 - Symbol.Bid);
            }
            //sej stop end

            //stringBuilder.AppendLine("Ticks: " + Ticks);
            //Chart.DrawStaticText("text", stringBuilder.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.Red);

        }
    }
        /*
foreach (var entry in _marketDepth.BidEntries)
{
     volume  = entry.Volume;
        entryPrice = entry.Price;
}
*/

    }

But this code error. I simply want to take out a specific data such as the total buy or sell volume ( or price of volume, other parameters) to support the calculation of the order. And the your link for indicators is just to show the screen, so I don't know how to get that data for robot. Can you introduce me deep market data example of the robot?

In addition, I tried to take data from the tick , for example selecting the tick chart =200, and robot get the remaining tick number (e.g. 50) of the tick chart 200 but my code error so that I close error code by /* */. Can you show me how to get the above two data from the tick chart for the robot?

Best Regards,

Duccauca


@duccauca

duccauca
11 Dec 2021, 05:40

Hi amusleh

Today I've programmed some content according to your instructions as follows:

Code cBot run ok, backtest for gbpusd from 1/11/21 to 10-12-21 with H1 (temporarily remove the error parts to run):

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

namespace cAlgo.Robots
{
    [Robot("PendingTestRobott")]
    public class PendingTestRobot : Robot
    {
        [Parameter("Position Label", DefaultValue = "BuyLabel")]
        public string BuyLabel { get; set; }

        [Parameter("Position Label", DefaultValue = "SejLabel")]
        public string SejLabel { get; set; }

        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Stop Loss (pips)", Group = "Protection", DefaultValue = 30, MinValue = 1)]
        public int StopLossInPips { get; set; }

        [Parameter("Take Profit (pips)", Group = "Protection", DefaultValue = 60, MinValue = 1)]
        public int TakeProfitInPips { get; set; }

        [Parameter("Candle", DefaultValue = 20, MinValue = 1)]
        public int CandleLeng { get; set; }

        [Parameter("StopDistance", DefaultValue = 3, MinValue = 1)]
        public int StopDistance { get; set; }

        int BuyL1, SejL1;
        int BuyId, SejId;

        protected override void OnStart()
        {
            Positions.Opened += PositionsOnOpened;
            Positions.Closed += PositionsOnClosed;
        }
        private void PositionsOnOpened(PositionOpenedEventArgs args)
        {
            var position = args.Position;

            if (position.Label == BuyLabel)
            {
                BuyId = position.Id;
                Print("Position open= {0}. Id= {1}, waiting for trigger", position.Label, BuyId);
            }
            if (position.Label == SejLabel)
            {
                SejId = position.Id;
                Print("Position open= {0}. Id= {1}, waiting for trigger", position.Label, SejId);
            }
        }

        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            if (position.Label == BuyLabel)
                if (position.Id == BuyId)
                {
                    BuyId = 0;
                    BuyL1 = 0;
                }
            if (position.Label == SejLabel)
                if (position.Id == SejId)
                {
                    SejId = 0;
                    SejL1 = 0;
                }
        }

        protected override void OnTick()
        {

            double HighN0 = MarketSeries.High.Last(0);
            double LowN0 = MarketSeries.Low.Last(0);
            double BuyDistance = (Symbol.Ask - LowN0) / 1E-05;
            double SejDistance = (HighN0 - Symbol.Bid) / 1E-05;

            //buy stop
            if (BuyDistance > CandleLeng && BuyL1 == 0)
            {
                /*
                //ExpirationTime error start:
                //DateTime ExpirationTime = Server.Time.AddMinute(10);
                //PlaceStopOrder(TradeType.Buy, Symbol, 100000, Symbol.Ask + StopDistance * Symbol.PipSize, BuyLabel, StopLossInPips,TakeProfitInPips,ExpirationTime);
                //ExpirationTime error end
                */
                //no ExpirationTime (no error):
                PlaceStopOrder(TradeType.Buy, Symbol, 100000, Symbol.Ask + StopDistance * Symbol.PipSize, BuyLabel, StopLossInPips, TakeProfitInPips);
                BuyL1 = 1;
                Print("Buy stop. LowN0= ", LowN0, "  Ask= ", Symbol.Ask, "  Open= ", Symbol.Ask + StopDistance * Symbol.PipSize, "  Distance=", Symbol.Ask - LowN0);
            }
            //buy stop end

            //sej stop
            if (SejDistance > CandleLeng && SejL1 == 0)
            {
                PlaceStopOrder(TradeType.Sell, Symbol, 100000, Symbol.Bid - StopDistance * Symbol.PipSize, SejLabel, StopLossInPips, TakeProfitInPips);
                SejL1 = 1;
                Print("Sej stop. HighN0= ", HighN0, "  Bid= ", Symbol.Bid, "  Open= ", Symbol.Bid - StopDistance * Symbol.PipSize, "  Distance=", HighN0 - Symbol.Bid);
            }
            //sej stop end
        }
    }
    /*            
            //draw text error start:
            var stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("BuyL1: " + BuyL1 + "  SejL1: " + SejL1);
            //draw text error end:
            */

    //public class
}

==> In this Bot, part 4 allows Bot01 to open a new order....(reset new order by Id) =>  ok.

And some of the error parts I framed /**/ to temporarily not run:

Part 3 (error ExpirationTime) please help me to write the ExpirationTime+ comments for order:

                //ExpirationTime error start:
                //DateTime ExpirationTime = Server.Time.AddMinute(10);
                //PlaceStopOrder(TradeType.Buy, Symbol, 100000, Symbol.Ask + StopDistance * Symbol.PipSize, BuyLabel, StopLossInPips,TakeProfitInPips,ExpirationTime);
                //ExpirationTime error end

Part 2- write text or parameters of the variables controlled out of the chart  (error) :

            //draw text error start:
            var stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("BuyL1: " + BuyL1 + "  SejL1: " + SejL1);
            //draw text error end 

Please help me to write 

 

Best Regards,

Duccauca

 

 


@duccauca

duccauca
09 Dec 2021, 15:48

Hello amusleh

Thank you for your response. I will do the code according to your instructions and will continue to respond in more detail.


@duccauca