Trang này cung cấp một số ví dụ mã Python và C# để tạo plugin gốc, bao gồm các công cụ cho giao dịch thủ công hoặc giao dịch thuật toán trong cTrader.
Kho lưu trữ mẫu plugin
Các mẫu mã plugin toàn diện, bao gồm các mẫu sẵn sàng chạy cho các khu vực giao diện người dùng và chức năng khác nhau, có sẵn trong các kho lưu trữ Python và C# riêng biệt trên GitHub.
Mẹo
Sử dụng các tham số có thể tùy chỉnh trong cả plugin C# và Python để đạt được sự linh hoạt cao hơn. Các tham số có thể tùy chỉnh cho plugin C# được khai báo trong mã C# thông thường, trong khi plugin Python yêu cầu các tham số có thể tùy chỉnh được khai báo trong các tệp .cs của chúng.
Hiển thị một trang web trong khung biểu đồ
Plugin sau đây hiển thị trang web cTrader Store bên trong một khung biểu đồ riêng biệt.
Mỗi phút một lần, plugin dưới đây lưu tổng lãi và lỗ (P&L) của tài khoản vào một tệp sử dụng tính năng bộ nhớ cục bộ và dấu thời gian hiện tại làm tên tệp. Nó cũng hiển thị cùng thông tin đó trong một phần riêng biệt trong Bảng ký hiệu đang hoạt động (ASP).
importclrclr.AddReference("cAlgo.API")fromcAlgo.APIimport*classGrossPnL():defon_start(self):self.textBlock=TextBlock()self.textBlock.Text="Starting..."self.textBlock.FontSize=15self.textBlock.FontWeight=FontWeight.ExtraBoldself.textBlock.TextAlignment=TextAlignment.Centerself.textBlock.Padding=Thickness(5,5,5,5)aspBlock=api.Asp.SymbolTab.AddBlock("Gross P&L")aspBlock.Child=self.textBlockapi.Timer.Start(60)defon_timer(self):timestamp=api.Server.TimeInUtcresult=timestamp.ToString("HH mm ss")api.LocalStorage.SetString(result,f"{api.Account.UnrealizedGrossProfit}",LocalStorageScope.Device)self.textBlock.Text=f"{result}: {api.Account.UnrealizedGrossProfit}"
Hiển thị một cửa sổ riêng biệt với điều khiển tùy chỉnh
Plugin dưới đây thêm một nút tùy chỉnh vào một cửa sổ tách rời. Khi nhấp vào, điều khiển này thêm một mức chốt lời cho tất cả các vị thế mở nhưng chỉ khi một vị thế không có mức chốt lời được thiết lập trước đó.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingcAlgo.API;usingcAlgo.API.Collections;usingcAlgo.API.Indicators;usingcAlgo.API.Internals;namespacecAlgo.Plugins{[Plugin(AccessRights = AccessRights.None)]publicclassProtectionPlugin:Plugin{privateButton_buttonAddTakeProfit;privateWindow_window;protectedoverridevoidOnStart(){_buttonAddTakeProfit=newButton{BackgroundColor=Color.SeaGreen,Height=50,Text="Add Take Profit"};_buttonAddTakeProfit.Click+=AddTakeProfit;_window=newWindow{Height=150,Width=150,Padding=newThickness(5,10,10,5),};_window.Child=_buttonAddTakeProfit;_window.Show();}privatevoidAddTakeProfit(ButtonClickEventArgsargs){foreach(varpositioninPositions){if(position.TakeProfitisnull){position.ModifyTakeProfitPips(20);}}}}}
importclrclr.AddReference("cAlgo.API")fromcAlgo.APIimport*classProtectionPlugin():defon_start(self):self.buttonAddTakeProfit=Button()self.buttonAddTakeProfit.BackgroundColor=Color.SeaGreenself.buttonAddTakeProfit.Height=50self.buttonAddTakeProfit.Text="Add Take Profit"self.buttonAddTakeProfit.Click+=self.On_add_take_profit_clickself.window=Window()self.window.Height=150self.window.Width=150self.window.Padding=Thickness(5,10,10,5)self.window.Child=self.buttonAddTakeProfitself.window.Show()defOn_add_take_profit_click(self,args):forpositioninapi.Positions:ifposition.TakeProfitisNone:position.ModifyTakeProfitPips(20)
Hiển thị thông tin về giá nến trong màn hình Màn hình theo dõi giao dịch
Khi được xây dựng, plugin này thêm một tab mới vào bảng Màn hình theo dõi giao dịch. Tab này chứa một lưới hai hàng hai cột hiển thị thông tin về giá nến cuối cùng đã biết cho khung thời gian m1 và ký hiệu "USDJPY".
Plugin sau đây phát hiện ChartFrame nào hiện đang hoạt động. Bên trong một khối tùy chỉnh trong ASP, plugin hiển thị phần trăm chênh lệch giữa giá hiện tại của ký hiệu mà ChartFrame này được mở và giá của ký hiệu này khoảng một tháng trước.
usingSystem;usingcAlgo.API;usingcAlgo.API.Collections;usingcAlgo.API.Indicators;usingcAlgo.API.Internals;namespacecAlgo.Plugins{[Plugin(AccessRights = AccessRights.None)]publicclassActiveFrameChangedSample:Plugin{// Declaring the necessary UI elementsprivateGrid_grid;privateTextBlock_percentageTextBlock;privateFrame_activeFrame;protectedoverridevoidOnStart(){// Initialising the grid and the TextBlock// displaying the percentage difference_grid=newGrid(1,1);_percentageTextBlock=newTextBlock{HorizontalAlignment=HorizontalAlignment.Center,VerticalAlignment=VerticalAlignment.Center,Text="Monthly change: ",};_grid.AddChild(_percentageTextBlock,0,0);// Initialising a new block inside the ASP// and adding the grid as a childvarblock=Asp.SymbolTab.AddBlock("Monthly Change Plugin");block.Child=_grid;// Attaching a custom handler to the// ActiveFrameChanged eventChartManager.ActiveFrameChanged+=ChartManager_ActiveFrameChanged;}privatevoidChartManager_ActiveFrameChanged(ActiveFrameChangedEventArgsobj){if(obj.NewFrameisChartFrame){// Casting the Frame into a ChartFramevarnewChartFrame=obj.NewFrameasChartFrame;// Attaining market data for the symbol for which// the currently active ChartFrame is openedvardailySeries=MarketData.GetBars(TimeFrame.Daily,newChartFrame.Symbol.Name);// Calculating the monthly change and displaying it// inside the TextBlockdoublemonthlyChange=(newChartFrame.Symbol.Bid-dailySeries.ClosePrices[dailySeries.ClosePrices.Count-30])/100;_percentageTextBlock.Text=$"Monthly change: {monthlyChange}";}}}}
importclrclr.AddReference("cAlgo.API")fromcAlgo.APIimport*classActiveFrameChangedSample():defon_start(self):# Initialising the grid and the TextBlock displaying the percentage differencegrid=Grid(1,1)self.percentageTextBlock=TextBlock()self.percentageTextBlock.HorizontalAlignment=HorizontalAlignment.Centerself.percentageTextBlock.VerticalAlignment=VerticalAlignment.Centerself.percentageTextBlock.Text="Monthly change: "grid.AddChild(self.percentageTextBlock,0,0)# Initialising a new block inside the ASP and adding the grid as a childblock=api.Asp.SymbolTab.AddBlock("Monthly Change Plugin")block.Child=grid# Attaching an event handler to the ActiveFrameChanged eventapi.ChartManager.ActiveFrameChanged+=self.on_chart_manager_active_frame_changeddefon_chart_manager_active_frame_changed(self,args):ifargs.NewFrameisNoneorisinstance(args.NewFrame.__implementation__,ChartFrame)==False:returnnewChartFrame=ChartFrame(args.NewFrame)# Attaining market data for the symbol for which the currently active ChartFrame is openeddailySeries=api.MarketData.GetBars(TimeFrame.Daily,newChartFrame.Symbol.Name)# Calculating the monthly change and displaying it inside the TextBlockmonthlyChange=(newChartFrame.Symbol.Bid-dailySeries.ClosePrices[dailySeries.ClosePrices.Count-30])/100self.percentageTextBlock.Text=f"Monthly change: {monthlyChange}"