Target_Profit_v1

Download script
Author: Jacob Yego
Target_Profit_v1

This automated script is designed to act as a profit and loss management tool for your trading platform. Its primary function is to monitor your active trades in real-time and automatically close them once they reach a specific price threshold defined by the user.

Core Functionality

The script works by continuously scanning all open positions on your current trading symbol. It distinguishes between "Buy" and "Sell" orders and calculates the current profit or loss for each based on the difference between the entry price and the current market price.

Key Logic Components:

  • Customizable Profit Targets: You can set a specific numerical value (the "TargetProfit") to define how much movement you want to see before the script takes action.
  • Dual-Mode Operation (Switchable Limits): The script includes a toggle feature that allows it to operate in two different ways:
    • Profit Taking: When the switch is off, the script monitors for your trades to reach a positive profit goal. Once the price moves in your favor by the designated amount, it closes the trade automatically.
    • Loss Limiting: When the switch is enabled, the logic reverses to act as a "Stop Loss." It monitors for the trade to move against you by the designated amount and closes the position to prevent further losses.
  • Automated Execution: Once the price hits your target (either profit or loss), the script immediately executes a closing command. It handles this automatically so you do not have to manually monitor the charts 24/7.
  • Feedback System: To keep you informed, the script provides instant feedback:
    • If a trade is closed successfully, it triggers an on-screen alert notification.
    • If there is any issue preventing a trade from closing (such as a connection error), it logs the specific error details in the background system log for your review.

Summary of Workflow

Essentially, this tool serves as an automated "exit strategy." You tell the script how many points you are willing to win or lose; the script then watches the market for you, and the moment that target is hit, it locks in your result by closing the trade immediately.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
Miscellaneous
It issuies visual alerts to the screen
16 Views
0 Downloads
0 Favorites
Target_Profit_v1
                            //---------------------------------------------//
                            //                    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