Saltar a contenido

Cómo crear plugins para Visualización de operaciones

La capacidad de añadir nuevas pestañas a Visualización de operaciones hace que la interfaz de usuario de cTrader sea verdaderamente personalizable y adaptable a diversas necesidades de operaciones.

En este artículo y su vídeo correspondiente, demostraremos cómo añadir objetos al panel de Visualización de operaciones utilizando un plugin.

Crear un plugin

Primero crearemos un plugin de sitio web, pero nuestro plugin final será una cuadrícula de dos por dos que muestra información sobre los precios de la última barra conocida para el marco temporal M1 y el símbolo USDJPY.

Puede comenzar yendo a la aplicación Algo y luego navegando a la pestaña Plugins. Haga clic en el botón Nuevo para crear un nuevo plugin. Marque la opción Blank. Asigne un nombre a su plugin, como "Información de la barra anterior", y haga clic en el botón Crear.

Añada una nueva pestaña al panel de Visualización de operaciones y nómbrela Información de la barra anterior.

1
2
var tradeWatchTab = TradeWatch.AddTab("Previous Bar Info");
tradeWatchTab.IsSelected = true;

Añada un componente WebView simple.

1
2
3
4
var webView = new WebView();                        
tradeWatchTab.Child = webView;

webView.NavigateAsync("https://ctrader.com/");

Puede copiar el código completo a continuación:

 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
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Plugins
{
    [Plugin(AccessRights = AccessRights.None)]
    public class PreviousBarInfo : Plugin
    {
        protected override void OnStart()
        {
            var tradeWatchTab = TradeWatch.AddTab("Previous Bar Info");
            tradeWatchTab.IsSelected = true;

            var webView = new WebView();                        
            tradeWatchTab.Child = webView;

            webView.NavigateAsync("https://ctrader.com/");

        }

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

Para compilar el plugin, haga clic en el botón Compilar o use los atajos de teclado Ctrl+B.

Vaya a la aplicación Operar para ver lo que está mostrando su plugin.

Utilizando el componente WebView, puede mostrar cualquier sitio web dentro de un plugin estableciendo la URL del sitio web en el código del plugin. Además, puede crear diferentes plugins para los sitios web que utiliza en sus operaciones diarias y activarlos o desactivarlos en la configuración de cTrader.

Añadir una cuadrícula y cuadros de texto al plugin

Vuelva a la aplicación Algo y edite el código del plugin.

Necesitamos reemplazar el componente WebView con un objeto de cuadrícula y hacer que cada cuadrícula sea un elemento secundario del plugin de Visualización de operaciones.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
var grid = new Grid(2, 2) 
{
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
    ShowGridLines = true,
    Height = 150,
    Width = 150,
};

tradeWatchTab.Child = grid;

Alinee la cuadrícula al centro del plugin y muestre las líneas de la cuadrícula.

Puede copiar el código completo a continuación:

 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
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Plugins
{
    [Plugin(AccessRights = AccessRights.None)]
    public class PreviousBarInfo : Plugin
    {
        protected override void OnStart()
        {
            var tradeWatchTab = TradeWatch.AddTab("Previous Bar Info");
            tradeWatchTab.IsSelected = true;

            var grid = new Grid(2, 2) 
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                ShowGridLines = true,
                Height = 150,
                Width = 150,
            };

            tradeWatchTab.Child = grid;

        }

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

Compile el plugin, luego vaya a la aplicación Operar para ver el resultado.

Continuaremos mejorando el código del plugin en la aplicación Algo.

Declare los cuatro cuadros de texto importantes (para cada uno de los valores de apertura, máximo, mínimo y cierre) y la variable de barras.

1
2
3
4
5
TextBlock _lowBlock;
TextBlock _highBlock;
TextBlock _closeBlock;
TextBlock _openBlock;
Bars _bars;

Añada una variable de barras que recupere los datos de las barras de un minuto para el símbolo USDJPY.

1
_bars = MarketData.GetBars(TimeFrame.Minute, "USDJPY");

Inicialice los cuadros de texto y alinéelos al centro de la celda.

 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
_lowBlock = new TextBlock 
{
    Text = "Low:" + _bars.LowPrices.LastValue,
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
};

_highBlock = new TextBlock 
{
    Text = "High:" + _bars.HighPrices.LastValue,
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
};

_closeBlock = new TextBlock 
{
    Text = "Close:" +_bars.ClosePrices.LastValue,
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
};

_openBlock = new TextBlock 
{
    Text = "Open:" + _bars.OpenPrices.LastValue,
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
};

Después de inicializar los cuadros de texto, podemos añadirlos a nuestras celdas de la cuadrícula.

1
2
3
4
grid.AddChild(_lowBlock, 0, 0);
grid.AddChild(_highBlock, 0, 1);
grid.AddChild(_openBlock, 1, 0);
grid.AddChild(_closeBlock, 1, 1);

Puede copiar el código completo a continuación:

 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
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Plugins
{
    [Plugin(AccessRights = AccessRights.None)]
    public class PreviousBarInfo : Plugin
    {

        TextBlock _lowBlock;
        TextBlock _highBlock;
        TextBlock _closeBlock;
        TextBlock _openBlock;
        Bars _bars;

        protected override void OnStart()
        {
            var tradeWatchTab = TradeWatch.AddTab("Previous Bar Info");
            tradeWatchTab.IsSelected = true;

            var grid = new Grid(2, 2) 
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                ShowGridLines = true,
                Height = 150,
                Width = 150,
            };

            tradeWatchTab.Child = grid;

            _bars = MarketData.GetBars(TimeFrame.Minute, "USDJPY");

            _lowBlock = new TextBlock 
            {
                Text = "Low:" + _bars.LowPrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            _highBlock = new TextBlock 
            {
                Text = "High:" + _bars.HighPrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            _closeBlock = new TextBlock 
            {
                Text = "Close:" +_bars.ClosePrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            _openBlock = new TextBlock 
            {
                Text = "Open:" + _bars.OpenPrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            grid.AddChild(_lowBlock, 0, 0);
            grid.AddChild(_highBlock, 0, 1);
            grid.AddChild(_openBlock, 1, 0);
            grid.AddChild(_closeBlock, 1, 1);

        }

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

Compile el plugin y luego vaya a la aplicación Operar para ver los cambios.

Suscribirse a eventos

Vuelva al código fuente del plugin en la aplicación Algo. Añada las siguientes líneas de código para suscribirse al evento de tick y hacer que los valores se actualicen en cada tick:

1
2
3
4
5
6
7
8
9
_bars.Tick += _bars_Tick;

private void _bars_Tick(BarsTickEventArgs obj)
{
    _lowBlock.Text = "Low: " +_bars.LowPrices.LastValue.ToString();
    _highBlock.Text = "High: " +_bars.HighPrices.LastValue.ToString();
    _openBlock.Text = "Open: " +_bars.HighPrices.LastValue.ToString();
    _closeBlock.Text = "Close: " +_bars.HighPrices.LastValue.ToString();
}

Puede copiar el código completo a continuación:

 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
81
82
83
84
85
86
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Plugins
{
    [Plugin(AccessRights = AccessRights.None)]
    public class PreviousBarInfo : Plugin
    {
        TextBlock _lowBlock;
        TextBlock _highBlock;
        TextBlock _closeBlock;
        TextBlock _openBlock;
        Bars _bars;

        protected override void OnStart()
        {
            var tradeWatchTab = TradeWatch.AddTab("Previous Bar Info");
            tradeWatchTab.IsSelected = true;

            var grid = new Grid(2, 2) 
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                ShowGridLines = true,
                Height = 150,
                Width = 150,
            };

            tradeWatchTab.Child = grid;

            _bars = MarketData.GetBars(TimeFrame.Minute, "USDJPY");

            _lowBlock = new TextBlock 
            {
                Text = "Low:" + _bars.LowPrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            _highBlock = new TextBlock 
            {
                Text = "High:" + _bars.HighPrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            _closeBlock = new TextBlock 
            {
                Text = "Close:" +_bars.ClosePrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            _openBlock = new TextBlock 
            {
                Text = "Open:" + _bars.OpenPrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            grid.AddChild(_lowBlock, 0, 0);
            grid.AddChild(_highBlock, 0, 1);
            grid.AddChild(_openBlock, 1, 0);
            grid.AddChild(_closeBlock, 1, 1);

            _bars.Tick += _bars_Tick;            

        }

        private void _bars_Tick(BarsTickEventArgs obj)
        {
            _lowBlock.Text = "Low: " +_bars.LowPrices.LastValue.ToString();
            _highBlock.Text = "High: " +_bars.HighPrices.LastValue.ToString();
            _openBlock.Text = "Open: " +_bars.HighPrices.LastValue.ToString();
            _closeBlock.Text = "Close: " +_bars.HighPrices.LastValue.ToString();
        }

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

Compile el plugin y vaya a la aplicación Operar.

Resumen

Después de leer este artículo, creemos que ahora puede añadir sitios web, cuadrículas, cuadros de texto y otros objetos útiles al panel de Visualización de operaciones.