ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • pinescript: objects,parameter
    개발 2023. 6. 1. 20:10

    1.type Circle을 정의하고 fields에 radius (type float), color(type color)

    2.function drawCircle을 작성하고 하나의 매개변수와 Circle object를 사용.

    function안에는 반지름과 원의 색깔을 print.

    label.new()로 차트에서 나타내시오

    3.Circle 오브젝트 생성하고 그 radius와 color영역을 유저인풋으로 받으시오.

    그리고 생성된 circle을 인수로 사용하여 drawCircle 함수를 호출하시오.

     


    해답

    //@version=5
    indicator("Circle Properties", overlay = true)

    // Define a new type called Circle
    type Circle
        float radius
        color col

    // Write a function called drawCircle
    drawCircle(Circle c) =>
        label.new(x = bar_index, y = close, text = "Radius: " + str.tostring(c.radius) + ", Color: " ,color = c.col)

    // User inputs for the circle's properties
    r = input(1.0, "Radius")
    c = input(color.red, "Color")

    // Create a new Circle object and set its properties
    circle = Circle.new(radius = r, col = c)

    // Call the drawCircle function
    drawCircle(circle)

    마우스 클릭을 인풋받아서 세 점을 잇는 삼각형을 그리는 인디케이터를 만드시오


    해답

    //@version=5
    indicator("Triangle", "", true)
    
    int   TIME_DEFAULT  = 0
    float PRICE_DEFAULT = 0.0
    
    x1Input = input.time(TIME_DEFAULT,   "Point 1", inline = "1", confirm = true)
    y1Input = input.price(PRICE_DEFAULT, "",        inline = "1", tooltip = "Pick point 1", confirm = true)
    x2Input = input.time(TIME_DEFAULT,   "Point 2", inline = "2", confirm = true)
    y2Input = input.price(PRICE_DEFAULT, "",        inline = "2", tooltip = "Pick point 2", confirm = true)
    x3Input = input.time(TIME_DEFAULT,   "Point 3", inline = "3", confirm = true)
    y3Input = input.price(PRICE_DEFAULT, "",        inline = "3", tooltip = "Pick point 3", confirm = true)
    
    // @type            Used to represent the coordinates and color to draw a triangle.
    // @field time1     Time of first point.
    // @field time2     Time of second point.
    // @field time3     Time of third point.
    // @field price1    Price of first point.
    // @field price2    Price of second point.
    // @field price3    Price of third point.
    // @field lineColor Color to be used to draw the triangle lines.
    type Triangle
        int   time1
        int   time2
        int   time3
        float price1
        float price2
        float price3
        color lineColor
    
    //@function Draws a triangle using the coordinates of the `t` object.
    //@param t  (Triangle) Object representing the triangle to be drawn.
    //@returns  The ID of the last line drawn.
    drawTriangle(Triangle t) =>
        line.new(t.time1, t.price1, t.time2, t.price2, xloc = xloc.bar_time, color = t.lineColor)
        line.new(t.time2, t.price2, t.time3, t.price3, xloc = xloc.bar_time, color = t.lineColor)
        line.new(t.time1, t.price1, t.time3, t.price3, xloc = xloc.bar_time, color = t.lineColor)
    
    // Draw the triangle only once on the last historical bar.
    if barstate.islastconfirmedhistory
        //@variable Used to hold the Triangle object to be drawn.
        Triangle triangle = Triangle.new()
    
        triangle.time1  := x1Input
        triangle.time2  := x2Input
        triangle.time3  := x3Input
        triangle.price1 := y1Input
        triangle.price2 := y2Input
        triangle.price3 := y3Input
        triangle.lineColor := color.purple
    
        drawTriangle(triangle)

     

     

    '개발' 카테고리의 다른 글

    pinescript: loops  (0) 2023.06.06
    pinescript: conditional structures  (0) 2023.06.05
    pinescript: variable declaration  (0) 2023.06.04
    pinescript: Operators  (0) 2023.06.02
    pinescript: Identifiers  (0) 2023.06.02
Designed by Tistory.