ข้ามไปที่เนื้อหา

พารามิเตอร์ที่ปรับแต่งได้ในอัลกอริทึม Python

เมื่อพัฒนา bots การเทรดด้วย Python, indicators เทคนิค หรือ plugins คุณอาจจำเป็นต้องเพิ่มและกำหนดค่าพารามิเตอร์ที่ปรับแต่งได้ parameters บทความนี้อธิบายวิธีการประกาศและใช้พารามิเตอร์ที่ปรับแต่งได้ใน Python algorithm สำหรับ cTrader

หมายเหตุ

โมเดลการดำเนินการสำหรับอัลกอริทึม Python ของ cTrader เกี่ยวข้องกับเครื่องยนต์ .NET/C# ซึ่งต้องการให้พารามิเตอร์ที่ปรับแต่งได้ทั้งหมดถูกประกาศภายในไฟล์ .cs ทุกครั้งที่คุณสร้างอัลกอริทึม Python ใน cTrader ไฟล์ .cs สำหรับอัลกอริทึมนั้นจะถูกสร้างขึ้นโดยอัตโนมัติและเก็บไว้ในโฟลเดอร์ที่เกี่ยวข้อง

ตำแหน่ง

ตำแหน่งของไฟล์ .cs สำหรับอัลกอริทึม Python ขึ้นอยู่กับประเภทของอัลกอริทึม ไม่ว่าจะเป็น cBot, indicator หรือ plugin รวมถึงชื่อของอัลกอริทึม

  • สำหรับ cBots: Documents/cAlgo/Sources/Robots/{cBot-name}/{cBot-name}/
  • สำหรับ indicators: Documents/cAlgo/Sources/Indicators/{Indicator-name}/{Indicator-name}/
  • สำหรับ plugins: Documents/cAlgo/Sources/Indicators/{Plugin-name}/{Plugin-name}/

คุณสามารถปฏิบัติตามคำแนะนำเหล่านี้เพื่อค้นหาไฟล์ .cs สำหรับอัลกอริทึม Python:

  1. คลิกขวาที่อัลกอริทึมใน cTrader Windows หรือ Mac แล้วเลือก Show in folder

  2. นำทางผ่าน {name-of-algorithm}/{name-of-algorithm} จากนั้นระบุไฟล์ .cs (ไฟล์ต้นฉบับ C#) จากรายการไฟล์

ไฟล์ .cs มักจะมีชื่อเดียวกับอัลกอริทึม โดยที่ช่องว่างทั้งหมดถูกเอาออก ตัวอย่างเช่น อัลกอริทึมที่มีชื่อว่า Amazing Aroon cBot จะทำให้เกิดไฟล์ AmazingArooncBot.cs

เคล็ดลับ

ในโฟลเดอร์เดียวกัน คุณสามารถเข้าถึงโค้ด Python หลักสำหรับอัลกอริทึมของคุณ ซึ่งเก็บไว้ในไฟล์ .py เช่น Amazing Aroon cBot_main.py ไฟล์ Python นี้มีรูปแบบการตั้งชื่อที่เรียบง่าย:

  • สำหรับ cBots: cBot-name_main.py
  • สำหรับ indicators: Indicator-name_main.py
  • สำหรับ plugins: 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; }
}

หมายเหตุ

คุณจะเห็นคลาสว่างเปล่าหากคุณสร้าง cBot Python ตั้งแต่ต้นโดยไม่ใช้เทมเพลต

พารามิเตอร์ใดๆ ที่ประกาศในไฟล์ .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")

# Import cAlgo API types
from cAlgo.API import *

# Import trading wrapper functions
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)

ข้อดี

ข้อดีหลักของพารามิเตอร์ที่ปรับแต่งได้คือพวกมันปรากฏเป็นฟิลด์ที่แก้ไขได้ใน UI ของแอป cTrader ทั้งหมด รวมถึง cTrader Windows, Mac, Web และ Mobile ค่าที่อยู่ในฟิลด์เหล่านี้สามารถปรับเปลี่ยนได้ง่ายเพื่อให้เหมาะกับความต้องการหรือการดำเนินการใดๆ โดยไม่จำเป็นต้องเข้าถึงหรือแก้ไขโค้ดต้นฉบับของอัลกอริทึม

ใน สโตร์ cTrader, ผู้ขาย มักใช้พารามิเตอร์ที่ปรับแต่งได้เพื่อให้แน่ใจว่า ผู้ซื้อ ของอัลกอริทึมสามารถปรับแต่ง algo ให้เหมาะกับเป้าหมายและเวิร์กโฟลว์ของพวกเขาได้

Image title