Skip to content

Instantly share code, notes, and snippets.

@FaberSanZ
Last active February 6, 2025 19:56
Show Gist options
  • Save FaberSanZ/3d29dcb103ddf57e577819730529f829 to your computer and use it in GitHub Desktop.
Save FaberSanZ/3d29dcb103ddf57e577819730529f829 to your computer and use it in GitHub Desktop.
//+------------------------------------------------------------------+
//| EA with Auto SL, Pips on Screen and Improved Design |
//| Copyright (c) Faber Leonardo. https://github.com/FaberSanZ |
//+------------------------------------------------------------------+
#property strict
input double SL_Pips = 1.0; // Stop Loss in pips
input string UserName = "Mr Faber SanZ"; // Customizable name
void OnTick()
{
int totalPositions = PositionsTotal();
double totalProfitPips = 0; // pip accumulator
double totalRiskReward = 0; // RR Accumulator
int countRR = 0; // Counter to calculate average RR
for (int i = totalPositions - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if (PositionSelectByTicket(ticket))
{
double priceEntry = PositionGetDouble(POSITION_PRICE_OPEN);
double currentPrice = PositionGetDouble(POSITION_PRICE_CURRENT);
double stopLoss = 0;
double slDistance = SL_Pips * _Point * MathPow(10, _Digits - SymbolInfoInteger(Symbol(), SYMBOL_DIGITS));
double profitPips = 0; // Pips of current position
double riskReward = 0; // RR of current position
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
stopLoss = priceEntry + slDistance; // SL 1 pip up
profitPips = (priceEntry - currentPrice) / _Point; // Pips on a sale
profitPips = profitPips / 10;
}
else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
stopLoss = priceEntry - slDistance; // SL 1 pip down
profitPips = (currentPrice - priceEntry) / _Point; // Pips on a buy
profitPips = profitPips / 10;
}
// Acumular los pips de todas las posiciones
totalProfitPips += profitPips;
// Calculate Risk/Reward Ratio (RR) for the current position
double slLevel = PositionGetDouble(POSITION_SL);
if (slLevel != 0)
{
double risk = MathAbs(priceEntry - slLevel);
double reward = MathAbs(currentPrice - priceEntry);
if (risk > 0)
{
riskReward = reward / risk;
totalRiskReward += riskReward; // Accumulate RR
countRR++; // Increase RR counter
}
}
// Adjust SL if not set correctly
if (PositionGetDouble(POSITION_SL) == 0 || PositionGetDouble(POSITION_SL) != stopLoss)
{
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);
request.action = TRADE_ACTION_SLTP;
request.position = ticket;
request.sl = stopLoss;
request.tp = PositionGetDouble(POSITION_TP);
request.symbol = Symbol();
if (!OrderSend(request, result))
Print("Error modifying SL: ", result.comment);
else
Print("Stop Loss set correctly.");
}
}
}
//TODO:
// Calculate the average RR
double average_rr = (totalProfitPips > 0) ? totalProfitPips * (SL_Pips / 10) : 0;
string nombreCompleto = UserName + " - Stride Markets";
string pipsText = "Pips: " + DoubleToString(totalProfitPips, 1);
string rrText = "RR: " + DoubleToString(average_rr, 2);
// Get the size of the chart window
long chartId = ChartID();
int screen_width = (int)ChartGetInteger(chartId, CHART_WIDTH_IN_PIXELS);
int screen_height = (int)ChartGetInteger(chartId, CHART_HEIGHT_IN_PIXELS);
int xOffset = screen_width * 0.40; // 40% from the left
int xOffset_data = screen_width * 0.90; // 90% from left for info
int xOffset_data_copy = screen_width * 0.80; // 90% from left for Copyright
int yOffset = screen_height * 0.95; //
create_text("NameText", nombreCompleto, 18, clrBlack, xOffset, 25);
create_text("InfoText", pipsText, 12, clrBlue, xOffset_data, 50);
create_text("CopyText", "Copyright (c) Faber Leonardo. https://github.com/FaberSanZ", 8, clrBlack, xOffset_data_copy, yOffset);
int spread = (int)SymbolInfoInteger(Symbol(), SYMBOL_SPREAD) * _Point;
string spreadText = "Spread: " + IntegerToString(spread);
// Show spread on chart
create_text("SpreadText", spreadText, 12, clrRed, xOffset_data, 80); // Mostrar en gráfico
}
void create_text(string objName, string text, int fontSize, color fontColor, int xOffset, int yOffset)
{
// Delete object if it already exists
if (ObjectFind(0, objName) >= 0)
ObjectDelete(0, objName);
// Create the new text object
if (!ObjectCreate(0, objName, OBJ_LABEL, 0, 0, 0))
{
Print("EError creating text object:", objName);
return;
}
// Configurar las propiedades del texto
ObjectSetInteger(0, objName, OBJPROP_CORNER, CORNER_LEFT_UPPER);
ObjectSetInteger(0, objName, OBJPROP_XDISTANCE, xOffset);
ObjectSetInteger(0, objName, OBJPROP_YDISTANCE, yOffset);
ObjectSetInteger(0, objName, OBJPROP_FONTSIZE, fontSize);
ObjectSetInteger(0, objName, OBJPROP_COLOR, fontColor);
ObjectSetString(0, objName, OBJPROP_TEXT, text);
}
@FaberSanZ
Copy link
Author

Update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment