ใน Pine Script เอาจริงๆ มันไม่สามารถใช้การ import ไฟล์อื่นๆ เข้ามาได้นะ เพราะงั้นการแยกไฟล์ก็คือ การใช้การรวมไฟล์ด้วยการใช้ python แหละ
merge_files.py
import os
# Files to concatenate in order
files = ['main.pine', 'part1.pine', 'part2.pine','part3.pine']
output_file = '___combined.pine'
with open(output_file, 'w') as outfile:
for fname in files:
with open(fname) as infile:
outfile.write(infile.read() + '\n')
print(f"Files combined into {output_file}")
และโค้ดในไฟล์ต่างๆ ก็มีประมาณนี้
main.pine
//@version=5
indicator("ทดสอบการแยกไฟล์แล้วเอามารวมทีหลัง", overlay=true, max_bars_back = 3000)
// just comment what files include
//@include part1.pine
//@include part2.pine
//@include part3.pine
part1.pine
//@section part1 tickerId = syminfo.tickerid tfM1 = "1" tfM5 = "5" tfM15 = "15" tfM30 = "30" tfH1 = "60" tfH4 = "240" tfD1 = "D"
part2.pine
//@section part2 colorGreen = #009688 colorRed = #DD5746 colorBlack = #000000 colorWhite = #ffffff colorYellow = #FFC470 colorPink = #ff79c5 colorBlue = #5bbcff
part3.pine
//@section part3 dt = time - time[1] lineXCurrent = time - 2 * dt lineX1 = time - 2 * dt lineX2 = time + 12 * dt labelX = time + 13 * dt lineXRight = time + 50 * dt
รวมไฟล์
หลังจากได้แยกไฟล์ที่ต้องการแล้ว ก็เรียงลำดับดีๆ ด้วยนะ pine script ข้อจำกัดมันเยอะ อะไรต้องมาก่อน อะไรต้องอยู่หลัง เรียงโค้ดดีๆ แล้วสั่งให้ python ทำการรวมไฟล์เลย
python merge_files.py
ไฟล์ที่ถูกรวมแล้วก็จะได้โค้ดต่อกันแบบนี้
//@version=5
indicator("ทดสอบการแยกไฟล์แล้วเอามารวมทีหลัง", overlay=true, max_bars_back = 3000)
// just comment what files include
//@include part1.pine
//@include part2.pine
//@include part3.pine
//@section part1
tickerId = syminfo.tickerid
tfM1 = "1"
tfM5 = "5"
tfM15 = "15"
tfM30 = "30"
tfH1 = "60"
tfH4 = "240"
tfD1 = "D"
//@section part2
colorGreen = #009688
colorRed = #DD5746
colorBlack = #000000
colorWhite = #ffffff
colorYellow = #FFC470
colorPink = #ff79c5
colorBlue = #5bbcff
//@section part3
dt = time - time[1]
lineXCurrent = time - 2 * dt
lineX1 = time - 2 * dt
lineX2 = time + 12 * dt
labelX = time + 13 * dt
lineXRight = time + 50 * dt
