mad-position-starter-calculator.pine
Copy
// 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
indicator(title='[MAD] Position starter & calculator', shorttitle='[MAD] Pos. S&C', overlay=true , max_boxes_count=500, max_lines_count=500, max_labels_count=500)
// load Standardparts
import djmad/Position_control/1 as POS_CON
import djmad/Mad_Standardparts/1 as STDP
//
// Variables and definitios {
var int colortheme = na
var M_Colors = matrix.new<color>(6, 10, na)
var int bartime = na
var POS_CON.t_Position_type[] myPosition = array.new<POS_CON.t_Position_type>()
var POS_CON.t_drawing_parameters myParameters = na
if bar_index == 200
bartime := STDP.f_getbartime()
//}
///////////////////// INPUTS IN TYPES /////////////////// {
if barstate.isfirst
colortheme := input.int(5, "Colortheme",minval = 0, maxval = 5, group='Colortheme (0 = Darkmode, 1 = Lightmode', tooltip='0 Darkmode, 1 Lightmode, 2 @Wotan_BitBlockArt, 3 @Linden_Blues, 4 @BitcoinFranken87')
var p_prolong_lines = input.bool(false,"Extend lines to the left", group='Colortheme (0 = Darkmode, 1 = Lightmode')
var p_showPos = input.bool(false, "Show contracts", inline="AC3", group='Leverage/Capital'),
var p_showLIQ = input.bool(false,"Show liquidation", group='Leverage/Capital')
var float SET_SL_PRICE = input.price(defval=100, title= "SL", inline="XE1", confirm = true, group='SL')
var float SET_SL_LOT = 100
var int SET_Starttime = input.time(timestamp("2020-02-19"),title= "Entry - Left limit", inline="EB1", confirm = true, group='Entrybox start'),
var float SET_Entry_Start = input.price(defval=100, title= "Entry_Boxbegin", inline="EB1", confirm = true, group='Entrybox start'),
var int SET_Stoptime = input.time(timestamp("2020-02-19"),title= "Entry - Right limit", inline="EB2", confirm = true, group='Entrybox stop'),
var float SET_Entry_Stop = input.price(defval=100, title= "Entry_Boxstop", inline="EB2", confirm = true, group='Entrybox stop'),
var float SET_Lot = input.float(1,"Contracts",minval = 0.000000000000000001, inline="AC3", group='Leverage/Capital'),
var float SET_Leverage = input.float(20,"Leverage",minval = 1,maxval = 100,step=1, inline="LE1", group='Leverage/Capital'),
var float SET_Maintenance = (100-input.float(50, "max. margin %", minval=0, maxval=100 , inline="LE1", group='Leverage/Capital',step = 1))/10000,
var float SET_TP_PRICE_1 = input.price(defval=100.0, title= "TP_1", inline="PO1", confirm = true, group='Targets')
var float SET_TP_PRICE_2 = input.price(defval=110.0, title= "TP_2", inline="PO2", confirm = true, group='Targets')
var float SET_TP_PRICE_3 = input.price(defval=120.0, title= "TP_3", inline="PO3", confirm = true, group='Targets')
var float SET_TP_PRICE_4 = input.price(defval=130.0, title= "TP_4", inline="PO4", confirm = true, group='Targets')
var float SET_TP_PRICE_5 = input.price(defval=140.0, title= "TP_5", inline="PO5", confirm = true, group='Targets')
var float SET_TP_LOT_1 = input.float(defval=20.0, title= "TP_1 %", inline="PO1", group='Targets')/100
var float SET_TP_LOT_2 = input.float(defval=20.0, title= "TP_2 %", inline="PO2", group='Targets')/100
var float SET_TP_LOT_3 = input.float(defval=20.0, title= "TP_3 %", inline="PO3", group='Targets')/100
var float SET_TP_LOT_4 = input.float(defval=20.0, title= "TP_4 %", inline="PO4", group='Targets')/100
var float SET_TP_LOT_5 = input.float(defval=20.0, title= "TP_5 %", inline="PO5", group='Targets')/100
var string pStr_fontsize = input.string(size.normal, title='Font-Size', options=[size.tiny, size.small, size.normal, size.large, size.huge], group='common')
var int pTextshift = input.int(70,"Text shifting",minval = 0,maxval = 100, inline="PP1", group='common')
var int pDecimals_price = input.int(2,"Decimals Price",minval = -3,maxval = 10, inline="PP1", group='common')
var int pDecimals_contracts = input.int(2,"Decimals Contracts",minval = -3,maxval = 10, inline="PP2", group='common')
var int pDecimals_percent = input.int(2,"Decimals Percent",minval = -3,maxval = 10, inline="PP2", group='common')
//////////////////// Input and Switch of Colors ///////////////////////
// }
///////////////////// INPUTS of colors /////////////////// {
matrix.set(M_Colors,0,0, input.color(color.rgb(255, 153, 000, 40), title= "LIQ D-T", inline="Liquidation", group='Colors'))
matrix.set(M_Colors,1,0, input.color(color.rgb(255, 153, 000, 40), title= "L-T", inline="Liquidation", group='Colors'))
matrix.set(M_Colors,0,2, input.color(color.rgb(255, 082, 082, 40), title= " SL D-T", inline="Stoploss", group='Colors'))
matrix.set(M_Colors,1,2, input.color(color.rgb(255, 082, 082, 40), title= "L-T", inline="Stoploss", group='Colors'))
matrix.set(M_Colors,0,4, input.color(color.rgb(255, 255, 255, 60), title= " Box D-T", inline="Entrybox", group='Colors'))
matrix.set(M_Colors,1,4, input.color(color.rgb(000, 000, 000, 60), title= "L-T", inline="Entrybox", group='Colors'))
matrix.set(M_Colors,0,6, input.color(color.rgb(255, 255, 255, 40), title= " TP D-T", inline="TPs", group='Colors'))
matrix.set(M_Colors,1,6, input.color(color.rgb(054, 058, 069, 40), title= "L-T", inline="TPs", group='Colors'))
matrix.set(M_Colors,0,8, input.color(color.rgb(255, 255, 255, 33), title= " Text D-T", inline="Text", group='Colors'))
matrix.set(M_Colors,1,8, input.color(color.rgb(000, 000, 000, 33), title= "L-T", inline="Text", group='Colors'))
matrix.set(M_Colors,0,1, input.color(color.rgb(255, 153, 000, 40), title= "LIQ draftline D-T", inline="Liquidation-helper", group='Colors'))
matrix.set(M_Colors,1,1, input.color(color.rgb(255, 153, 000, 40), title= "L-T", inline="Liquidation-helper", group='Colors'))
matrix.set(M_Colors,0,3, input.color(color.rgb(255, 082, 082, 40), title= " SL draftline D-T", inline="Stoploss-helper", group='Colors'))
matrix.set(M_Colors,1,3, input.color(color.rgb(255, 082, 082, 40), title= "L-T", inline="Stoploss-helper", group='Colors'))
matrix.set(M_Colors,0,5, input.color(color.rgb(035, 116, 071, 40), title= " Box draftline D-T", inline="Entrybox-helper", group='Colors'))
matrix.set(M_Colors,1,5, input.color(color.rgb(120, 134, 123, 40), title= "L-T", inline="Entrybox-helper", group='Colors'))
matrix.set(M_Colors,0,7, input.color(color.rgb(255, 255, 255, 50), title= " TP draftline D-T", inline="TPs-helper", group='Colors'))
matrix.set(M_Colors,1,7, input.color(color.rgb(054, 058, 069, 50), title= "L-T", inline="TPs-helper", group='Colors'))
//color Wotan
matrix.set(M_Colors,2,0, color.rgb(144, 026, 026, 00))
matrix.set(M_Colors,2,2, color.rgb(255, 120, 093, 40))
matrix.set(M_Colors,2,4, color.rgb(255, 161, 094, 72))
matrix.set(M_Colors,2,6, color.rgb(000, 151, 065, 40))
matrix.set(M_Colors,2,8, color.rgb(000, 192, 192, 00))
matrix.set(M_Colors,2,1, color.rgb(255, 179, 000, 60))
matrix.set(M_Colors,2,3, color.rgb(251, 075, 075, 60))
matrix.set(M_Colors,2,5, color.rgb(000, 086, 009, 60))
matrix.set(M_Colors,2,7, color.rgb(255, 255, 255, 50))
//color Lindener
matrix.set(M_Colors,3,0, color.rgb(255, 136, 000, 00))
matrix.set(M_Colors,3,2, color.rgb(237, 047, 047, 40))
matrix.set(M_Colors,3,4, color.rgb(080, 183, 078, 50))
matrix.set(M_Colors,3,6, color.rgb(060, 065, 078, 33))
matrix.set(M_Colors,3,8, color.rgb(000, 000, 000, 00))
matrix.set(M_Colors,3,1, color.rgb(255, 179, 000, 40))
matrix.set(M_Colors,3,3, color.rgb(251, 075, 075, 40))
matrix.set(M_Colors,3,5, color.rgb(064, 149, 073, 13))
matrix.set(M_Colors,3,7, color.rgb(068, 071, 083, 50))
//color Matze
matrix.set(M_Colors,4,0, color.rgb(190, 021, 021, 00))
matrix.set(M_Colors,4,2, color.rgb(234, 040, 040, 40))
matrix.set(M_Colors,4,4, color.rgb(018, 111, 205, 90))
matrix.set(M_Colors,4,6, color.rgb(000, 237, 103, 00))
matrix.set(M_Colors,4,8, color.rgb(255, 255, 255, 00))
matrix.set(M_Colors,4,1, color.rgb(255, 000, 166, 00))
matrix.set(M_Colors,4,3, color.rgb(255, 015, 015, 00))
matrix.set(M_Colors,4,5, color.rgb(064, 121, 255, 00))
matrix.set(M_Colors,4,7, color.rgb(255, 255, 255, 00))
//color Dark Light compatible
matrix.set(M_Colors,5,0, color.rgb(144, 026, 026, 20))
matrix.set(M_Colors,5,2, color.rgb(255, 120, 093, 20))
matrix.set(M_Colors,5,4, color.rgb(255, 161, 094, 80))
matrix.set(M_Colors,5,6, color.rgb(000, 151, 065, 20))
matrix.set(M_Colors,5,8, color.rgb(000, 192, 192, 20))
matrix.set(M_Colors,5,1, color.rgb(255, 179, 000, 20))
matrix.set(M_Colors,5,3, color.rgb(251, 075, 075, 20))
matrix.set(M_Colors,5,5, color.rgb(000, 086, 009, 20))
matrix.set(M_Colors,5,7, color.rgb(180, 180, 180, 20))
//select colorset
var A_Colors = matrix.row(M_Colors,colortheme)
///////////////////// }
// fill a single position with manual Data from Inputs{
myParameters := POS_CON.t_drawing_parameters.new(ShowPos = p_showPos, ShowLIQ = p_showLIQ, A_Colors = A_Colors, Prolong_lines = p_prolong_lines, Str_fontsize = pStr_fontsize, Textshift = pTextshift, Decimals_contracts = pDecimals_contracts, Decimals_price = pDecimals_price, Decimals_percent = pDecimals_percent, bartime = bartime)
var TP_Variant_type = POS_CON.t_TP_Variant.new(TP_Type = "Direct", TP_Parameter_1 = 0, TP_Parameter_2 = 0, TP_Parameter_3 = 0, TP_Parameter_4 = 0)
var TPs_array1 = array.new<POS_CON.t_TPs>(1, POS_CON.t_TPs.new(TP_Price = SET_TP_PRICE_1, TP_Lot = SET_TP_LOT_1, TP_Variant = TP_Variant_type, TP_Active = true))
var TPs_array2 = array.new<POS_CON.t_TPs>(1, POS_CON.t_TPs.new(TP_Price = SET_TP_PRICE_2, TP_Lot = SET_TP_LOT_2, TP_Variant = TP_Variant_type, TP_Active = true))
var TPs_array3 = array.new<POS_CON.t_TPs>(1, POS_CON.t_TPs.new(TP_Price = SET_TP_PRICE_3, TP_Lot = SET_TP_LOT_3, TP_Variant = TP_Variant_type, TP_Active = true))
var TPs_array4 = array.new<POS_CON.t_TPs>(1, POS_CON.t_TPs.new(TP_Price = SET_TP_PRICE_4, TP_Lot = SET_TP_LOT_4, TP_Variant = TP_Variant_type, TP_Active = true))
var TPs_array5 = array.new<POS_CON.t_TPs>(1, POS_CON.t_TPs.new(TP_Price = SET_TP_PRICE_5, TP_Lot = SET_TP_LOT_5, TP_Variant = TP_Variant_type, TP_Active = true))
var TPs_array_all = array.concat(TPs_array1,array.concat(TPs_array2,array.concat(TPs_array3,array.concat(TPs_array4,TPs_array5))))
var SLs_array1 = array.new<POS_CON.t_SLs>(1,POS_CON.t_SLs.new(SL_Price = SET_SL_PRICE, SL_Lot = na, SL_Active = na))
var SLs_array_all = SLs_array1
myPosition.unshift(
POS_CON.t_Position_type.new(Lot = SET_Lot, Leverage = SET_Leverage, Maintenance= SET_Maintenance, Starttime = SET_Starttime, Entry_Start = SET_Entry_Start, Stoptime = SET_Stoptime, Entry_Stop = SET_Entry_Stop, Entryprice = (SET_Entry_Start+SET_Entry_Stop)/2,
TPs = TPs_array_all,
SLs = SLs_array_all))
//Bitblockart Release
// Inputs for alarm texts
tooltip_variables = '{{close}}:\n Replaced with the current closing price.\n\n
{{open}}\n Replaced with the current opening price.\n\n
{{high}}\n Replaced with the current highest price.\n\n
{{low}}\n Replaced with the current lowest price.\n\n
{{close}}\n Replaced with the current close price.\n\n
{{timestamp}}\n Replaced with the current timestamp in the format "From":"YYYY:MM:DDTHH:MM:SS000000Z".\n\n
{{tp_number}}\n Replaced with the current take profit number.\n\n
{{username}}\n Replaced with the current username.\n\n
{{direction}}\n Replaced with the trade direction: "1" for long, "-1" for short.\n\n
{{ticker}}\n Replaced with the current ticker symbol.\n\n
{{exchange}}\n Replaced with the current exchange prefix.'
var bool table_active = input.bool(false, title='Table active', group='Table')
var string table_size = input.string(size.normal, title='Table size', options=[size.tiny, size.small, size.normal, size.large, size.huge], group='Table')
var string table_loc = input.string(position.top_right, title='Table location', options=[position.top_right,position.top_center,position.middle_left,position.middle_center,position.middle_right,position.bottom_left,position.bottom_center], group='Table')
// // Inputs for alarm texts
var string entry_box_alert_text = input.string("Entry order filled: @ {{close}}", title="Entry Box Alert Text", group="Alert Texts")
var string exit_box_alert_text = input.string("Complete order filled: @ {{close}}", title="Exit Box Alert Text", group="Alert Texts")
var string sl_alert_text = input.string("Price hit the Stop Loss: @ {{close}}", title="Stop Loss Alert Text", group="Alert Texts")
var string tp_alert_text = input.string("Price hit TP{{tp_number}}: @ {{close}}", title="Take Profit Alert Text", group="Alert Texts")
var string username = input.string("", title="username/apikey", group="Alert Texts")
// }
// Live view and alerts
// Extract data from myPosition {
var float entry_start = na
var float entry_stop = na
var float entry_start_time = na
var float entry_stop_time = na
var float sl_price = na
var float[] tp_prices = array.new_float(5,na)
// Blockers for alerts
var bool trade_active = false
var bool entry_box_alert_filled = false
var bool sl_alert_triggered = false
var bool[] tp_alert_triggered = array.new_bool(5,false)
var bool trade_short = false
var bool trade_long = false
if barstate.isfirst
sl_price := myPosition.get(0).SLs.get(0).SL_Price
if array.size(myPosition) > 0 and not trade_active
entry_start_time := myPosition.get(0).Starttime
entry_stop_time := myPosition.get(0).Stoptime
entry_start := myPosition.get(0).Entry_Start
entry_stop := myPosition.get(0).Entry_Stop
sl_price := array.size(myPosition.get(0).SLs) > 0 ? myPosition.get(0).SLs.get(0).SL_Price : na
for i = 0 to array.size(myPosition.get(0).TPs) - 1
array.set(tp_prices, i, myPosition.get(0).TPs.get(i).TP_Price)
// }
// Initialize the table
// Functions
// Function to update the table {
var table strategyTable = table.new(table_loc, 5, 7, border_color=color.gray, frame_color=color.black, frame_width=1, border_width=1)
f_update_table() =>
var int row = 0
var string direction =na
var string entry_range = na
var string sl = na
var string status = na
table.cell(strategyTable, 0, 1, "Status", text_halign=text.align_center, bgcolor=color.gray, text_size = table_size)
table.cell(strategyTable, 0, 2, "Direction", text_halign=text.align_center, bgcolor=color.gray, text_size = table_size)
table.cell(strategyTable, 0, 3, "Entrys", text_halign=text.align_center, bgcolor=color.gray, text_size = table_size)
table.cell(strategyTable, 0, 4, "SL", text_halign=text.align_center, bgcolor=color.gray, text_size = table_size)
table.cell(strategyTable, 1, 0, "Position", text_halign=text.align_center, bgcolor=color.gray, text_size = table_size)
table.cell(strategyTable, 2, 0, "TPs", text_halign=text.align_center, bgcolor=color.gray, text_size = table_size)
table.cell(strategyTable, 3, 0, "Status", text_halign=text.align_center, bgcolor=color.gray, text_size = table_size)
direction := trade_long ? "Long" : trade_short ? "Short" : "N/A"
entry_range := str.tostring(entry_start,format.mintick) + " to " + str.tostring(entry_stop,format.mintick)
sl := na(sl_price) ? "N/A" : str.tostring(sl_price,format.mintick)
status := sl_alert_triggered? "SL" : entry_box_alert_filled ? "filled" : trade_active ? "Active" : "Inactive"
table.cell(strategyTable, 1, 1, status, text_halign=text.align_center, bgcolor=sl_alert_triggered? color.red : entry_box_alert_filled ? color.rgb(76, 175, 79, 25) : trade_active ? color.rgb(76, 175, 79, 25) : color.rgb(33, 149, 243, 25), text_size = table_size)
table.cell(strategyTable, 1, 2, direction, text_halign=text.align_center, bgcolor=direction == "Long" ? color.rgb(76, 175, 79, 25) : color.rgb(255, 82, 82, 25) , text_size = table_size)
table.cell(strategyTable, 1, 3, entry_range, text_halign=text.align_center, text_size = table_size)
table.cell(strategyTable, 1, 4, sl, text_halign=text.align_center, text_size = table_size)
// Add individual TPs
row := 0
for i = 0 to array.size(tp_prices) - 1
row += 1
var string tp_status = ""
tp_status := array.get(tp_alert_triggered, i) == true ? "filled" : "pending"
table.cell(strategyTable, 2, row, "TP" + str.tostring(i + 1) + ": " + str.tostring(array.get(tp_prices, i),format.mintick), text_halign=text.align_center, bgcolor = tp_status == "filled" ? color.rgb(76, 175, 79, 25):na, text_size = table_size)
table.cell(strategyTable, 3, row, tp_status, text_halign=text.align_center, bgcolor = tp_status == "filled" ? color.rgb(76, 175, 79, 25):na, text_size = table_size)
// Replace placeholders in alert templates
f_replace_placeholders(string _template, int tp_number = na) =>
var string template = str.replace_all(_template, "{{close}}", str.tostring(close))
template := str.replace_all(template, "{{open}}", str.tostring(open))
template := str.replace_all(template, "{{high}}", str.tostring(high))
template := str.replace_all(template, "{{low}}", str.tostring(low))
template := str.replace_all(template, "{{timestamp}}", str.tostring(year) + ':' + str.tostring(month) + ':' + str.tostring(dayofmonth) + 'T' + str.tostring(hour) + ':' + str.tostring(minute) + ':' + str.tostring(second) + '000000Z')
template := str.replace_all(template, "{{tp_number}}", str.tostring(tp_number))
template := str.replace_all(template, "{{username}}", str.tostring(username))
template := str.replace_all(template, "{{direction}}", trade_long == true ? "1" : trade_short == true ? "-1" : "0")
template := str.replace_all(template, "{{ticker}}", str.tostring(syminfo.ticker))
template := str.replace_all(template, "{{exchange}}", str.tostring(syminfo.prefix))
template
// }
///////////////////// Runtime / Alerts //////////////////////// {
// Runtime
if (barstate.isrealtime or barstate.islast) and myPosition.size() > 0
STDP.clear_all()
POS_CON.f_drawposition(_Position = myPosition, _Parameters = myParameters, _Position_index = 0)
if not trade_short and not trade_long
if sl_price < math.min(entry_start, entry_stop)
trade_long := true
if sl_price > math.max(entry_start, entry_stop)
trade_short := true
// Alert for Entry Box
if not na(entry_start) and not na(entry_stop) and not sl_alert_triggered and (time > entry_start_time and time < entry_stop_time)
if low <= math.max(entry_start, entry_stop) and not trade_active and trade_long
alert(f_replace_placeholders(entry_box_alert_text), alert.freq_once_per_bar)
trade_active := true
else if high >= math.min(entry_start, entry_stop) and not trade_active and trade_short
alert(f_replace_placeholders(entry_box_alert_text), alert.freq_once_per_bar)
trade_active := true
if not na(entry_start) and not na(entry_stop) and not sl_alert_triggered and (time > entry_start_time) and trade_active
// Alert for Exiting the Entry Box (Fully Filled)
if not sl_alert_triggered and not entry_box_alert_filled
if ( trade_short and ( close >= math.max(entry_start, entry_stop) or high >= math.max(entry_start, entry_stop) ) ) or
( trade_long and ( low <= math.min(entry_start, entry_stop) or close <= math.min(entry_start, entry_stop) ) )
alert(f_replace_placeholders(exit_box_alert_text), alert.freq_once_per_bar)
entry_box_alert_filled := true
// Alert for Stop Loss
if not na(sl_price)
if ( (trade_short and (high >= sl_price or close >= sl_price) ) or ( trade_long and (low <= sl_price or close <= sl_price) ) ) and not sl_alert_triggered
alert(f_replace_placeholders(sl_alert_text), alert.freq_once_per_bar)
sl_alert_triggered := true
// Alert for Take Profits
if not sl_alert_triggered
for i = 0 to array.size(tp_prices) - 1
if array.get(tp_alert_triggered, i) == false
temp_tp = array.get(tp_prices, i)
if (trade_short and (close <= temp_tp or low <= temp_tp )) or (trade_long and (close >= temp_tp or high >= temp_tp))
alert(f_replace_placeholders(tp_alert_text, i+1), alert.freq_once_per_bar)
array.set(tp_alert_triggered, i, true)
//log.info(str.tostring(tp_alert_triggered))
// Update the table
if table_active
f_update_table()
// }
Show all 316 lines