ProfitReachedSnippet

Download script
ProfitReachedSnippet

This script is designed to act as a safety and management layer for automated trading by monitoring whether a specific profit goal has been achieved. Its primary purpose is to prevent the system from opening new trades once your target profit for a particular timeframe and currency pair has been met, ensuring that your trading strategy adheres to predefined goals.

Core Functionality

The logic operates through three main phases: identification, verification, and execution control.

1. Contextual Identification The script identifies the specific market environment it is currently monitoring. It creates a unique identifier based on the currency pair being traded (the "Symbol") and the specific timeframe (such as 15-minute, Hourly, or Daily charts). This ensures that if you are trading multiple assets simultaneously, the profit target for one does not interfere with the targets of another.

2. Persistent Memory (Global Tracking) One of the key features of this script is its ability to "remember" status even if the software is restarted or the platform is closed. It utilizes global variables to store the profit status.

  • When the profit target is reached, the script saves a "success" flag to the system's memory.
  • If the script is restarted, it automatically checks this memory to see if the goal was already met, preventing the system from re-entering trades prematurely.

3. Trade Control Logic The script continuously monitors your open positions. The logic flows as follows:

  • Monitoring: It constantly checks if the current profit level meets your specified criteria.
  • Action: Once that profit target is hit, the script triggers a command to close any remaining open orders.
  • Prevention: After successfully closing the orders and marking the profit as "reached," the script creates a barrier. If the "Use Profit Reached" setting is active, the system will recognize this status and block any new trades from being opened until the status is manually reset.

Summary of Workflow

Essentially, this tool acts as an automated "circuit breaker." It watches your trades, closes them once they become profitable, and then puts a lock on the trading activity for that specific asset and timeframe to preserve your gains and prevent the system from restarting trade cycles automatically.

9 Views
0 Downloads
0 Favorites
ProfitReachedSnippet
//+------------------------------------------------------------------+
//|Code Snippet                                                      |
//+------------------------------------------------------------------+

// Inputs

bool UseProfitReached = true;

//+---------------------------------------------------+
//|General controls                                   |
//+---------------------------------------------------+
bool ProfitReached = false;

// Global Variable Names

string      gProfitReachedName;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   
       GetGlobalVars();
   
//----
   return(0);
  }
  
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//---- 
   ProfitReached = HandleOpenPositions();
   
   if ((UseProfitReached == true) && (ProfitReached == true)) return (0);
   
   
   ResetGlobals();
   // Add code to place new trades
   
     
//----
   return(0);
  }

void ResetGlobals()
{
   if (ProfitReached == true)
   {
      ProfitReached = false;
      SaveProfitReached (ProfitReached);
   }
}

//+------------------------------------------------------------------+
//| Handle Open Positions                                            |
//| Check if open position(s) have reached profit target               |
//+------------------------------------------------------------------+
bool HandleOpenPositions()
{
  bool myProfitReached = false;

// If profit target is reached set the proper variable to stop trading
   if (CheckForProfitReached())
   {
     myProfitReached = true;
     SaveProfitReached(ProfitReached);
     CloseOpenOrders();
     
   }
   return(myProfitReached);
}

bool CheckForProfitReached()
{
// Add your code to check for profit reached
}

void CloseOpenOrders()
{
// Add your code to close open orders
}

string tf2txt(int tf)
{
   switch(tf)
   {
      case PERIOD_M1: return("M1");
      case PERIOD_M5: return("M5");
      case PERIOD_M15: return("M15");
      case PERIOD_M30: return("M30");
      case PERIOD_H1: return("H1");
      case PERIOD_H4: return("H4");
      case PERIOD_D1: return("D1");
      case PERIOD_W1: return("W1");
      case PERIOD_MN1: return("MN");
   
   }
   return("??");
}



void GetGlobalVars()
{
   NameGlobalVars();
   InitGlobalVars();
   GetGlobalVarValues();
}
  
void GetGlobalVarValues()
{
   double temp;

// Booleans stored as double so convert to boolean
// > 0 true, < 0 false

    temp = GlobalVariableGet(gProfitReachedName);
    if (temp > 0) ProfitReached = true; else ProfitReached = false;
    
    
}

void InitGlobalVars()
{

// check variable before use

  if(!GlobalVariableCheck(gProfitReachedName))
    GlobalVariableSet(gProfitReachedName,-10);
    
}

void NameGlobalVars()
{
   string  prefix;

   prefix = "RH_" + Symbol() +  tf2txt(Period());
   gProfitReachedName = prefix +  "_ProfitReached";
   
}

void SaveProfitReached(bool myVal)
{
    if (myVal == true)
       GlobalVariableSet(gProfitReachedName,10.0);
    else
       GlobalVariableSet(gProfitReachedName,-10.0);
}


//+------------------------------------------------------------------+



Comments