I want to set profit and stop loss for many positions all at once!
Hello. This is chobitto fx.
Well, as the title suggests,EA CreateI made something that can set the profit and stop loss for multiple positions based on the source code output by
Posi-Posi disease therapy, perhaps. (?)
Now, without further ado, here are the functions.
//+--------------------------------------------------------------------------------------------------------+
//| Function to bulk change stop loss and take profit for positions and pending orders |
//| Process: Bulk change stop loss and take profit of all positions and limit orders for the specified symbol |
//| Arguments: symbol - currency pair |
//| newStopLoss - new stop loss value (specified in the price of the symbol) |
//| newTakeProfit - new take profit value (specified in the price of the symbol) |
//| orderType - order type (specify OP_BUY, OP_SELL. -1 means apply to all) |
//| Returns: none |
//+--------------------------------------------------------------------------------------------------------+
void updateAllStopLossAndTakeProfit(string symbol, double newTakeProfit, double newStopLoss, int orderType = -1)
{
// Change stop loss and take profit for open positions and pending orders
for (int i = 0; i < OrdersTotal(); i++)
{
// Select order
if (OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == symbol)
{
int currentOrderType = OrderType();
// Process only specified order types (orderType -1 means all)
if (orderType == -1 || currentOrderType == orderType)
{
// If new stop loss or take profit differs from current value, process
if ((newStopLoss == 0 || OrderStopLoss() != newStopLoss) ||
(newTakeProfit == 0 || OrderTakeProfit() != newTakeProfit))
{
// If open position
if ((currentOrderType == OP_BUY &&
(newStopLoss == 0 || newStopLoss < Bid) &&
(newTakeProfit == 0 || newTakeProfit > Bid)) ||
(currentOrderType == OP_SELL &&
(newStopLoss == 0 || newStopLoss > Ask) &&
(newTakeProfit == 0 || newTakeProfit < Ask)))
{
bool result = OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, newTakeProfit, 0);
if (result)
{
Print("Successfully updated Stop Loss and Take Profit for order #", OrderTicket());
}
else
{
Print("Failed to update Stop Loss and Take Profit for order #", OrderTicket(), ". Error: ", GetLastError());
}
}
// If pending limit order
else if ((currentOrderType == OP_BUYLIMIT &&
(newStopLoss == 0 || newStopLoss < Bid) &&
(newTakeProfit == 0 || newTakeProfit > Bid)) ||
(currentOrderType == OP_SELLLIMIT &&
(newStopLoss == 0 || newStopLoss > Ask) &&
(newTakeProfit == 0 || newTakeProfit < Ask)))
{
bool result = OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, newTakeProfit, 0);
if (result)
{
Print("Successfully updated Stop Loss and Take Profit for pending order #", OrderTicket());
}
else
{
Print("Failed to update Stop Loss and Take Profit for pending order #", OrderTicket(), ". Error: ", GetLastError());
}
}
else
{
Print("Invalid Stop Loss or Take Profit level for order #", OrderTicket());
}
}
}
}
}
}Here's how to use it.
For a buying position
updateAllStopLossAndTakeProfit("USDJPY", 160, 159, OP_BUY);
For a selling position
updateAllStopLossAndTakeProfit("USDJPY", 159, 160, OP_SELL);
Simple, right.
* Positions that are pending will also be changed in bulk.
EA Createis in line with the rules, so the following might also be okay.
It might be nice to input settings from parameters as an input.
input double newBuyProfitPrice = 0; // New take profit price for buy positions input double newBuyStopLossPrice = 0; // New stop loss price for buy positions input double newSellProfitPrice = 0; // New take profit price for sell positions input double newSellStopLossPrice = 0; // New stop loss price for sell positions
// Update take profit and stop loss for buy positions updateAllStopLossAndTakeProfit(Symbol(), newBuyProfitPrice, newBuyStopLossPrice, OP_BUY); // Update take profit and stop loss for sell positions updateAllStopLossAndTakeProfit(Symbol(), newSellProfitPrice, newSellStopLossPrice, OP_SELL);
See you again.
File download is below.
× ![]()