คุณสมบัติใหม่ใน Pine Script: str.format() และ array.from()

May 21, 2021

การอัปเดตภาษาไพน์สคริปต์นี้เพิ่มฟังก์ชันสำหรับการทำงานกับสตริงและอาร์เรย์

ฟังก์ชัน str.format() ช่วยให้คุณสามารถแปลงอาร์กิวเมนต์ที่ส่งผ่านไปยังสตริงในรูปแบบที่ระบุได้:

str.format(formatString, arg0, arg1, ...) -> string

formatString รวม N สคริปต์ไว้ภายในวงเล็บ {}” เพื่อแทนที่อาร์กิวเมนต์ที่จัดรูปแบบที่สอดคล้องกัน (arg0, arg1..argN ซึ่งเป็นสตริงหรือตัวเลข) และรูปแบบบางส่วนที่กล่าวถึงด้านล่างนี้

ดังนั้น สำหรับการส่งค่าสตริง คุณไม่จำเป็นต้องใช้โครงสร้างโดยใช้ฟังก์ชัน str.replace_all() อีกต่อไป:

txt := "Time to {0} some {1}!"

txt := str.replace_all(txt, "{0}", close > open ? "buy" : "sell")

txt := str.replace_all(txt, "{1}", syminfo.ticker)

แต่คุณสามารถทำได้ด้วยวิธีใหม่ สะดวกกว่า และสั้นกว่า:

txt = str.format("Time to {0} some {1}!", close > open ? "buy" : "sell", syminfo.ticker)

Subscripts {0} และ {1} ที่อยู่ในวงเล็บจะถูกแทนที่ด้วยอาร์กิวเมนต์ลำดับที่สอดคล้องกัน: {0} – ผลลัพธ์ของการดำเนินการของ ternary โอเปอเรเตอร์ สตริง buy” หรือ sell” ถูกส่งกลับโดยขึ้นอยู่กับความไม่เท่ากันของราคา ปิดและเปิด {1} – syminfo.ticker สัญลักษณ์ปัจจุบันบนแผนภูมิ:

เมื่อใช้ number เทมเพลต ตัวเลขที่แปลงเป็นสตริงสามารถแปลงเป็นรูปแบบอื่นได้ เช่น เพื่อใช้จำกัดจำนวนตำแหน่งทศนิยม

str.format("{0,number,#.#}", 1.34) // returns: 1.3

ตัดเศษส่วนของตัวเลข:

str.format("{0,number,integer}", 1.34) // returns: 1

แสดงรูปแบบเป็นการแสดงผลทางการเงิน:

str.format("The cash turnover amounted to {0,number,currency}", 1340000) 

// “The cash turnover amounted to $1,340,000.00”.

รับคืนค่าเป็นเปอร์เซ็นต์

str.format("Expected return is {0,number,percent} - {1,number,percent}", 0.1, 0.2)  

// returns: Expected return is 10% - 20%. 

ฟังก์ชัน str.format() รองรับ date เทมเพลตสำหรับการจัดรูปแบบวันที่/เวลา

สคริปต์นี้แสดงจำนวนเดือน สัปดาห์ และวันตั้งแต่ต้นปี:

//@version=4
study("str_format_date")

//formatting string with date pattern
txt = str.format("Current bar date: {0,date, Y-MM-d'.' \n M 'months \n' F 'weeks \n' D 'days \n from the beginning of the year' }", timenow)

//printing label
var l1 = label.new(na, na, color = #FFFFFF00)
label.set_x(l1, bar_index)
label.set_yloc(l1, yloc.belowbar)
label.set_text(l1, txt)

plot(close)

มีตัวแปร timenow ตัวแปรเดียวเท่านั้นที่ถูกส่งไปยังฟังก์ชัน str.format() ซึ่งมีการแสดงเวลาของ UNIX: วันที่ เวลา และเขตเวลาปัจจุบันของตลาดหลักทรัพย์ของสัญลักษณ์บนแผนภูมิ สตริงถูกจัดรูปแบบโดยใช้อักขระ Y, M, D, F (ปี, เดือน, วัน, สัปดาห์ของเดือน), อักขระพิเศษ \n สำหรับการขึ้นบรรทัดใหม่ และเครื่องหมายอัญประกาศเดี่ยว ‘’ เพื่อแทรกข้อความระหว่างอักขระ

สคริปต์นี้ส่งออกสตริงที่จัดรูปแบบไปยังป้ายกำกับและผลลัพธ์ที่สอดคล้องกันของฟังก์ชัน str.format() ด้วยอักขระต่างๆ:

//@version=4
study("str_format_date_time")

// creating date-time variable: 2021.05.04. 14:30:59
t = timestamp("GMT+0", 2021, 05, 04, 14, 30, 59)

// function for left side of the label
f_print_left(_text)  => var _label = label.new(bar_index, na, _text, xloc.bar_index, yloc.price, color(na), label.style_label_right, color.green, size.large, text.align_right), label.set_xy(_label, bar_index, 0), label.set_text(_label, _text)

// function for right side of the label
f_print_right(_text) => var _label = label.new(bar_index, na, _text, xloc.bar_index, yloc.price, color(na), label.style_label_left, color.red, size.large, text.align_left), label.set_xy(_label, bar_index, 0), label.set_text(_label, _text)

var string format = 
  "{0,date,y.MM.dd hh:mm:ss}\n" +
  "{1,date,short}\n" +
  "{2,date,medium}\n" +
  "{3,date,long}\n" +
  "{4,date,full}\n" +
  "{5,date,h a z (zzzz)}\n" +
  "{6,time,short}\n" +
  "{7,time,medium}\n" +
  "{8,time,long}\n" +
  "{9,time,full}\n" + 
  "{10,time,hh:mm:ss}\n" +
  "{11,time,HH:mm:ss}\n"

// printing format string 
f_print_left(format)
//printing formatted string
f_print_right(str.format(format, t, t, t, t, t, t, t, t, t, t, t, t))

คอลัมน์ด้านซ้ายของป้ายกำกับมีอักขระที่อยู่ใน {} ด้านขวาคือผลลัพธ์ของฟังก์ชัน str.format:

ตัวแปร t ถูกส่งไปยังฟังก์ชัน str.format() ซึ่งมีการแสดงเวลาของ UNIX: วันที่ เวลา และเขตเวลาปัจจุบัน

นวัตกรรมต่อไปคือฟังก์ชัน array.from() ซึ่งรับจำนวนตัวแปรของอินพุตอาร์กิวเมนต์ประเภทใดประเภทหนึ่งต่อไปนี้: float, int, bool, string, label, line, color และส่งคืนอาร์เรย์ของประเภทที่เกี่ยวข้อง ฟังก์ชันนี้ให้คุณประกาศอาร์เรย์ กำหนดค่าเริ่มต้นให้กับอาร์เรย์ และกำหนดผลลัพธ์ให้กับตัวแปรใดๆ ในโค้ดเพียงบรรทัดเดียว

ก่อนหน้านี้ ในการสร้างอาร์เรย์และเติมค่าเริ่มต้น คุณต้องใช้ฟังก์ชัน array.new() และฟังก์ชัน array.push() หรือ array.set()

color[] plotColors = array.new_color(size=0)
array.push(plotColors, color.red)
array.push(plotColors, color.green)
array.push(plotColors, #0000FF)

ในตอนนี้ ในการสร้างอาร์เรย์ที่จะมีสีของแผนภูมิ คุณสามารถใช้โค้ดเพียงบรรทัดเดียว:

plotColors = array.from(color.red, color.green, #0000FF)

ฟังก์ชันจะแปลงอาร์เรย์ plotColors เป็นประเภท color[] โดยอัตโนมัติตามอาร์กิวเมนต์ที่ได้รับ:

//@version=4
study(title="array_from")

f_arr (series1, series2, color1, color2) =>
    [ array.from(series1, series2), array.from(color1, color2) ]

[lines, colors] = f_arr(close, open, color.red, color.green)    

plot ( array.get(lines, 0), color = array.get(colors, 0))
plot ( array.get(lines, 1), color = array.get(colors, 1))

ข้อมูลเกี่ยวกับฟังก์ชัน str.format() และ array.from() เหล่านี้มีอยู่ในคู่มืออ้างอิงของเราเสมอ

หากคุณต้องการทราบเกี่ยวกับการอัปเดต Pine Script โปรดดูส่วน บันทึกประจำรุ่น บัญชีผู้ใช้งาน PineCoders ยังออกอากาศการอัพเดทในช่องเทเลแกรม Squawk Box, Twitter และ แชทสาธารณะ Pine Script บน TradingView

เราหวังว่าคุณจะพบว่าการปรับปรุงเหล่านี้มีประโยชน์ และโปรดแบ่งปันความคิดเห็นของคุณกับเราต่อไป เรากำลังสร้าง TradingView สำหรับผู้ใช้ของเรา และเราชอบที่จะได้ยินสิ่งที่คุณคิดเกี่ยวกับนวัตกรรมของเรา

 


ติดตาม TradingView ประเทศไทย ได้ทาง Line ID: @tradingview_th และทวิตเตอร์ (Twitter) ค้นหา “TradingView Thailand

Look first Then leap

TradingView is built for you, so make sure you're getting the most of our awesome features
เปิดชาร์ต