Pythonアルゴリズムのカスタマイズ可能なパラメータ
Python取引ロボット、技術インジケーター、またはプラグインを開発する際に、カスタマイズ可能なパラメータを追加および設定する必要がある場合があります。 この記事では、cTraderのPythonアルゴリズムでカスタマイズ可能なパラメータを宣言および使用する方法について説明します。
注意
cTrader Pythonアルゴリズムの実行モデルは.NET/C#エンジンを使用しており、すべてのカスタマイズ可能なパラメータを.csファイル内で宣言する必要があります。 cTraderでPythonアルゴリズムを作成するたびに、そのアルゴリズムの.csファイルが自動的に生成され、関連するフォルダに保存されます。
場所
Pythonアルゴリズムの.csファイルの場所は、アルゴリズムのタイプ(cBot、インジケーター、またはプラグイン)およびアルゴリズムの名前によって異なります。
- cBotの場合:
Documents/cAlgo/Sources/Robots/{cBot-name}/{cBot-name}/ - インジケーターの場合:
Documents/cAlgo/Sources/Indicators/{Indicator-name}/{Indicator-name}/ - プラグインの場合:
Documents/cAlgo/Sources/Indicators/{Plugin-name}/{Plugin-name}/
Pythonアルゴリズムの.csファイルを見つけるには、次の手順に従ってください:
-
cTrader WindowsまたはMacでアルゴリズムを右クリックし、フォルダを表示を選択します。
-
{name-of-algorithm}/{name-of-algorithm}をナビゲートし、ファイルのリストから.csファイル(C#ソースファイル)を特定します。
.csファイルは通常、アルゴリズムと同じ名前で、すべてのスペースが削除されています。 たとえば、Amazing Aroon cBotという名前のアルゴリズムは、AmazingArooncBot.csファイルになります。
ヒント
同じフォルダ内で、アルゴリズムのメインPythonコードにアクセスできます。これは、Amazing Aroon cBot_main.pyのような.pyファイルに保存されています。 Pythonファイルは次のような簡単な命名規則に従います:
- cBotの場合:
cBot-name_main.py - インジケーターの場合:
Indicator-name_main.py - プラグインの場合:
Plugin-name_main.py
内容
.csファイルをNotePadやVisual Studio codeなどのテキストまたはコードエディタで開くと、次のようなコードが表示されます:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 | using cAlgo.API;
namespace cAlgo.Robots;
[Robot(AccessRights = AccessRights.None, AddIndicators = true)]
public partial class AmazingArooncBot : Robot
{
[Parameter("Volume (Lots)", DefaultValue = 0.01)]
public double VolumeInLots { get; set; }
[Parameter("Stop Loss (Pips)", DefaultValue = 10, MaxValue = 100, MinValue = 1, Step = 1)]
public double StopLossInPips { get; set; }
[Parameter("Take Profit (Pips)", DefaultValue = 10, MaxValue = 100, MinValue = 1, Step = 1)]
public double TakeProfitInPips { get; set; }
[Parameter("Label", DefaultValue = "AmazingArooncBot")]
public string Label { get; set; }
[Parameter("Periods", DefaultValue = 25, Group = "Aroon", MinValue = 2)]
public int Periods { get; set; }
}
|
注意
テンプレートを使用せずにPython cBotをゼロから作成した場合、空のクラスが表示されます。
.csファイルで宣言されたパラメータは、コードエディタに表示されるコードを含むメインPythonファイルで使用できます。 以下のメインPythonコードは、カスタマイズ可能なパラメータの使用法を示しています:
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 | import clr
clr.AddReference("cAlgo.API")
# <strong>Import cAlgo API types</strong>
from cAlgo.API import *
# <strong>Import trading wrapper functions</strong>
from robot_wrapper import *
class AmazingArooncBot():
def on_start(self):
self.volumeInUnits = api.Symbol.QuantityToVolumeInUnits(api.VolumeInLots)
self.aroon = api.Indicators.Aroon(api.Periods)
def on_bar_closed(self):
if self.aroon.Up.Last(0) > self.aroon.Down.Last(0) and self.aroon.Up.Last(1) < self.aroon.Down.Last(1):
self.close_positions(TradeType.Sell)
api.ExecuteMarketOrder(TradeType.Buy, api.SymbolName, self.volumeInUnits, api.Label, api.StopLossInPips, api.TakeProfitInPips)
elif self.aroon.Up.Last(0) < self.aroon.Down.Last(0) and self.aroon.Up.Last(1) > self.aroon.Down.Last(1):
self.close_positions(TradeType.Buy)
api.ExecuteMarketOrder(TradeType.Sell, api.SymbolName, self.volumeInUnits, api.Label, api.StopLossInPips, api.TakeProfitInPips)
def get_bot_positions(self):
return api.Positions.FindAll(api.Label)
def close_positions(self, tradeType):
for position in self.get_bot_positions():
if position.TradeType != tradeType:
continue
api.ClosePosition(position)
|
利点
カスタマイズ可能なパラメータの主な利点は、cTrader Windows、Mac、Web、モバイルを含むすべてのcTraderアプリのUIに編集可能なフィールドとして表示されることです。 これらのフィールドの値は、アルゴリズムのソースコードにアクセスまたは変更することなく、簡単に変更してニーズや操作に合わせることができます。

cTraderストアでは、販売者がカスタマイズ可能なパラメータを使用して、アルゴリズムの購入者がアルゴリズムを自分の目標やワークフローに合わせて調整できるようにすることがよくあります。
