이 페이지는 cTrader에서 수동 또는 알고리즘 트레이딩을 위한 도구를 포함한 네이티브 플러그인을 만들기 위한 여러 Python 및 C# 코드 예제를 제공합니다.
플러그인 샘플 저장소
다양한 UI 영역 및 기능을 위한 실행 준비가 된 템플릿을 포함한 포괄적인 플러그인 코드 샘플은 GitHub의 별도 Python 및 C# 저장소에서 사용할 수 있습니다.
팁
C# 및 Python 플러그인 모두에서 사용자 정의 가능한 매개변수를 사용하여 더 큰 유연성을 달성하세요. C# 플러그인의 사용자 정의 가능한 매개변수는 일반 C# 코드에서 선언되며, Python 플러그인은 .cs 파일에서 사용자 정의 가능한 매개변수를 선언해야 합니다.
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}"
사용자 지정 컨트롤이 있는 별도 창 표시
아래 플러그인은 분리된 창에 사용자 지정 버튼을 추가합니다. 클릭 시 컨트롤은 포지션에 이전에 설정된 이익실현이 없는 경우에만 모든 열린 포지션에 대해 이익실현 수준을 추가합니다.
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)
트레이드 워치 디스플레이에 바 가격 정보 표시
이 플러그인이 구축되면 트레이드 워치 패널에 새로운 탭이 추가됩니다. 이 탭에는 m1 시간 프레임과 "USDJPY" 심벌에 대한 마지막으로 알려진 바 가격 정보를 표시하는 2x2 그리드가 포함되어 있습니다.
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}"