Metatader5执行止损时,为什么if语句出错?

问题描述 投票:0回答:1

代码在第 143 行和第 153 行(在止损范围内)给了我一个错误。我尝试纠正它,但是当我更改代码时,我收到更多错误。请问谁能帮帮我

// Include the definition of OrderType and order types
#include <Trade\Trade.mqh>

//+------------------------------------------------------------------+
//|                                                      martin2.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property strict

// Include the definition of SELECT_BY_POS
#define SELECT_BY_POS 0

// Inputs
input int RSIPeriod = 14;                      // RSI period
input double OverboughtLevel = 70;             // Overbought level
input double OversoldLevel = 30;               // Oversold level
input double InitialLotSize = 0.01;            // Initial lot size
input double MartingaleMultiplier = 2.0;       // Martingale multiplier
input double StopLossPercentage = 30.0;        // Stop loss percentage

// Global variables
int orderCount = 0;                             // Order counter

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    double rsi = iRSI(Symbol(), 0, RSIPeriod, 0); // Get current RSI value from bar 0
    
    double currentBid = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Get current Bid price
    double currentAsk = SymbolInfoDouble(_Symbol, SYMBOL_ASK); // Get current Ask price

    // Check if RSI is above the overbought level
    if (rsi > OverboughtLevel)
    {
        // Calculate lot size for this order
        double lotSize = CalculateLotSize();

        // Structures for request and result of the operation
        MqlTradeRequest request;
        MqlTradeResult result;
        
        // Fill the sell request
        request.action = TRADE_ACTION_DEAL;
        request.symbol = Symbol();
        request.volume = lotSize;
        request.type = ORDER_TYPE_SELL;
        request.price = currentBid; // Use current Bid price
        request.sl = CalculateStopLoss(currentBid); // Calculate stop loss using Bid price
        request.comment = "Martingale RSI";
        request.type_time = ORDER_TIME_GTC;
        request.magic = 123456;
        request.deviation = 20;

        // Send the sell order
        if (OrderSend(request, result))
        {
            orderCount++; // Increment order counter
        }
        else
        {
            Print("Error opening sell order: ", GetLastError());
        }
    }

    // Check if RSI is below the oversold level
    if (rsi < OversoldLevel)
    {
        // Calculate lot size for this order
        double lotSize = CalculateLotSize();

        // Structures for request and result of the operation
        MqlTradeRequest request;
        MqlTradeResult result;

        // Fill the buy request
        request.action = TRADE_ACTION_DEAL;
        request.symbol = Symbol();
        request.volume = lotSize;
        request.type = ORDER_TYPE_BUY;
        request.price = currentAsk; // Use current Ask price
        request.sl = CalculateStopLoss(currentAsk); // Calculate stop loss using Ask price
        request.comment = "Martingale RSI";
        request.type_time = ORDER_TIME_GTC;
        request.magic = 123456;
        request.deviation = 20;

        // Send the buy order
        if (OrderSend(request, result))
        {
            orderCount++; // Increment order counter
        }
        else
        {
            Print("Error opening buy order: ", GetLastError());
        }
    }
}

//+------------------------------------------------------------------+
//| Function to calculate lot size                                   |
//+------------------------------------------------------------------+
double CalculateLotSize()
{
    double lotSize = InitialLotSize * MathPow(MartingaleMultiplier, orderCount);
    return lotSize;
}

//+------------------------------------------------------------------+
//| Function to calculate stop loss                                  |
//+------------------------------------------------------------------+
double CalculateStopLoss(double entryPrice)
{
    double stopLossPrice;

    int totalOrders = OrdersTotal(); // Get total number of orders

    // Check if there are open orders
    if (totalOrders == 0)
    {
        Print("Error: No open orders.");
        return 0;
    }

    // Select the last order using the order number and selection mode
    if (!OrderSelect(totalOrders - 1, SELECT_BY_POS))
    {
        Print("Error selecting order.");
        return 0;
    }

    // Calculate stop loss distance in points
    double stopLossDistance = entryPrice * StopLossPercentage / 100.0 / _Point;

    // Calculate order type of the last order
    int orderType

该代码是带有 rsi 的 marigale 代码的实验,但止损为 30%。该错误似乎是由于逻辑错误而出现的,但我不太明白。也许这是语法

bots trading metatrader5
1个回答
0
投票

可能您的

request.sl
应该标准化为相关数字: NormalizeDouble(sl,_Digits)

© www.soinside.com 2019 - 2024. All rights reserved.