I have tried to create an EA which does the following:
If open position moves in profit by 140 points, stop-loss moves in favour by 75 points, then, if open position moves in profit by 265 points, stop loss moves in favour again by 175 points from previous position.
Here is an example of the code:
// Define input parameters
input double firstTrigger = 140.0;
input double firstMove = 75.0;
input double secondTrigger = 265.0;
input double secondMove = 175.0;
// Define variables
double initialStopLoss;
double currentStopLoss;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
// Loop through open positions
for (int i = 0; i < PositionsTotal(); i++)
{
if (PositionSelect(i))
{
// Check if the position is in profit
if (PositionGetDouble(POSITION_PROFIT) > 0)
{
// Calculate the current profit in points
double profitInPoints = PositionGetDouble(POSITION_PROFIT) / SymbolInfoDouble(_Symbol, SYMBOL_POINT);
// Check for the second trigger first
if (profitInPoints >= secondTrigger)
{
currentStopLoss = PositionGetDouble(POSITION_SL) + secondMove * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
}
// Then check for the first trigger
else if (profitInPoints >= firstTrigger)
{
currentStopLoss = PositionGetDouble(POSITION_SL) + firstMove * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
}
// If neither trigger is reached, maintain the initial stop loss
else
{
currentStopLoss = initialStopLoss;
}
// Update the stop loss if it has changed
if (currentStopLoss != PositionGetDouble(POSITION_SL))
{
bool result = PositionModify(PositionGetInteger(POSITION_TICKET), currentStopLoss, PositionGetDouble(POSITION_TP));
if (result)
{
Print("Stop loss updated for Position #", IntegerToString(PositionGetInteger(POSITION_TICKET)));
}
else
{
Print("Failed to update stop loss for Position #", IntegerToString(PositionGetInteger(POSITION_TICKET)));
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans, const MqlTradeRequest &request, const MqlTradeResult &result)
{
// Check for a new position
if (trans.type == TRADE_TRANSACTION_DEAL_ADD && request.action == TRADE_ACTION_DEAL)
{
// Reset the trailing stop data for the new position
if (PositionSelect(Symbol(), request.volume))
{
initialStopLoss = PositionGetDouble(POSITION_SL);
currentStopLoss = initialStopLoss;
}
}
}
Hello,
I have tried to create an EA which does the following:
If open position moves in profit by 140 points, stop-loss moves in favour by 75 points,
then, if open position moves in profit by 265 points, stop loss moves in favour again by 175 points from previous position.
Here is an example of the code:
// Define input parameters input double firstTrigger = 140.0; input double firstMove = 75.0; input double secondTrigger = 265.0; input double secondMove = 175.0; // Define variables double initialStopLoss; double currentStopLoss; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTick() { // Loop through open positions for (int i = 0; i < PositionsTotal(); i++) { if (PositionSelect(i)) { // Check if the position is in profit if (PositionGetDouble(POSITION_PROFIT) > 0) { // Calculate the current profit in points double profitInPoints = PositionGetDouble(POSITION_PROFIT) / SymbolInfoDouble(_Symbol, SYMBOL_POINT); // Check for the second trigger first if (profitInPoints >= secondTrigger) { currentStopLoss = PositionGetDouble(POSITION_SL) + secondMove * SymbolInfoDouble(_Symbol, SYMBOL_POINT); } // Then check for the first trigger else if (profitInPoints >= firstTrigger) { currentStopLoss = PositionGetDouble(POSITION_SL) + firstMove * SymbolInfoDouble(_Symbol, SYMBOL_POINT); } // If neither trigger is reached, maintain the initial stop loss else { currentStopLoss = initialStopLoss; } // Update the stop loss if it has changed if (currentStopLoss != PositionGetDouble(POSITION_SL)) { bool result = PositionModify(PositionGetInteger(POSITION_TICKET), currentStopLoss, PositionGetDouble(POSITION_TP)); if (result) { Print("Stop loss updated for Position #", IntegerToString(PositionGetInteger(POSITION_TICKET))); } else { Print("Failed to update stop loss for Position #", IntegerToString(PositionGetInteger(POSITION_TICKET))); } } } } } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTradeTransaction(const MqlTradeTransaction &trans, const MqlTradeRequest &request, const MqlTradeResult &result) { // Check for a new position if (trans.type == TRADE_TRANSACTION_DEAL_ADD && request.action == TRADE_ACTION_DEAL) { // Reset the trailing stop data for the new position if (PositionSelect(Symbol(), request.volume)) { initialStopLoss = PositionGetDouble(POSITION_SL); currentStopLoss = initialStopLoss; } } }Here are the errors I have received:
Any advice on this would be greatly appreciated, thanks.