TakeProfit EA HiLoTradeAway
Download scriptThis script is designed to automate the management of active trades on the MetaTrader platform, specifically focusing on two key strategies: incremental profit taking and automated stop-loss adjustments. By using predefined parameters, it helps manage trade positions dynamically as market prices fluctuate.
Core Functionalities
1. Dynamic Profit Taking The script monitors open trades to see if they have reached specific price targets. Instead of closing an entire position at once, it is designed to close a predetermined portion of the trade (based on a specific lot size) when a target is hit.
A unique feature of this logic is its "scaling" capability: every time a profit target is successfully met and a portion of the trade is closed, the script automatically increases the distance required for the next target. This means that as the trade progresses in your favor, the script moves the goalposts further out, allowing you to capture more significant price movements over time.
2. Automated Stop-Loss Management The script includes a secondary mechanism to protect gains once a trade has moved into a profitable position. If enabled, it monitors the distance between the current market price and your entry price. Once the trade reaches a specific initial profit threshold, the script can automatically modify the "Stop Loss" level.
This ensures that if the market reverses suddenly, the trade is closed at a guaranteed profit level rather than returning to the original break-even point or resulting in a loss.
How it Works in Practice
- Monitoring: The script constantly scans all active trades on the current symbol.
- Execution: It calculates the real-time distance (in pips) of each trade from its starting price.
- Action:
- If the price hits your first target, it closes a small piece of the trade and recalculates a new, larger target for the remainder.
- Simultaneously, it checks if the trade is far enough in profit to "lock in" that progress by moving the stop-loss order forward.
Summary of Benefits
For a trader, this logic provides a disciplined way to manage "runners"—trades that are moving in the right direction but require active management. It removes the emotional difficulty of deciding when to take profits and how to protect gains, automating those decisions based on the specific increments you set at the beginning.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| Copyright 2006, Taylor Stockwell |
//| stockwet@yahoo.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2006, Taylor Stockwell"
#property link "mailto:stockwet@yahoo.com"
extern int First_Target = 10;
extern int Target_Increment = 6;
extern double Close_Lots = 0.1;
extern int First_Stop = 10;
extern int Stop_Differential = 0;
extern bool Move_Stops = true;
// Global Variables
int ft; //This variable will be incremented by the target increment after every successful take profit.
int fs; // This variable will change as the trade progresses if Move_Stops is true.
int curPipValue; // Checks to see the difference between the open price and current price in pips.
void ManageTrade()
{
// Create another set of variables used in the code.
// These variables provide an abbreviated way of calling variables.
// Mostly, they are more easily written in the code than using the user
// friendly external variables.
int ti=Target_Increment; //This variable will not change.
double cl=Close_Lots; // This variable will not change.
// Additional variables used in the code.
int trange = 0; // Use a range versus a specific pip amount as prices may get jumped.
int totalorders = OrdersTotal(); // Calls a function to get all the open orders.
// Starts initial "for" loop. This loop will go through all the open orders.
// If a target is reached, the script will close a portion of the trade and, possibly, move the stop loss.
for(int j=0; j<totalorders;j++)
{
OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
if(ft == 0) ft = First_Target;
// if(OrderType() == OP_SELL && OrderSymbol()==Symbol())
// {
if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{
// Get the current pip amount on a buy order.
curPipValue = (Bid - OrderOpenPrice())/Point;
if(ft == 0) ft = First_Target;
trange=ft+5;
// Check if the current pip amount is within the appropriate range
if(curPipValue >= ft-1 && curPipValue <= trange)
{
// First, if target is reached, then take profit.
if(OrderClose(OrderTicket(), cl, Bid, 3, YellowGreen))
{
// Increment First_Target
ft += ti;
Comment(ft);
return(0);
}
}
}
else if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{
// Get the current pip amount on a sell order.
curPipValue = (OrderOpenPrice()-Ask)/Point;
if(ft == 0) ft = First_Target;
trange=ft+5;
// Check if the current pip amount is within the appropriate range
if(curPipValue >= ft-1 && curPipValue <= trange)
{
if(OrderClose(OrderTicket(), cl, Ask, 3, YellowGreen))
{
// Increment First_Target
ft +=ti;
Comment(ft);
return(0);
}
}
}
}
}
void MoveStops()
{
// Starts initial "for" loop. This loop will go through all the open orders.
// If a target is reached, the script will move the stop loss.
int sd=Stop_Differential; // This variable will not change.
// Additional variables used in the code.
int trange = 0; // Use a range versus a specific pip amount as prices may get jumped.
int totalorders2 = OrdersTotal(); // Calls a function to get all the open orders.
for(int j=0; j<totalorders2;j++)
{
OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
if(fs == 0) fs = First_Stop;
trange=First_Stop+5;
// if(OrderType() == OP_SELL && OrderSymbol()==Symbol())
// {
if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{
// Get the current pip amount on a buy order.
curPipValue = (Bid - OrderOpenPrice())/Point;
// Check if the current pip amount is within the appropriate range
if(Move_Stops)
{
if(curPipValue >= First_Stop && curPipValue <= trange)
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+Stop_Differential*Point, OrderTakeProfit(),0,Plum);
}
return(0);
}
}
else if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{
// Get the current pip amount on a buy order.
curPipValue = (OrderOpenPrice()-Ask)/Point;
// Check if the current pip amount is within the appropriate range
if(Move_Stops)
{
if(curPipValue >= First_Stop && curPipValue <= trange)
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-Stop_Differential*Point, OrderTakeProfit(),0,Plum);
}
return(0);
}
}
}
}
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
MoveStops();
ManageTrade();
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
ft=0;
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
MoveStops();
ManageTrade();
}
Comments