Position_control
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © djmad
//@version=5

// @description This is a library for defining positions and working with them. Additionally i added here my standardblocks which i use in other scripts
library("Position_control")

import djmad/Mad_Standardparts/1 as STDP

// Definition of the type Position and its subtypes
// defining the aviable trailstops
export type t_TP_Variant
    string                                  TP_Type  //Trailstop, Direktsell, Fractal Trail,...
    int                                     TP_Parameter_1
    int                                     TP_Parameter_2
    float                                   TP_Parameter_3
    float                                   TP_Parameter_4   

// defining how a Takeprofit is handled
export type t_TPs
    float                                   TP_Price
    float                                   TP_Lot
    t_TP_Variant                            TP_Variant
    bool                                    TP_Active

// defining how a Takeprofit is handled
export type t_SLs
    float                                   SL_Price
    float                                   SL_Lot
    bool                                    SL_Active

// defining the complete position
export type t_Position_type
    float                                   Lot
    float                                   Leverage
    float                                   Maintenance
    int                                     Starttime
    float                                   Entry_Start
    int                                     Stoptime
    float                                   Entry_Stop
    float                                   Entryprice
    array<t_TPs>                            TPs
    array<t_SLs>                            SLs

// drawout parameters
export type t_drawing_parameters
    bool                                    ShowPos
    bool                                    ShowLIQ
    array<color>                            A_Colors
    bool                                    Prolong_lines
    string                                  Str_fontsize
    int                                     Textshift
    int                                     Decimals_contracts
    int                                     Decimals_price
    int                                     Decimals_percent
    int                                     bartime

//}


// @function Calculate the leverage used in a trade.
// @description This function calculates the leverage used in a trade, based on the value of the trade, the maintenance margin, and the direction of the trade.
// @param _Leverage The leverage used in the trade, as a floating point number.
// @param _maintenance The maintenance margin percentage, as a floating point number.
// @param _value The value of the trade, as a floating point number.
// @param _direction The direction of the trade, either "long" or "short".
// @returns The leverage used in the trade, as a floating point number.
export f_calculateLeverage(float _Leverage, float _maintenance, float _value, string _direction) =>
    if _direction == "short"
        (_Leverage * _value)/(_Leverage - 1 + (_maintenance * _Leverage))
    else if _direction == "long"
        (_Leverage * _value)/(_Leverage + 1 - (_maintenance * _Leverage))



// @function Calculate the profit or loss for a given trade.
// @description This function calculates the profit or loss for a given trade, based on the position type, maximum take profit, position index, and whether to show the profit as a percentage or a value.
// @param _Position An array of position types for the trade.
// @param _max_TP The maximum take profit for the trade, as an integer value.
// @param _Position_index The index of the position in the array, as an integer value.
// @param _show_profit A boolean value indicating whether to show the profit as a percentage or a value.
// @returns The profit or loss for the trade, as a floating point number.
export f_calculate_PL(t_Position_type [] _Position, int _max_TP, int _Position_index, bool _show_profit, int _i_decimals_contracts = 2, int _i_decimals_prercent = 2) =>
    var float profit            = na
    var float loss              = na
    var float getTPs_1i         = na
    var float tpsum             = 0

    var _getA_RW        =   array.get(_Position, _Position_index)
    var entry_price     =   math.avg(_getA_RW.Entry_Start,_getA_RW.Entry_Stop)

    tpsum := 0
    for i = 0 to _getA_RW.TPs.size()-1
        tpsum := tpsum + _getA_RW.TPs.get(i).TP_Lot

    if math.round(tpsum,2) != 1
        for i1 = 0 to _getA_RW.TPs.size()-1
            temp_TP    = _getA_RW.TPs.get(i1)
            temp_TP.TP_Lot /=  tpsum
            _getA_RW.TPs.set(i1, temp_TP)
    profit                      := 0
    loss                        := 0

     //calculation verified by Crader_Florians Excel calculation, 2 approaches, same result, fine one check at least from not me ;-)
    for i2 = 0 to math.min(_getA_RW.TPs.size()-1,_max_TP-1)
        profit += (_getA_RW.TPs.get(i2).TP_Price - entry_price) * _getA_RW.TPs.get(i2).TP_Lot
    loss := (entry_price - _getA_RW.SLs.get(0).SL_Price)

    result = _show_profit?
     (STDP.roundTo(math.abs(profit*_getA_RW.Lot/10000),_i_decimals_contracts)):
     (STDP.roundTo(math.abs(profit/loss),_i_decimals_prercent))
    result

// @function draws a position on the chart
// @description via sending in a typo of Position this function is able to drawout Stoploss, Entrybox, Takeprofits and the required labels with information 
// @param _Position array of type t_Position_type containing the position information.
// @param _max_TP maximum number of take-profit levels.
// @param _Position_index the index of the current position.
// @param _i_decimals_contracts the number of decimal places to display for the contract size. Default is 2.
// @param _i_decimals_percent the number of decimal places to display for the percentage values. Default is 2.
// @returns None but boxes / lines / labels on the chart itself
export f_drawposition(t_Position_type [] _Position, t_drawing_parameters _Parameters, int _Position_index = 0) =>
    if not na(_Position)
        if _Position.size() > 0
            var box [] pos_boxes = array.new_box()
            var label [] pos_label = array.new_label()
            var line [] pos_lines = array.new_line()
            var line [] TP_lines = array.new_line()
            var label [] TP_label = array.new_label()
            var line [] SL_lines = array.new_line()
            var label [] SL_label = array.new_label()
            var line [] LIQ_lines = array.new_line()
            var label [] LIQ_label = array.new_label()
            var string TP_text = na
            var string TP_text_contracts_1 = na
            var string TP_text_contracts_2 = na

            _getA_RO   =   array.get(_Position,  _Position_index)
            entry_price =   math.avg(_getA_RO.Entry_Start,_getA_RO.Entry_Stop)
////////////}

            // Print Liquidation Line{
                // This block creates a liquidation line on the chart based on the user's input, represented by the ShowLIQ, Prolong_lines, A_Colors, and LIQ_lines variables.
                // The f_calculateLeverage function calculates the leverage based on the direction (short or long) provided and returns the result.
                // If ShowLIQ is true, two new lines are created to represent the liquidation line on the chart.
                // If Prolong_lines is also true, a third line is created to extend the liquidation line to the left of the chart.
                // The lines are then added to the LIQ_lines array and the oldest line is removed if the array has more than 40 lines.
            if _Parameters.ShowLIQ
                
                array.unshift(LIQ_lines,
                 line.new(
                 x1             =   _getA_RO.Starttime                     , 
                 y1             =   f_calculateLeverage(_getA_RO.Leverage, _getA_RO.Maintenance, entry_price, _getA_RO.SLs.get(0).SL_Price < entry_price? "long":"short")     	,
                 x2             =   _getA_RO.Stoptime                  	, 
                 y2             =   f_calculateLeverage(_getA_RO.Leverage, _getA_RO.Maintenance, entry_price, _getA_RO.SLs.get(0).SL_Price < entry_price? "long":"short")       	,
                 xloc           =   xloc.bar_time                   	, 
                 extend         =   extend.none                     	, 
                 color          =   array.get(_Parameters.A_Colors,0)    	,  
                 style          =   line.style_solid                	, 
                 width          =   1             
                 )
                 )

                array.unshift(LIQ_lines,
                 line.new(
                 x1             =   _getA_RO.Stoptime                     , 
                 y1             =   f_calculateLeverage(_getA_RO.Leverage, _getA_RO.Maintenance, entry_price, _getA_RO.SLs.get(0).SL_Price < entry_price? "long":"short")     	,
                 x2             =   time                  	, 
                 y2             =   f_calculateLeverage(_getA_RO.Leverage, _getA_RO.Maintenance, entry_price, _getA_RO.SLs.get(0).SL_Price < entry_price? "long":"short")       	,
                 xloc           =   xloc.bar_time                   	, 
                 extend         =   extend.none                     	, 
                 color          =   array.get(_Parameters.A_Colors,1)    	,  
                 style          =   line.style_dotted                	, 
                 width          =   1             
                 )
                 )
            
                if _Parameters.Prolong_lines
                    array.unshift(LIQ_lines,
                     line.new(
                     x1             =   _getA_RO.Starttime - _Parameters.bartime*3              , 
                     y1             =   f_calculateLeverage(_getA_RO.Leverage, _getA_RO.Maintenance, entry_price, _getA_RO.SLs.get(0).SL_Price < entry_price? "long":"short")     	,
                     x2             =   _getA_RO.Starttime                  	, 
                     y2             =   f_calculateLeverage(_getA_RO.Leverage, _getA_RO.Maintenance, entry_price, _getA_RO.SLs.get(0).SL_Price < entry_price? "long":"short")       	,
                     xloc           =   xloc.bar_time                   	, 
                     extend         =   extend.left                     	, 
                     color          =   array.get(_Parameters.A_Colors,1)   	,  
                     style          =   line.style_dotted                	, 
                     width          =   1             
                     )
                     )

                if array.size(LIQ_lines) > 40
                    line.delete(array.pop(LIQ_lines))
                // }

            // Print Liquidation Labels {

                array.unshift(LIQ_label,
                 label.new(
//                 x              =   _getA_RO.Stoptime                      , 
                 x              =   time                      , 
                 y              =   f_calculateLeverage(_getA_RO.Leverage, _getA_RO.Maintenance, entry_price, _getA_RO.SLs.get(0).SL_Price < entry_price? "long":"short")           ,
                 text           =   STDP.shifting(_Parameters.Textshift) + "LIQ L= x"+ str.tostring(_getA_RO.Leverage) + " M="  + str.tostring(_getA_RO.Maintenance*10000) + "%  =" +  str.tostring(STDP.roundTo(f_calculateLeverage(_getA_RO.Leverage, _getA_RO.Maintenance, entry_price, _getA_RO.SLs.get(0).SL_Price < entry_price? "long":"short"),_Parameters.Decimals_price))     ,
                 xloc           =   xloc.bar_time                       , 
                 textcolor      =   array.get(_Parameters.A_Colors,8)       ,  
                 style          =   label.style_none                    ,
                 textalign      =   text.align_left,
                 size           =   _Parameters.Str_fontsize
                 )
                 )

                if array.size(LIQ_label) > 20
                    label.delete(array.pop(LIQ_label))


                // }

            // Print SL Line{
			    // This block of code adds stop-loss lines to the chart using the line.new() function.
                // The stop-loss lines are created based on the values of _getA_RO, which is a user-defined input.
                // The lines are added to the SL_lines array using the array.unshift() function.              
                // The first line.new() function call creates a solid line that represents the initial stop-loss level.
                // The line starts at the beginning of the trade (_getA_RO.Starttime) and ends at the stop-loss level (_getA_RO.SLs.get(0).SL_Price).
                // The line's color and width are also set using the A_Colors array and a fixed value, respectively.
                // The second line.new() function call creates a dotted line that represents the updated stop-loss level.
                // The line starts at the stop-loss level (_getA_RO.SLs.get(0).SL_Price) and ends at the current time.
                // The line's color and style are also set using the A_Colors array and a fixed value, respectively.
                // The Prolong_lines variable determines whether to add an extra dotted line to the left of the initial stop-loss line.
                // If Prolong_lines is true, the third line.new() function call creates a dotted line that extends to the left of the initial stop-loss line.
                // The if statement checks whether the SL_lines array has more than 40 elements.
                // If so, it removes the oldest line using array.pop() and deletes it from the chart using line.delete().

            array.unshift(SL_lines,
             line.new(
             x1             =   _getA_RO.Starttime                     , 
             y1             =   _getA_RO.SLs.get(0).SL_Price     	,
             x2             =   _getA_RO.Stoptime                  	, 
             y2             =   _getA_RO.SLs.get(0).SL_Price       	,
             xloc           =   xloc.bar_time                   	, 
             extend         =   extend.none                     	, 
             color          =   array.get(_Parameters.A_Colors,2)    	,  
             style          =   line.style_solid                	, 
             width          =   2             
             )
             )

            array.unshift(SL_lines,
             line.new(
             x1             =   _getA_RO.Stoptime                     , 
             y1             =   _getA_RO.SLs.get(0).SL_Price     	,
             x2             =   time                  	, 
             y2             =   _getA_RO.SLs.get(0).SL_Price       	,
             xloc           =   xloc.bar_time                   	, 
             extend         =   extend.none                     	, 
             color          =   array.get(_Parameters.A_Colors,3)   	,  
             style          =   line.style_dotted                	, 
             width          =   1             
             )
             )

            if _Parameters.Prolong_lines
                array.unshift(SL_lines,
                 line.new(
                 x1             =   _getA_RO.Starttime - _Parameters.bartime*3              , 
                 y1             =   _getA_RO.SLs.get(0).SL_Price       	,
                 x2             =   _getA_RO.Starttime                  	, 
                 y2             =   _getA_RO.SLs.get(0).SL_Price       	,
                 xloc           =   xloc.bar_time                   	, 
                 extend         =   extend.left                     	, 
                 color          =   array.get(_Parameters.A_Colors,3)    	,  
                 style          =   line.style_dotted                	, 
                 width          =   1             
                 )
                 )

            if array.size(SL_lines) > 40
                line.delete(array.pop(SL_lines))
            // }
            
            // Print SL Labels {
                // This block of code creates a label that displays the stop-loss information for the current trade.
                // The TP_text variable is used to construct the label text, which includes the stop-loss price and the percentage change from the entry price.
                // The TP_text_contracts_1 variable is used to display the contract size if ShowPos is true.
                // The label is created using the label.new() function and added to the SL_label array using the array.unshift() function.

                // The if statement checks whether the SL_label array has more than 20 elements.
                // If so, it removes the oldest label using array.pop() and deletes it from the chart using label.delete().

                // This block of code creates a rectangle that highlights the entry price range for the current trade.
                // The rectangle is created using the box.new() function and added to the pos_boxes array using the array.unshift() function.

                // The if statement checks whether the pos_boxes array has more than 400 elements.
                // If so, it removes the oldest rectangle using array.pop() and deletes it from the chart using box.delete().

            if _Parameters.ShowPos 
                TP_text_contracts_1 := 
                 " / " + 
                 str.tostring(  -1 * math.abs( STDP.roundTo(   _getA_RO.Lot * (   (    entry_price/ _getA_RO.SLs.get(0).SL_Price  ) -1)   ,   _Parameters.Decimals_contracts   ) ) )   + 
                 "C"
            else
                TP_text_contracts_1 := ""
            
            TP_text           :=   STDP.shifting(_Parameters.Textshift) + 
             "SL @ "    + str.tostring( STDP.roundTo(_getA_RO.SLs.get(0).SL_Price,_Parameters.Decimals_price)) + 
             " = "      + str.tostring( STDP.roundTo(       (math.abs((entry_price > _getA_RO.SLs.get(0).SL_Price) ? ((_getA_RO.SLs.get(0).SL_Price - entry_price)/_getA_RO.SLs.get(0).SL_Price) :((entry_price - _getA_RO.SLs.get(0).SL_Price)/entry_price )))   *   -100,_Parameters.Decimals_percent) ) + 
             "%" +
             TP_text_contracts_1

            array.unshift(SL_label,
             label.new(
             x              =   time                  , 
             y              =   _getA_RO.SLs.get(0).SL_Price                        ,
             text           =   TP_text                         ,
             xloc           =   xloc.bar_time                   , 
             textcolor      =   array.get(_Parameters.A_Colors,8)        ,  
             style          =   label.style_none                ,
             textalign      =   text.align_left,
             size           =   _Parameters.Str_fontsize
             
             )
             )

            if array.size(SL_label) > 20
                label.delete(array.pop(SL_label))
            // }

            // Print Entry Box {
                // Print Entry Box - This section of the code is responsible for creating and displaying the entry box on the chart
                // First, it creates a new box using the values from the _getB object, including the start and stop times, and the entry start and stop prices
                // It then adds this box to the "pos_boxes" array.
                // If the size of the "pos_boxes" array is greater than 60, it removes the last element from the array.
                // If "Prolong_lines" is true, it creates two new lines and adds them to the "pos_lines" array.
                // These lines extend from the start time of the entry period 3 times the length of a bar time to the start time of the entry period.
                // If the size of the "pos_lines" array is greater than 40, it removes the last element from the array.

            array.unshift(pos_boxes,
             box.new(
             left               =   _getA_RO.Starttime                     , 
             bottom             =   _getA_RO.Entry_Start                   , 
             right              =   _getA_RO.Stoptime                      ,
             top                =   _getA_RO.Entry_Stop                    , 
             xloc               =   xloc.bar_time                       , 
             extend             =   extend.none                         , 
             bgcolor            =   array.get(_Parameters.A_Colors,4)        , 
             border_style       =   line.style_solid                    , 
             border_width       =   1
             )
             )

            if array.size(pos_boxes) > 60
                box.delete(array.pop(pos_boxes))
            //}
            
            // Print Entry Lines {
                // This block of code adds two dotted lines to the chart to mark the entry price range for the current trade.
                // The lines are created using the line.new() function and added to the pos_lines array using the array.unshift() function.
                // The first line marks the upper limit of the range, and the second line marks the lower limit of the range.
                // The lines start at the stop time of the current trade (_getA_RO.Stoptime) and extend to the current time.
                // The color of the lines is set using the A_Colors array.

                // The Prolong_lines variable determines whether to add an extra set of dotted lines to the left of the entry price range.
                // If Prolong_lines is true, the second if statement adds two more dotted lines to the chart to extend the entry price range to the left.
                
                // The if statement checks whether the pos_lines array has more than 40 elements.
                // If so, it removes the oldest line using array.pop() and deletes it from the chart using line.delete().

            //Right side
            array.unshift(pos_lines,
             line.new(
             x1             =   _getA_RO.Stoptime              , 
             y1             =   _getA_RO.Entry_Start       	,
             x2             =   time                  	, 
             y2             =   _getA_RO.Entry_Start       	,
             xloc           =   xloc.bar_time                   	, 
             extend         =   extend.none                     	, 
             color          =   array.get(_Parameters.A_Colors,5)   	,  
             style          =   line.style_dotted                	, 
             width          =   1             
             )
             )
            array.unshift(pos_lines,
             line.new(
             x1             =   _getA_RO.Stoptime              , 
             y1             =   _getA_RO.Entry_Stop       	,
             x2             =   time                  	, 
             y2             =   _getA_RO.Entry_Stop       	,
             xloc           =   xloc.bar_time                   	, 
             extend         =   extend.none                     	, 
             color          =   array.get(_Parameters.A_Colors,5)    	,  
             style          =   line.style_dotted                	, 
             width          =   1             
             )
             )            
            
            if _Parameters.Prolong_lines
                //Left side
                array.unshift(pos_lines,
                 line.new(
                 x1             =   _getA_RO.Starttime - _Parameters.bartime*3              , 
                 y1             =   _getA_RO.Entry_Start       	,
                 x2             =   _getA_RO.Starttime                  	, 
                 y2             =   _getA_RO.Entry_Start       	,
                 xloc           =   xloc.bar_time                   	, 
                 extend         =   extend.left                     	, 
                 color          =   array.get(_Parameters.A_Colors,5)    	,  
                 style          =   line.style_dotted                	, 
                 width          =   1             
                 )
                 )
                array.unshift(pos_lines,
                 line.new(
                 x1             =   _getA_RO.Starttime - _Parameters.bartime*3              , 
                 y1             =   _getA_RO.Entry_Stop       	,
                 x2             =   _getA_RO.Starttime                  	, 
                 y2             =   _getA_RO.Entry_Stop       	,
                 xloc           =   xloc.bar_time                   	, 
                 extend         =   extend.left                     	, 
                 color          =   array.get(_Parameters.A_Colors,5)    	,  
                 style          =   line.style_dotted                	, 
                 width          =   1             
                 )
                 )

            if array.size(pos_lines) > 40
                line.delete(array.pop(pos_lines))
            // }
            
            // Print Entry Labels {
                // This block of code creates a label that displays the entry price information for the current trade.
                // The EP_text_contracts variable is used to display the contract size if ShowPos is true.
                // The label text is constructed using the STDP.shifting() function to shift the text to the left, and STDP.roundTo() function to round the price and contract size to the desired decimal places.
                // The label is created using the label.new() function and added to the pos_label array using the array.unshift() function.

                // The if statement checks whether the pos_label array has more than 20 elements.
                // If so, it removes the oldest label using array.pop() and deletes it from the chart using label.delete().

            var string EP_text_contracts = na

            if _Parameters.ShowPos
                EP_text_contracts := " = " + str.tostring(STDP.roundTo(_getA_RO.Lot,_Parameters.Decimals_contracts))
            else
                EP_text_contracts := ""

            array.unshift(pos_label,
             label.new(
             x              =   time                      , 
             y              =   entry_price           ,
             text           =   
              STDP.shifting(_Parameters.Textshift) + "Entry " + str.tostring(STDP.roundTo(_getA_RO.Entry_Start,_Parameters.Decimals_price)) + " - " + str.tostring(STDP.roundTo(_getA_RO.Entry_Stop,_Parameters.Decimals_price)) + "\n" +
              STDP.shifting(_Parameters.Textshift) + "Avg. @ "+str.tostring(STDP.roundTo(entry_price,_Parameters.Decimals_price)) + EP_text_contracts + "C"   ,
             xloc           =   xloc.bar_time                       , 
             textcolor      =   array.get(_Parameters.A_Colors,8)       ,  
             style          =   label.style_none                    ,
             textalign      =   text.align_left,
             size           =   _Parameters.Str_fontsize
             )
             )

            if array.size(pos_label) > 20
                label.delete(array.pop(pos_label))
            // }

            // Print TPs Lines{
                // This block of code creates a solid line for each take profit (TP) level that has a non-zero lot size.
                // The lines are created using the line.new() function and added to the TP_lines array using the array.unshift() function.
                // The lines start at the start time of the current trade (_getA_RO.Starttime) and extend to the current time.
                // The color of the lines is set using the A_Colors array.
                
                // The Prolong_lines variable determines whether to add an extra set of dotted lines to the left of each TP level.
                // If Prolong_lines is true, the second if statement adds a dotted line to the chart to extend each TP level to the left.
                
                // The if statement checks whether the TP_lines array has more than 300 elements.
                // If so, it removes the oldest line using array.pop() and deletes it from the chart using line.delete().

            for i = 0 to _getA_RO.TPs.size()-1
                if _getA_RO.TPs.get(i).TP_Lot != 0
                    array.unshift(TP_lines,
                     line.new(
                     x1             =   _getA_RO.Starttime         , 
                     y1             =   _getA_RO.TPs.get(i).TP_Price       	,
                     x2             =   time                  	, 
                     y2             =   _getA_RO.TPs.get(i).TP_Price       	,
                     xloc           =   xloc.bar_time                   	, 
                     extend         =   extend.none                     	, 
                     color          =   array.get(_Parameters.A_Colors,6)   	,  
                     style          =   line.style_solid                	, 
                     width          =   1             
                     )
                     )

                    if _Parameters.Prolong_lines
                        array.unshift(TP_lines,
                         line.new(
                         x1             =   _getA_RO.Starttime - _Parameters.bartime*3              , 
                         y1             =   _getA_RO.TPs.get(i).TP_Price       	,
                         x2             =   _getA_RO.Starttime                  	, 
                         y2             =   _getA_RO.TPs.get(i).TP_Price       	,
                         xloc           =   xloc.bar_time                   	, 
                         extend         =   extend.left                     	, 
                         color          =   array.get(_Parameters.A_Colors,6)    	,  
                         style          =   line.style_dotted                	, 
                         width          =   1             
                         )
                         )
                if array.size(TP_lines) > 300
                    line.delete(array.pop(TP_lines))

            // }
            
            // Print TP Labels {
			    // Print TP Labels - This section of the code is responsible for creating and displaying the Take Profit (TP) labels on the chart
			    // It loops through the "TPs" matrix from the _getB object and for each non-zero value in the matrix, it does the following:
			    // First, it creates two string variables "TP_text" and "TP_text_contracts_1" and assigns them the value "na"
			    // Then, it checks the value of the "ShowPos" variable. If it is true, it assigns the value of "TP_text_contracts_1" to a string that concatenates "= " 
			    // with the rounded value of _getA_RO.Lot multiplied by the value in the matrix, and "i_decimals_contracts" and "C"
			    // Else, it assigns the value of "TP_text_contracts_1" to an empty string.
			    // Then it assigns the value of "TP_text" to a string that concatenates several pieces of information such as the TP number, sell percentage, and P/L and Gain.
			    // It creates a new label using the values from the _getB object and the "TP_text" variable and adds this label to the "TP_label" array.
			    // If the size of the "TP_label" array is greater than 400, it removes the last element from the array.

            for ia = 0 to _getA_RO.TPs.size()-1
                if _getA_RO.TPs.get(ia).TP_Lot != 0

                    if _Parameters.ShowPos
                        TP_text_contracts_1 := "= " + (str.tostring(STDP.roundTo(_getA_RO.Lot*_getA_RO.TPs.get(ia).TP_Lot,_Parameters.Decimals_contracts))) + "C"
                        TP_text_contracts_2 := " / " + str.tostring(f_calculate_PL(_Position = _Position, 
                         _max_TP = ia+1, 
                         _Position_index = 0, 
                         _show_profit = true, 
                         _i_decimals_contracts = _Parameters.Decimals_contracts, 
                         _i_decimals_prercent = _Parameters.Decimals_percent)) + "C"
                    else
                        TP_text_contracts_1 := ""
                        TP_text_contracts_2 := ""

                    shortperc=((_getA_RO.TPs.get(ia).TP_Price - entry_price )/ entry_price*100)
                    longperc=((entry_price - _getA_RO.TPs.get(ia).TP_Price )/ entry_price*100)


                    TP_text           :=   
                     STDP.shifting(_Parameters.Textshift) +     "TP "+str.tostring(ia+1) + " - @ " + str.tostring(STDP.roundTo(_getA_RO.TPs.get(ia).TP_Price,_Parameters.Decimals_price)) + 
                     " / Sell " + str.tostring(STDP.roundTo(_getA_RO.TPs.get(ia).TP_Lot*100,_Parameters.Decimals_percent)) + "% "  + TP_text_contracts_1 + "\n" + 
                     STDP.shifting(_Parameters.Textshift) +     "P/L=" + str.tostring(STDP.roundTo(f_calculate_PL(_Position = _Position, _max_TP = ia+1, _Position_index = 0, _show_profit = false, _i_decimals_contracts = _Parameters.Decimals_contracts, _i_decimals_prercent = _Parameters.Decimals_percent),_Parameters.Decimals_percent)) +  
                     " / Move = " + 
                     str.tostring(  STDP.roundTo(    (entry_price > _getA_RO.TPs.get(ia).TP_Price ? longperc:shortperc) , _Parameters.Decimals_percent               )  ) +
                     "%" + TP_text_contracts_2,

                    array.unshift(TP_label,
                     label.new(
                     x              =   time                      , 
                     y              =   _getA_RO.TPs.get(ia).TP_Price           ,
                     text           =   TP_text,
                     xloc           =   xloc.bar_time                       , 
                     textcolor      =   array.get(_Parameters.A_Colors,8)       ,  
                     style          =   label.style_none                    ,
                     textalign      =   text.align_left,
                     size           =   _Parameters.Str_fontsize
                     )
                     )

                if array.size(TP_label) > 400
                    label.delete(array.pop(TP_label))
            // }
