ManageTakeProfit stockwet
Download scriptThis script is designed as an automated management tool for trading positions. Its primary purpose is to monitor active trades in real-time and execute specific actions—such as taking partial profits, adjusting risk levels, or closing losing trades—based on predefined price movements.
The logic is divided into three main functional areas:
1. Automated Profit Taking (ManageTrade)
The script monitors the profit of your open positions by tracking how many "pips" (price units) the market has moved in your favor. When the price reaches a specific target:
- Partial Closing: Instead of closing the entire trade at once, the script closes a predetermined portion of the position (based on a lot size you define).
- Dynamic Targets: Every time a target is successfully met and a partial profit is taken, the script automatically increases the next target. This allows the trade to "run," letting you lock in profits while keeping the remainder of the position open for potentially larger gains.
2. Dynamic Risk Management (MoveStops)
This function handles the protection of your trades by moving "Stop Loss" orders. It looks at how much profit a trade has accumulated:
- Trailing Protection: Once a trade reaches a specific initial success threshold, the script can automatically adjust your Stop Loss price.
- Safety Buffer: By moving the stop loss as the price progresses, the script helps ensure that once a trade is in the profit, you have a mechanism to protect those gains from being wiped out by a sudden market reversal.
3. Emergency Exit Strategy (KillTrade)
This serves as an automated "safety net" to manage risk during unfavorable market conditions.
- Hard Loss Limit: If you enable this feature, the script constantly monitors whether a trade is moving against you.
- Automatic Liquidation: If the price drops below a specific maximum loss limit that you have set, the script will immediately close the entire position. This is designed to prevent a losing trade from expanding further and impacting your overall account balance.
Execution Flow
The script operates on a continuous loop. Every time the market updates (the "tick"), it runs through these three checks in sequence:
- It checks if any safety stops need to be moved to protect current progress.
- It checks if any profit targets have been hit to lock in money.
- It checks if any trades have exceeded the maximum allowed loss and needs to be terminated.
//+------------------------------------------------------------------+
//| Copyright 2006, Taylor Stockwell |
//| stockwet@yahoo.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2006, Taylor Stockwell"
#property link "mailto:stockwet@yahoo.com"
extern int First_Target = 20;
extern int Target_Increment = 20;
extern double Close_Lots = 0.3;
extern bool Move_Stops = true;
extern int First_Stop = 10;
extern int Stop_Differential = 0;
extern bool Use_Max_Loss = true;
extern int Max_Loss = 19;
// 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.
int trangeVal = 5;
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+trangeVal;
// 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+trangeVal;
// 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+trangeVal;
// 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-1 && 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-1 && curPipValue <= trange)
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-Stop_Differential*Point, OrderTakeProfit(),0,Plum);
}
return(0);
}
}
}
}
void KillTrade()
{
// 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 totalorders2 = OrdersTotal(); // Calls a function to get all the open orders.
int maxloss = 2 + 15 + Point;
for(int j=0; j<totalorders2;j++)
{
OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
// 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(Use_Max_Loss)
{
//if(curPipValue <= -1 * maxloss)
if(curPipValue < -1 * (Max_Loss-1))
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
}
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(Use_Max_Loss)
{
//if(curPipValue <= -1 * maxloss)
if(curPipValue < -1 * (Max_Loss-1))
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
}
return(0);
}
}
}
}
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
MoveStops();
ManageTrade();
KillTrade();
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
ft=0;
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
MoveStops();
ManageTrade();
KillTrade();
}
Comments