This script is designed to automate the management of your trading positions by automatically closing them once they reach a specific profit or loss milestone. It acts as a "set and forget" safety net, monitoring your active trades in real-time and executing an exit command the moment your predefined price target is hit.
Core Functionality
The tool works by constantly scanning your open positions on the current financial instrument you are trading. Instead of requiring you to watch the charts manually, it calculates the difference between your entry price and the current market price.
Key Features and Logic:
- Customizable Targets: You can define a specific numerical value (the "Target Profit") that determines how much price movement is required before the script takes action.
- Dual-Mode Operation: The script features a versatile switch that allows you to toggle between two primary modes of operation:
- Profit Taking: In this standard mode, the script monitors your trades and closes them automatically once they reach a specific positive profit target.
- Loss Limitation (Stop Loss): By flipping a single setting, the logic reverses. In this mode, the script identifies when a trade has moved against you by a certain amount and closes it instantly to preserve your capital.
- Smart Trade Recognition: The logic is intelligent enough to distinguish between "Buy" and "Sell" orders. It understands that for a Buy order, profit is made as the price goes up, while for a Sell order, profit is made as the price goes down. It adjusts its calculations automatically based on the type of trade you have opened.
- Execution & Confirmation: When a target is hit, the script attempts to close the trade immediately at the current market price. If the closure is successful, it provides an immediate on-screen alert notification to keep you informed. If any technical issues occur during the closing process, it logs the specific error details for your review.
Summary
Essentially, this tool removes the emotional element of trading by providing an automated "exit door." Whether you want to lock in gains or protect yourself from heavy losses, this script ensures that your trades are closed at exactly the price level you intended, regardless of whether you are looking at the screen at that moment.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//---------------------------------------------//
// Target Profit script
// Copyright Jacob Yego //
//---------------------------------------------//
#property copyright "Jacob Yego"
#property link ""
extern int TargetProfit = 10; // Your Profit target
bool loswitch=false; //switch to true for losslimit
double CurProfit;
int Slippage=5;
int cnt,laser;
int ls=0;
int start()
{
if (loswitch==true) {ls=-1;} else {ls=1;}
int total = OrdersTotal();
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol()==Symbol() )
{
if(OrderType()==OP_BUY)
{
CurProfit=Bid-OrderOpenPrice() ;
if (loswitch && CurProfit<= (TargetProfit*ls*Point) || CurProfit>=(TargetProfit*ls*Point))
{
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,White);
laser=GetLastError();
if(laser==0)
{
Alert(" Order : ", OrderTicket() , "Closed Succesfully" );
Sleep(1000);
}
else
{
Print("--ERROR-- Closing BUY Bid=",Bid,OrderTicket()," error=",laser);
}
}
}
if(OrderType()==OP_SELL)
{
CurProfit=OrderOpenPrice()-Ask;
if (loswitch && CurProfit<= (TargetProfit*ls*Point) || CurProfit>=(TargetProfit*ls*Point))
{
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White);
laser=GetLastError();
if(laser==0)
{
Alert( " Order :", OrderTicket() ," Closed Succesfully" );
Sleep(1000);
}
else
{
Print("--ERROR-- Closing SELL Ask=",Ask,OrderTicket()," error=",laser);
}
}
}
}
}
return(0);
}
Comments