Set Fixed SL and TP

Applewhite

Newbie
Apr 10, 2024
1
0
1
21
Hi I have a problem I have to attach it to the chart everytime after I placed a trade to have a fixed sl and tp, otherwise it doesnt work
 

eodai

Newbie
Apr 10, 2024
1
0
1
29
Please Help!

I'm trying to figure this out for a while now. I've not been able to get through with it.

I want to loop through the recently closed trade and if it's a sell order and it's stoploss was hit, a buy trade should triggers immediately.
The issue now is " a buy trade open when a sell trade is still active " How do I fix it to open when the sell trade closes?

Here's my code below!

Code:
    // Loop through all recently closed positions
    for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
        {
            // Check if it's a sell order and its stoploss was hit
            if (OrderType() == OP_SELL && OrderStopLoss() > 0 && OrderStopLoss() >= OrderClosePrice())
            {     
                // Place buy trade
                ticket = OrderSend(Symbol(), OP_BUY, lotSize, entryPrice, 3, 0, 0, "Buy order placed by MyScript", 0, 0, clrNONE);
                
                // Check for errors
                if (ticket < 0)
                {
                    Print("Error placing buy order: ", GetLastError());
                }
                else
                {
                    Print("Buy order placed successfully at price: ", entryPrice);
                }
            }
        }
    }
 

ShortNote

Trader
Jul 29, 2024
1
0
6
31
The Set Fixed SL and TP script has been updated to version 1.01 to introduce numerous changes:
  • Added an MT5 version of the script.
  • Added an option to apply stop-loss and take-profit based on the current price rather than open price.
  • Added an option to work with pending orders as well.
  • Added an option to set SL and TP not only for the current trading symbol.
  • Added an option to skip either SL or TP by setting the respective input parameter to zero.
  • Added a filter based on trade's direction.
  • Changed the script to work based on points rather than pips.
  • Removed the Slippage input parameter (wasn't used).
  • Refactored the MQL4 code.
hello, thanks for this script, but please is there any other way this scripts works like an EA, so when its been added to a chart regardless of a new position let it automatically work for it, instead of the regular way of applying it to the chart again ater putting on a new position.
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,240
1,507
144
Odesa
www.earnforex.com
hello, thanks for this script, but please is there any other way this scripts works like an EA, so when its been added to a chart regardless of a new position let it automatically work for it, instead of the regular way of applying it to the chart again ater putting on a new position.
No, its code should rewritten to work as an EA.
 

toshin

Newbie
Oct 24, 2024
4
0
1
39
Hello, is it possible to set TPs according to this rule:
I want to risk 1000 USD from entry to SL.
At Target 1:1 = 1000 USD, it closes 50%.
At target 1:2 = it closes remaining of the positions.