Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

Hello

  1. User

    User: Hello Assistant: Hello! How can I assist you today? User: Add trailing stop Assistant: To add a trailing stop, you typically need to follow these general steps, depending on the trading platform you are using. Here’s a basic outline:

    1. Open your trading platform.
    2. Select the asset you want to trade.
    3. Choose the option to place a trade.
    4. Select the order type:
      • Look for the 'Trailing Stop' option, which might be under advanced order types.
    5. Set the trailing point:
      • This is usually a specific number of points (pips) or a percentage that you want the stop loss to trail behind the market price.
    6. Review and confirm the order.

    Make sure to check the specific instructions for your trading platform, as the process may vary. If you need more detailed steps for a specific trading platform, let me know! User: Add trailing stop to this EA

    //+------------------------------------------------------------------+ //|                                              FastScalpingEA.mq4   | //|                        Copyright 2023, Your Name                 | //|                                   https://www.yourwebsite.com     | //+------------------------------------------------------------------+ #property copyright "2023 Your Name" #property link      "https://www.yourwebsite.com" #property version   "1.00"

    input double LotSize = 0.1;      // Lot size input int StopLoss = 10;          // Stop loss in pips input int TakeProfit = 10;        // Take profit in pips input int MA_Period = 20;         // Moving Average period input double RSI_Period = 14;     // RSI period input double RSI_Overbought = 70; // RSI overbought level input double RSI_Oversold = 30;   // RSI oversold level input int Slippage = 3;           // Slippage

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

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

    //+------------------------------------------------------------------+ //| Expert tick function                                            | //+------------------------------------------------------------------+ void OnTick()   {    double ma = iMA(Symbol(), 0, MA_Period, 0, MODE_SMA, PRICE_CLOSE, 0);    double rsi = iRSI(Symbol(), 0, RSI_Period, PRICE_CLOSE, 0);    double currentPrice = Close[0];

       // Check for Buy signal    if (currentPrice > ma && rsi < RSI_Oversold)      {       if (OrderSelect(0, SELECT_BY_POS) == false || OrderType() != OP_BUY)         {          CloseOrders(OP_SELL);          OpenOrder(OP_BUY);         }      }

       // Check for Sell signal    if (currentPrice < ma && rsi > RSI_Overbought)      {       if (OrderSelect(0, SELECT_BY_POS) == false || OrderType() != OP_SELL)         {          CloseOrders(OP_BUY);          OpenOrder(OP_SELL);         }      }   }

    //+------------------------------------------------------------------+ //| Function to open orders                                          | //+------------------------------------------------------------------+ void OpenOrder(int orderType)   {    double price = (orderType == OP_BUY) ? Ask : Bid;    double sl = (orderType == OP_BUY) ? price - StopLoss * Point : price + StopLoss * Point;    double tp = (orderType == OP_BUY) ? price + TakeProfit * Point : price - TakeProfit * Point;

       int ticket = OrderSend(Symbol(), orderType, LotSize, price, Slippage, sl, tp, "Fast Scalping EA", 0, 0, clrBlue);    if (ticket < 0)      {       Print("Error opening order: ", GetLastError());      }   }

    //+------------------------------------------------------------------+ //| Function to close orders                                         | //+------------------------------------------------------------------+ void CloseOrders(int orderType)   {    for (int i = OrdersTotal() - 1; i >= 0; i--)      {       if (OrderSelect(i, SELECT_BY_POS) && OrderType() == orderType)         {          OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clrRed);         }      }   } //+------------------------------------------------------------------+ Assistant:

  2. Assistant

    To implement a trailing stop in your existing Expert Advisor (EA) code for MetaTrader 4 (MT4), you can follow these steps to modify the code. The trailing stop functionality should adjust the stop loss on open orders as the price moves in favor of the trade. Below is an updated version of your FastScalpingEA.mq4 code with a trailing stop feature added:

    //+------------------------------------------------------------------+
    //|                          FastScalpingEA.mq4                     |
    //|                          Copyright 2023, Your Name               |
    //|                          https://www.yourwebsite.com              |
    //+------------------------------------------------------------------+
    #property copyright "2023 Your Name"
    #property link      "https://www.yourwebsite.com"
    #property version   "1.01"
    
    input double LotSize = 0.1;      // Lot size
    input int StopLoss = 10;         // Initial stop loss in pips
    input int TakeProfit = 10;       // Take profit in pips
    input int TrailingStop = 10;     // Trailing stop in pips
    input int MA_Period = 20;        // Moving Average period
    input double RSI_Period = 14;    // RSI period
    input double RSI_Overbought = 70; // RSI overbought level
    input double RSI_Oversold = 30;   // RSI oversold level
    input int Slippage = 3;           // Slippage
    
    //+------------------------------------------------------------------+
    //| Expert initialization function                                    |
    //+------------------------------------------------------------------+
    int OnInit() {
        return (INIT_SUCCEEDED);
    }
    
    //+------------------------------------------------------------------+
    //| Expert deinitialization function                                  |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason) {
    }
    
    //+------------------------------------------------------------------+
    //| Expert tick function                                             |
    //+------------------------------------------------------------------+
    void OnTick() {
        double ma = iMA(Symbol(), 0, MA_Period, 0, MODE_SMA, PRICE_CLOSE, 0);
        double rsi = iRSI(Symbol(), 0, RSI_Period, PRICE_CLOSE, 0);
        double currentPrice = Close[0];
    
        // Check for Buy signal
        if (currentPrice > ma && rsi < RSI_Oversold) {
            if (OrderSelect(0, SELECT_BY_POS) == false || OrderType() != OP_BUY) {
                CloseOrders(OP_SELL);
                OpenOrder(OP_BUY);
            }
        }
    
        // Check for Sell signal
        if (currentPrice < ma && rsi > RSI_Overbought) {
            if (OrderSelect(0, SELECT_BY_POS) == false || OrderType() != OP_SELL) {
                CloseOrders(OP_BUY);
                OpenOrder(OP_SELL);
            }
        }
    
        // Update trailing stop for open orders
        UpdateTrailingStop();
    }
    
    //+------------------------------------------------------------------+
    //| Function to open orders                                           |
    //+------------------------------------------------------------------+
    void OpenOrder(int orderType) {
        double price = (orderType == OP_BUY) ? Ask : Bid;
        double sl = (orderType == OP_BUY) ? price - StopLoss * Point : price + StopLoss * Point;
        double tp = (orderType == OP_BUY) ? price + TakeProfit * Point : price - TakeProfit * Point;
    
        int ticket = OrderSend(Symbol(), orderType, LotSize, price, Slippage, sl, tp, "Fast Scalping EA", 0, 0, clrBlue);
        if (ticket < 0) {
            Print("Error opening order: ", GetLastError());
        }
    }
    
    //+------------------------------------------------------------------+
    //| Function to close orders                                          |
    //+------------------------------------------------------------------+
    void CloseOrders(int orderType) {
        for (int i = OrdersTotal() - 1; i >= 0; i--) {
            if (OrderSelect(i, SELECT_BY_POS) && OrderType() == orderType) {
                OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clrRed);
            }
        }
    }
    
    //+------------------------------------------------------------------+
    //| Function to update trailing stop                                  |
    //+------------------------------------------------------------------+
    void UpdateTrailingStop() {
        for (int i = OrdersTotal() - 1; i >= 0; i--) {
            if (OrderSelect(i, SELECT_BY_POS) && OrderType() == OP_BUY) {
                double newStopLoss = Bid - TrailingStop * Point;
                // Move stop loss if the new stop loss is greater
                if (OrderStopLoss() < newStopLoss) {
                    OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrBlue);
                }
            } else if (OrderSelect(i, SELECT_BY_POS) && OrderType() == OP_SELL) {
                double newStopLoss = Ask + TrailingStop * Point;
                // Move stop loss if the new stop loss is lower
                if (OrderStopLoss() > newStopLoss || OrderStopLoss() == 0) {
                    OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrRed);
                }
            }
        }
    }
    
    //+------------------------------------------------------------------+
    

    Explanation of Changes:

    1. Added Input for Trailing Stop: The variable TrailingStop specifies how many pips the trailing stop should be set.

    2. UpdateTrailingStop Function: This function checks open buy and sell orders, computes a new stop loss based on the trailing stop distance, and adjusts the stop loss if the new value is favorable (i.e., it moves closer to the market price).

    3. Called UpdateTrailingStop() in OnTick(): This ensures that the trailing stop logic is executed on each tick.

    Ensure you test the updated EA in a demo account before using it in a live trading environment. This will help you to verify that it works as intended.