跳转至

如何创建自定义窗口插件

插件允许您创建包含网站、工具或按钮的自定义窗口,这些按钮在点击时会执行特定操作。 在本文及其对应的视频中,我们将向您展示如何使用插件创建包含操作按钮的自定义窗口。

创建插件

我们将创建一个带有按钮的自定义窗口,当点击该按钮时,它会为所有未平仓头寸设置 止盈。 我们将从窗口和按钮元素开始。

选择 Algo 应用程序并转到 Plugins 选项卡。 点击新建按钮。 确保选择了 Blank 模板。 为您的插件输入一个名称,例如“Custom Window Plugin”,然后点击 Create

声明按钮和窗口。

1
2
private Button _buttonAddTakeProfit;
private Window _window;

初始化按钮。

1
2
3
4
5
6
_buttonAddTakeProfit = new Button
{
    BackgroundColor = Color.SeaGreen,
    Height = 50,
    Text = "Add Take Profit"
};

初始化窗口并将按钮作为其子元素添加。

1
2
3
4
5
6
7
8
9
_window = new Window
{
    Height = 150,
    Width = 150,
    Padding = new Thickness(5, 10, 10, 5)
};

_window.Child = _buttonAddTakeProfit;
_window.Show();

您可以复制以下完整代码:

 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
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 CustomWindowPlugin : Plugin

    {

        private Button _buttonAddTakeProfit;
        private Window _window;

        protected override void OnStart()
        {

            _buttonAddTakeProfit = new Button
            {
                BackgroundColor = Color.SeaGreen,
                Height = 50,
                Text = "Add Take Profit"
            };

            _window = new Window
            {
                Height = 150,
                Width = 150,
                Padding = new Thickness(5, 10, 10, 5)
            };

            _window.Child = _buttonAddTakeProfit;
            _window.Show();

        }

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

点击 Build 按钮或使用 Ctrl+B 快捷键构建插件。 一个带有 Add Take Profit 按钮的自定义窗口应该会出现。

您可以移动、隐藏、调整大小或关闭窗口。

优化插件

我们将通过返回源代码并添加一个处理按钮点击事件的事件来优化插件。

1
_buttonAddTakeProfit.Click += _buttonAddTakeProfit_Click;

添加为任何缺少止盈的头寸设置止盈选项的逻辑。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
private void _buttonAddTakeProfit_Click(ButtonClickEventArgs args)
{
    foreach (var position in Positions)
    {
        if (position.TakeProfit is null)
        {
            position.ModifyTakeProfitPips(20);
        }
    }
}

您可以复制以下完整代码:

 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
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 CustomWindowPlugin : Plugin

    {
        private Button _buttonAddTakeProfit;
        private Window _window;

        protected override void OnStart()
        {
            _buttonAddTakeProfit = new Button
            {
                BackgroundColor = Color.SeaGreen,
                Height = 50,
                Text = "Add Take Profit"
            };

            _buttonAddTakeProfit.Click += _buttonAddTakeProfit_Click;

            _window = new Window
            {
                Height = 150,
                Width = 150,
                Padding = new Thickness(5, 10, 10, 5)
            };

            _window.Child = _buttonAddTakeProfit;
            _window.Show();
        }

        private void _buttonAddTakeProfit_Click(ButtonClickEventArgs args)
        {
            foreach (var position in Positions)
            {
                if (position.TakeProfit is null)
                {
                    position.ModifyTakeProfitPips(20);
                }
            }
        }        

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

构建插件。 转到 Trade 应用程序,如果没有未平仓头寸,请打开一些头寸,并使用 Add Take Profit 按钮确认按钮是否正常工作。

总结

我们相信本文已经教会您如何创建包含操作按钮和其他有用元素的自定义窗口。