ट्रेलिंग स्टॉप लॉस डबल मूविंग एवरेज रणनीति

लेखक:चाओझांग, दिनांकः 2023-12-29 16:59:17
टैगः

img

अवलोकन

यह रणनीति लंबी और छोटी पदों को निर्धारित करने के लिए तेज और धीमी गति से चलती औसत क्रॉसओवर का उपयोग करती है। जब तेज एमए धीमी एमए को पार करता है और जब तेज एमए धीमी एमए से नीचे पार करता है तो स्थिति बंद हो जाती है। उच्च लाभ का पीछा करने के लिए, रणनीति एक ट्रैलिंग स्टॉप लॉस तंत्र को अपनाती है। लंबी स्थिति खोलने के बाद प्रवेश मूल्य के ठीक नीचे स्टॉप लॉस मूल्य निर्धारित करने के बजाय, यह एक ट्रैलिंग स्टॉप लॉस मूल्य निर्धारित करता है जो मूल्य वृद्धि के बाद तब तक बढ़ता है जब तक कि मूल्य गिरावट स्टॉप लॉस मूल्य सीमा तक नहीं पहुंच जाती।

यह कैसे काम करता है

यह रणनीति प्रवेश और निकास निर्धारित करने के लिए तेज और धीमी सरल चलती औसत (एसएमए) लाइनों का उपयोग करती है। जब तेज एसएमए धीमी एसएमए के ऊपर से गुजरती है, तो यह एक अपट्रेंड का संकेत देती है ताकि रणनीति लंबी हो। जब तेज एसएमए धीमी एसएमए के नीचे से गुजरती है, तो यह एक प्रवृत्ति उलट का संकेत देती है ताकि रणनीति स्थिति को बंद करने की तैयारी करे।

लाभ को अधिकतम करने के लिए, रणनीति एक ट्रेलिंग स्टॉप लॉस तंत्र पेश करती है। लंबी स्थिति खोलने के बाद एक निश्चित स्टॉप लॉस मूल्य का उपयोग करने के बजाय, यह एक ट्रेलिंग स्टॉप लॉस मूल्य निर्धारित करता है जो मूल्य वृद्धि के बाद ऊपर जाता है। हर बार जब कीमत एक निश्चित प्रतिशत बढ़ जाती है, तो ट्रेलिंग स्टॉप लॉस मूल्य एक पूर्वनिर्धारित प्रतिशत से समायोजित होता है। जब कीमत वापस खींचती है और ट्रेलिंग स्टॉप लॉस मूल्य को हिट करती है, तो यह स्थिति को बंद करने के लिए स्टॉप लॉस ऑर्डर को ट्रिगर करती है।

विशेष रूप से, ट्रेलिंग स्टॉप लॉस की कीमत की गणना इस प्रकार की जाती हैः

अनुवर्ती स्टॉप लॉस मूल्य = मूल्य × (1 - स्टॉप लॉस अनुवर्ती प्रतिशत)

स्टॉप लॉस ट्रेलिंग प्रतिशत को रणनीति पैरामीटर Deviation % द्वारा परिभाषित किया गया है। रणनीति हर बार बंद होने पर ट्रेलिंग स्टॉप लॉस की कीमत की पुनः गणना करती है। नई ट्रेलिंग स्टॉप लॉस की कीमत पिछली बार की तुलना में कम नहीं हो सकती है, ताकि यह सुनिश्चित किया जा सके कि स्टॉप लॉस की कीमत केवल ऊपर की ओर बढ़े, न कि नीचे की ओर।

जब कीमत गिरती है और स्टॉप लॉस की कीमत पर पहुंच जाती है, तो यह क्लोजिंग सिग्नल को ट्रिगर करता है और बाजार ऑर्डर द्वारा स्थिति को बंद कर दिया जाएगा।

लाभ

  • अच्छे बैकटेस्ट परिणामों के साथ प्रवृत्ति दिशा निर्धारित करने के लिए दोहरी चलती औसत का उपयोग करें
  • उच्च लाभ प्राप्त करने के लिए ट्रैलिंग स्टॉप लॉस को अपनाएं
  • अनुकूलन योग्य चलती औसत अवधि और स्टॉप लॉस ट्रेलिंग प्रतिशत
  • स्टॉप लॉस लाइन ऊपर की ओर बढ़ती रहती है जब ट्रेंड ऊपर जाता है, ज्यादातर मुनाफे में लॉक होता है
  • आगे के घाटे से बचने के लिए रुझान के उलट होने पर त्वरित स्टॉप लॉस

जोखिम और समाधान

  • गलत चलती औसत क्रॉसओवर समय गलत संकेत का कारण बन सकता है। इष्टतम एमए संयोजन खोजने के लिए विभिन्न मापदंडों का परीक्षण करें
  • अत्यधिक आक्रामक ट्रेलिंग स्टॉप लॉस समय से पहले बंद हो सकता है। स्टॉप लॉस ट्रेलिंग प्रतिशत पैरामीटर को ठीक से समायोजित करें
  • मूल्य अंतराल सीधे स्टॉप लॉस मूल्य में प्रवेश कर सकते हैं। रुझान का न्याय करने के लिए अन्य संकेतकों को जोड़ने पर विचार करें और रेंज अवधि के दौरान व्यापार से बचें

अनुकूलन दिशाएँ

  • इष्टतम संयोजन खोजने के लिए विभिन्न चलती औसत अवधि मापदंडों का परीक्षण करें
  • इष्टतम स्टॉप लॉस स्तर खोजने के लिए विभिन्न स्टॉप लॉस ट्रैलिंग प्रतिशत मापदंडों का परीक्षण करें
  • आवर्त घटनाओं से प्रभावित होने से बचने के लिए रेंज अवधि के दौरान व्यापार को निलंबित करने के लिए अन्य संकेतक जोड़ें

निष्कर्ष

इस रणनीति में ट्रेंड की दिशा का न्याय करने के लिए मूविंग एवरेज इंडिकेटर और ट्रेलिंग स्टॉप लॉस मैकेनिज्म को मिलाकर लाभ को लॉक किया जाता है, ट्रेनिंग डेटा पर अच्छा प्रदर्शन होता है। पैरामीटर को अनुकूलित करके और जोखिमों को नियंत्रित करके, इसमें स्थिर लाभ प्राप्त करने की क्षमता होती है। हालांकि, कोई भी रणनीति नुकसान से पूरी तरह से बच नहीं सकती है। स्थिति आकार को समायोजित करने, विभिन्न उत्पादों का परीक्षण करने और जोखिमों को विविध करने की सिफारिश की जाती है।


/*backtest
start: 2022-12-22 00:00:00
end: 2023-12-28 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 
//  -----------------------------------------------------------------------------
//  Copyright 2022 Iason Nikolas | jason5480
//  Trailing Buy 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:  @jason5480
//  Revision: v1.0.1
//  Date:     24-Feb-2022
//
//  Description
//  =============================================================================
//  This strategy will go long if fast MA crosses over slow MA.
//  If the 'Enable Trailing` is checked then the strategy instead of exiting from the position
//  directly it will follow the price upwards (percentagewise) with small steps
//  If the price drops by this percentage then the exit order will be executed
//
//  The strategy has the following parameters:
//
//  Fast SMA Length - How many candles back to calculte the fast SMA.
//  Slow SMA Length - How many candles back to calculte the slow SMA.
//  Enable Trailing - Enable or disable the trailing
//  Deviation % - The step to follow the price when the open position condition is met.
//  Source Exit Control - The source price to compare with the exit price to trigger the exit order when trailing.
//  
//  -----------------------------------------------------------------------------
//  Disclaimer:
//    1. I am not licensed financial advisors or broker dealer. I do not tell you 
//       when or what to buy or sell. I developed this software which enables you 
//       execute manual or automated 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.
//  -----------------------------------------------------------------------------
//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// SETUP ============================================================================================================

strategy(title = 'Trailing Sell',
         shorttitle = 'TS',
         overlay = true,
         pyramiding = 0,
         default_qty_type = strategy.percent_of_equity,
         default_qty_value = 100,
         initial_capital = 100000)

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// FILTERS ==========================================================================================================

// INPUT ============================================================================================================
usefromDate = input.bool(defval = true, title = 'From', inline = "From Date", group = "Filters")
fromDate = input(defval = timestamp('01 Jan 2021 00:00 UTC'), title = '', inline = "From Date", group = 'Filters')
usetoDate = input.bool(defval = false, title = 'To ', inline = "To Date", group = "Filters")
toDate = input(defval = timestamp('31 Dec 2121 23:59 UTC'), title = '', inline = "To Date", group = 'Filters')

// LOGIC ============================================================================================================
isWithinPeriod() => true // create function "within window of time"

// PLOT =============================================================================================================
bgcolor(color = isWithinPeriod() ? color.new(color.gray, 90) : na, title = 'Period')

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// STRATEGY =========================================================================================================

// INPUT ============================================================================================================
fastMALen = input.int(defval = 21, title = 'Fast/Slow SMA Length', inline = 'MA Length', group = 'Strategy')
slowMALen = input.int(defval = 49, title = '', tooltip = 'How many candles back to calculte the fast/slow SMA.', inline = 'MA Length', group = 'Strategy')

// LOGIC ============================================================================================================
fastMA = ta.sma(close, fastMALen)
slowMA = ta.sma(close, slowMALen)

bool openLongPosition = isWithinPeriod() and ta.crossover(fastMA, slowMA)
bool closeLongPosition = ta.crossunder(fastMA, slowMA)

// PLOT =============================================================================================================
var fastColor = color.new(#0056BD, 0)
plot(series = fastMA, title = 'Fast SMA', color = fastColor, linewidth = 1, style = plot.style_line)
var slowColor = color.new(#FF6A00, 0)
plot(series = slowMA, title = 'Slow SMA', color = slowColor, linewidth = 1, style = plot.style_line)

plotshape(series = closeLongPosition and strategy.position_size > 0 ? fastMA : na, title = 'Sell', text = 'Sell', style = shape.labeldown, location = location.absolute, color = color.new(color.red, 0), textcolor = color.new(color.white, 0), size = size.tiny)

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// EXIT ============================================================================================================

// INPUT ============================================================================================================
enableTrailing = input.bool(defval = true, title = 'Enable Trailing', tooltip = 'Enable or disable the trailing for exit position.', group = 'Exit')
devExitPerc = input.float(defval = 3.0, title = 'Deviation %', minval = 0.01, maxval = 100, step = 0.05, tooltip = 'The step to follow the price when the open position condition is met.', group = 'Exit') / 100
ctrLongExitSrc = input.source(defval = low, title = 'Source Exit Control', tooltip = 'The source price to compare with the exit price to trigger the exit order when trailing.', group = 'Exit')

// LOGIC ============================================================================================================
var bool exitLongPosition = false

int barsSinceOpenLong = nz(ta.barssince(openLongPosition), 999999)
int barsSinceCloseLong = nz(ta.barssince(closeLongPosition), 999999)
int barsSinceExitLong = nz(ta.barssince(exitLongPosition), 999999)
bool closeLongIsActive = barsSinceOpenLong >= barsSinceCloseLong
bool exitLongIsPending = barsSinceExitLong >= barsSinceCloseLong
bool tryExitLongPosition = isWithinPeriod() and closeLongIsActive and exitLongIsPending

float longExitPrice = na
longExitPrice := if closeLongPosition and strategy.position_size > 0
    close * (1 - devExitPerc)
else if tryExitLongPosition
    math.max(high * (1 - devExitPerc), nz(longExitPrice[1], 999999))
else
    na

exitLongPosition := enableTrailing ? isWithinPeriod() and ta.crossunder(closeLongPosition ? close : ctrLongExitSrc, longExitPrice) : closeLongPosition

// PLOT =============================================================================================================
var sellPriceColor = color.new(#e25141, 0)
plot(series = enableTrailing ? longExitPrice : na, title = 'Long Sell Price', color = sellPriceColor, linewidth = 1, style = plot.style_linebr)

//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// POSITION ORDERS ==================================================================================================

// LOGIC ============================================================================================================
// getting into LONG position
strategy.entry(id = 'Long Entry', direction = strategy.long, when = openLongPosition, alert_message = 'Long(' + syminfo.ticker + '): Started')
// submit close order on trend reversal
strategy.close(id = 'Long Entry', when = exitLongPosition, comment = 'Close Long', alert_message = 'Long(' + syminfo.ticker + '): Closed at market price')

// PLOT =============================================================================================================
var posColor = color.new(color.white, 0)
plot(series = strategy.position_avg_price, title = 'Position', color = posColor, linewidth = 1, style = plot.style_linebr)

// ==================================================================================================================

अधिक