Advertisement
Maurizio-Ciullo

Study Bot Scalping-Exponential-Tick ETH/PERP Ver-5 BYBIT 5M LONG E SHORT

Jun 10th, 2023 (edited)
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © gabriele_t84
  3.  
  4. // ATTENZIONE QUESTO STUDY SARA' QUELLO CHE GENERERA' GLI ALERT A TV-HUB PERCHE' GIRA AL TICK
  5.  
  6. // Questa strategia era la vecchia Scalping Exponential Bot forse con qualcosa di diverso non lo so ... ma ottimizzata sui dati di bybit e non ftx come
  7. // lo era la precedente.
  8.  
  9. // Questa strategia è stata finita solo da Gabriele ma non ho niente apparte il codice strategia, no report/ no altro.
  10.  
  11. // Non è stata testata bene, è rimasta nel casseto a metà, è stata solo ottimizzata da Gabriele sui nuovi dati di Bybit
  12. // ma non è mai stata attivata o convalidata.
  13.  
  14. // Questo è uno study il quale stavamo facendo delle prove per andare live direttamente dallo study perchè lo strategy
  15. // si incasina con varip "tick indicatori e source".
  16.  
  17. // C'è un'altro strategy che abbiamo creato identico a questo e non uno study
  18. // ma stò aspettando Gabriele che me lo passa.
  19.  
  20. //Scalping exponetial bot tendenza data da due medie, close sopra la terza media, trailing stop doppio,
  21. //Periodo in sample 1/5/21 - 1/10/21
  22. //Periodo out of sample 1/10/21 - 28/04/22
  23.  
  24. //@version=5
  25. indicator("Study Bot Scalping-Exponential-Tick ETH/PERP Ver-5 BYBIT 5M LONG E SHORT", overlay = true, precision = 2,
  26.           max_bars_back=500  )
  27.  
  28. //// variabili varip ///////
  29.  
  30. //////close varip
  31. varip price = 0.0
  32. price := close
  33. if barstate.isrealtime and price != close
  34.     price := close
  35.  
  36. plot(price, title='price')  
  37.  
  38. ///// vlow varip
  39. varip vlow = 0.0
  40. vlow := low
  41. if barstate.isrealtime and vlow != low
  42.     vlow := low
  43.  
  44. //// high varip
  45. varip vhigh = 0.0
  46. vhigh := high
  47. if barstate.isrealtime and vhigh != high
  48.     vhigh := high
  49.  
  50. // if barstate.isnew
  51. //     ticks := 0
  52. //     lastPrice := close
  53.  
  54.      
  55.      
  56. // Input
  57. input_target_long = input.float(title="input_take_profit_long", defval=5.0, minval=0.1, maxval=1000, group='Target')
  58. input_stop_loss_long = input.float(title="input_stop_loss_long", defval=1.3, minval=0.1, maxval=1000, group='Stop Loss')
  59. input_target_short = input.float(title="input_take_profit_short", defval=10.0, minval=0.1, maxval=1000, group='Target')
  60. input_stop_loss_short = input.float(title="input_stop_loss_short", defval=1.1, minval=0.1, maxval=1000, group='Stop Loss')
  61. lunghezza_adx = input.int(title="lunghezza adx", defval=9, minval=0, maxval=1000, group='Differenziale Dmi')
  62. differenziale_adx_basso_long = input.float(title="differenziale_adx_basso_long", defval=0.0, minval=0.0, maxval=1000, group='Differenziale Adx', tooltip="Opera In Base Alla Tendenza")
  63. differenziale_adx_alto_long = input.float(title="differenziale_adx_alto_long", defval=27.0, minval=0.1, maxval=1000, group='Differenziale Adx', tooltip="Opera In Base Alla Tendenza")
  64. differenziale_adx_basso_short = input.float(title="differenziale_adx_basso_short", defval=30.0, minval=0.1, maxval=1000, group='Differenziale Adx', tooltip="Opera In Base Alla Tendenza")
  65. differenziale_adx_alto_short = input.float(title="differenziale_adx_alto_short", defval=60.0, minval=0.1, maxval=1000, group='Differenziale Adx', tooltip="Opera In Base Alla Tendenza")
  66. lunghezza_rsi = input.int(title="lunghezza_rsi", defval=9, minval=1, maxval=1000, group='Differenziale Rsi', tooltip="Opera In Base Ai Range Rsi")
  67. differenziale_rsi_long = input.float(title="differenziale_rsi_long", defval=65.0, minval=0.1, maxval=1000, group='Differenziale Rsi', tooltip="Opera In Base Ai Range Rsi")
  68. differenziale_rsi_short = input.float(title="differenziale_rsi_short", defval=45.0, minval=0.1, maxval=1000, group='Differenziale Rsi', tooltip="Opera In Base Ai Range Rsi")
  69. lunghezza_media_long = input.int(title="media_long", defval=130, minval=1, maxval=1000, group='Medie')
  70. lunghezza_media_short = input.int(title="media_short", defval=110, minval=1, maxval=1000, group='Medie')
  71. lunghezza_media_tendenza_veloce_long = input.int(title="media_tendenza_veloce_long", defval=42, minval=1, maxval=1000, group='Medie')
  72. lunghezza_media_tendenza_lenta_long  = input.int(title="media_tendenza_lenta_long", defval=216, minval=1, maxval=1000, group='Medie')
  73. lunghezza_media_tendenza_veloce_short = input.int(title="media_tendenza_veloce_short", defval=54, minval=1, maxval=1000, group='Medie')
  74. lunghezza_media_tendenza_lenta_short  = input.int(title="media_tendenza_lenta_short", defval=108, minval=1, maxval=1000, group='Medie')
  75. input_trailing_stop_trigger_long1 = input.int(title="trigger long 1", defval=50, minval=1, maxval=1000, group='Trailing')
  76. input_trailing_stop_close_long1 = input.int(title="close long 1", defval=20, minval=1, maxval=1000, group='Trailing')
  77. input_trailing_stop_trigger_long2 = input.int(title="trigger long 2", defval=70, minval=1, maxval=1000, group='Trailing')
  78. input_trailing_stop_close_long2 = input.int(title="close long 2", defval=50, minval=1, maxval=1000, group='Trailing')
  79. input_trailing_stop_trigger_short1 = input.int(title="trigger short 1", defval=40, minval=1, maxval=1000, group='Trailing')
  80. input_trailing_stop_close_short1 = input.int(title="close short 1", defval=20, minval=1, maxval=1000, group='Trailing')
  81. input_trailing_stop_trigger_short2 = input.int(title="trigger short 2", defval=70, minval=1, maxval=1000, group='Trailing')
  82. input_trailing_stop_close_short2 = input.int(title="close short 2", defval=50, minval=1, maxval=1000, group='Trailing')
  83. perc_apertura_minima_long = input.float(title="perc_apertura_minima_long", defval=0.07, minval=0.01, maxval=1000, group='apertura minima')
  84. perc_apertura_minima_short = input.float(title="perc_apertura_minima_short", defval=0.05, minval=0.01, maxval=1000, group='apertura minima')
  85. in_solo_long = input.bool(title='Solo long', defval=false, inline='1', group='Direzione')
  86. in_solo_short = input.bool(title='Solo short', defval=false, inline='1', group='Direzione')
  87.  
  88.  
  89. // Medie Mobili
  90. media_long = ta.ema(close, lunghezza_media_long)
  91. plot(media_long, color=color.new(color.blue, 0), title='Ema Long', linewidth=2)
  92. media_tendenza_lenta_long = ta.ema(close, lunghezza_media_tendenza_lenta_long)
  93. media_tendenza_veloce_long = ta.ema(close, lunghezza_media_tendenza_veloce_long)
  94. plot(media_tendenza_lenta_long, color=color.new(color.purple, 0), title='media_tendenza_lenta_long', linewidth=2)
  95. plot(media_tendenza_veloce_long, color=color.new(color.aqua, 0), title='media_tendenza_veloce_long', linewidth=2)
  96. media_short = ta.ema(close, lunghezza_media_short)
  97. plot(media_short, color=color.new(color.red, 0), title='Ema Short', linewidth=2)
  98. media_tendenza_lenta_short = ta.ema(close, lunghezza_media_tendenza_lenta_short)
  99. media_tendenza_veloce_short = ta.ema(close, lunghezza_media_tendenza_veloce_short)
  100. plot(media_tendenza_lenta_short, color=color.new(color.olive, 0), title='media_tendenza_lenta', linewidth=2)
  101. plot(media_tendenza_veloce_short, color=color.new(color.lime, 0), title='media_tendenza_veloce', linewidth=2)
  102.  
  103.  
  104. //Apertura minima
  105.  
  106. apertura_minima_long   = (media_long / 100) * perc_apertura_minima_long
  107. apertura_minima_short   = (media_short / 100) * perc_apertura_minima_short
  108.  
  109.  
  110. // Variabili Rsi Long e Short
  111. valore_rsi = ta.rsi(close, lunghezza_rsi)
  112. plot(valore_rsi, title = "valore_rsi", color=color.blue)
  113.  
  114.  
  115. // Variabili Adx
  116. [di_pos, di_neg, adx] = ta.dmi(lunghezza_adx, lunghezza_adx)
  117. valore_adx = adx
  118. plot(adx, title="adx_long")
  119.  
  120.  
  121. // Calcolo del tempo con il tempo attuale
  122. var orario_barra = 0.0
  123. varip secondi = 0.0
  124. if barstate.isrealtime and secondi != timenow
  125.     secondi := timenow
  126. plot(secondi, title="secondi")
  127.  
  128.  
  129.  
  130. //Condizioni entrata
  131. // condEntryLong =  close > (media_long + apertura_minima_long)  and valore_adx > differenziale_adx_long_basso and valore_adx < differenziale_adx_long_alto  and valore_rsi < differenziale_rsi_long and not in_solo_short
  132. //condEntryLong_Provvissoria =  close > media_long
  133. //condEntryShort_Provvisoria = close < media_short
  134. condEntryLong_Provvisoria = ta.crossover(close, media_long) and close > (media_long + apertura_minima_long) and media_tendenza_veloce_long > media_tendenza_lenta_long and valore_adx > differenziale_adx_basso_long and valore_adx < differenziale_adx_alto_long  and valore_rsi < differenziale_rsi_long and not in_solo_short
  135. condEntryShort_Provvisoria = ta.crossunder(close, media_short) and close < (media_short - apertura_minima_short)  and media_tendenza_lenta_short > media_tendenza_veloce_short and valore_adx > differenziale_adx_basso_short and valore_adx < differenziale_adx_alto_short  and valore_rsi > differenziale_rsi_short and not in_solo_long
  136.  
  137.  
  138.  
  139. var openlong = 0
  140. if condEntryLong_Provvisoria
  141.     openlong += 1
  142. plot(openlong, title ='openlong')
  143. var openshort = 0
  144. if condEntryShort_Provvisoria
  145.     openshort += 1
  146. plot(openshort, title ='openshort')
  147.  
  148. condEntryLong = condEntryLong_Provvisoria and openlong == 1 and not in_solo_short and openshort == 0
  149. entryprice_long = ta.valuewhen(condEntryLong, close, 0)
  150.  
  151. plotshape(condEntryLong)
  152.  
  153. condEntryShort = condEntryShort_Provvisoria and openshort == 1 and not in_solo_long and openlong == 0
  154. entryprice_short = ta.valuewhen(condEntryShort, close, 0)
  155.  
  156. plotshape(condEntryShort)
  157.  
  158.  
  159.  
  160.  
  161. var contaoperazionilong=0
  162. if condEntryLong
  163.     contaoperazionilong +=1
  164.    
  165. plot(contaoperazionilong, title = 'numero operazioni long')
  166.  
  167.  
  168. var indice_entry_bar_long = 0
  169. if condEntryLong[1]
  170.     indice_entry_bar_long :=  bar_index
  171.  
  172.  
  173. lunghezza_trailing_long = bar_index - indice_entry_bar_long +1
  174. plot(lunghezza_trailing_long, title = 'lunghezza trailing long')
  175. varip highesthigh=0.0
  176. highesthigh := ta.highest(vhigh, lunghezza_trailing_long)
  177.  
  178.  
  179. /// USCITE STOP TARGET E TRAIL
  180. stop_loss_long_price =(entryprice_long - (entryprice_long * input_stop_loss_long) / 100)
  181. take_profit_long_price = (entryprice_long + (entryprice_long * input_target_long) / 100)
  182. trailing_stop_trigger_long1 = entryprice_long + (entryprice_long * input_target_long /100) * (input_trailing_stop_trigger_long1/100)
  183. trailing_stop_close_long1 = entryprice_long + (entryprice_long * input_target_long /100) * (input_trailing_stop_close_long1/100)
  184. trailing_stop_trigger_long2 = entryprice_long + (entryprice_long * input_target_long /100) * (input_trailing_stop_trigger_long2/100)
  185. trailing_stop_close_long2 = entryprice_long + (entryprice_long * input_target_long /100) * (input_trailing_stop_close_long2/100)
  186.  
  187.  
  188. //condExitLongStop = (openlong >=1 and ((vlow <= stop_loss_long_price and lunghezza_trailing != 2 ) or (vlow <= stop_loss_long_price and lunghezza_trailing == 2 and close<open)))
  189. condExitLongStop = (openlong[1] >=1 and  (vlow <= stop_loss_long_price))
  190. condExitLongTarget =  (openlong[1]>=1 and  (vhigh >= take_profit_long_price))
  191.  
  192. if condExitLongStop
  193.     openlong:=0
  194.     openshort:=0
  195. if condExitLongTarget
  196.     openlong:=0
  197.     openshort:=0
  198. condExitLongStopint = if condExitLongStop == true
  199.     1
  200. else
  201.     0
  202. plot(condExitLongStopint, title = 'cond uscita stop long')
  203.  
  204. condExitLongTargetint = if condExitLongTarget == true
  205.     1
  206. else
  207.     0
  208. plot(condExitLongTargetint, title = 'cond uscita target long')
  209.  
  210.  
  211.  
  212. // IsLongOpen := condEntryLong[1] ? true : condExitLong[1] ? false : IsLongOpen[1]
  213.  
  214.  
  215. // //plot(IsLongOpen ? highesthigh : na, title='highesthigh', style = plot.style_cross, linewidth = 1)
  216.  
  217. // trailing_long1 = openlong[1] >=1 and ((highesthigh >= trailing_stop_trigger_long1) and ( vlow <= trailing_stop_close_long1) and not (vlow < trailing_stop_close_long1 and vhigh > trailing_stop_trigger_long1))
  218. // trailing_long2 = openlong[1] >=1 and ((highesthigh >= trailing_stop_trigger_long2) and ( vlow <= trailing_stop_close_long2) and not (vlow < trailing_stop_close_long2 and vhigh > trailing_stop_trigger_long2))
  219. trailing_long1 = (openlong[1] >=1 and ((highesthigh >= trailing_stop_trigger_long1 and highesthigh < trailing_stop_trigger_long2 ) and ( vlow <= trailing_stop_close_long1) and not (vlow < trailing_stop_close_long1 and vhigh == highesthigh and high[1] != highesthigh))) or (openlong[1] >=1 and (highesthigh == vhigh and highesthigh != high[1]) and ((highesthigh >= trailing_stop_trigger_long1 and highesthigh < trailing_stop_trigger_long2) and (price <= trailing_stop_close_long1)))
  220. trailing_long2 = (openlong[1] >=1 and ((highesthigh >= trailing_stop_trigger_long2 and highesthigh < take_profit_long_price) and ( vlow <= trailing_stop_close_long2) and not (vlow < trailing_stop_close_long2 and vhigh == highesthigh and high[1] != highesthigh))) or (openlong[1] >=1 and (highesthigh == vhigh and highesthigh != high[1]) and ((highesthigh >= trailing_stop_trigger_long2 and highesthigh < take_profit_long_price) and (price <= trailing_stop_close_long2)))
  221. if trailing_long1 or trailing_long2
  222.     openlong:=0
  223.     openshort:=0
  224.  
  225. IsLongOpen = false
  226. IsLongOpen := condEntryLong[1] ? true : condExitLongStop[1] ? false : condExitLongTarget[1] ? false : trailing_long1[1] ? false : trailing_long2[1] ? false : IsLongOpen[1]
  227.  
  228. IsLongOpenint = if IsLongOpen == true
  229.     1
  230. else
  231.     0
  232. plot(IsLongOpenint, title = 'islongopen')
  233.  
  234. // Contatori
  235.  
  236. varip contatraillong1 = 0
  237. if highesthigh >= trailing_stop_trigger_long1
  238.     contatraillong1 += 1
  239. plot(contatraillong1, title = 'contatraillong1')
  240.  
  241. varip contatraillong2 = 0
  242. if highesthigh >= trailing_stop_trigger_long2
  243.     contatraillong2 += 1
  244. plot(contatraillong2, title = 'contatraillong2')
  245.  
  246.  
  247. plot(IsLongOpen ? entryprice_long : na , color = color.blue , style=plot.style_linebr,  linewidth=2, title="Entry Price Long") // stampa l'entry price in rosso se short in blu se long
  248. plot(IsLongOpen ?  stop_loss_long_price : na, color=color.red, style=plot.style_cross, linewidth=2, title="Stop Loss Long")
  249. plot(IsLongOpen ?  take_profit_long_price : na, color=color.green, style=plot.style_cross, linewidth=2, title="Target Long")
  250. plot(IsLongOpen ?  trailing_stop_trigger_long1 :  na, color= color.blue , style=plot.style_cross, linewidth=2, title="Trigger1 Long")
  251. plot(IsLongOpen ?  trailing_stop_close_long1 :  na, color= color.blue , style=plot.style_cross, linewidth=2, title="Close 1 Long")
  252. plot(IsLongOpen ?  trailing_stop_trigger_long2 :  na, color= color.blue , style=plot.style_cross, linewidth=2, title="Trigger2 Long")
  253. plot(IsLongOpen ?  trailing_stop_close_long2 :  na, color= color.blue , style=plot.style_cross, linewidth=2, title="Close 2 Long")
  254. plot(IsLongOpen ? highesthigh : na, title='highesthigh', style = plot.style_cross, linewidth = 1)
  255.  
  256.  
  257.  
  258. ////Alert//////
  259.  
  260.  
  261. // Esempio inserire ticker nel command
  262. // short = '{"pair":"'+ syminfo.basecurrency + syminfo.currency +'","unitsPercent":"10","unitsType" ETC... ETC... ETC...
  263.  
  264. // Nei command sotto inseriamo il prezzo del trailing in tempo reale
  265.  
  266. entry_command_long = '{"pair":"ETHUSDT","unitsPercent":90,"unitsType":"percentBalance","exchange":"Bybit","apiKey":"Main Account","broadcastSignal":"061e29d2-b9e8-47b6-8c69-9cbd95e55550","token":"e6d67d6e-1a5f-4e53-a9fd-6276dfa2a34b","isBuy":true,"isMarket":true,"stopLossPercent":"-1.3","stopLossType":"percent","leverage":"1","marginType":"ISOLATED","targets":[{"idx":1,"amount":"100","takeProfitPercent":"5"}],"targetType":"percent","targetAmountInPercent":true,"closeCurrentPosition":true,"preventPyramiding":true,"cancelAllOrders":true}' // Buy Long + Stop + Target TV-Hub
  267. //stop_limit_command_long = '{stop_limit_command_long' + str.tostring(price) + '}' // Solo Per Uscite Stop Limit Exchange Supportati
  268. exit_stop_market_command_long = '{"pair":"ETHUSDT","unitsPercent":90,"exchange":"Bybit","apiKey":"Main Account","broadcastSignal":"061e29d2-b9e8-47b6-8c69-9cbd95e55550","token":"e6d67d6e-1a5f-4e53-a9fd-6276dfa2a34b","isClose":true}'//'{exit_stop_market_command_long' + str.tostring(price) + '}' // Close Long TV-Hub
  269. exit_target_command_long = '{"pair":"ETHUSDT","unitsPercent":90,"exchange":"Bybit","apiKey":"Main Account","broadcastSignal":"061e29d2-b9e8-47b6-8c69-9cbd95e55550","token":"e6d67d6e-1a5f-4e53-a9fd-6276dfa2a34b","isClose":true}'//'{exit_target_command_long'  + str.tostring(price) + '}' // Close Long TV-Hub
  270. trailing1_limit_command_long = '{"pair":"ETHUSDT","exchange":"Bybit","apiKey":"Main Account","broadcastSignal":"061e29d2-b9e8-47b6-8c69-9cbd95e55550","token":"e6d67d6e-1a5f-4e53-a9fd-6276dfa2a34b","isStopLoss":true,"stopLossSizeType":"currentPosition","stopLossPercent":"","stopLossType":"absolute","stopLoss":'+str.tostring(trailing_stop_close_long1)+',"isBuy":true}' // Buy Stop Loss Use Absolute Value TV-Hub
  271. trailing1_market_command_long = '{"pair":"ETHUSDT","unitsPercent":90,"exchange":"Bybit","apiKey":"Main Account","broadcastSignal":"061e29d2-b9e8-47b6-8c69-9cbd95e55550","token":"e6d67d6e-1a5f-4e53-a9fd-6276dfa2a34b","isClose":true}'//'{trailing1_market_command_long' + str.tostring(price) + '}' // Close Long TV-Hub
  272. trailing2_limit_command_long = '{"pair":"ETHUSDT","exchange":"Bybit","apiKey":"Main Account","broadcastSignal":"061e29d2-b9e8-47b6-8c69-9cbd95e55550","token":"e6d67d6e-1a5f-4e53-a9fd-6276dfa2a34b","isStopLoss":true,"stopLossSizeType":"currentPosition","stopLossPercent":"","stopLossType":"absolute","stopLoss":'+str.tostring(trailing_stop_close_long2)+',"isBuy":true}' // Buy Stop Loss Use Absolute Value TV-Hub
  273. trailing2_market_command_long = '{"pair":"ETHUSDT","unitsPercent":90,"exchange":"Bybit","apiKey":"Main Account","broadcastSignal":"061e29d2-b9e8-47b6-8c69-9cbd95e55550","token":"e6d67d6e-1a5f-4e53-a9fd-6276dfa2a34b","isClose":true}'//'{Trailing Long 2 market '  + str.tostring(price) + ' }' // Close Long TV-Hub
  274.  
  275.  
  276.  
  277. // inseriamo ordine ingresso market con target limit senza stop stringa tvhub
  278. // Facciamo una sorta di screen del tempo reale che si aggiorna ad ogni tick appena entriamo in posizione e lo confrontiamo con i secondi normali dandogli una differenza in millesecondi aggiunti
  279. // prima di mandare altri ordini per non instasare gli ordini.
  280. if condEntryLong[1]
  281.     alert(entry_command_long)
  282. //    orario_barra := last_bar_time
  283. //if condEntryLong[1] and (secondi - orario_barra) > 10000 // 10 secondi di ritardo circa in "millisecondi"
  284. //    alert(stop_limit_command_long) // reduce only              {2nda schermata  sell solo stop-loss} {Use SL size from current position}
  285. //plot(secondi - tempo_attuale, "title=sec-temp")
  286. //plot(tempo_attuale, title="temp_att")
  287.  
  288. // usciamo a stop market se non siamo usciti stop limit stringa tvhub
  289. if condExitLongStop and IsLongOpen
  290.     alert(exit_stop_market_command_long) // reduce only
  291. // usciamo a target market reduce only se non siamo usciti target limit stringa tvhub
  292. if condExitLongTarget and IsLongOpen
  293.     alert(exit_target_command_long) // reduce only
  294. // piazzo un limit sul close long 1 stringa tvhub
  295. if openlong[1] >=1 and IsLongOpen and highesthigh >= trailing_stop_trigger_long1 and contatraillong1 == 1
  296.     alert(trailing1_limit_command_long) // reduce only
  297. // mando un ordine market chiusura emergenza al tocco di close long 1 stringa tv hub
  298. if trailing_long1 and IsLongOpen
  299.     alert(trailing1_market_command_long) //reduce only
  300. // piazzo un limit sul close long 2 stringa tvhub
  301. if openlong[1] >=1 and IsLongOpen and highesthigh >= trailing_stop_trigger_long2 and contatraillong2 == 1
  302.     alert(trailing2_limit_command_long) // reduce only
  303. // mando un ordine market chiusura emergenza al tocco di close long 1 stringa tv hub
  304. if trailing_long2 and IsLongOpen
  305.     alert(trailing2_market_command_long) //reduce only
  306.  
  307. if IsLongOpen == false
  308.     contatraillong1:=0
  309.     contatraillong2:=0
  310.     lunghezza_trailing_long:=0
  311.     //contaalert:=0
  312.  
  313. if IsLongOpen
  314.     openshort:=0
  315.  
  316. ///////////////////////////////////////////////////////SHORT////////////////////////////////////////////////////////////
  317.  
  318. //condEntryShort =  close < (media_short - apertura_minima_short)   and valore_adx > differenziale_adx_short_basso and valore_adx < differenziale_adx_short_alto  and valore_rsi > differenziale_rsi_short and not in_solo_long
  319.  
  320.  
  321.  
  322.  
  323.  
  324. var contaoperazionishort=0
  325. if condEntryShort
  326.     contaoperazionishort +=1
  327.    
  328. plot(contaoperazionishort, title = 'numero operazioni short')
  329. plotshape(condEntryShort, color = color.red)
  330.  
  331. var indice_entry_bar_short = 0
  332. if condEntryShort[1]
  333.     indice_entry_bar_short :=  bar_index
  334.  
  335. lunghezza_trailing_short = bar_index - indice_entry_bar_short +1
  336. plot(lunghezza_trailing_short, title = 'lunghezza trailing short')
  337. varip lowestlow=0.0
  338. lowestlow := ta.lowest(vlow, lunghezza_trailing_short)
  339.  
  340.  
  341.  
  342. /// USCITE STOP TARGET E TRAIL
  343. stop_loss_short_price =(entryprice_short + (entryprice_short * input_stop_loss_short) / 100)
  344. take_profit_short_price = (entryprice_short - (entryprice_short * input_target_short) / 100)
  345. trailing_stop_trigger_short1 = entryprice_short - (entryprice_short * input_target_short /100) * (input_trailing_stop_trigger_short1/100)
  346. trailing_stop_close_short1 = entryprice_short - (entryprice_short * input_target_short /100) * (input_trailing_stop_close_short1/100)
  347. trailing_stop_trigger_short2 = entryprice_short - (entryprice_short * input_target_short /100) * (input_trailing_stop_trigger_short2/100)
  348. trailing_stop_close_short2 = entryprice_short - (entryprice_short * input_target_short /100) * (input_trailing_stop_close_short2/100)
  349.  
  350.  
  351. //condExitLongStop = (openlong >=1 and ((vlow <= stop_loss_long_price and lunghezza_trailing != 2 ) or (vlow <= stop_loss_long_price and lunghezza_trailing == 2 and close<open)))
  352. condExitShortStop = (openshort[1] >=1 and  (vhigh >= stop_loss_short_price))
  353. condExitShortTarget =  (openshort[1]>=1 and  (vlow <= take_profit_short_price))
  354.  
  355. if condExitShortStop
  356.     openshort:=0
  357.     openlong:=0
  358.  
  359. if condExitShortTarget
  360.     openshort:=0
  361.     openlong:=0
  362.    
  363. condExitShortStopint = if condExitShortStop == true
  364.     1
  365. else
  366.     0
  367. plot(condExitShortStopint, title = 'cond uscita stop short')
  368.  
  369. condExitShortTargetint = if condExitShortTarget == true
  370.     1
  371. else
  372.     0
  373. plot(condExitShortTargetint, title = 'cond uscita target short')
  374.  
  375.  
  376. trailing_short1 = (openshort[1] >=1 and ((lowestlow <= trailing_stop_trigger_short1 and lowestlow > trailing_stop_trigger_short2 ) and ( vhigh >= trailing_stop_close_short1) and not (vhigh > trailing_stop_close_short1 and vlow == lowestlow and low[1] != lowestlow))) or (openshort[1] >=1 and (lowestlow == vlow and lowestlow != low[1]) and ((lowestlow <= trailing_stop_trigger_short1 and lowestlow > trailing_stop_trigger_short2) and (price >= trailing_stop_close_short1)))
  377. trailing_short2 = (openshort[1] >=1 and ((lowestlow <= trailing_stop_trigger_short2 and lowestlow > take_profit_short_price ) and ( vhigh >= trailing_stop_close_short2) and not (vhigh > trailing_stop_close_short2 and vlow == lowestlow and low[1] != lowestlow))) or (openshort[1] >=1 and (lowestlow == vlow and lowestlow != low[1]) and ((lowestlow <= trailing_stop_trigger_short2 and lowestlow > take_profit_short_price) and (price >= trailing_stop_close_short2)))
  378. if trailing_short1 or trailing_short2
  379.     openshort:=0
  380.     openlong:=0
  381.  
  382. IsShortOpen = false
  383. IsShortOpen := condEntryShort[1] ? true : condExitShortStop[1] ? false : condExitShortTarget[1] ? false : trailing_short1[1] ? false : trailing_short2[1] ? false : IsShortOpen[1]
  384.  
  385. IsShortOpenint = if IsShortOpen == true
  386.     1
  387. else
  388.     0
  389. plot(IsShortOpenint, title = 'isshortopen')
  390.  
  391. // Contatori
  392.  
  393. varip contatrailshort1 = 0
  394. if lowestlow <= trailing_stop_trigger_short1
  395.     contatrailshort1 += 1
  396. plot(contatrailshort1, title = 'contatrailshort1')
  397.  
  398. varip contatrailshort2 = 0
  399. if lowestlow >= trailing_stop_trigger_short2
  400.     contatrailshort2 += 1
  401. plot(contatrailshort2, title = 'contatrailshort2')
  402.  
  403.  
  404. plot(IsShortOpen ? entryprice_short : na , color = color.red , style=plot.style_linebr,  linewidth=2, title="Entry Price Short") // stampa l'entry price in rosso se short in blu se long
  405. plot(IsShortOpen ?  stop_loss_short_price : na, color=color.red, style=plot.style_cross, linewidth=2, title="Stop Loss Short ")
  406. plot(IsShortOpen ?  take_profit_short_price : na, color=color.green, style=plot.style_cross, linewidth=2, title="Target Short")
  407. plot(IsShortOpen ?  trailing_stop_trigger_short1 :  na, color= color.red , style=plot.style_cross, linewidth=2, title="Trigger1 Short")
  408. plot(IsShortOpen ?  trailing_stop_close_short1 :  na, color= color.red , style=plot.style_cross, linewidth=2, title="Close1 Short")
  409. plot(IsShortOpen ?  trailing_stop_trigger_short2 :  na, color= color.red , style=plot.style_cross, linewidth=2, title="Trigger2 Short")
  410. plot(IsShortOpen ?  trailing_stop_close_short2 :  na, color= color.red , style=plot.style_cross, linewidth=2, title="Close1 Short")
  411. plot(IsShortOpen ? lowestlow : na, title='lowestlow', style = plot.style_cross, color= color.red, linewidth = 1)
  412.  
  413.  
  414.  
  415. ////Alert//////
  416.  
  417. entry_command_short = '{"pair":"ETHUSDT","unitsPercent":"90","unitsType":"percentBalance","exchange":"Bybit","apiKey":"Main Account","broadcastSignal":"061e29d2-b9e8-47b6-8c69-9cbd95e55550","token":"e6d67d6e-1a5f-4e53-a9fd-6276dfa2a34b","isSell":true,"isMarket":true,"stopLossPercent":"-1.1","stopLossType":"absolute","stopLoss":"1845.07","leverage":"1","marginType":"ISOLATED","targets":[{"idx":1,"amount":"100","takeProfitPercent":"10"}],"targetType":"percent","targetAmountInPercent":true,"closeCurrentPosition":true,"preventPyramiding":true,"cancelAllOrders":true}' // Sell Short + Stop + Target TV-Hub
  418. //stop_limit_command_short = '{stop_limit_command_short'+ str.tostring(price) + '}' // Solo Per Uscite Stop Limit Exchange Supportati
  419. exit_stop_market_command_short = '{"pair":"ETHUSDT","unitsPercent":"90","exchange":"Bybit","apiKey":"Main Account","broadcastSignal":"061e29d2-b9e8-47b6-8c69-9cbd95e55550","token":"e6d67d6e-1a5f-4e53-a9fd-6276dfa2a34b","isClose":true}' // Close Short TV-Hub
  420. exit_target_command_short = '{"pair":"ETHUSDT","unitsPercent":"90","exchange":"Bybit","apiKey":"Main Account","broadcastSignal":"061e29d2-b9e8-47b6-8c69-9cbd95e55550","token":"e6d67d6e-1a5f-4e53-a9fd-6276dfa2a34b","isClose":true}' // Close Short TV-Hub
  421. trailing1_limit_command_short= '{"pair":"ETHUSDT","exchange":"Bybit","apiKey":"Main Account","broadcastSignal":"061e29d2-b9e8-47b6-8c69-9cbd95e55550","token":"e6d67d6e-1a5f-4e53-a9fd-6276dfa2a34b","isStopLoss":true,"stopLossSizeType":"currentPosition","stopLossPercent":"","stopLossType":"absolute","stopLoss":'+str.tostring(trailing_stop_close_short1)+',"isSell":true}' // Sell Stop Loss Use Absolute Value TV-Hub
  422. trailing1_market_command_short = '{"pair":"ETHUSDT","unitsPercent":"90","exchange":"Bybit","apiKey":"Main Account","broadcastSignal":"061e29d2-b9e8-47b6-8c69-9cbd95e55550","token":"e6d67d6e-1a5f-4e53-a9fd-6276dfa2a34b","isClose":true}' // Close Short TV-Hub
  423. trailing2_limit_command_short = '{"pair":"ETHUSDT","exchange":"Bybit","apiKey":"Main Account","broadcastSignal":"061e29d2-b9e8-47b6-8c69-9cbd95e55550","token":"e6d67d6e-1a5f-4e53-a9fd-6276dfa2a34b","isStopLoss":true,"stopLossSizeType":"currentPosition","stopLossPercent":"","stopLossType":"absolute","stopLoss":'+str.tostring(trailing_stop_close_short2)+',"isSell":true}' // Sell Stop Loss Use Absolute Value TV-Hub
  424. trailing2_market_command_short = '{"pair":"ETHUSDT","unitsPercent":"90","exchange":"Bybit","apiKey":"Main Account","broadcastSignal":"061e29d2-b9e8-47b6-8c69-9cbd95e55550","token":"e6d67d6e-1a5f-4e53-a9fd-6276dfa2a34b","isClose":true}' // Close Short TV-Hub
  425.  
  426. // entry_command_short = '{}' //'{entry_command_short' + str.tostring(price) + '}'
  427. // stop_limit_command_short = '{stop_limit_command_short'+ str.tostring(price) + '}'
  428. // exit_stop_market_command_short = '{exit_stop_market_command_short' + str.tostring(price) + '}'
  429. // exit_target_command_short = '{exit_target_command_short' + str.tostring(price) + '}'
  430. // trailing1_limit_command_short= '{trailing1_limit_command_short' + str.tostring(price) + '}'
  431. // trailing1_market_command_short = '{trailing1_market_command_short' + str.tostring(price) + '}'
  432. // trailing2_limit_command_short = '{trailing2_limit_command_short'  + str.tostring(price) + '}'
  433. // trailing2_market_command_short = '{trailing2_market_command_short' + str.tostring(price) + '}'
  434.  
  435.  
  436.  
  437. // inseriamo ordine ingresso market con target limit senza stop stringa tvhub
  438. if condEntryShort[1]
  439.     alert(entry_command_short)
  440. //    orario_barra := last_bar_time
  441. //if condEntryShort[1] and (secondi - orario_barra) > 10000 // 10 secondi di ritardo circa in "millisecondi"
  442. //    alert(stop_limit_command_short) // reduce only              {2nda schermata  sell solo stop-loos} {Use SL size from current position}
  443. //plot(secondi - tempo_attuale, "title=sec-temp")
  444. //plot(tempo_attuale, title="temp_att")
  445.  
  446. // usciamo a stop market se non siamo usciti stop limit stringa tvhub
  447. if condExitShortStop and IsShortOpen
  448.     alert(exit_stop_market_command_short) // reduce only
  449. // usciamo a target market reduce only se non siamo usciti target limit stringa tvhub
  450. if condExitShortTarget and IsShortOpen
  451.     alert(exit_target_command_short) // reduce only
  452. // piazzo un limit sul close long 1 stringa tvhub
  453. if openshort[1] >=1 and IsShortOpen and lowestlow <= trailing_stop_trigger_short1 and contatrailshort1 == 1
  454.     alert(trailing1_limit_command_short) // reduce only
  455. // mando un ordine market chiusura emergenza al tocco di close long 1 stringa tv hub
  456. if trailing_short1 and IsShortOpen
  457.     alert(trailing1_market_command_short) //reduce only
  458. // piazzo un limit sul close long 2 stringa tvhub
  459. if openshort[1] >=1 and IsShortOpen and lowestlow <= trailing_stop_trigger_short2 and contatrailshort2 == 1
  460.     alert(trailing2_limit_command_short) // reduce only
  461. // mando un ordine market chiusura emergenza al tocco di close long 1 stringa tv hub
  462. if trailing_short2 and IsShortOpen
  463.     alert(trailing2_market_command_short) //reduce only
  464.  
  465. if IsShortOpen == false
  466.     contatrailshort1:=0
  467.     contatrailshort2:=0
  468.     lunghezza_trailing_short:=0
  469.  
  470. if IsShortOpen
  471.     openlong:=0
  472.    
  473.     //contaalert:=0
  474.  
  475. ///////////////////////////////////////////////CONTROLLO ERROTI////////////////////////////////////////////////
  476. errore = (vlow < trailing_stop_close_long1 and vhigh > trailing_stop_trigger_long1) or (vlow < trailing_stop_close_long2 and vhigh > trailing_stop_trigger_long2)
  477. //plotshape(errore, color= color.red , size = size.large)
  478.  
  479. var contaerrori = 0
  480. if errore
  481.     contaerrori +=1
  482. plot(contaerrori, title = 'conta errori')    
  483.  
  484.  
  485. // Nome Alert: Study Scalping Exponential Tick 5M
  486. // URl Webhook: https://alerts.tv-hub.org/api/ExecuteTradeSignalClassic
  487.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement