Ir para o conteúdo

Como criar plugins para a Observação da Negociação

A capacidade de adicionar novos separadores à Observação da Negociação torna a interface do utilizador do cTrader verdadeiramente personalizável e adaptável a várias necessidades de negociação.

Neste artigo e no vídeo correspondente, iremos demonstrar como adicionar objetos ao painel de Observação da Negociação utilizando um plugin.

Criar um plugin

Primeiro, iremos criar um plugin de website, mas o nosso plugin final será uma grelha de dois por dois que apresenta informações sobre os preços da última barra conhecida para o intervalo de tempo M1 e o símbolo USDJPY.

Pode começar por ir à aplicação Algo e depois navegar até ao separador Plugins. Clique no botão Novo para criar um novo plugin. Selecione a opção Em branco. Dê um nome ao seu plugin, como por exemplo "Informações da Barra Anterior", e clique no botão Criar.

Adicione um novo separador ao painel de Observação da Negociação e dê-lhe o nome Informações da Barra Anterior.

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

Adicione um componente WebView simples.

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

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

Pode copiar o código completo abaixo:

 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 criar o plugin, clique no botão Criar ou utilize as teclas de atalho Ctrl+B.

Vá à aplicação Trade para ver o que o seu plugin está a apresentar.

Utilizando o componente WebView, pode apresentar qualquer website dentro de um plugin, definindo o URL do website no código do plugin. Além disso, pode criar diferentes plugins para os websites que utiliza na sua negociação diária e ativá-los ou desativá-los nas definições do cTrader.

Adicionar uma grelha e caixas de texto ao plugin

Volte à aplicação Algo e edite o código do plugin.

Precisamos de substituir o componente WebView por um objeto de grelha e fazer com que cada grelha seja um elemento filho do plugin da Observação da Negociação.

 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;

Alinhe a grelha ao centro do plugin e apresente as linhas da grelha.

Pode copiar o código completo abaixo:

 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
        }
    }        
}

Crie o plugin e depois vá à aplicação Trade para ver o resultado.

Vamos continuar a melhorar o código do plugin na aplicação Algo.

Declare as quatro caixas de texto importantes (para cada um dos valores de abertura, máximo, mínimo e fecho) e a variável das barras.

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

Adicione uma variável de barras que recupera os dados das barras de minuto para o símbolo USDJPY.

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

Inicialize as caixas de texto e alinhe-as ao meio da célula.

 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,
};

Depois de inicializar as caixas de texto, podemos adicioná-las às nossas células da grelha.

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

Pode copiar o código completo abaixo:

 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
        }
    }        
}

Crie o plugin e depois vá à aplicação Trade para ver as alterações.

Subscrever eventos

Volte ao código-fonte do plugin na aplicação Algo. Adicione as seguintes linhas de código para subscrever o evento de tick e fazer com que os valores sejam atualizados a 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();
}

Pode copiar o código completo abaixo:

 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
        }
    }        
}

Crie o plugin e vá à aplicação Trade.

Resumo

Depois de ler este artigo, acreditamos que agora pode adicionar websites, grelhas, caixas de texto e outros objetos úteis ao painel de Observação da Negociação.