ข้ามไปที่เนื้อหา

วิธีจัดการ cBot และอินดิเคเตอร์โดยใช้อัลกอริทึม

ฟังก์ชันอัลกอริทึมที่จัดการอัลกอริทึมอื่นช่วยให้นักเทรดสามารถเพิ่ม cBot และอินดิเคเตอร์ลงในกราฟโดยใช้โค้ด ด้วยความสามารถนี้ พวกเขาสามารถวางแผนและพัฒนากลยุทธ์การเทรดที่มีประสิทธิภาพ ปรับเปลี่ยนแบบไดนามิก ดำเนินการหลายกลยุทธ์ และใช้การควบคุมความเสี่ยงแบบอัตโนมัติ

ในบทความนี้และวิดีโอที่เกี่ยวข้อง เราจะแสดงวิธีสร้างและทำงานกับ cBot ที่จัดการอัลกอริทึมอื่น

เพิ่มอินดิเคเตอร์โดยใช้ cBot

ในแอป Algo เปิดแท็บ cBots ค้นหาและเลือกตัวอย่าง Sample Trend cBot ซึ่งใช้ค่าเฉลี่ยเคลื่อนที่

กำหนดอินดิเคเตอร์สองตัว

1
2
ChartIndicator _indicator1;
ChartIndicator _indicator2;

เพิ่มอินดิเคเตอร์สองตัวลงในกราฟ

1
2
_indicator1 = Chart.Indicators.Add("Simple Moving Average", SourceSeries, FastPeriods, MAType);
_indicator2 = Chart.Indicators.Add("Simple Moving Average", SourceSeries, SlowPeriods, MAType);

ลักษณะของอินดิเคเตอร์สามารถปรับแต่งได้ผ่านการตั้งค่าเส้นเอาต์พุต ตัวเลือกที่ปรับแต่งได้รวมถึงสี ความหนา และรูปแบบเส้น

เราจะทำให้เส้นของอินดิเคเตอร์ตัวแรกเป็นสีแดงและหนา

1
2
_indicator1.Lines[0].Color = Color.Red;
_indicator1.Lines[0].Thickness = 3;

การดำเนินการเดียวกันสามารถใช้เพื่อลบอินดิเคเตอร์ออกจากกราฟได้ตลอดเวลา เราจะทำให้การเปลี่ยนแปลงมีผลเมื่อแท่งเปลี่ยน

1
2
3
4
5
protected override void OnBarClosed()
{
    Chart.Indicators.Remove(_indicator1);
    Chart.Indicators.Remove(_indicator2);
}

คุณสามารถคัดลอกโค้ดทั้งหมดด้านล่างนี้:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AddIndicators = true)]
    public class SampleTrendcBot : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("MA Type", Group = "Moving Average")]
        public MovingAverageType MAType { get; set; }

        [Parameter("Source", Group = "Moving Average")]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Slow Periods", Group = "Moving Average", DefaultValue = 10)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", Group = "Moving Average", DefaultValue = 5)]
        public int FastPeriods { get; set; }

        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Sample Trend cBot";   

        ChartIndicator _indicator1;
        ChartIndicator _indicator2;

        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);

            _indicator1 = Chart.Indicators.Add("Simple Moving Average", SourceSeries, FastPeriods, MAType);
            _indicator2 = Chart.Indicators.Add("Simple Moving Average", SourceSeries, SlowPeriods, MAType);

            _indicator1.Lines[0].Color = Color.Red;
            _indicator1.Lines[0].Thickness = 3;
        }

        protected override void OnBarClosed()
        {
            Chart.Indicators.Remove(_indicator1);
            Chart.Indicators.Remove(_indicator2);
        }

        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, SymbolName, TradeType.Buy);
            var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);

            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)
            {
                if (shortPosition != null)
                    ClosePosition(shortPosition);

                ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label);
            }
            else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
            {
                if (longPosition != null)
                    ClosePosition(longPosition);

                ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, label);
            }
        }

        private double VolumeInUnits
        {
            get { return Symbol.QuantityToVolumeInUnits(Quantity); }
        }
    }
}

ในการสร้าง cBot ให้ใช้ปุ่มลัด Ctrl+B หรือคลิก Build

ไปที่แอป Trade เลือกกราฟ EURUSD คลิกไอคอน cBot ค้นหาและเลือก Sample Trend cBot

เมื่อหน้าต่าง เพิ่มอินสแตนซ์ ปรากฏขึ้น ให้คลิก ใช้ แล้วเริ่ม cBot

คุณควรเห็นว่าค่าเฉลี่ยเคลื่อนที่สองค่าที่ใช้สำหรับการเทรดได้ถูกเพิ่มลงในกราฟแล้ว

เริ่ม cBot โดยใช้ cBot อีกตัว

เราจะแสดงวิธีจัดการ cBot ผ่าน cBot อีกตัว ครั้งนี้ เราจะสร้าง cBot ว่างใหม่ตั้งแต่ต้น

ไปที่แอป Algo และคลิกปุ่ม ใหม่ ใต้แท็บ cBots เลือกตัวเลือก ว่างเปล่า ป้อนชื่อเช่น เพิ่ม cBots แล้วคลิก สร้าง

เราเริ่มต้นด้วยการกำหนดออบเจ็กต์โรบอทแผนภูมิสองตัว

1
2
ChartRobot _robot1;
ChartRobot _robot2;

จากนั้นเราเพิ่มโรบอทเหล่านั้นลงในกราฟในเมธอด OnStart()

1
2
_robot1 = Chart.Robots.Add("Sample Trend cBot", 0.01, MovingAverageType.Simple, Bars.ClosePrices, 10, 5);
_robot2 = Chart.Robots.Add("Sample Trend cBot", 0.01, MovingAverageType.Simple, Bars.ClosePrices, 12, 7);

เราสามารถเพิ่มตัวจัดการเหตุการณ์เพื่อพิมพ์ข้อความเมื่อใดก็ตามที่ cBot เริ่มต้น

1
2
3
4
5
6
Chart.Robots.RobotStarted += ChartRobots_RobotStarted;

private void ChartRobots_RobotStarted(ChartRobotStartedEventArgs obj)
{
    Print ("Robot Started");
}

เขียนตรรกะบางอย่างภายในเมธอด OnBarClosed() เพื่อเริ่มโรบอทตัวแรกเมื่อแท่งเปลี่ยน หยุดมัน แล้วเริ่มโรบอทตัวที่สองในแท่งถัดไป

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
protected override void OnBarClosed()
{
    if (_robot1.State == RobotState.Stopped)
    {
        _robot1.Start();
        _robot2.Stop();
            }
    else if (_robot1.State == RobotState.Running)
    {
        _robot1.Stop();
        _robot2.Start();
    }
}

คุณสามารถคัดลอกโค้ดทั้งหมดด้านล่างนี้:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None, AddIndicators = true)]
    public class AddcBots : Robot
    {

        ChartRobot _robot1;
        ChartRobot _robot2;

        protected override void OnStart()
        {
            _robot1 = Chart.Robots.Add("Sample Trend cBot", 0.01, MovingAverageType.Simple, Bars.ClosePrices, 10, 5);
            _robot2 = Chart.Robots.Add("Sample Trend cBot", 0.01, MovingAverageType.Simple, Bars.ClosePrices, 12, 7);

            Chart.Robots.RobotStarted += ChartRobots_RobotStarted;
        }

        private void ChartRobots_RobotStarted(ChartRobotStartedEventArgs obj)
        {
            Print ("Robot Started");

        }

        protected override void OnBarClosed()
        {
            if (_robot1.State == RobotState.Stopped)
            {
                _robot1.Start();
                _robot2.Stop();
            }

            else if (_robot1.State == RobotState.Running)
            {
                _robot2.Start();
                _robot1.Stop();
            }
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

หลังจากที่คุณสร้าง cBot แล้ว กลับไปที่แอป Trade ค้นหาและเลือก เพิ่ม cBots แล้วเริ่ม cBot

เมื่อกล่องโต้ตอบ คำขออนุญาต ปรากฏขึ้น ให้คลิก อนุญาต

อินสแตนซ์สองตัวของ Sample Trend cBot ควรปรากฏบนกราฟ

รอให้แท่งแรกเสร็จสมบูรณ์และคุณควรเห็นอินสแตนซ์แรกของ Sample Trend cBot เริ่มต้นโดยอัตโนมัติ

ในแท่งถัดไป คุณควรเห็นอินสแตนซ์ที่สองของ Sample Trend cBot เริ่มต้นโดยอัตโนมัติ

คุณสามารถสังเกตได้ว่า cBot ดำเนินการตามตรรกะของเราและจัดการ cBot อีกสองตัวตามเงื่อนไขที่เปลี่ยนแปลง

Modificar los parámetros del cBot durante la ejecución

Es posible que necesite cambiar los parámetros de un cBot mientras se está ejecutando. Por ejemplo, decide actualizar rápidamente el código sobre la marcha después de recibir noticias o actualizaciones financieras importantes.

En lugar de detener e iniciar nuestro cBot, vamos a modificar inmediatamente el parámetro SlowPeriods para el primer cBot.

1
2
3
4
5
6
else if(_robot1.State == RobotState.Running)
{
_robot1.Stop();
_robot1.Parameters["SlowPeriods"].Value = (int)_robot2.Parameters["SlowPeriods"].Value + 1;
_robot1.Start();
}

Ahora, reconstruiremos el cBot.

คุณสามารถคัดลอกโค้ดทั้งหมดด้านล่างนี้:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None, AddIndicators = true)]
    public class AddcBots : Robot
    {

        ChartRobot _robot1;
        ChartRobot _robot2;

        protected override void OnStart()
        {
            _robot1 = Chart.Robots.Add("Sample Trend cBot", 0.01, MovingAverageType.Simple, Bars.ClosePrices, 10, 5);
            _robot2 = Chart.Robots.Add("Sample Trend cBot", 0.01, MovingAverageType.Simple, Bars.ClosePrices, 12, 7);

            Chart.Robots.RobotStarted += ChartRobots_RobotStarted;
        }

        private void ChartRobots_RobotStarted(ChartRobotStartedEventArgs obj)
        {
            Print ("Robot Started");

        }

        protected override void OnBarClosed()
        {
            if (_robot1.State == RobotState.Stopped)
            {
                _robot1.Start();
                _robot2.Stop();
            }

            else if (_robot1.State == RobotState.Running)
            {
                _robot1.Stop();
                _robot1.Parameters["SlowPeriods"].Value = (int)_robot2.Parameters["SlowPeriods"].Value + 1;
                _robot1.Start();
            }
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

Navegue hasta la app Trade e inicie el cBot para ver cómo cambia el valor del indicador en cada barra.

สรุป

Esperamos que este artículo le haya ayudado a entender cómo usar algoritmos para iniciar, controlar y gestionar otros algoritmos.