ATR اور ٹریلنگ اسٹاپ پر مبنی سپر ٹرینڈ حکمت عملی


تخلیق کی تاریخ: 2023-11-28 14:56:59 آخر میں ترمیم کریں: 2023-11-28 14:56:59
کاپی: 0 کلکس کی تعداد: 495
1
پر توجہ دیں
1166
پیروکار

ATR اور ٹریلنگ اسٹاپ پر مبنی سپر ٹرینڈ حکمت عملی

جائزہ

یہ حکمت عملی اوسطا حقیقی اتار چڑھاو کی حد ((ATR) اشارے پر مبنی ہے جس میں ایک متحرک اسٹاپ نقصان لائن اور الٹ لائن ڈیزائن کی گئی ہے۔ یہ قیمت میں تبدیلی کے مطابق ٹریلنگ اسٹاپ نقصان کی پیروی کرے گا۔ خاص طور پر ، اگر قیمت میں 1٪ سے زیادہ کی تبدیلی ہوتی ہے تو ، اسٹاپ نقصان کی لائن منافع کی طرف ایک مقررہ تناسب میں منتقل ہوجاتی ہے۔ جب قیمت اسٹاپ نقصان کی حد کو توڑ دیتی ہے تو ، پوزیشنوں کو خود بخود صاف کردیا جاتا ہے۔ یہ منافع کو لاک کرسکتا ہے ، لیکن نقصان کو بھی کم کرسکتا ہے۔

حکمت عملی کا اصول

یہ حکمت عملی اے ٹی آر اشارے کا استعمال کرتے ہوئے اسٹاپ نقصان کی لائن کا حساب لگاتی ہے۔ اس کا مخصوص فارمولا مندرجہ ذیل ہے:

atr = multplierFactor * atr(barsBack)

longStop = hl2 - atr 
shortStop = hl2 + atr

اس میں ملٹی پلیئر فیکٹر اے ٹی آر بڑھانے کا فیکٹر ہے ، بارس بیک اے ٹی آر کی تعداد ہے۔ اے ٹی آر قدر جتنی زیادہ ہوگی ، مارکیٹ میں اتار چڑھاؤ اتنا ہی زیادہ ہوگا۔

اے ٹی آر کی قیمت کے مطابق لمبی پوزیشن اسٹاپ لائن longStop اور مختصر پوزیشن اسٹاپ لائن shortStop کا حساب لگائیں۔ جب قیمت ان دونوں لائنوں سے تجاوز کرتی ہے تو تجارتی سگنل جاری کیا جاتا ہے۔

اس کے علاوہ، اس حکمت عملی نے ایک سمت متغیر متعارف کرایا ہے جو رجحان کی سمت کا تعین کرتا ہے:

direction = 1
direction := nz(direction[1], direction) 
direction := direction == -1 and close > shortStopPrev ? 1 : direction == 1 and close < longStopPrev ? -1 : direction

اگر سمت 1 ہے تو یہ ایک کثیر سر رجحان میں ہے، اگر سمت -1 ہے تو یہ ایک خالی سر رجحان میں ہے۔

مختلف رنگوں کی سٹاپ لائنوں کو سمت متغیر کی قیمتوں کے مطابق ڈرائنگ کیا جاتا ہے۔

if (direction == 1)
    valueToPlot := longStop
    colorToPlot := color.green 
else
    valueToPlot := shortStop
    colorToPlot := color.red

اس سے موجودہ رجحان کی سمت اور اسٹاپ نقصان کی لائن کی پوزیشن واضح طور پر دیکھی جاسکتی ہے۔

ٹریکنگ سٹاپ نقصان

اس حکمت عملی کا ایک اہم نکتہ یہ ہے کہ اس میں ٹریکنگ اسٹاپ میکانزم متعارف کرایا گیا ہے ، جس میں قیمتوں کے عمل کے مطابق اسٹاپ لائن کو حقیقی وقت میں ایڈجسٹ کیا جاسکتا ہے۔

اس کی منطق کچھ اس طرح ہے:

strategyPercentege = (close - updatedEntryPrice) / updatedEntryPrice * 100.00
rideUpStopLoss = hasOpenTrade() and strategyPercentege > 1

if (rideUpStopLoss) 
    stopLossPercent := stopLossPercent + strategyPercentege - 1.0
    newStopLossPrice = updatedEntryPrice + (updatedEntryPrice * stopLossPercent) / 100
    stopLossPrice := max(stopLossPrice, newStopLossPrice)
    updatedEntryPrice := stopLossPrice

اگر قیمت میں داخلے کی قیمت کے مقابلے میں 1٪ سے زیادہ اضافہ ہوتا ہے تو ، اسٹاپ نقصان کی لائن کو اوپر کی طرف سے ٹریک کریں۔ اس حصے میں جہاں ایڈجسٹمنٹ کی چوڑائی 1٪ سے زیادہ ہے۔

اس طرح ، زیادہ منافع کو لاک کیا جاسکتا ہے اور نقصانات کو کم کیا جاسکتا ہے۔

طاقت کا تجزیہ

روایتی موبائل سٹاپ حکمت عملی کے مقابلے میں اس حکمت عملی کا سب سے بڑا فائدہ یہ ہے کہ مارکیٹ کی صورتحال کے مطابق سٹاپ لائن کو متحرک طور پر ایڈجسٹ کیا جاسکتا ہے۔ اس کے کچھ فوائد یہ ہیں:

  1. رجحانات کے دوران زیادہ منافع کو لاک کرنا

ٹریکنگ سٹاپ میکانیزم سٹاپ لائن کو مسلسل منافع کی طرف منتقل کرنے کی اجازت دیتا ہے، تاکہ مارکیٹ میں مضبوط ہونے پر زیادہ منافع کو لاک کیا جا سکے.

  1. اسٹاپ نقصان کو کم کرنے کے لئے اسٹاپ آؤٹ

جب مارکیٹ کے رجحانات میں تبدیلی آتی ہے تو ، فکسڈ متحرک اسٹاپ لائن کو آسانی سے چھوڑ دیا جاتا ہے۔ جبکہ اس حکمت عملی کی اسٹاپ لائن مارکیٹ کی اتار چڑھاؤ پر مبنی ہے ، جس سے قیمت میں ہونے والی تبدیلیوں کو معقول طور پر ٹریک کیا جاسکتا ہے ، اور اسٹاپ کو ختم کرنے کے دوران اس کو چھوڑ دیا جاسکتا ہے۔

  1. آسان آپریشن اور آسانی سے آٹومیشن

یہ حکمت عملی مکمل طور پر اشارے پر مبنی ہے اور اس میں کوئی پیچیدہ رجحانات کی منطق نہیں ہے۔ اس سے خود کار طریقے سے تجارت بہت آسانی سے کی جاسکتی ہے۔

  1. اپنی مرضی کے مطابق پیرامیٹرز، مختلف نسلوں کے لئے مناسب

اے ٹی آر سائیکل، بڑھانے کے فیکٹر، اور سٹاپ مارجن جیسے پیرامیٹرز کو اپنی مرضی کے مطابق کیا جا سکتا ہے، اور مختلف قسم کے پیرامیٹرز کے لئے بہتر بنایا جا سکتا ہے، تاکہ حکمت عملی زیادہ عالمگیر ہو.

خطرے کا تجزیہ

اس حکمت عملی کے بہت سے فوائد کے باوجود ، مندرجہ ذیل خطرات سے آگاہ رہنا چاہئے:

  1. ٹرینڈ ٹرن آؤٹ کا اندازہ نہیں لگایا جا سکتا، اور اس کے نتیجے میں گرنے کا خطرہ ہے

یہ حکمت عملی اس بات کا تعین نہیں کرتی ہے کہ آیا رجحان ختم ہو گیا ہے یا نہیں۔

  1. غلط پیرامیٹرز کی ترتیب نقصان کو بڑھا سکتا ہے

اگر اے ٹی آر سائیکل پیرامیٹرز کو بہت مختصر طور پر ترتیب دیا گیا ہے تو ، اسٹاپ نقصان کی لائن بہت حساس ہوسکتی ہے ، اور یہ زلزلے کے واقعات کی کثرت سے متحرک ہوسکتی ہے۔

  1. اس کاپی رائٹ کی بازیافت کو نقصان پہنچانے کا خطرہ

اس حکمت عملی میں اسٹاپ سپورٹ کے طور پر ٹائپنگ پوائنٹس پر غور نہیں کیا گیا ہے۔ اس طرح ، مختصر لائنوں کے ردعمل پر مارکیٹ سے باہر پھینک دیا جاسکتا ہے۔

مندرجہ بالا خطرات کے لئے، آپ کو مندرجہ ذیل پہلوؤں سے بہتر بنانے کی ضرورت ہے:

  1. رجحانات کے ردوبدل کا اندازہ لگانے کے لئے رجحانات کے ردوبدل کے اشارے کے ساتھ مل کر

  2. پیرامیٹرز کو بہتر بنانے کے لئے ٹیسٹ، بہترین پیرامیٹرز کا مجموعہ منتخب کریں

  3. معاونت کے قریب اسٹاپ نقصان کی حد میں اضافہ

اصلاح کی سمت

اس حکمت عملی میں مزید اصلاحات کی گنجائش موجود ہے:

  1. K لائن شکل کے ساتھ فیصلہ

رجحان کے الٹ جانے کے امکانات کو کچھ عام K لائن شکلوں کی شناخت کے ذریعہ طے کیا جاسکتا ہے ، جیسے کہ ریٹرن ، شوٹر اسٹار وغیرہ۔ اس سے اونچائی کو مارنے کے خطرے سے بچا جاسکتا ہے۔

  1. متحرک ٹریکنگ پیرامیٹرز کو بہتر بنائیں

اے ٹی آر سائیکل ، بڑھاوا فیکٹر اور دیگر پیرامیٹرز کو بھی متحرک طور پر تبدیل کیا جاسکتا ہے ، جس میں بڑے پیمانے پر اتار چڑھاؤ والے بازاروں میں طویل اے ٹی آر سائیکل اور زیادہ کھپت کی حد استعمال کی جاسکتی ہے۔

  1. مشین لرننگ ماڈل کے ساتھ

LSTM، rnn، وغیرہ جیسے گہری سیکھنے کے ماڈل کے ذریعہ ممکنہ قیمت کی حد کی پیش گوئی کرنے کے بعد، سٹاپ نقصان کی فاصلے کو متحرک طور پر ایڈجسٹ کریں.

خلاصہ کریں۔

اس حکمت عملی میں اے ٹی آر اشارے کا استعمال کرتے ہوئے موبائل اسٹاپ لائن ڈیزائن کیا گیا ہے ، اور ٹریکنگ اسٹاپ میکانزم متعارف کرایا گیا ہے ، جو مارکیٹ میں ہونے والی تبدیلیوں کے مطابق اسٹاپ پوزیشن کو ریئل ٹائم میں ایڈجسٹ کرسکتا ہے۔ اس سے زیادہ منافع کو لاک کیا جاسکتا ہے ، جبکہ خطرہ بھی کم کیا جاسکتا ہے۔ اس حکمت عملی کو مزید اصلاح کے ذریعہ ، مارکیٹ کی مختلف حالتوں کے لئے زیادہ موافقت بخش بنایا جاسکتا ہے ، جو ایک زیادہ لچکدار تجارتی حکمت عملی ہے۔

حکمت عملی کا ماخذ کوڈ
/*backtest
start: 2022-11-21 00:00:00
end: 2023-11-27 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 
//  -----------------------------------------------------------------------------
//  Copyright 2019 Mauricio Pimenta | exit490
//  SuperTrend with Trailing Stop Loss script may be freely distributed under the MIT license.
//
//  Permission is hereby granted, free of charge, 
//  to any person obtaining a copy of this software and associated documentation files (the "Software"), 
//  to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, 
//  publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
//  subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
//  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//  -----------------------------------------------------------------------------
//
//  Authors:  @exit490
//  Revision: v1.0.0
//  Date:     5-Aug-2019
//
//  Description
//  ===========
//  SuperTrend is a moving stop and reversal line based on the volatility (ATR).
//  The strategy will ride up your stop loss when price moviment 1%.
//  The strategy will close your operation when the market price crossed the stop loss.
//  The strategy will close operation when the line based on the volatility will crossed
//
//  The strategy has the following parameters:
//
//  INITIAL STOP LOSS - Where can isert the value to first stop.
//  POSITION TYPE - Where can to select trade position.
//  ATR PERIOD - To select number of bars back to execute calculation
//  ATR MULTPLIER - To add a multplier factor on volatility
//  BACKTEST PERIOD - To select range.
//  
//  -----------------------------------------------------------------------------
//  Disclaimer:
//    1. I am not licensed financial advisors or broker dealers. I do not tell you 
//       when or what to buy or sell. I developed this software which enables you 
//       execute manual or automated trades multplierFactoriplierFactoriple trades using TradingView. The 
//       software allows you to set the criteria you want for entering and exiting 
//       trades.
//    2. Do not trade with money you cannot afford to lose.
//    3. I do not guarantee consistent profits or that anyone can make money with no 
//       effort. And I am not selling the holy grail.
//    4. Every system can have winning and losing streaks.
//    5. Money management plays a large role in the results of your trading. For 
//       example: lot size, account size, broker leverage, and broker margin call 
//       rules all have an effect on results. Also, your Take Profit and Stop Loss 
//       settings for individual pair trades and for overall account equity have a 
//       major impact on results. If you are new to trading and do not understand 
//       these items, then I recommend you seek education materials to further your
//       knowledge.
//
//    YOU NEED TO FIND AND USE THE TRADING SYSTEM THAT WORKS BEST FOR YOU AND YOUR 
//    TRADING TOLERANCE.
//
//    I HAVE PROVIDED NOTHING MORE THAN A TOOL WITH OPTIONS FOR YOU TO TRADE WITH THIS PROGRAM ON TRADINGVIEW.
//    
//    I accept suggestions to improve the script.
//    If you encounter any problems I will be happy to share with me.
//  -----------------------------------------------------------------------------
//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ //

strategy(title = "SUPERTREND ATR WITH TRAILING STOP LOSS",
         shorttitle = "SUPERTREND ATR WITH TSL",
         overlay = true,
         precision = 8,
         calc_on_order_fills = true,
         calc_on_every_tick = true,
         backtest_fill_limits_assumption = 0,
         default_qty_type = strategy.percent_of_equity,
         default_qty_value = 100,
         initial_capital = 1000,
         currency = currency.USD,
         linktoseries = true)

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ //

// === BACKTEST RANGE ===
backTestSectionFrom = input(title = "═══════════════ FROM ═══════════════", defval = true, type = input.bool)

FromMonth       = input(defval = 1, title = "Month", minval = 1)
FromDay         = input(defval = 1, title = "Day", minval = 1)
FromYear        = input(defval = 2019, title = "Year", minval = 2014)

backTestSectionTo = input(title = "════════════════ TO ════════════════", defval = true, type = input.bool)
ToMonth         = input(defval = 31, title = "Month", minval = 1)
ToDay           = input(defval = 12, title = "Day", minval = 1)
ToYear          = input(defval = 9999, title = "Year", minval = 2014)

backTestPeriod() => (time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59))

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ //

parameterSection = input(title = "═════════════ STRATEGY ═════════════", defval = true, type = input.bool)
// === INPUT TO SELECT POSITION ===
positionType = input(defval="LONG", title="Position Type", options=["LONG", "SHORT"])

// === INPUT TO SELECT INITIAL STOP LOSS
initialStopLossPercent = input(defval = 3.0, minval = 0.0, title="Initial Stop Loss")

// === INPUT TO SELECT BARS BACK
barsBack = input(title="ATR Period", defval=1)

// === INPUT TO SELECT MULTPLIER FACTOR 
multplierFactor = input(title="ATR multplierFactoriplier", step=0.1, defval=3.0)

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ //

// LOGIC TO FIND DIRECTION WHEN THERE IS TREND CHANGE ACCORDING VOLATILITY
atr = multplierFactor * atr(barsBack)

longStop = hl2 - atr
longStopPrev = nz(longStop[1], longStop)
longStop := close[1] > longStopPrev ? max(longStop, longStopPrev) : longStop

shortStop = hl2 + atr
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := close[1] < shortStopPrev ? min(shortStop, shortStopPrev) : shortStop

direction = 1
direction := nz(direction[1], direction)
direction := direction == -1 and close > shortStopPrev ? 1 : direction == 1 and close < longStopPrev ? -1 : direction

longColor = color.blue
shortColor = color.blue

var valueToPlot = 0.0
var colorToPlot = color.white

if (direction == 1)
    valueToPlot := longStop
    colorToPlot := color.green
else
    valueToPlot := shortStop
    colorToPlot := color.red

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ //
//
// === GLOBAL VARIABLES AND FUNCTIONS TO STORE IMPORTANT CONDITIONALS TO TRAILING STOP
hasEntryLongConditional() => direction == 1
hasCloseLongConditional() => direction == -1

hasEntryShortConditional() => direction == -1
hasCloseShortConditional() => direction == 1

stopLossPercent = positionType == "LONG" ? initialStopLossPercent * -1 : initialStopLossPercent

var entryPrice = 0.0
var updatedEntryPrice = 0.0
var stopLossPrice = 0.0

hasOpenTrade() => strategy.opentrades != 0
notHasOpenTrade() => strategy.opentrades == 0

strategyClose() =>
    if positionType == "LONG"
        strategy.close("LONG", when=true)
    else 
        strategy.close("SHORT", when=true)

strategyOpen() =>
    if positionType == "LONG"
        strategy.entry("LONG", strategy.long, when=true)
    else 
        strategy.entry("SHORT", strategy.short, when=true)

isLong() => positionType == "LONG" ? true : false
isShort() => positionType == "SHORT" ? true : false


//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ //
//
// === LOGIC TO TRAILING STOP IN LONG POSITION

if (isLong() and backTestPeriod())

    crossedStopLoss = close <= stopLossPrice
    terminateOperation = hasOpenTrade() and (crossedStopLoss or hasCloseLongConditional())

    if (terminateOperation)
        entryPrice := 0.0
        updatedEntryPrice := entryPrice
        stopLossPrice := 0.0
        strategyClose()
    
    startOperation = notHasOpenTrade() and hasEntryLongConditional()

    if(startOperation)
        entryPrice := close
        updatedEntryPrice := entryPrice
        stopLossPrice := entryPrice + (entryPrice * stopLossPercent) / 100
        strategyOpen()
        
    strategyPercentege = (close - updatedEntryPrice) / updatedEntryPrice * 100.00
    rideUpStopLoss = hasOpenTrade() and strategyPercentege > 1

    if (isLong() and rideUpStopLoss)
        stopLossPercent := stopLossPercent + strategyPercentege - 1.0
        newStopLossPrice = updatedEntryPrice + (updatedEntryPrice * stopLossPercent) / 100  
        stopLossPrice := max(stopLossPrice, newStopLossPrice)
        updatedEntryPrice := stopLossPrice

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ //
//
// === LOGIC TO TRAILING STOP IN SHORT POSITION

if (isShort() and backTestPeriod())

    crossedStopLoss = close >= stopLossPrice
    terminateOperation = hasOpenTrade() and (crossedStopLoss or hasCloseShortConditional())

    if (terminateOperation)
        entryPrice := 0.0
        updatedEntryPrice := entryPrice
        stopLossPrice := 0.0
        strategyClose()
    
    startOperation = notHasOpenTrade() and hasEntryShortConditional()

    if(startOperation)
        entryPrice := close
        updatedEntryPrice := entryPrice
        stopLossPrice := entryPrice + (entryPrice * stopLossPercent) / 100
        strategyOpen()
        
    strategyPercentege = (close - updatedEntryPrice) / updatedEntryPrice * 100.00
    rideDownStopLoss = hasOpenTrade() and strategyPercentege < -1

    if (rideDownStopLoss)
        stopLossPercent := stopLossPercent + strategyPercentege + 1.0
        newStopLossPrice = updatedEntryPrice + (updatedEntryPrice * stopLossPercent) / 100  
        stopLossPrice := min(stopLossPrice, newStopLossPrice)
        updatedEntryPrice := stopLossPrice

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 
//
// === DRAWING SHAPES     

entryPricePlotConditinal = entryPrice == 0.0 ? na : entryPrice
trailingStopLossPlotConditional = stopLossPrice == 0.0  ? na : stopLossPrice

plotshape(entryPricePlotConditinal, title= "Entry Price", color=color.blue, style=shape.circle, location=location.absolute, size=size.tiny)
plotshape(trailingStopLossPlotConditional, title= "Stop Loss", color=color.red, style=shape.circle, location=location.absolute, size=size.tiny)

plot(valueToPlot == 0.0 ? na : valueToPlot, title="BuyLine", linewidth=2, color=colorToPlot)
plotshape(direction == 1 and direction[1] == -1 ? longStop : na, title="Buy", style=shape.labelup, location=location.absolute, size=size.normal, text="Buy", transp=0, textcolor = color.white, color=color.green, transp=0)
plotshape(direction == -1 and direction[1] == 1 ? shortStop : na, title="Sell", style=shape.labeldown, location=location.absolute, size=size.normal, text="Sell", transp=0, textcolor = color.white, color=color.red, transp=0)

alertcondition(direction == 1 and direction[1] == -1 ? longStop : na, title="Buy", message="Buy!")
alertcondition(direction == -1 and direction[1] == 1 ? shortStop : na, title="Sell", message="Sell!")