Advertisement
xmd79

AG328

Sep 17th, 2023
333
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.87 KB | None | 0 0
  1. //@version=5
  2. strategy(title='AG328', shorttitle='AG328', overlay=true, commission_type = strategy.commission.cash_per_contract, commission_value = 0.000025, initial_capital = 1000, currency = currency.USD,default_qty_value = 1000)
  3.  
  4. // Настройки для включения/выключения торговли в Лонг и Шорт
  5. longEnabled = input(true, title="Торговля в Лонг")
  6. shortEnabled = input(true, title="Торговля в Шорт")
  7.  
  8. smaEnabled = input(true, title="Включить SMA89")
  9. tradeInGrey = input(false, title = "Сигнал в серой зоне")
  10.  
  11. pipsBuyStop = input.int(0, title="Пунктов добавить для Buy ордера", minval=-50, step=1, maxval=50)
  12. pipsSellStop = input.int(0, title="Пунктов добавить для Sell ордера", minval=-50, step=1, maxval=50)
  13.  
  14. // Const
  15. LicenseID = 6889430941909
  16. contracts = input.float(0.01, title="Контрактов на сделку:", minval=0, step=0.01, maxval=10)
  17.  
  18. var float sma = na
  19.  
  20. var float UW = na
  21. var float DW = na
  22. var bool weeklyLongPriority = na
  23. var bool weeklyShortPriority = na
  24.  
  25. var float UD = na
  26. var float DD = na
  27. var bool dailyLongPriority = na
  28. var bool dailyShortPriority = na
  29.  
  30. var float UP = na
  31. var float DOWN = na
  32. var bool h4LongPriority = na
  33. var bool h4ShortPriority = na
  34.  
  35. var bool LongCondition = na
  36. var bool ShortCondition = na
  37.  
  38. var bool GreenZone = na
  39. var bool GreyZone = na
  40. var bool RedZone = na
  41.  
  42. var float LongOrder = 0
  43. var float ShortOrder = 0
  44.  
  45. var float LongTP = 0
  46. var float ShortTP = 0
  47.  
  48. var float LongTake = 0
  49. var float ShortTake = 0
  50.  
  51. var float AA = 0
  52. var float BB = 0
  53. var float CC = 0
  54. var float D = 0
  55.  
  56. var float AAA = 0
  57. var float BBB = 0
  58. var float CCC = 0
  59. var float DDD = 0
  60.  
  61. var float stopLong = 0
  62. var float stopShort = 0
  63.  
  64. var string olderTF = ""
  65. var string oldestTF = ""
  66. var string pivotTF = ""
  67.  
  68. // Создаем входную настройку для ТФ Пивота
  69. maxValuePivotTF = input.int(2, title="ТФ Пивота старше на:", minval=1, step=1, maxval=3)
  70.  
  71. // Шаг цены инструмента
  72. stepSize = syminfo.mintick
  73. currentTF = timeframe.period // Получаем текущий ТФ
  74.  
  75. if currentTF == "1" // Определяем 2 более старших ТФ
  76. olderTF := "5"
  77. oldestTF := "15"
  78. pivotTF := (maxValuePivotTF == 1 ? "5" : (maxValuePivotTF == 2 ? "15" : "60"))
  79. if currentTF == "5"
  80. olderTF := "15"
  81. oldestTF := "60"
  82. pivotTF := (maxValuePivotTF == 1 ? "15" : (maxValuePivotTF == 2 ? "60" : "240"))
  83. if currentTF == "15"
  84. olderTF := "60"
  85. oldestTF := "240"
  86. pivotTF := (maxValuePivotTF == 1 ? "60" : (maxValuePivotTF == 2 ? "240" : "D"))
  87. if currentTF == "60"
  88. olderTF := "240"
  89. oldestTF := "D"
  90. pivotTF := (maxValuePivotTF == 1 ? "240" : (maxValuePivotTF == 2 ? "D" : "W"))
  91. if currentTF == "240"
  92. olderTF := "D"
  93. oldestTF := "W"
  94. pivotTF := (maxValuePivotTF == 1 ? "D" : (maxValuePivotTF == 2 ? "W" : "M"))
  95. if currentTF == "D"
  96. olderTF := "W"
  97. oldestTF := "M"
  98. pivotTF := (maxValuePivotTF == 1 ? "W" : (maxValuePivotTF == 2 ? "M" : "3M"))
  99. if currentTF == "W"
  100. olderTF := "M"
  101. oldestTF := "3M"
  102. pivotTF := (maxValuePivotTF == 1 ? "M" : (maxValuePivotTF == 2 ? "3M" : "3M"))
  103. // Рассчитываем бары ТФ+2
  104. weekHigh0 = request.security(syminfo.tickerid, oldestTF, high)
  105. weekHigh1 = request.security(syminfo.tickerid, oldestTF, high[1])
  106. weekHigh2 = request.security(syminfo.tickerid, oldestTF, high[2])
  107. weekHigh3 = request.security(syminfo.tickerid, oldestTF, high[3])
  108. weekHigh4 = request.security(syminfo.tickerid, oldestTF, high[4])
  109.  
  110. weekLow0 = request.security(syminfo.tickerid, oldestTF, low)
  111. weekLow1 = request.security(syminfo.tickerid, oldestTF, low[1])
  112. weekLow2 = request.security(syminfo.tickerid, oldestTF, low[2])
  113. weekLow3 = request.security(syminfo.tickerid, oldestTF, low[3])
  114. weekLow4 = request.security(syminfo.tickerid, oldestTF, low[4])
  115.  
  116. // ТФ+2 Фракталы
  117. weekFractal_UP = weekHigh2 > weekHigh1 and weekHigh2 > weekHigh0 and weekHigh2 > weekHigh3 and weekHigh2 > weekHigh4
  118. weekFractal_DOWN = weekLow2 < weekLow1 and weekLow2 < weekLow0 and weekLow2 < weekLow3 and weekLow2 < weekLow4
  119.  
  120. if weekFractal_UP
  121. UW := weekHigh2
  122. UW
  123. if weekFractal_DOWN
  124. DW := weekLow2
  125. DW
  126. // Рисуем UW, DW
  127. plot(UW, title = "UW", color=color.green)
  128. plot(DW, title = "DW", color=color.red)
  129.  
  130. // ТФ+2 priority
  131. if close > UW
  132. weeklyLongPriority := true
  133. weeklyLongPriority
  134. else if close < DW
  135. weeklyLongPriority := false
  136. weeklyLongPriority
  137. //weeklyColor = weeklyLongPriority ? color.new(color.green, transp=70) : color.new(color.red, transp=70)
  138. //bgcolor(weeklyColor, title = "WeeklyPriority")
  139.  
  140. //-----------------------------------------------
  141. // Рассчитываем дневные бары
  142.  
  143. dayHigh0 = request.security(syminfo.tickerid, olderTF, high)
  144. dayHigh1 = request.security(syminfo.tickerid, olderTF, high[1])
  145. dayHigh2 = request.security(syminfo.tickerid, olderTF, high[2])
  146. dayHigh3 = request.security(syminfo.tickerid, olderTF, high[3])
  147. dayHigh4 = request.security(syminfo.tickerid, olderTF, high[4])
  148.  
  149. dayLow0 = request.security(syminfo.tickerid, olderTF, low)
  150. dayLow1 = request.security(syminfo.tickerid, olderTF, low[1])
  151. dayLow2 = request.security(syminfo.tickerid, olderTF, low[2])
  152. dayLow3 = request.security(syminfo.tickerid, olderTF, low[3])
  153. dayLow4 = request.security(syminfo.tickerid, olderTF, low[4])
  154.  
  155. // Дневные Фракталы
  156. dayFractal_UP = dayHigh2 > dayHigh1 and dayHigh2 > dayHigh0 and dayHigh2 > dayHigh3 and dayHigh2 > dayHigh4
  157. dayFractal_DOWN = dayLow2 < dayLow1 and dayLow2 < dayLow0 and dayLow2 < dayLow3 and dayLow2 < dayLow4
  158.  
  159. if dayFractal_UP
  160. UD := dayHigh2
  161. UD
  162. if dayFractal_DOWN
  163. DD := dayLow2
  164. DD
  165. // Рисуем UD, DD
  166. //plot(UD, title = "UD", color=color.green)
  167. //plot(DD, title = "DD", color=color.red)
  168.  
  169. // Daily priority
  170. if close > UD
  171. dailyLongPriority := true
  172. dailyLongPriority
  173. else if close < DD
  174. dailyLongPriority := false
  175. dailyLongPriority
  176. //dailyColor = dailyLongPriority ? color.new(color.green, transp=70) : color.new(color.red, transp=70)
  177. //bgcolor(dailyColor, title = "DailyPriority")
  178.  
  179. //-----------------------------------------------
  180. // Рассчитываем 4-часовые бары
  181.  
  182. h4High0 = request.security(syminfo.tickerid, currentTF, high)
  183. h4High1 = request.security(syminfo.tickerid, currentTF, high[1])
  184. h4High2 = request.security(syminfo.tickerid, currentTF, high[2])
  185. h4High3 = request.security(syminfo.tickerid, currentTF, high[3])
  186. h4High4 = request.security(syminfo.tickerid, currentTF, high[4])
  187.  
  188. h4Low0 = request.security(syminfo.tickerid, currentTF, low)
  189. h4Low1 = request.security(syminfo.tickerid, currentTF, low[1])
  190. h4Low2 = request.security(syminfo.tickerid, currentTF, low[2])
  191. h4Low3 = request.security(syminfo.tickerid, currentTF, low[3])
  192. h4Low4 = request.security(syminfo.tickerid, currentTF, low[4])
  193.  
  194. // H4 Фракталы
  195. h4Fractal_UP = h4High2 > h4High1 and h4High2 > h4High0 and h4High2 > h4High3 and h4High2 > h4High4
  196. h4Fractal_DOWN = h4Low2 < h4Low1 and h4Low2 < h4Low0 and h4Low2 < h4Low3 and h4Low2 < h4Low4
  197.  
  198. if h4Fractal_UP
  199. UP := h4High2
  200. UP
  201. if h4Fractal_DOWN
  202. DOWN := h4Low2
  203. DOWN
  204. // Рисуем UP, DOWN
  205. plot(UP, title='UP', color=color.new(color.green, 0))
  206. plot(DOWN, title='DOWN', color=color.new(color.red, 0))
  207.  
  208. // SMA89
  209. sma89 = ta.sma(close, 89)
  210. plot(smaEnabled ? sma89 : na, title='sma89', color=color.new(color.white, transp=10))
  211. //smaColor = close > sma89 ? color.new(color.green, transp=70) : color.new(color.red, transp=70)
  212. //bgcolor(smaColor, title = "smaPriority")
  213.  
  214. // Condition
  215. LongCondition := weeklyLongPriority and dailyLongPriority and (smaEnabled ? close > sma89 : true)
  216. ShortCondition := weeklyLongPriority == false and dailyLongPriority == false and (smaEnabled ? close < sma89 : true)
  217. ConditionColor = LongCondition ? color.new(color.green, transp=85) : ShortCondition ? color.new(color.red, transp=85) : color.new(color.gray, transp=85)
  218. bgcolor(ConditionColor, title='Condition')
  219.  
  220. // LOGIC LONG
  221.  
  222. if AA == 0 and h4Fractal_UP
  223. AA := UP
  224. if (AA[1] != 0 and BB == 0 and h4Fractal_DOWN) or (AA[1] != 0 and BB != 0 and D == 2 and h4Fractal_DOWN)
  225. BB := DOWN
  226. D := 1
  227. if BB != 0 and D == 1 and ta.crossunder(low, BB)
  228. D := 2
  229. if AA != 0 and BB != 0
  230. if D == 2 and (D[1] == 1 or D[2] == 1 or D[3] == 1) and h4Fractal_UP
  231. CC := UP
  232. else if D == 1 and h4Fractal_UP
  233. CC := UP
  234. if (AA != 0 and high > AA) or (LongOrder != 0 and high > LongOrder + pipsBuyStop * stepSize) or (tradeInGrey ? ShortCondition : not LongCondition)
  235. AA := 0
  236. BB := 0
  237. CC := 0
  238. D := 0
  239. //
  240. //plot(AA != 0 ? AA : na, title='A', color=color.new(color.white, transp=10), linewidth=2, style=plot.style_linebr)
  241. //plot(BB != 0 ? BB : na, title='B', color=color.new(color.gray, transp=10), linewidth=2, style=plot.style_linebr)
  242. //plot(CC != 0 ? CC : na, title='C', color=color.new(color.blue, transp=10), linewidth=2, style=plot.style_linebr)
  243. //plot(D != 0 ? D : na, title='D', color=color.new(color.green, transp=80), linewidth=2, style=plot.style_linebr)
  244.  
  245. // LOGIC SHORT
  246. if AAA == 0 and h4Fractal_DOWN
  247. AAA := DOWN
  248. if (AAA[1] != 0 and BBB == 0 and h4Fractal_UP) or (AAA[1] != 0 and BBB[1] != 0 and DDD == 2 and h4Fractal_UP)
  249. BBB := UP
  250. DDD := 1
  251. if BBB != 0 and DDD == 1 and ta.crossover(high, BBB)
  252. DDD := 2
  253. if AAA != 0 and BBB != 0
  254. if DDD == 2 and (DDD[1] == 1 or DDD[2] == 1 or DDD[3] == 1) and h4Fractal_DOWN
  255. CCC := DOWN
  256. else if DDD == 1 and h4Fractal_DOWN
  257. CCC := DOWN
  258. if (AAA != 0 and low < AAA) or (ShortOrder != 0 and low < ShortOrder - pipsSellStop * stepSize) or (tradeInGrey ? LongCondition : not ShortCondition)
  259. AAA := 0
  260. BBB := 0
  261. CCC := 0
  262. DDD := 0
  263. //
  264. //plot(AAA != 0 ? AAA : na, title='ShortA', color=color.new(color.white, transp=10), linewidth=2, style=plot.style_linebr)
  265. //plot(BBB != 0 ? BBB : na, title='ShortB', color=color.new(color.gray, transp=10), linewidth=2, style=plot.style_linebr)
  266. //plot(CCC != 0 ? CCC : na, title='ShortC', color=color.new(color.blue, transp=10), linewidth=2, style=plot.style_linebr)
  267. //plot(DDD != 0 ? DDD : na, title='ShortD', color=color.new(color.green, transp=80), linewidth=2, style=plot.style_linebr)
  268.  
  269.  
  270. // LongOrder
  271. if (tradeInGrey ? not ShortCondition : LongCondition) and CC != 0 and D == 2 and strategy.position_size[1] == 0 and longEnabled
  272. LongOrder := CC
  273. LongOrder
  274. else if (tradeInGrey ? ShortCondition : not LongCondition) or strategy.position_size[1] > 0 or (LongOrder != 0 and high > LongOrder + pipsBuyStop * stepSize)
  275. LongOrder := 0
  276. LongOrder
  277. plot(LongOrder != 0 ? LongOrder : na, title='LongOrder', color=color.new(color.yellow, transp=10), linewidth=2, style=plot.style_linebr)
  278.  
  279. // ShortOrder
  280. if (tradeInGrey ? not LongCondition : ShortCondition) and CCC != 0 and DDD == 2 and strategy.position_size[1] == 0 and shortEnabled
  281. ShortOrder := CCC
  282. ShortOrder
  283. else if (tradeInGrey ? LongCondition : not ShortCondition) or strategy.position_size[1] < 0 or (ShortOrder != 0 and low < ShortOrder - pipsSellStop * stepSize)
  284. ShortOrder := 0
  285. ShortOrder
  286. plot(ShortOrder != 0 ? ShortOrder : na, title='ShortOrder', color=color.new(color.orange, transp=10), linewidth=2, style=plot.style_linebr)
  287.  
  288. // Fibo Pivots
  289. H = request.security(syminfo.tickerid, pivotTF, high[1])
  290. L = request.security(syminfo.tickerid, pivotTF, low[1])
  291. C = request.security(syminfo.tickerid, pivotTF, close[1])
  292.  
  293. PP = (H + L + C) / 3
  294. R3 = PP + 1.000 * (H - L)
  295. R2 = PP + 0.618 * (H - L)
  296. R1 = PP + 0.382 * (H - L)
  297. S1 = PP - 0.382 * (H - L)
  298. S2 = PP - 0.618 * (H - L)
  299. S3 = PP - 1.000 * (H - L)
  300.  
  301. //plot(PP)
  302. //plot(R3)
  303. //plot(R2)
  304. //plot(R1)
  305. //plot(S1)
  306. //plot(S2)
  307. //plot(S3)
  308.  
  309. // Расчет цены Лонг Тейка
  310. if S3 - LongOrder > LongOrder - DOWN
  311. LongTP := S3
  312. LongTP
  313. else if S2 - LongOrder > LongOrder - DOWN
  314. LongTP := S2
  315. LongTP
  316. else if S1 - LongOrder > LongOrder - DOWN
  317. LongTP := S1
  318. LongTP
  319. else if PP - LongOrder > LongOrder - DOWN
  320. LongTP := PP
  321. LongTP
  322. else if R1 - LongOrder > LongOrder - DOWN
  323. LongTP := R1
  324. LongTP
  325. else if R2 - LongOrder > LongOrder - DOWN
  326. LongTP := R2
  327. LongTP
  328. else if R3 - LongOrder > LongOrder - DOWN
  329. LongTP := R3
  330. LongTP
  331. else
  332. LongTP := 0
  333. LongTP
  334. //
  335. //plot(LongTake)
  336.  
  337. if strategy.position_size == 0
  338. if LongTP == 0 and LongOrder != 0
  339. LongTake := LongOrder + LongOrder - DOWN
  340. LongTake
  341. else
  342. LongTake := LongTP
  343. LongTake
  344.  
  345. plot(series=strategy.position_size > 0 ? LongTake : na, title='LongTake', color=color.new(color.rgb(99, 253, 104), transp=0), linewidth=1, style=plot.style_linebr)
  346.  
  347. // Расчет цены Шорт Тейка
  348. if ShortOrder - R3 > UP - ShortOrder
  349. ShortTP := R3
  350. ShortTP
  351. else if ShortOrder - R2 > UP - ShortOrder
  352. ShortTP := R2
  353. ShortTP
  354. else if ShortOrder - R1 > UP - ShortOrder
  355. ShortTP := R1
  356. ShortTP
  357. else if ShortOrder - PP > UP - ShortOrder
  358. ShortTP := PP
  359. ShortTP
  360. else if ShortOrder - S1 > UP - ShortOrder
  361. ShortTP := S1
  362. ShortTP
  363. else if ShortOrder - S2 > UP - ShortOrder
  364. ShortTP := S2
  365. ShortTP
  366. else if ShortOrder - S3 > UP - ShortOrder
  367. ShortTP := S3
  368. ShortTP
  369. else
  370. ShortTP := 0
  371. ShortTP
  372. //
  373. //plot(ShortTP)
  374. if strategy.position_size == 0
  375. if ShortTP == 0 and ShortOrder != 0
  376. ShortTake := ShortOrder - (UP - ShortOrder)
  377. ShortTake
  378. else
  379. ShortTake := ShortTP
  380. ShortTake
  381.  
  382. plot(series=strategy.position_size < 0 ? ShortTake : na, title='ShortTake', color=color.new(color.rgb(99, 253, 104), transp=0), linewidth=1, style=plot.style_linebr)
  383.  
  384. // StopForLONG and SHORT
  385. stopLong := math.min(DOWN,ta.lowest(low,3)) - pipsSellStop*stepSize
  386. //plot(stopLong)
  387. stopShort := math.max(UP,ta.highest(high,3)) + pipsBuyStop*stepSize
  388. //plot(stopShort)
  389.  
  390. // TRADES LONG
  391. if LongOrder > 0 and close < LongOrder and longEnabled and LongCondition
  392. strategy.entry('Long', strategy.long, stop=LongOrder + pipsBuyStop*stepSize)
  393. if LongOrder == 0 or not LongCondition or not longEnabled
  394. strategy.cancel('Long')
  395. strategy.exit('CloseLong', from_entry='Long', stop=stopLong, limit=LongTake - pipsSellStop*stepSize)
  396.  
  397. // LONG ALERT !!!
  398. if longEnabled and LongCondition and LongOrder[1] == 0 and LongOrder != 0
  399. alert(str.tostring(LicenseID)+',buystop,GBPUSDb,price=' +str.tostring(LongOrder + pipsBuyStop*stepSize)+',risk='+str.tostring(contracts), alert.freq_once_per_bar_close)
  400. if longEnabled and LongCondition and LongOrder[1] != 0 and LongOrder != 0 and LongOrder != LongOrder[1]
  401. alert(str.tostring(LicenseID)+',cancellongbuystop,GBPUSDb,price='+str.tostring(LongOrder + pipsBuyStop*stepSize)+',risk='+str.tostring(contracts), alert.freq_once_per_bar_close)
  402. if (strategy.position_size > 0 and (LongTake != LongTake[1] or stopLong != stopLong[1])) or (strategy.position_size > 0 and strategy.position_size[1] == 0 )
  403. alert(str.tostring(LicenseID)+',newsltplong,GBPUSDb,sl='+str.tostring(stopLong)+',tp='+str.tostring(LongTake - pipsSellStop*stepSize), alert.freq_once_per_bar_close)
  404. if strategy.position_size == 0 and ((LongCondition[1] and not LongCondition) or not longEnabled) and (LongOrder[1] != 0 and LongOrder == 0)
  405. alert(str.tostring(LicenseID)+',cancellong,GBPUSDb', alert.freq_once_per_bar_close)
  406.  
  407. // TRADES SHORT
  408. if ShortOrder > 0 and close > ShortOrder and shortEnabled and ShortCondition
  409. strategy.entry('Short', strategy.short, stop=ShortOrder - pipsSellStop*stepSize)
  410. if ShortOrder == 0 or not ShortCondition or not shortEnabled
  411. strategy.cancel('Short')
  412. strategy.exit('CloseShort', from_entry='Short', stop=stopShort, limit=ShortTake + pipsBuyStop*stepSize)
  413.  
  414. // SHORT ALERT !!!
  415. if shortEnabled and ShortCondition and ShortOrder[1] == 0 and ShortOrder != 0
  416. alert(str.tostring(LicenseID)+',sellstop,GBPUSDb,price=' +str.tostring(ShortOrder - pipsSellStop*stepSize)+',risk='+str.tostring(contracts), alert.freq_once_per_bar_close)
  417. if shortEnabled and ShortCondition and ShortOrder[1] != 0 and ShortOrder != 0 and ShortOrder != ShortOrder[1]
  418. alert(str.tostring(LicenseID)+',cancelshortsellstop,GBPUSDb,price='+str.tostring(ShortOrder - pipsSellStop*stepSize)+',risk='+str.tostring(contracts), alert.freq_once_per_bar_close)
  419. if (strategy.position_size < 0 and (ShortTake != ShortTake[1] or stopShort != stopShort[1])) or (strategy.position_size < 0 and strategy.position_size[1] == 0)
  420. alert(str.tostring(LicenseID)+',newsltpshort,GBPUSDb,sl='+str.tostring(stopShort)+',tp='+str.tostring(ShortTake + pipsBuyStop*stepSize), alert.freq_once_per_bar_close)
  421. if strategy.position_size == 0 and ((ShortCondition[1] and not ShortCondition) or not shortEnabled) and (ShortOrder[1] != 0 and ShortOrder == 0)
  422. alert(str.tostring(LicenseID)+',cancelshort,GBPUSDb', alert.freq_once_per_bar_close)
  423.  
  424.  
Advertisement
Comments
  • hemal9022
    1 year
    # text 0.12 KB | 0 0
    1. i got all types of premium tradingview indicators codes available on telegram - https://t.me/tradingview_premium_indicator
Add Comment
Please, Sign In to add comment
Advertisement