// Get close and open prices from specific timeframe
close_tf = request.security(syminfo.tickerid, tim, close)
open_tf = request.security(syminfo.tickerid, tim, open)
// Create a function for entry conditions
f_checkEntryConditions(close_tf, open_tf, upper1, lower1) =>
longCondition = ta.crossover(close_tf, open_tf) and close < upper1
shortCondition = ta.crossunder(close_tf, open_tf) and close > lower1
[longCondition, shortCondition]
// Get the conditions
[longCondition, shortCondition] = f_checkEntryConditions(close_tf, open_tf, upper1, lower1)
// Define take profit and stop loss levels
takeProfit = input.float(1.5, title='Take Profit (%)')
stopLoss = input.float(1.0, title='Stop Loss (%)')
// Execute long entry and exit
if longCondition
strategy.entry('long', strategy.long)
strategy.exit('TP/SL Long', from_entry='long', limit=close * (1 + takeProfit / 100), stop=close * (1 - stopLoss / 100))
// Execute short entry and exit
if shortCondition
strategy.entry('short', strategy.short)
strategy.exit('TP/SL Short', from_entry='short', limit=close * (1 - takeProfit / 100), stop=close * (1 + stopLoss / 100))
Assistant
این کد یک استراتژی معاملاتی در پلتفرم TradingView است که با استفاده از زبان برنامهنویسی Pine Script نوشته شده است. در زیر، هر بخش کد توضیح داده شده است:
خط 1: تعیین نسخه
pinescript
//@version=5
این خط نشاندهنده نسخه Pine Script است که این کد با آن نوشته شده است. نسخه 5 آخرین نسخه از این زبان است.
خط 2: تعریف استراتژی
pinescript
strategy('[VV] Ehsan Buy Sell', overlay=true)
در اینجا یک استراتژی به نام "[VV] Ehsan Buy Sell" تعریف شده است. overlay=true به این معنی است که این استراتژی بر روی نمودار قیمت اثر میگذارد.
خط 3: ورودی زمانبندی
pinescript
tim = input.timeframe('375')
این خط تعیین میکند که زمانبندی خاصی (در اینجا 375 دقیقه) برای معاملات مورد استفاده قرار بگیرد.
در این قسمت، چند ورودی برای استراتژی قرار داده شده است. length برای طول محاسبات، src برای نقطه داده (در اینجا قیمت بسته شدن)، و دو متغیر دیگر (Band1 و Band2) برای عوامل باندها تعریف شدهاند.
یک تابع به نام f_calculateBands تعریف میشود که میانگین متحرک ساده (SMA) و انحراف معیار (Standard Deviation) را محاسبه میکند و باندهای بالایی و پایینی را برمیگرداند.
// Get close and open prices from specific timeframe
close_tf = request.security(syminfo.tickerid, tim, close)
open_tf = request.security(syminfo.tickerid, tim, open)
// Create a function for entry conditions
f_checkEntryConditions(close_tf, open_tf, upper1, lower1) =>
longCondition = ta.crossover(close_tf, open_tf) and close < upper1
shortCondition = ta.crossunder(close_tf, open_tf) and close > lower1
[longCondition, shortCondition]
// Get the conditions
[longCondition, shortCondition] = f_checkEntryConditions(close_tf, open_tf, upper1, lower1)
// Define take profit and stop loss levels
takeProfit = input.float(1.5, title='Take Profit (%)')
stopLoss = input.float(1.0, title='Stop Loss (%)')
// Execute long entry and exit
if longCondition
strategy.entry('long', strategy.long)
strategy.exit('TP/SL Long', from_entry='long', limit=close * (1 + takeProfit / 100), stop=close * (1 - stopLoss / 100))
// Execute short entry and exit
if shortCondition
strategy.entry('short', strategy.short)
strategy.exit('TP/SL Short', from_entry='short', limit=close * (1 - takeProfit / 100), stop=close * (1 + stopLoss / 100))
Assistant
برای بهینهسازی کد استراتژی معاملاتی شما، میتوان چند نکته را در نظر گرفت. این نکات شامل بهینهسازی عملکرد، افزایش خوانایی، و قابلیت تنظیم مجدد است. در زیر چند دستور و توصیه برای بهبود کد ارائه شده است:
1. استفاده از متغیرهای مشترك
اگر دو یا چند بار از یک محاسبه مشابه استفاده میکنید، میتوانید آن را در یک متغیر ذخیره کنید تا کد شما کمتر تکرار شود. به عنوان مثال:
pinescript
// Define common variables
close_adjusted_long = close * (1 + takeProfit / 100)
close_adjusted_short = close * (1 - takeProfit / 100)
2. بهبود نامگذاری متغیرها
استفاده از نامهای متغیر واضحتر میتواند خوانایی کد را افزایش دهد. به عنوان مثال، upper1 را به upperBand1 و مشابه به آن تغییر دهید.
3. افزودن قابلیت تنظیم بیشتر
میتوانید ورودیهای مربوط به باندها و سطوح سود و ضرر را بیشتر کنید؛ برای مثال:
// Execute long entry and exit
if longCondition
strategy.entry('long', strategy.long)
strategy.exit('TP/SL Long', from_entry='long', limit=close * (1 + takeProfit / 100), stop=close * (1 - stopLoss / 100))
// Execute short entry and exit
if shortCondition
strategy.entry('short', strategy.short)
strategy.exit('TP/SL Short', from_entry='short', limit=close * (1 - takeProfit / 100), stop=close * (1 + stopLoss / 100))
Assistant
برای طراحی یک ربات که به طور خودکار پارامترهای کد استراتژی شما را تغییر دهد، میتوانید یک مدل ساده با استفاده از حلقهها و شرایط مختلف را ایجاد کنید. در اینجا، من یک نمونه ساده از ربات تنظیمکننده پارامترها برای استراتژی شما را ارائه میکنم. این ربات به شما اجازه میدهد تا پارامترهای مختلف را به طور خودکار تغییر دهید و بررسی کنید که کدام تغییرات بهترین عملکرد را دارند.
این ربات به صورت یک استراتژی معاملاتی جدید نوشته خواهد شد که پارامترهای شما را با استفاده از پدیده جستجو در فضای پارامترها تغییر میدهد.
کد ربات تنظیم کننده پارامتر
pinescript
User
خطای این کد دستوری را برایم برطرف کن//@version=5
strategy('[VV] Parameter Optimization Bot', overlay=true)
// Loop through combinations of parameters
for i = 0 to array.size(lengths)-1
for j = 0 to array.size(band1_values)-1
for k = 0 to array.size(band2_values)-1
for m = 0 to array.size(takeProfit_values)-1
for n = 0 to array.size(stopLoss_values)-1
length = array.get(lengths, i)
Band1 = array.get(band1_values, j)
Band2 = array.get(band2_values, k)
takeProfit = array.get(takeProfit_values, m)
stopLoss = array.get(stopLoss_values, n)
code
// Band calculation function
f_calculateBands(src, length, bandFactor) =>
basis = ta.sma(src, length)
dev = bandFactor * ta.stdev(src, length)
[basis, basis + dev, basis - dev]
// Calculate bands
[basis, upperBand1, lowerBand1] = f_calculateBands(close, length, Band1)
[basis2, upperBand2, lowerBand2] = f_calculateBands(close, length, Band2)
// Entry conditions
longCondition = ta.crossover(close, open) and close < upperBand1
shortCondition = ta.crossunder(close, open) and close > lowerBand1
// Execute long entry and exit
if longCondition
strategy.entry('long', strategy.long)
strategy.exit('TP/SL Long', from_entry='long', limit=close * (1 + takeProfit / 100), stop=close * (1 - stopLoss / 100))
// Execute short entry and exit
if shortCondition
strategy.entry('short', strategy.short)
strategy.exit('TP/SL Short', from_entry='short', limit=close * (1 - takeProfit / 100), stop=close * (1 + stopLoss / 100))
Assistant
کد شما دارای چند اشکال است که باید برطرف شود. یکی از اصلیترین مشکلات این است که توابع جدید نمیتوانند درون حلقهها تعریف شوند. Pine Script به طور خاص برای کار با توابع و حلقهها محدودیتهایی دارد و شما نمیتوانید از توابع درون حلقهها برای استراتژیهای تکراری استفاده کنید.
در زیر، نسخهی اصلاحشده کد را مشاهده میکنید:
کد اصلاحشده:
pinescript
User
بهینه سازی این کد را بایم تشریح کن//@version=5
strategy('[VV] Parameter Optimization Bot', overlay=true)
// Band calculation function
f_calculateBands(src, length, bandFactor) =>
basis = ta.sma(src, length)
dev = bandFactor * ta.stdev(src, length)
[basis, basis + dev, basis - dev]
var float bestProfit = na // Variable to store the best profit
var int bestLength = na
var float bestBand1 = na
var float bestBand2 = na
var float bestTakeProfit = na
var float bestStopLoss = na
// Loop through combinations of parameters only once per tick
for i = 0 to array.size(lengths)-1
for j = 0 to array.size(band1_values)-1
for k = 0 to array.size(band2_values)-1
for m = 0 to array.size(takeProfit_values)-1
for n = 0 to array.size(stopLoss_values)-1
length = array.get(lengths, i)
Band1 = array.get(band1_values, j)
Band2 = array.get(band2_values, k)
takeProfit = array.get(takeProfit_values, m)
stopLoss = array.get(stopLoss_values, n)
code
// Calculate bands
[basis, upperBand1, lowerBand1] = f_calculateBands(close, length, Band1)
[basis2, upperBand2, lowerBand2] = f_calculateBands(close, length, Band2)
// Entry conditions
longCondition = ta.crossover(close, open) and close < upperBand1
shortCondition = ta.crossunder(close, open) and close > lowerBand1
// Execute long entry and exit
if longCondition
strategy.entry('long', strategy.long)
strategy.exit('TP/SL Long', from_entry='long', limit=close * (1 + takeProfit / 100), stop=close * (1 - stopLoss / 100))
// Execute short entry and exit
if shortCondition
strategy.entry('short', strategy.short)
strategy.exit('TP/SL Short', from_entry='short', limit=close * (1 - takeProfit / 100), stop=close * (1 + stopLoss / 100))
Assistant
برای بهینهسازی کد استراتژی معاملاتی شما، میتوانیم چندین نکته و روش را بررسی کنیم که به بهبود عملکرد و افزایش دقت نتایج کمک میکنند.
1. استفاده از متغیرهای جهانی
استفاده از متغیرهای جهانی برای ذخیره نتایج، به شما این امکان را میدهد که در عوض استفاده از متغیرهای محلی، نتایج مختلف را ذخیره و مقایسه کنید. در کد شما، این مورد انجام شده است.
2. اضافه کردن عملکرد ارزیابی
بهینهسازی یک ربات معاملاتی معمولاً شامل ارزیابی عملکرد معاملاتی با پارامترهای مختلف میباشد. شما میتوانید با استفاده از متغیرهای اضافی و محاسبات، سود کل را محاسبه کرده و به صورت داینامیک پارامترها را بهروز کنید.
3. استفاده از روشهای معیار
شما میتوانید یک معیار برای ارزیابی و انتخاب بهترین پارامترها تعریف کنید. به عنوان مثال، میتوانید از مجموع سود، نسبت شکست یا نسبت شارپ استفاده کنید.
4. کاهش تعداد متغیرها
به جای تعریف متغیرهای متعدد، میتوانید از آرایههای ترکیبی برای ترکیب ورودیها و جلوگیری از پیچیدگیهای غیرضروری استفاده کنید.
5. بهبود شرایط ورودی
بهینهسازی شرایط ورود هم میتواند شامل بهینهسازی مدلهای تحلیل تکنیکال باشد. به عنوان مثال، بررسی موقعیتهای بیشخرید یا بیشفروش و استفاده از آنها برای تأیید حرکات قیمت.
6. افزایش تعداد پارامترها
شما میتوانید با اضافه کردن پارامترهای جدید جهت بررسی نسبتهای بیشتری از بهینهسازی استفاده کنید. به عنوان مثال:
دورههای مختلف برای باندها
فیلترهای اضافی برای تحلیل تکنیکال
7. تسهیل تست مجدد
مطمئن شوید که کد شما قابلیت تست مجدد را دارد و میتواند با شرایط مختلف مالی و زمانی کار کند.
کد بهینهسازیشده
در زیر یک نسخه بهینهشده از کد شما وجود دارد که تعدادی از این نکات را در خود جای داده است:
pinescript
User
خطای نوشتاری این کد را برطرف کن//@version=5
strategy('[VV] Parameter Optimization Bot', overlay=true)
var float bestProfit = na
var int bestLength = na
var float bestBand1 = na
var float bestBand2 = na
var float bestTakeProfit = na
var float bestStopLoss = na
// Variable to accumulate gains
var float totalProfit = 0.0
// Loop through combinations of parameters once per tick
for i = 0 to array.size(lengths)-1
for j = 0 to array.size(band1_values)-1
for k = 0 to array.size(band2_values)-1
for m = 0 to array.size(takeProfit_values)-1
for n = 0 to array.size(stopLoss_values)-1
length = array.get(lengths, i)
Band1 = array.get(band1_values, j)
Band2 = array.get(band2_values, k)
takeProfit = array.get(takeProfit_values, m)
stopLoss = array.get(stopLoss_values, n)
code
// Calculate bands
[basis, upperBand1, lowerBand1] = f_calculateBands(close, length, Band1)
longCondition = ta.crossover(close, open) and close < upperBand1
shortCondition = ta.crossunder(close, open) and close > lowerBand1
// Long entry and exit
if longCondition
strategy.entry('long', strategy.long)
strategy.exit('TP/SL Long', from_entry='long', limit=close * (1 + takeProfit / 100), stop=close * (1 - stopLoss / 100))
// Short entry and exit
if shortCondition
strategy.entry('short', strategy.short)
strategy.exit('TP/SL Short', from_entry='short', limit=close * (1 - takeProfit / 100), stop=close * (1 + stopLoss / 100))
// Calculate total profit for optimization
totalProfit := totalProfit + (strategy.position_size > 0 ? strategy.closedtrades.profit : 0)
Assistant
در کد شما، چندین نکته وجود دارد که میتواند باعث بروز خطا یا رفتار ناخواسته شود. با توجه به نسخه Pine Script و قواعد نوشتاری، در اینجا چندین اصلاح انجام شده است:
کد اصلاح شده
pinescript
User
در قسمت محاسبات سود معاملات در این کد دستوری خطایی دیده میشود اصلاح کن//@version=5
strategy('[VV] Parameter Optimization Bot', overlay=true)
tim = input.timeframe('375')
// Parameter ranges for optimization
lengths = array.new_int(3, 0) // Initialize array with size and default value
array.set(lengths, 0, 50)
array.set(lengths, 1, 81)
array.set(lengths, 2, 100)
band1_values = array.new_float(3, 0.0) // Initialize array with size and default value
array.set(band1_values, 0, 1.0)
array.set(band1_values, 1, 1.5)
array.set(band1_values, 2, 2.0)
band2_values = array.new_float(3, 0.0) // Initialize array with size and default value
array.set(band2_values, 0, 2.0)
array.set(band2_values, 1, 2.5)
array.set(band2_values, 2, 3.0)
takeProfit_values = array.new_float(3, 0.0) // Initialize array with size and default value
array.set(takeProfit_values, 0, 1.0)
array.set(takeProfit_values, 1, 1.5)
array.set(takeProfit_values, 2, 2.0)
stopLoss_values = array.new_float(3, 0.0) // Initialize array with size and default value
array.set(stopLoss_values, 0, 0.5)
array.set(stopLoss_values, 1, 1.0)
array.set(stopLoss_values, 2, 1.5)
// Function to calculate bands
f_calculateBands(src, length, bandFactor) =>
basis = ta.sma(src, length)
dev = bandFactor * ta.stdev(src, length)
[basis, basis + dev, basis - dev]
var float bestProfit = na
var int bestLength = na
var float bestBand1 = na
var float bestBand2 = na
var float bestTakeProfit = na
var float bestStopLoss = na
// Variable to accumulate gains
var float totalProfit = 0.0
// Loop through combinations of parameters once per tick
for i = 0 to array.size(lengths)-1
for j = 0 to array.size(band1_values)-1
for k = 0 to array.size(band2_values)-1
for m = 0 to array.size(takeProfit_values)-1
for n = 0 to array.size(stopLoss_values)-1
length = array.get(lengths, i)
Band1 = array.get(band1_values, j)
Band2 = array.get(band2_values, k)
takeProfit = array.get(takeProfit_values, m)
stopLoss = array.get(stopLoss_values, n)
code
Assistant
در کد شما، محاسبات سود معاملات نشاندهندهی یک اشکال منطقی است. در حقیقت، strategy.closedtrades.profit یک مقدار تکعددی را نشان میدهد که اطلاعات مربوط به سود را از معاملات بسته شده ارائه میدهد. اما برای محاسبهی کل سود، شما باید از سایر امکانات موجود در Pine Script استفاده کنید.
در زیر نسخهی اصلاح شدهای از کد شما ارائهشده است:
این خطوط قیمتهای باز و بسته شدن را از زمانبندی تعیین شده (tim) دریافت میکنند.
خطوط 38-45: تابع شرایط ورود
pinescript
f_checkEntryConditions(close_tf, open_tf, upper1, lower1) =>
longCondition = ta.crossover(close_tf, open_tf) and close < upper1
shortCondition = ta.crossunder(close_tf, open_tf) and close > lower1
[longCondition, shortCondition]
تابع f_checkEntryConditions دو شرط برای ورود به موقعیتهای خرید و فروش را تعریف میکند. برای ورود به خرید، قیمت بسته شدن باید از قیمت باز عبور کند و کمتر از باند بالایی باشد. برای ورود به فروش، قیمت بسته شدن باید زیر قیمت باز قرار بگیرد و بیشتر از باند پایینی باشد.
همچنین اگر شرایط فروش برقرار باشد، یک موقعیت فروش ایجاد و خروج بر اساس درصد مشخص شده انجام میشود.
این کد در کل یک استراتژی معاملاتی کامل را در TradingView پیادهسازی میکند که به معاملهگران کمک میکند تا بر اساس تقاطع قیمتهای مختلف و باندهای مشخص، وارد و خارج از موقعیتها شوند.
این تغییرات میتوانند به شما کمک کنند که کد شما خواناتر، بهینهتر و انعطافپذیرتر باشد.
//@version=5
strategy('[VV] Parameter Optimization Bot', overlay=true)
tim = input.timeframe('375')
// Parameter ranges for optimization
lengths = array.new_int(3)
array.set(lengths, 0, 50)
array.set(lengths, 1, 81)
array.set(lengths, 2, 100)
band1_values = array.new_float(3)
array.set(band1_values, 0, 1.0)
array.set(band1_values, 1, 1.5)
array.set(band1_values, 2, 2.0)
band2_values = array.new_float(3)
array.set(band2_values, 0, 2.0)
array.set(band2_values, 1, 2.5)
array.set(band2_values, 2, 3.0)
takeProfit_values = array.new_float(3)
array.set(takeProfit_values, 0, 1.0)
array.set(takeProfit_values, 1, 1.5)
array.set(takeProfit_values, 2, 2.0)
stopLoss_values = array.new_float(3)
array.set(stopLoss_values, 0, 0.5)
array.set(stopLoss_values, 1, 1.0)
array.set(stopLoss_values, 2, 1.5)
// Loop through combinations of parameters
for i = 0 to array.size(lengths)-1
for j = 0 to array.size(band1_values)-1
for k = 0 to array.size(band2_values)-1
for m = 0 to array.size(takeProfit_values)-1
for n = 0 to array.size(stopLoss_values)-1
length = array.get(lengths, i)
Band1 = array.get(band1_values, j)
Band2 = array.get(band2_values, k)
takeProfit = array.get(takeProfit_values, m)
stopLoss = array.get(stopLoss_values, n)
// Band calculation function
f_calculateBands(src, length, bandFactor) =>
basis = ta.sma(src, length)
dev = bandFactor * ta.stdev(src, length)
[basis, basis + dev, basis - dev]
// Calculate bands
[basis, upperBand1, lowerBand1] = f_calculateBands(close, length, Band1)
[basis2, upperBand2, lowerBand2] = f_calculateBands(close, length, Band2)
// Entry conditions
longCondition = ta.crossover(close, open) and close < upperBand1
shortCondition = ta.crossunder(close, open) and close > lowerBand1
// Execute long entry and exit
if longCondition
strategy.entry('long', strategy.long)
strategy.exit('TP/SL Long', from_entry='long', limit=close * (1 + takeProfit / 100), stop=close * (1 - stopLoss / 100))
// Execute short entry and exit
if shortCondition
strategy.entry('short', strategy.short)
strategy.exit('TP/SL Short', from_entry='short', limit=close * (1 - takeProfit / 100), stop=close * (1 + stopLoss / 100))
توضیحات:
تعریف پارامترها: در این کد، برای هر پارامتر چهارچوبهای قابل تغییر تعریف شده است. این مقادیر متغیر در آرایهها ذخیره میشوند.
حلقهها: پنج حلقه تو در تو به شما اجازه میدهد تا تمامی ترکیبهای ممکن از پارامترها را امتحان کنید.
شرایط ورود: بر اساس ترکیبات مختلف پارامترها، شرایط ورود و خروج برای معاملات بررسی میشود.
ابزارهای تجزیه و تحلیل: برای آنکه ربات عملکرد بهینه خود را بهتر بسنجد، شما میتوانید برخی از ابزارهای تجزیه و تحلیل دیگری اضافه کنید تا بتوانید رفتارهای مختلف تحت شرایط انجام معاملات را ثبت کنید.
نکات:
توجه به زمان اجرا: استفاده از حلقههای تو در تو ممکن است باعث افزایش زمان محاسبات شود؛ بنابراین این استراتژی بر روی دادههای تاریخی ممکن است زمان بیشتری بگیرد.
فراهمآوری نتیجه: میتوانید نتایج به دست آمده را جمعآوری کنید و بررسی کنید کدام ترکیب بهترین عملکرد را داشته است.
لطفا توجه داشته باشید که این ربات تنها یک مثال از روشهای بهینهسازی پارامترها است و برای استفاده واقعی نیاز به آزمون و تنظیمات بیشتری دارد.
//@version=5
strategy('[VV] Parameter Optimization Bot', overlay=true)
tim = input.timeframe('375')
// Parameter ranges for optimization
lengths = array.new_int(3)
array.set(lengths, 0, 50)
array.set(lengths, 1, 81)
array.set(lengths, 2, 100)
band1_values = array.new_float(3)
array.set(band1_values, 0, 1.0)
array.set(band1_values, 1, 1.5)
array.set(band1_values, 2, 2.0)
band2_values = array.new_float(3)
array.set(band2_values, 0, 2.0)
array.set(band2_values, 1, 2.5)
array.set(band2_values, 2, 3.0)
takeProfit_values = array.new_float(3)
array.set(takeProfit_values, 0, 1.0)
array.set(takeProfit_values, 1, 1.5)
array.set(takeProfit_values, 2, 2.0)
stopLoss_values = array.new_float(3)
array.set(stopLoss_values, 0, 0.5)
array.set(stopLoss_values, 1, 1.0)
array.set(stopLoss_values, 2, 1.5)
// Band calculation function
f_calculateBands(src, length, bandFactor) =>
basis = ta.sma(src, length)
dev = bandFactor * ta.stdev(src, length)
[basis, basis + dev, basis - dev]
var float bestProfit = na // Variable to store the best profit
var int bestLength = na
var float bestBand1 = na
var float bestBand2 = na
var float bestTakeProfit = na
var float bestStopLoss = na
// Loop through combinations of parameters only once per tick
for i = 0 to array.size(lengths)-1
for j = 0 to array.size(band1_values)-1
for k = 0 to array.size(band2_values)-1
for m = 0 to array.size(takeProfit_values)-1
for n = 0 to array.size(stopLoss_values)-1
length = array.get(lengths, i)
Band1 = array.get(band1_values, j)
Band2 = array.get(band2_values, k)
takeProfit = array.get(takeProfit_values, m)
stopLoss = array.get(stopLoss_values, n)
// Calculate bands
[basis, upperBand1, lowerBand1] = f_calculateBands(close, length, Band1)
[basis2, upperBand2, lowerBand2] = f_calculateBands(close, length, Band2)
// Entry conditions
longCondition = ta.crossover(close, open) and close < upperBand1
shortCondition = ta.crossunder(close, open) and close > lowerBand1
// Execute long entry and exit
if longCondition
strategy.entry('long', strategy.long)
strategy.exit('TP/SL Long', from_entry='long', limit=close * (1 + takeProfit / 100), stop=close * (1 - stopLoss / 100))
// Execute short entry and exit
if shortCondition
strategy.entry('short', strategy.short)
strategy.exit('TP/SL Short', from_entry='short', limit=close * (1 - takeProfit / 100), stop=close * (1 + stopLoss / 100))
// Here you would evaluate the performance and set the best parameters if needed
// This is a placeholder logic and needs to be replaced with actual performance evaluation
// Assuming profit is monitored, update the best parameters accordingly
نکات اصلاح:
تعریف توابع: تابع f_calculateBands در ابتدای کد تعریف شده تا در تمام قسمتهای کد قابلیت دسترسی داشته باشد.
متغیرهای ذخیره کننده: متغیرهای جدیدی ایجاد شده تا بهترین پارامترها را ذخیره کنند. در صورت نیاز به تنظیم مجدد، میتوانید از این متغیرها برای ارزیابی استفاده کنید.
سادهسازی منطق بهتر: منطق ارزیابی و انتخاب بهترین پارامترها به صورت ساده در نظر گرفته شده است. برای ارزیابی واقعی، شما باید یک منطق مناسب برای اندازهگیری عملکرد (مثل محاسبه سود یا ضرر) اضافه کنید.
عملکرد كد: توابع و حلقهها از نظر اجرایی بهینهتر شدهاند.
نکته نهایی:
توجه داشته باشید که نحوه ارزیابی و انتخاب بهترین پارامترها باید بر اساس تاریخ مالی واقعی انجام شود. این کد نیاز به تنظیمات و آزمایشهای بیشتری دارد تا بهترین نتایج را برای آمادهسازی استراتژی به عمل بیاورد.
// Here you would evaluate the performance and set the best parameters if needed
// This is a placeholder logic and needs to be replaced with actual performance evaluation
// Assuming profit is monitored, update the best parameters accordingly
//@version=5
strategy('[VV] Parameter Optimization Bot', overlay=true)
tim = input.timeframe('375')
// Parameter ranges for optimization
lengths = array.new_int(3)
array.set(lengths, 0, 50)
array.set(lengths, 1, 81)
array.set(lengths, 2, 100)
band1_values = array.new_float(3)
array.set(band1_values, 0, 1.0)
array.set(band1_values, 1, 1.5)
array.set(band1_values, 2, 2.0)
band2_values = array.new_float(3)
array.set(band2_values, 0, 2.0)
array.set(band2_values, 1, 2.5)
array.set(band2_values, 2, 3.0)
takeProfit_values = array.new_float(3)
array.set(takeProfit_values, 0, 1.0)
array.set(takeProfit_values, 1, 1.5)
array.set(takeProfit_values, 2, 2.0)
stopLoss_values = array.new_float(3)
array.set(stopLoss_values, 0, 0.5)
array.set(stopLoss_values, 1, 1.0)
array.set(stopLoss_values, 2, 1.5)
f_calculateBands(src, length, bandFactor) =>
basis = ta.sma(src, length)
dev = bandFactor * ta.stdev(src, length)
[basis, basis + dev, basis - dev]
var float bestProfit = na
var int bestLength = na
var float bestBand1 = na
var float bestBand2 = na
var float bestTakeProfit = na
var float bestStopLoss = na
// Variable to accumulate gains
var float totalProfit = 0.0
// Loop through combinations of parameters once per tick
for i = 0 to array.size(lengths)-1
for j = 0 to array.size(band1_values)-1
for k = 0 to array.size(band2_values)-1
for m = 0 to array.size(takeProfit_values)-1
for n = 0 to array.size(stopLoss_values)-1
length = array.get(lengths, i)
Band1 = array.get(band1_values, j)
Band2 = array.get(band2_values, k)
takeProfit = array.get(takeProfit_values, m)
stopLoss = array.get(stopLoss_values, n)
// Calculate bands
[basis, upperBand1, lowerBand1] = f_calculateBands(close, length, Band1)
longCondition = ta.crossover(close, open) and close < upperBand1
shortCondition = ta.crossunder(close, open) and close > lowerBand1
// Long entry and exit
if longCondition
strategy.entry('long', strategy.long)
strategy.exit('TP/SL Long', from_entry='long', limit=close * (1 + takeProfit / 100), stop=close * (1 - stopLoss / 100))
// Short entry and exit
if shortCondition
strategy.entry('short', strategy.short)
strategy.exit('TP/SL Short', from_entry='short', limit=close * (1 - takeProfit / 100), stop=close * (1 + stopLoss / 100))
// Calculate total profit for optimization
totalProfit := totalProfit + (strategy.position_size > 0 ? strategy.closedtrades.profit : 0)
// Update best parameters based on total profit
if not na(totalProfit) and (na(bestProfit) or totalProfit > bestProfit)
bestProfit := totalProfit
bestLength := length
bestBand1 := Band1
bestBand2 := Band2
bestTakeProfit := takeProfit
bestStopLoss := stopLoss
// You can print the best parameters on the chart for review
label.new(bar_index, high, text="Best Length: " + str.tostring(bestLength) + "\nBest Band1: " + str.tostring(bestBand1) + "\nBest Band2: " + str.tostring(bestBand2) + "\nBest TP: " + str.tostring(bestTakeProfit) + "\nBest SL: " + str.tostring(bestStopLoss), color=color.new(color.green, 90))
توضیحات تغییرات:
محاسبه و ذخیرهسازی سود کل: یک متغیر بهنام totalProfit برای جمعآوری سود ایجاد شده و بهروزرسانی آن در طی معاملات انجام میشود.
بهروزرسانی بهترین پارامترها: بخشی که بررسی میکند آیا سود کلی بیشتر از بهترین سود قبلی است و در صورت درست بودن، پارامترها بهروز میشوند.
برچسبگذاری روی نمودار: برای نمایش بهترین پارامترهای بهدستآمده بر روی نمودار، از برچسب استفاده شده است.
این روشها کمک میکند تا استراتژی شما به طور داینامیک بهبود یابد و با دادههای واقعی بهینهسازی شود.
// Update best parameters based on total profit
if not na(totalProfit) and (na(bestProfit) or totalProfit > bestProfit)
bestProfit := totalProfit
bestLength := length
bestBand1 := Band1
bestBand2 := Band2
bestTakeProfit := takeProfit
bestStopLoss := stopLoss
// You can print the best parameters on the chart for review
label.new(bar_index, high, text="Best Length: " + str.tostring(bestLength) + "\nBest Band1: " + str.tostring(bestBand1) + "\nBest Band2: " + str.tostring(bestBand2) + "\nBest TP: " + str.tostring(bestTakeProfit) + "\nBest SL: " + str.tostring(bestStopLoss), color=color.new(color.green, 90))
//@version=5
strategy('[VV] Parameter Optimization Bot', overlay=true)
tim = input.timeframe('375')
// Parameter ranges for optimization
lengths = array.new_int(3, 0) // Initialize array with size and default value
array.set(lengths, 0, 50)
array.set(lengths, 1, 81)
array.set(lengths, 2, 100)
band1_values = array.new_float(3, 0.0) // Initialize array with size and default value
array.set(band1_values, 0, 1.0)
array.set(band1_values, 1, 1.5)
array.set(band1_values, 2, 2.0)
band2_values = array.new_float(3, 0.0) // Initialize array with size and default value
array.set(band2_values, 0, 2.0)
array.set(band2_values, 1, 2.5)
array.set(band2_values, 2, 3.0)
takeProfit_values = array.new_float(3, 0.0) // Initialize array with size and default value
array.set(takeProfit_values, 0, 1.0)
array.set(takeProfit_values, 1, 1.5)
array.set(takeProfit_values, 2, 2.0)
stopLoss_values = array.new_float(3, 0.0) // Initialize array with size and default value
array.set(stopLoss_values, 0, 0.5)
array.set(stopLoss_values, 1, 1.0)
array.set(stopLoss_values, 2, 1.5)
// Function to calculate bands
f_calculateBands(src, length, bandFactor) =>
basis = ta.sma(src, length)
dev = bandFactor * ta.stdev(src, length)
[basis, basis + dev, basis - dev]
var float bestProfit = na
var int bestLength = na
var float bestBand1 = na
var float bestBand2 = na
var float bestTakeProfit = na
var float bestStopLoss = na
// Variable to accumulate gains
var float totalProfit = 0.0
// Loop through combinations of parameters once per tick
for i = 0 to array.size(lengths)-1
for j = 0 to array.size(band1_values)-1
for k = 0 to array.size(band2_values)-1
for m = 0 to array.size(takeProfit_values)-1
for n = 0 to array.size(stopLoss_values)-1
length = array.get(lengths, i)
Band1 = array.get(band1_values, j)
Band2 = array.get(band2_values, k)
takeProfit = array.get(takeProfit_values, m)
stopLoss = array.get(stopLoss_values, n)
// Calculate bands
[basis, upperBand1, lowerBand1] = f_calculateBands(close, length, Band1)
longCondition = ta.crossover(close, open) and close < upperBand1
shortCondition = ta.crossunder(close, open) and close > lowerBand1
// Long entry and exit
if longCondition
strategy.entry('long', strategy.long)
strategy.exit('TP/SL Long', from_entry='long', limit=close * (1 + takeProfit / 100), stop=close * (1 - stopLoss / 100))
// Short entry and exit
if shortCondition
strategy.entry('short', strategy.short)
strategy.exit('TP/SL Short', from_entry='short', limit=close * (1 - takeProfit / 100), stop=close * (1 + stopLoss / 100))
// Calculate total profit for optimization
totalProfit := totalProfit + (strategy.position_size > 0 ? strategy.closedtrades.profit : 0)
// Update best parameters based on total profit
if not na(totalProfit) and (na(bestProfit) or totalProfit > bestProfit)
bestProfit := totalProfit
bestLength := length
bestBand1 := Band1
bestBand2 := Band2
bestTakeProfit := takeProfit
bestStopLoss := stopLoss
// You can print the best parameters on the chart for review
label.new(bar_index, high, text="Best Length: " + str.tostring(bestLength) + "\nBest Band1: " + str.tostring(bestBand1) + "\nBest Band2: " + str.tostring(bestBand2) + "\nBest TP: " + str.tostring(bestTakeProfit) + "\nBest SL: " + str.tostring(bestStopLoss), color=color.new(color.green, 90))
توضیحات اصلاحات
Initialization of Arrays: برای ایجاد آرایهها با مقادیر اولیه، از روش array.new_xxx(size, default_value) استفاده شد. این کار باعث میشود که مقادیر نامعتبر باعث ایجاد خطا نشوند.
تعریف تابع: تابع f_calculateBands به درستی تعریف شده و با توجه به آن از آن در محاسبات استفاده شده است.
نوشتار و قالببندی: همه چیز به صورت منظم و خوانا نوشته شده است و استفاده از کامنتها برای توضیحات نیز به کاربر کمک میکند.
بررسی شرطها: منطقهای شرطی به همان شکل کار میکنند اما با افزایش خوانایی.
با اجرای این تغییرات، کد باید به درستی کار کند و مشکلات نوشتاری را برطرف میکند. حتماً بعد از این تغییرات آن را تست کنید تا از عملکرد صحیح آن اطمینان حاصل کنید.
// Calculate bands
[basis, upperBand1, lowerBand1] = f_calculateBands(close, length, Band1)
longCondition = ta.crossover(close, open) and close < upperBand1
shortCondition = ta.crossunder(close, open) and close > lowerBand1
// Long entry and exit
if longCondition
strategy.entry('long', strategy.long)
strategy.exit('TP/SL Long', from_entry='long', limit=close * (1 + takeProfit / 100), stop=close * (1 - stopLoss / 100))
// Short entry and exit
if shortCondition
strategy.entry('short', strategy.short)
strategy.exit('TP/SL Short', from_entry='short', limit=close * (1 - takeProfit / 100), stop=close * (1 + stopLoss / 100))
// Calculate total profit for optimization
totalProfit := totalProfit + (strategy.position_size > 0 ? strategy.closedtrades.profit : 0)
// Update best parameters based on total profit
if not na(totalProfit) and (na(bestProfit) or totalProfit > bestProfit)
bestProfit := totalProfit
bestLength := length
bestBand1 := Band1
bestBand2 := Band2
bestTakeProfit := takeProfit
bestStopLoss := stopLoss
// You can print the best parameters on the chart for review
label.new(bar_index, high, text="Best Length: " + str.tostring(bestLength) + "\nBest Band1: " + str.tostring(bestBand1) + "\nBest Band2: " + str.tostring(bestBand2) + "\nBest TP: " + str.tostring(bestTakeProfit) + "\nBest SL: " + str.tostring(bestStopLoss), color=color.new(color.green, 90))
//@version=5
strategy('[VV] Parameter Optimization Bot', overlay=true)
tim = input.timeframe('375')
// Parameter ranges for optimization
lengths = array.new_int(3, 0)
array.set(lengths, 0, 50)
array.set(lengths, 1, 81)
array.set(lengths, 2, 100)
band1_values = array.new_float(3, 0.0)
array.set(band1_values, 0, 1.0)
array.set(band1_values, 1, 1.5)
array.set(band1_values, 2, 2.0)
band2_values = array.new_float(3, 0.0)
array.set(band2_values, 0, 2.0)
array.set(band2_values, 1, 2.5)
array.set(band2_values, 2, 3.0)
takeProfit_values = array.new_float(3, 0.0)
array.set(takeProfit_values, 0, 1.0)
array.set(takeProfit_values, 1, 1.5)
array.set(takeProfit_values, 2, 2.0)
stopLoss_values = array.new_float(3, 0.0)
array.set(stopLoss_values, 0, 0.5)
array.set(stopLoss_values, 1, 1.0)
array.set(stopLoss_values, 2, 1.5)
// Function to calculate bands
f_calculateBands(src, length, bandFactor) =>
basis = ta.sma(src, length)
dev = bandFactor * ta.stdev(src, length)
[basis, basis + dev, basis - dev]
// Variables to store performance metrics
var float bestProfit = na
var int bestLength = na
var float bestBand1 = na
var float bestBand2 = na
var float bestTakeProfit = na
var float bestStopLoss = na
// Variable to accumulate gains
var float totalProfit = 0.0
var float lastProfit = 0.0
// Loop through combinations of parameters
for i = 0 to array.size(lengths) - 1
for j = 0 to array.size(band1_values) - 1
for k = 0 to array.size(band2_values) - 1
for m = 0 to array.size(takeProfit_values) - 1
for n = 0 to array.size(stopLoss_values) - 1
length = array.get(lengths, i)
Band1 = array.get(band1_values, j)
Band2 = array.get(band2_values, k)
takeProfit = array.get(takeProfit_values, m)
stopLoss = array.get(stopLoss_values, n)
// Calculate bands
[basis, upperBand1, lowerBand1] = f_calculateBands(close, length, Band1)
longCondition = ta.crossover(close, open) and close < upperBand1
shortCondition = ta.crossunder(close, open) and close > lowerBand1
// Long entry and exit
if longCondition
strategy.entry('long', strategy.long)
strategy.exit('TP/SL Long', from_entry='long', limit=close * (1 + takeProfit / 100), stop=close * (1 - stopLoss / 100))
// Short entry and exit
if shortCondition
strategy.entry('short', strategy.short)
strategy.exit('TP/SL Short', from_entry='short', limit=close * (1 - takeProfit / 100), stop=close * (1 + stopLoss / 100))
// Calculate total profit and store last profit
if strategy.closedtrades > 0
lastProfit := strategy.closedtrades.profit(strategy.closedtrades - 1) // Get the profit of the last closed trade
totalProfit := totalProfit + lastProfit
// Update best parameters based on total profit
if not na(totalProfit) and (na(bestProfit) or totalProfit > bestProfit)
bestProfit := totalProfit
bestLength := length
bestBand1 := Band1
bestBand2 := Band2
bestTakeProfit := takeProfit
bestStopLoss := stopLoss
// Display the best parameters on the chart
label.new(bar_index, high, text="Best Length: " + str.tostring(bestLength) +
"\nBest Band1: " + str.tostring(bestBand1) +
"\nBest Band2: " + str.tostring(bestBand2) +
"\nBest TP: " + str.tostring(bestTakeProfit) +
"\nBest SL: " + str.tostring(bestStopLoss),
color=color.new(color.green, 90))
توضیحات اصلاحات:
محاسبه سود: بهجای استفاده از strategy.position_size > 0 ? strategy.closedtrades.profit : 0، ما از strategy.closedtrades.profit(strategy.closedtrades - 1) برای بدست آوردن سود آخرین معامله بستهشده استفاده کردیم. این روش موجب مطمئنشدن از این میشود که فقط سود آخرین معامله بستهشده در محاسبات لحاظ شود.
متغیرهای ذخیرهسازی: متغیر lastProfit برای ذخیره سود آخرین معامله بستهشده تعریف شده و بهروزرسانی سود کل انجامشده است.
قابلیت خوانایی: قالببندی متن و نحوه نمایش اطلاعات بر روی نمودار برای بهبود برقراری ارتباط موثر با کاربر بهتر شده است.
لطفا کد را آزمایش کنید و مطمئن شوید که دادهها به درستی محاسبه شده و نتایج نهایی با انتظارات شما مطابقت دارد.