알고리즘을 사용하여 cBot 및 지표 관리하기 VIDEO
알고리즘이 다른 알고리즘을 관리하는 기능을 통해 트레이더는 코드를 사용하여 차트에 cBot 및 지표를 추가할 수 있습니다. 이 기능을 통해 효과적인 거래 전략을 계획하고 개발할 수 있으며, 동적 조정을 수행하고 여러 전략을 실행하며 자동화된 위험 관리를 적용할 수 있습니다.
이 문서와 해당 비디오에서는 다른 알고리즘을 관리하는 cBot을 생성하고 작업하는 방법을 보여드리겠습니다.
cBot을 사용하여 지표 추가 Algo 앱에서 cBots 탭을 엽니다. Sample Trend cBot 샘플을 검색하고 선택합니다. 이 샘플은 Moving Averages를 사용합니다.
두 개의 지표를 정의합니다.
ChartIndicator _indicator1 ;
ChartIndicator _indicator2 ;
차트에 두 개의 지표를 추가합니다.
_indicator1 = Chart . Indicators . Add ( "Simple Moving Average" , SourceSeries , FastPeriods , MAType );
_indicator2 = Chart . Indicators . Add ( "Simple Moving Average" , SourceSeries , SlowPeriods , MAType );
지표의 외관은 출력 라인 설정을 통해 사용자 정의할 수 있습니다. 사용자 정의 가능한 옵션에는 색상, 두께 및 라인 스타일이 포함됩니다.
첫 번째 지표의 라인을 빨간색과 두껍게 설정합니다.
_indicator1 . Lines [ 0 ]. Color = Color . Red ;
_indicator1 . Lines [ 0 ]. Thickness = 3 ;
동일한 작업을 통해 언제든지 차트에서 지표를 제거할 수 있습니다. 변경 사항이 바가 변경될 때 적용되도록 설정합니다.
protected override void OnBarClosed ()
{
Chart . Indicators . Remove ( _indicator1 );
Chart . Indicators . Remove ( _indicator2 );
}
아래에서 전체 코드를 복사할 수 있습니다:
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 using cAlgo.API ;
using cAlgo.API.Indicators ;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AddIndicators = true)]
public class SampleTrendcBot : Robot
{
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get ; set ; }
[Parameter("MA Type", Group = "Moving Average")]
public MovingAverageType MAType { get ; set ; }
[Parameter("Source", Group = "Moving Average")]
public DataSeries SourceSeries { get ; set ; }
[Parameter("Slow Periods", Group = "Moving Average", DefaultValue = 10)]
public int SlowPeriods { get ; set ; }
[Parameter("Fast Periods", Group = "Moving Average", DefaultValue = 5)]
public int FastPeriods { get ; set ; }
private MovingAverage slowMa ;
private MovingAverage fastMa ;
private const string label = "Sample Trend cBot" ;
ChartIndicator _indicator1 ;
ChartIndicator _indicator2 ;
protected override void OnStart ()
{
fastMa = Indicators . MovingAverage ( SourceSeries , FastPeriods , MAType );
slowMa = Indicators . MovingAverage ( SourceSeries , SlowPeriods , MAType );
_indicator1 = Chart . Indicators . Add ( "Simple Moving Average" , SourceSeries , FastPeriods , MAType );
_indicator2 = Chart . Indicators . Add ( "Simple Moving Average" , SourceSeries , SlowPeriods , MAType );
_indicator1 . Lines [ 0 ]. Color = Color . Red ;
_indicator1 . Lines [ 0 ]. Thickness = 3 ;
}
protected override void OnBarClosed ()
{
Chart . Indicators . Remove ( _indicator1 );
Chart . Indicators . Remove ( _indicator2 );
}
protected override void OnTick ()
{
var longPosition = Positions . Find ( label , SymbolName , TradeType . Buy );
var shortPosition = Positions . Find ( label , SymbolName , TradeType . Sell );
var currentSlowMa = slowMa . Result . Last ( 0 );
var currentFastMa = fastMa . Result . Last ( 0 );
var previousSlowMa = slowMa . Result . Last ( 1 );
var previousFastMa = fastMa . Result . Last ( 1 );
if ( previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null )
{
if ( shortPosition != null )
ClosePosition ( shortPosition );
ExecuteMarketOrder ( TradeType . Buy , SymbolName , VolumeInUnits , label );
}
else if ( previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null )
{
if ( longPosition != null )
ClosePosition ( longPosition );
ExecuteMarketOrder ( TradeType . Sell , SymbolName , VolumeInUnits , label );
}
}
private double VolumeInUnits
{
get { return Symbol . QuantityToVolumeInUnits ( Quantity ); }
}
}
}
cBot을 빌드하려면 Ctrl + B 단축키를 사용하거나 빌드 를 클릭하세요.
Trade 앱으로 이동합니다. EURUSD 차트를 선택하고, cBot 아이콘을 클릭한 후 Sample Trend cBot 을 검색하고 선택합니다.
인스턴스 추가 창이 나타나면 적용 을 클릭한 후 cBot을 시작합니다.
거래에 사용되는 두 개의 Moving Averages가 차트에 추가된 것을 확인할 수 있습니다.
다른 cBot을 사용하여 cBot 시작 다른 cBot을 통해 cBot을 관리하는 방법을 시연합니다. 이번에는 처음부터 새로운 빈 cBot을 생성합니다.
Algo 앱으로 이동하여 cBots 탭 아래의 새로 만들기 버튼을 클릭합니다. 빈 옵션을 선택하고, Add cBots 와 같은 이름을 입력한 후 생성 을 클릭합니다.
두 개의 차트 로봇 객체를 정의하는 것으로 시작합니다.
ChartRobot _robot1 ;
ChartRobot _robot2 ;
그런 다음 OnStart() 메서드에서 이러한 로봇을 차트에 추가합니다.
_robot1 = Chart . Robots . Add ( "Sample Trend cBot" , 0.01 , MovingAverageType . Simple , Bars . ClosePrices , 10 , 5 );
_robot2 = Chart . Robots . Add ( "Sample Trend cBot" , 0.01 , MovingAverageType . Simple , Bars . ClosePrices , 12 , 7 );
또한 cBot이 시작될 때마다 메시지를 출력하기 위한 이벤트 핸들러를 추가할 수 있습니다.
Chart . Robots . RobotStarted += ChartRobots_RobotStarted ;
private void ChartRobots_RobotStarted ( ChartRobotStartedEventArgs obj )
{
Print ( "Robot Started" );
}
OnBarClosed() 메서드 내에 일부 로직을 작성하여 바가 변경될 때 첫 번째 로봇을 시작하고, 중지한 다음 다음 바에서 두 번째 로봇을 시작합니다.
1
2
3
4
5
6
7
8
9
10
11
12
13 protected override void OnBarClosed ()
{
if ( _robot1 . State == RobotState . Stopped )
{
_robot1 . Start ();
_robot2 . Stop ();
}
else if ( _robot1 . State == RobotState . Running )
{
_robot1 . Stop ();
_robot2 . Start ();
}
}
아래에서 전체 코드를 복사할 수 있습니다:
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 using System ;
using cAlgo.API ;
using cAlgo.API.Collections ;
using cAlgo.API.Indicators ;
using cAlgo.API.Internals ;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None, AddIndicators = true)]
public class AddcBots : Robot
{
ChartRobot _robot1 ;
ChartRobot _robot2 ;
protected override void OnStart ()
{
_robot1 = Chart . Robots . Add ( "Sample Trend cBot" , 0.01 , MovingAverageType . Simple , Bars . ClosePrices , 10 , 5 );
_robot2 = Chart . Robots . Add ( "Sample Trend cBot" , 0.01 , MovingAverageType . Simple , Bars . ClosePrices , 12 , 7 );
Chart . Robots . RobotStarted += ChartRobots_RobotStarted ;
}
private void ChartRobots_RobotStarted ( ChartRobotStartedEventArgs obj )
{
Print ( "Robot Started" );
}
protected override void OnBarClosed ()
{
if ( _robot1 . State == RobotState . Stopped )
{
_robot1 . Start ();
_robot2 . Stop ();
}
else if ( _robot1 . State == RobotState . Running )
{
_robot2 . Start ();
_robot1 . Stop ();
}
}
protected override void OnStop ()
{
// Handle cBot stop here
}
}
}
cBot을 빌드한 후 Trade 앱으로 돌아가서 Add cBots 를 검색하고 선택한 후 cBot을 시작합니다.
권한 요청 대화 상자가 나타나면 허용 을 클릭합니다.
Sample Trend cBot의 두 인스턴스가 차트에 나타나야 합니다.
첫 번째 바가 완료될 때까지 기다리면 Sample Trend cBot의 첫 번째 인스턴스가 자동으로 시작되는 것을 확인할 수 있습니다.
다음 바에서는 Sample Trend cBot의 두 번째 인스턴스가 자동으로 시작되는 것을 확인할 수 있습니다.
cBot이 변경되는 조건에 따라 두 개의 다른 cBot을 관리하고 우리의 로직을 실행하는 방식을 관찰할 수 있습니다.
실행 중 cBot 매개 변수 수정 실행 중인 cBot의 매개 변수를 변경해야 할 수도 있습니다. 예를 들어, 중요한 금융 뉴스나 업데이트를 받은 후 즉시 코드를 업데이트하기로 결정할 수 있습니다.
cBot을 중지하고 다시 시작하는 대신, 첫 번째 cBot의 SlowPeriods 매개 변수를 즉시 수정합니다.
else if ( _robot1 . State == RobotState . Running )
{
_robot1 . Stop ();
_robot1 . Parameters [ "SlowPeriods" ]. Value = ( int ) _robot2 . Parameters [ "SlowPeriods" ]. Value + 1 ;
_robot1 . Start ();
}
이제 cBot을 다시 빌드합니다.
아래에서 전체 코드를 복사할 수 있습니다:
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 using System ;
using cAlgo.API ;
using cAlgo.API.Collections ;
using cAlgo.API.Indicators ;
using cAlgo.API.Internals ;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None, AddIndicators = true)]
public class AddcBots : Robot
{
ChartRobot _robot1 ;
ChartRobot _robot2 ;
protected override void OnStart ()
{
_robot1 = Chart . Robots . Add ( "Sample Trend cBot" , 0.01 , MovingAverageType . Simple , Bars . ClosePrices , 10 , 5 );
_robot2 = Chart . Robots . Add ( "Sample Trend cBot" , 0.01 , MovingAverageType . Simple , Bars . ClosePrices , 12 , 7 );
Chart . Robots . RobotStarted += ChartRobots_RobotStarted ;
}
private void ChartRobots_RobotStarted ( ChartRobotStartedEventArgs obj )
{
Print ( "Robot Started" );
}
protected override void OnBarClosed ()
{
if ( _robot1 . State == RobotState . Stopped )
{
_robot1 . Start ();
_robot2 . Stop ();
}
else if ( _robot1 . State == RobotState . Running )
{
_robot1 . Stop ();
_robot1 . Parameters [ "SlowPeriods" ]. Value = ( int ) _robot2 . Parameters [ "SlowPeriods" ]. Value + 1 ;
_robot1 . Start ();
}
}
protected override void OnStop ()
{
// Handle cBot stop here
}
}
}
Trade 앱으로 이동하여 cBot을 시작하고 각 바에서 지표 값이 어떻게 변경되는지 확인합니다.
요약 이 문서가 알고리즘을 사용하여 다른 알고리즘을 시작, 제어 및 관리하는 방법을 이해하는 데 도움이 되길 바랍니다.