حکمت عملی ایک دو طرفہ تجارتی نظام ہے جو وقت کے وقفوں کے ساتھ ایک ایکسپونیشنل موونگ ایوریج (EMA) کو جوڑتا ہے۔ نظام ایک ہی وقت میں، EMA اشارے کے دوسرے سیٹ کے کراس اوور سگنل کی نگرانی کے ذریعے یا وقت قریب آنے پر مختلف ادوار کے EMAs کے پوزیشن کے تعلق کے مطابق ایک مقررہ وقت کے وقفے کے مطابق طے کرتا ہے۔ اگلے تجارتی دور میں، نظام ریورس ہیجنگ کے لین دین کے لیے مناسب وقت کا انتخاب کرتا ہے، اس طرح دو طرفہ تجارتی مواقع کی گرفت کا احساس ہوتا ہے۔
حکمت عملی کی کارروائی دو بنیادی میکانزم پر مبنی ہے: مقررہ وقت کے وقفوں پر بنیادی تجارت اور لچکدار ریورسل ٹریڈنگ۔ بنیادی لین دین رجحان کی سمت کا تعین کرنے کے لیے 5⁄40 منٹ EMA کی متعلقہ پوزیشن پر مبنی ہے، اور لین دین ہر تجارتی وقفے پر کیا جاتا ہے (پہلے سے طے شدہ 30 منٹ ہے)۔ ریورس ٹریڈنگ 5⁄10 منٹ EMA کے کراس اوور سگنل کی نگرانی کے ذریعے کی جاتی ہے، یا اگلی بڑی تجارت سے 1 منٹ پہلے، جو بھی پہلے ہو۔ لین دین کی بروقتیت کو یقینی بنانے کے لیے صارف کی طرف سے طے شدہ ٹائم ونڈو کے اندر پوری ٹرانزیکشن کی جاتی ہے۔
یہ ایک جامع حکمت عملی ہے جو ٹرینڈ ٹریکنگ اور ریورس ٹریڈنگ کو یکجا کرتی ہے وقت کے وقفوں اور EMA انڈیکیٹرز کے ذریعے، یہ تجارتی مواقع کی دو طرفہ گرفت حاصل کرتی ہے۔ حکمت عملی انتہائی حسب ضرورت ہے اور اس میں خطرے پر قابو پانے کی اچھی صلاحیت ہے، لیکن اس کے لیے پیرامیٹر کی اصلاح اور مارکیٹ کے حقیقی حالات کی بنیاد پر رسک مینجمنٹ میں بہتری کی ضرورت ہے۔ جب ریئل ٹائم میں لاگو کیا جاتا ہے، تو یہ سفارش کی جاتی ہے کہ کافی بیک ٹیسٹنگ اور پیرامیٹر کی اصلاح کریں، اور مارکیٹ کی خصوصیات کی بنیاد پر ٹارگٹڈ ایڈجسٹمنٹ کریں۔
/*backtest
start: 2025-01-02 00:00:00
end: 2025-01-09 00:00:00
period: 3m
basePeriod: 3m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("SPX EMA Strategy with Opposite Trades", overlay=true)
// User-defined inputs
tradeIntervalMinutes = input.int(30, title="Main Trade Interval (in minutes)", minval=1)
oppositeTradeDelayMinutes = input.int(1, title="Opposite Trade time from next trade (in minutes)", minval=1) // Delay of opposite trade (1 min before the next trade)
startHour = input.int(10, title="Start Hour", minval=0, maxval=23)
startMinute = input.int(30, title="Start Minute", minval=0, maxval=59)
stopHour = input.int(15, title="Stop Hour", minval=0, maxval=23)
stopMinute = input.int(0, title="Stop Minute", minval=0, maxval=59)
// User-defined EMA periods for main trade and opposite trade
mainEmaShortPeriod = input.int(5, title="Main Trade EMA Short Period", minval=1)
mainEmaLongPeriod = input.int(40, title="Main Trade EMA Long Period", minval=1)
oppositeEmaShortPeriod = input.int(5, title="Opposite Trade EMA Short Period", minval=1)
oppositeEmaLongPeriod = input.int(10, title="Opposite Trade EMA Long Period", minval=1)
// Calculate the EMAs for main trade
emaMainShort = ta.ema(close, mainEmaShortPeriod)
emaMainLong = ta.ema(close, mainEmaLongPeriod)
// Calculate the EMAs for opposite trade (using different periods)
emaOppositeShort = ta.ema(close, oppositeEmaShortPeriod)
emaOppositeLong = ta.ema(close, oppositeEmaLongPeriod)
// Condition to check if it is during the user-defined time window
startTime = timestamp(year, month, dayofmonth, startHour, startMinute)
stopTime = timestamp(year, month, dayofmonth, stopHour, stopMinute)
currentTime = timestamp(year, month, dayofmonth, hour, minute)
// Ensure the script only trades within the user-defined time window
isTradingTime = currentTime >= startTime and currentTime <= stopTime
// Time condition: Execute the trade every tradeIntervalMinutes
var float lastTradeTime = na
timePassed = na(lastTradeTime) or (currentTime - lastTradeTime) >= tradeIntervalMinutes * 60 * 1000
// Entry Conditions for Main Trade
longCondition = emaMainShort > emaMainLong // Enter long if short EMA is greater than long EMA
shortCondition = emaMainShort < emaMainLong // Enter short if short EMA is less than long EMA
// Detect EMA crossovers for opposite trade (bullish or bearish)
bullishCrossoverOpposite = ta.crossover(emaOppositeShort, emaOppositeLong) // Opposite EMA short crosses above long
bearishCrossoverOpposite = ta.crossunder(emaOppositeShort, emaOppositeLong) // Opposite EMA short crosses below long
// Track the direction of the last main trade (true for long, false for short)
var bool isLastTradeLong = na
// Track whether an opposite trade has already been executed after the last main trade
var bool oppositeTradeExecuted = false
// Execute the main trades if within the time window and at the user-defined interval
if isTradingTime and timePassed
if longCondition
strategy.entry("Main Long", strategy.long)
isLastTradeLong := true // Mark the last trade as long
oppositeTradeExecuted := false // Reset opposite trade status
lastTradeTime := currentTime
// label.new(bar_index, low, "Main Long", color=color.green, textcolor=color.white, size=size.small)
else if shortCondition
strategy.entry("Main Short", strategy.short)
isLastTradeLong := false // Mark the last trade as short
oppositeTradeExecuted := false // Reset opposite trade status
lastTradeTime := currentTime
// label.new(bar_index, high, "Main Short", color=color.red, textcolor=color.white, size=size.small)
// Execute the opposite trade only once after the main trade
if isTradingTime and not oppositeTradeExecuted
// 1 minute before the next main trade or EMA crossover
if (currentTime - lastTradeTime) >= (tradeIntervalMinutes - oppositeTradeDelayMinutes) * 60 * 1000 or bullishCrossoverOpposite or bearishCrossoverOpposite
if isLastTradeLong
// If the last main trade was long, enter opposite short trade
strategy.entry("Opposite Short", strategy.short)
//label.new(bar_index, high, "Opposite Short", color=color.red, textcolor=color.white, size=size.small)
else
// If the last main trade was short, enter opposite long trade
strategy.entry("Opposite Long", strategy.long)
//label.new(bar_index, low, "Opposite Long", color=color.green, textcolor=color.white, size=size.small)
// After entering the opposite trade, set the flag to true so no further opposite trades are placed
oppositeTradeExecuted := true
// Plot the EMAs for visual reference
plot(emaMainShort, title="Main Trade Short EMA", color=color.blue)
plot(emaMainLong, title="Main Trade Long EMA", color=color.red)
plot(emaOppositeShort, title="Opposite Trade Short EMA", color=color.purple)
plot(emaOppositeLong, title="Opposite Trade Long EMA", color=color.orange)