برای این کد رباتی بنویس که بعد از اینکه واگرایی اتفاق افتاد پوزیشن بگیرد. حجم پوزیشن 1…
- User
برای این کد رباتی بنویس که بعد از اینکه واگرایی اتفاق افتاد پوزیشن بگیرد. حجم پوزیشن 1 درصد از بالانس باشد.tp ان 20 پیپ. استاپ ان 10 پیپ
//+------------------------------------------------------------------+ //| RSI_Divergence_TrendLines.mq5 | //| Copyright 2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #property indicator_separate_window #property indicator_buffers 1 #property indicator_plots 1
// RSI Line #property indicator_label1 "RSI" #property indicator_type1 DRAW_LINE #property indicator_color1 clrBlue #property indicator_style1 STYLE_SOLID #property indicator_width1 2
// Input parameters input int RSI_Period = 14; // RSI Period input ENUM_APPLIED_PRICE RSI_Price = PRICE_CLOSE; // RSI Applied Price input int Lookback_Bars = 5; // Lookback bars for pivot detection input int Min_Bars_Between = 5; // Minimum bars between divergences input double RSI_Overbought = 70.0; // RSI Overbought level input double RSI_Oversold = 30.0; // RSI Oversold level input color Bullish_Line_Color = clrLime; // Bullish divergence line color input color Bearish_Line_Color = clrRed; // Bearish divergence line color input int Line_Width = 2; // Trend line width input ENUM_LINE_STYLE Line_Style = STYLE_SOLID; // Trend line style
// Indicator buffers double RSI_Buffer[];
// Global variables int rsi_handle; string indicator_prefix = "RSI_Div_"; int line_counter = 0;
//+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { // Set indicator buffers SetIndexBuffer(0, RSI_Buffer, INDICATOR_DATA);
// Create RSI handle rsi_handle = iRSI(_Symbol, _Period, RSI_Period, RSI_Price); if(rsi_handle == INVALID_HANDLE) { Print("Failed to create RSI handle"); return INIT_FAILED; } // Set indicator properties IndicatorSetString(INDICATOR_SHORTNAME, "RSI Divergence TrendLines (" + IntegerToString(RSI_Period) + ")"); IndicatorSetInteger(INDICATOR_DIGITS, 2); // Set levels IndicatorSetDouble(INDICATOR_LEVELVALUE, 0, RSI_Overbought); IndicatorSetDouble(INDICATOR_LEVELVALUE, 1, 50.0); IndicatorSetDouble(INDICATOR_LEVELVALUE, 2, RSI_Oversold); // Delete old trend lines DeleteAllTrendLines(); return INIT_SUCCEEDED;}
//+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { if(rates_total < RSI_Period + Lookback_Bars * 2) return 0;
// Get RSI values if(CopyBuffer(rsi_handle, 0, 0, rates_total, RSI_Buffer) <= 0) return 0; // Only calculate on new bar static datetime last_time = 0; if(time[rates_total-1] == last_time) return rates_total; last_time = time[rates_total-1]; int start = MathMax(prev_calculated - 1, Lookback_Bars * 2); if(prev_calculated == 0) DeleteAllTrendLines(); // Scan for divergences for(int i = start; i < rates_total - Lookback_Bars; i++) { // Check for bullish divergence if(RSI_Buffer[i] < RSI_Oversold) { int current_price_low = FindPriceLow(i, Lookback_Bars, low); int current_rsi_low = FindRSILow(i, Lookback_Bars); if(current_price_low == i && current_rsi_low == i) { // Look for previous low int prev_price_low = FindPreviousPriceLow(i, Min_Bars_Between, low); int prev_rsi_low = FindPreviousRSILow(i, Min_Bars_Between); if(prev_price_low > 0 && prev_rsi_low > 0) { // Check divergence conditions if(low[i] < low[prev_price_low] && RSI_Buffer[i] > RSI_Buffer[prev_rsi_low]) { // Draw trend lines for bullish divergence DrawPriceTrendLine(time[prev_price_low], low[prev_price_low], time[i], low[i], Bullish_Line_Color, "Price_Bull_"); DrawRSITrendLine(time[prev_rsi_low], RSI_Buffer[prev_rsi_low], time[i], RSI_Buffer[i], Bullish_Line_Color, "RSI_Bull_"); } } } } // Check for bearish divergence if(RSI_Buffer[i] > RSI_Overbought) { int current_price_high = FindPriceHigh(i, Lookback_Bars, high); int current_rsi_high = FindRSIHigh(i, Lookback_Bars); if(current_price_high == i && current_rsi_high == i) { // Look for previous high int prev_price_high = FindPreviousPriceHigh(i, Min_Bars_Between, high); int prev_rsi_high = FindPreviousRSIHigh(i, Min_Bars_Between); if(prev_price_high > 0 && prev_rsi_high > 0) { // Check divergence conditions if(high[i] > high[prev_price_high] && RSI_Buffer[i] < RSI_Buffer[prev_rsi_high]) { // Draw trend lines for bearish divergence DrawPriceTrendLine(time[prev_price_high], high[prev_price_high], time[i], high[i], Bearish_Line_Color, "Price_Bear_"); DrawRSITrendLine(time[prev_rsi_high], RSI_Buffer[prev_rsi_high], time[i], RSI_Buffer[i], Bearish_Line_Color, "RSI_Bear_"); } } } } } return rates_total;}
//+------------------------------------------------------------------+ //| Draw price trend line on main chart | //+------------------------------------------------------------------+ void DrawPriceTrendLine(datetime time1, double price1, datetime time2, double price2, color line_color, string prefix) { string line_name = prefix + indicator_prefix + IntegerToString(line_counter++);
if(ObjectCreate(0, line_name, OBJ_TREND, 0, time1, price1, time2, price2)) { ObjectSetInteger(0, line_name, OBJPROP_COLOR, line_color); ObjectSetInteger(0, line_name, OBJPROP_WIDTH, Line_Width); ObjectSetInteger(0, line_name, OBJPROP_STYLE, Line_Style); ObjectSetInteger(0, line_name, OBJPROP_RAY_RIGHT, false); ObjectSetInteger(0, line_name, OBJPROP_RAY_LEFT, false); ObjectSetInteger(0, line_name, OBJPROP_BACK, true); ObjectSetString(0, line_name, OBJPROP_TOOLTIP, "Price Divergence Line"); }}
//+------------------------------------------------------------------+ //| Draw RSI trend line on indicator window | //+------------------------------------------------------------------+ void DrawRSITrendLine(datetime time1, double rsi1, datetime time2, double rsi2, color line_color, string prefix) { string line_name = prefix + indicator_prefix + IntegerToString(line_counter++);
if(ObjectCreate(ChartID(), line_name, OBJ_TREND, ChartWindowFind(), time1, rsi1, time2, rsi2)) { ObjectSetInteger(ChartID(), line_name, OBJPROP_COLOR, line_color); ObjectSetInteger(ChartID(), line_name, OBJPROP_WIDTH, Line_Width); ObjectSetInteger(ChartID(), line_name, OBJPROP_STYLE, Line_Style); ObjectSetInteger(ChartID(), line_name, OBJPROP_RAY_RIGHT, false); ObjectSetInteger(ChartID(), line_name, OBJPROP_RAY_LEFT, false); ObjectSetInteger(ChartID(), line_name, OBJPROP_BACK, true); ObjectSetString(ChartID(), line_name, OBJPROP_TOOLTIP, "RSI Divergence Line"); }}
//+------------------------------------------------------------------+ //| Find price low within lookback period | //+------------------------------------------------------------------+ int FindPriceLow(int current_bar, int lookback, const double &low[]) { double current_low = low[current_bar];
for(int i = current_bar - lookback; i <= current_bar + lookback; i++) { if(i >= 0 && i < ArraySize(low) && i != current_bar) { if(low[i] <= current_low) return -1; } } return current_bar;}
//+------------------------------------------------------------------+ //| Find RSI low within lookback period | //+------------------------------------------------------------------+ int FindRSILow(int current_bar, int lookback) { double current_rsi = RSI_Buffer[current_bar];
for(int i = current_bar - lookback; i <= current_bar + lookback; i++) { if(i >= 0 && i < ArraySize(RSI_Buffer) && i != current_bar) { if(RSI_Buffer[i] <= current_rsi) return -1; } } return current_bar;}
//+------------------------------------------------------------------+ //| Find price high within lookback period | //+------------------------------------------------------------------+ int FindPriceHigh(int current_bar, int lookback, const double &high[]) { double current_high = high[current_bar];
for(int i = current_bar - lookback; i <= current_bar + lookback; i++) { if(i >= 0 && i < ArraySize(high) && i != current_bar) { if(high[i] >= current_high) return -1; } } return current_bar;}
//+------------------------------------------------------------------+ //| Find RSI high within lookback period | //+------------------------------------------------------------------+ int FindRSIHigh(int current_bar, int lookback) { double current_rsi = RSI_Buffer[current_bar];
for(int i = current_bar - lookback; i <= current_bar + lookback; i++) { if(i >= 0 && i < ArraySize(RSI_Buffer) && i != current_bar) { if(RSI_Buffer[i] >= current_rsi) return -1; } } return current_bar;}
//+------------------------------------------------------------------+ //| Find previous price low | //+------------------------------------------------------------------+ int FindPreviousPriceLow(int current_low, int min_distance, const double &low[]) { for(int i = current_low - min_distance; i >= Lookback_Bars; i--) { if(FindPriceLow(i, Lookback_Bars, low) == i) return i; } return -1; }
//+------------------------------------------------------------------+ //| Find previous RSI low | //+------------------------------------------------------------------+ int FindPreviousRSILow(int current_low, int min_distance) { for(int i = current_low - min_distance; i >= Lookback_Bars; i--) { if(FindRSILow(i, Lookback_Bars) == i) return i; } return -1; }
//+------------------------------------------------------------------+ //| Find previous price high | //+------------------------------------------------------------------+ int FindPreviousPriceHigh(int current_high, int min_distance, const double &high[]) { for(int i = current_high - min_distance; i >= Lookback_Bars; i--) { if(FindPriceHigh(i, Lookback_Bars, high) == i) return i; } return -1; }
//+------------------------------------------------------------------+ //| Find previous RSI high | //+------------------------------------------------------------------+ int FindPreviousRSIHigh(int current_high, int min_distance) { for(int i = current_high - min_distance; i >= Lookback_Bars; i--) { if(FindRSIHigh(i, Lookback_Bars) == i) return i; } return -1; }
//+------------------------------------------------------------------+ //| Delete all trend lines created by this indicator | //+------------------------------------------------------------------+ void DeleteAllTrendLines() { int total_objects = ObjectsTotal(ChartID(), ChartWindowFind());
for(int i = total_objects - 1; i >= 0; i--) { string obj_name = ObjectName(ChartID(), i, ChartWindowFind()); if(StringFind(obj_name, indicator_prefix) >= 0) { ObjectDelete(ChartID(), obj_name); } } total_objects = ObjectsTotal(0, 0); for(int i = total_objects - 1; i >= 0; i--) { string obj_name = ObjectName(0, i, 0); if(StringFind(obj_name, indicator_prefix) >= 0) { ObjectDelete(0, obj_name); } }}
//+------------------------------------------------------------------+ //| Indicator deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { if(rsi_handle != INVALID_HANDLE) IndicatorRelease(rsi_handle);
DeleteAllTrendLines();}
Assistant
برای نوشتن یک ربات که بر اساس واگرایی RSI معامله باز کند، باید کد Expert Advisor را بر اساس شرایط معاملاتی مورد نظرتان بنویسید. در زیر یک نمونه کد برای انجام این کار آورده شده است. این کد بر اساس واگرایی RSI که در اندیکاتور شما تعریف شده است، پوزیشن باز میکند.
//+------------------------------------------------------------------+ //| RSI_Div_EA.mq5 | //| Copyright 2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" // Input parameters input double RiskPercent = 1.0; // Risk percentage per trade input int RSI_Period = 14; // RSI Period input ENUM_APPLIED_PRICE RSI_Price = PRICE_CLOSE; // RSI Applied Price input int Lookback_Bars = 5; // Lookback bars for pivot detection input int Min_Bars_Between = 5; // Minimum bars between divergences input double RSI_Overbought = 70.0; // RSI Overbought level input double RSI_Oversold = 30.0; // RSI Oversold level input int TP_Pips = 20; // Take Profit in pips input int SL_Pips = 10; // Stop Loss in pips // Global variables int rsi_handle; double RSI_Buffer[]; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // Create RSI handle rsi_handle = iRSI(_Symbol, _Period, RSI_Period, RSI_Price); if(rsi_handle == INVALID_HANDLE) { Print("Failed to create RSI handle"); return INIT_FAILED; } // Allocate memory for RSI buffer ArraySetAsSeries(RSI_Buffer, true); return INIT_SUCCEEDED; } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { if(rsi_handle != INVALID_HANDLE) IndicatorRelease(rsi_handle); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { // Get RSI values int rates_total = iBars(_Symbol, _Period); if(CopyBuffer(rsi_handle, 0, 0, rates_total, RSI_Buffer) <= 0) return; // Scan for divergences for(int i = rates_total - 2; i >= Lookback_Bars; i--) { // Check for bullish divergence if(RSI_Buffer[i] < RSI_Oversold) { int current_price_low = FindPriceLow(i, Lookback_Bars, iLow(_Symbol, _Period)); int current_rsi_low = FindRSILow(i, Lookback_Bars); if(current_price_low == i && current_rsi_low == i) { // Look for previous low int prev_price_low = FindPreviousPriceLow(i, Min_Bars_Between, iLow(_Symbol, _Period)); int prev_rsi_low = FindPreviousRSILow(i, Min_Bars_Between); if(prev_price_low > 0 && prev_rsi_low > 0) { // Check divergence conditions if(iLow(_Symbol, _Period, i) < iLow(_Symbol, _Period, prev_price_low) && RSI_Buffer[i] > RSI_Buffer[prev_rsi_low]) { // Open long position OpenLongPosition(TP_Pips, SL_Pips); } } } } // Check for bearish divergence if(RSI_Buffer[i] > RSI_Overbought) { int current_price_high = FindPriceHigh(i, Lookback_Bars, iHigh(_Symbol, _Period)); int current_rsi_high = FindRSIHigh(i, Lookback_Bars); if(current_price_high == i && current_rsi_high == i) { // Look for previous high int prev_price_high = FindPreviousPriceHigh(i, Min_Bars_Between, iHigh(_Symbol, _Period)); int prev_rsi_high = FindPreviousRSIHigh(i, Min_Bars_Between); if(prev_price_high > 0 && prev_rsi_high > 0) { // Check divergence conditions if(iHigh(_Symbol, _Period, i) > iHigh(_Symbol, _Period, prev_price_high) && RSI_Buffer[i] < RSI_Buffer[prev_rsi_high]) { // Open short position OpenShortPosition(TP_Pips, SL_Pips); } } } } } } //+------------------------------------------------------------------+ //| Find price low within lookback period | //+------------------------------------------------------------------+ int FindPriceLow(int current_bar, int lookback, const double &low[]) { double current_low = low[current_bar]; for(int i = current_bar - lookback; i <= current_bar + lookback; i++) { if(i >= 0 && i < ArraySize(low) && i != current_bar) { if(low[i] <= current_low) return -1; } } return current_bar; } //+------------------------------------------------------------------+ //| Find RSI low within lookback period | //+------------------------------------------------------------------+ int FindRSILow(int current_bar, int lookback) { double current_rsi = RSI_Buffer[current_bar]; for(int i = current_bar - lookback; i <= current_bar + lookback; i++) { if(i >= 0 && i < ArraySize(RSI_Buffer) && i != current_bar) { if(RSI_Buffer[i] <= current_rsi) return -1; } } return current_bar; } //+------------------------------------------------------------------+ //| Find price high within lookback period | //+------------------------------------------------------------------+ int FindPriceHigh(int current_bar, int lookback, const double &high[]) { double current_high = high[current_bar]; for(int i = current_bar - lookback; i <= current_bar + lookback; i++) { if(i >= 0 && i < ArraySize(high) && i != current_bar) { if(high[i] >= current_high) return -1; } } return current_bar; } //+------------------------------------------------------------------+ //| Find RSI high within lookback period | //+------------------------------------------------------------------+ int FindRSIHigh(int current_bar, int lookback) { double current_rsi = RSI_Buffer[current_bar]; for(int i = current_bar - lookback; i <= current_bar + lookback; i++) { if(i >= 0 && i < ArraySize(RSI_Buffer) && i != current_bar) { if(RSI_Buffer[i] >= current_rsi) return -1; } } return current_bar; } //+------------------------------------------------------------------+ //| Find previous price low | //+------------------------------------------------------------------+ int FindPreviousPriceLow(int current_low, int min_distance, const double &low[]) { for(int i = current_low - min_distance; i >= Lookback_Bars; i--) { if(FindPriceLow(i, Lookback_Bars, low) == i) return i; } return -1; } //+------------------------------------------------------------------+ //| Find previous RSI low | //+------------------------------------------------------------------+ int FindPreviousRSILow(int current_low, int min_distance) { for(int i = current_low - min_distance; i >= Lookback_Bars; i--) { if(FindRSILow(i, Lookback_Bars) == i) return i; } return -1; } //+------------------------------------------------------------------+ //| Find previous price high | //+------------------------------------------------------------------+ int FindPreviousPriceHigh(int current_high, int min_distance, const double &high[]) { for(int i = current_high - min_distance; i >= Lookback_Bars; i--) { if(FindPriceHigh(i, Lookback_Bars, high) == i) return i; } return -1; } //+------------------------------------------------------------------+ //| Find previous RSI high | //+------------------------------------------------------------------+ int FindPreviousRSIHigh(int current_high, int min_distance) { for(int i = current_high - min_distance; i >= Lookback_Bars; i--) { if(FindRSIHigh(i, Lookback_Bars) == i) return i; } return -1; } //+------------------------------------------------------------------+ //| Open long position | //+------------------------------------------------------------------+ void OpenLongPosition(int tp_pips, int sl_pips) { double lot_size = CalculateLotSize(RiskPercent); double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double sl = ask - sl_pips * _Point; double tp = ask + tp_pips * _Point; MqlTradeRequest request = {0}; request.type = OP_BUY; request.price = ask; request.volume = lot_size; request.sl = sl; request.tp = tp; request.magic = 0; request.comment = "RSI Divergence Long"; request.type_filling = ORDER_FILLING_FOK; MqlTradeResult result = {0}; OrderSend(request, result); } //+------------------------------------------------------------------+ //| Open short position | //+------------------------------------------------------------------+ void OpenShortPosition(int tp_pips,
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.