0
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
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---