0% found this document useful (0 votes)
166 views3 pages

Swing Strategy Mql5

This document outlines a custom trading indicator for analyzing market trends by identifying swing highs and lows. It includes input parameters for lookback periods, risk management, and label display options. The code contains functions for calculating position size based on risk and for detecting pivot points in market data.

Uploaded by

aa7456lie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views3 pages

Swing Strategy Mql5

This document outlines a custom trading indicator for analyzing market trends by identifying swing highs and lows. It includes input parameters for lookback periods, risk management, and label display options. The code contains functions for calculating position size based on risk and for detecting pivot points in market data.

Uploaded by

aa7456lie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#property copyright "Your Name"

#property link "Your Website"


#property version "1.00"
#property indicator_chart_window

//--- Input Parameters


input int lookbackLeft = 5; // Lookback Left Bars
input int lookbackRight = 5; // Lookback Right Bars
input bool showLabels = true; // Show Labels
input double riskPercent = 1.0; // Risk Per Trade (%)
input double stopLossPercent = 2.0; // Stop Loss (%)
input double takeProfitPercent = 4.0; // Take Profit (%)

//--- Global Variables


double highPivot, lowPivot;
datetime highPivotTime, lowPivotTime;

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping

//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
int start = prev_calculated > 0 ? prev_calculated - 1 : lookbackLeft +
lookbackRight;

for(int i = start; i < rates_total; i++)


{
//--- Find Swing High
highPivot = FindPivotHigh(high, i, lookbackLeft, lookbackRight);
if(!isNaN(highPivot))
{
highPivotTime = time[i];
if(showLabels)
{
string labelText = "H";
ObjectCreate("HighLabel_" + TimeToString(highPivotTime), OBJ_TEXT, 0,
highPivotTime, highPivot + SymbolInfoDouble(Symbol(), SYMBOL_POINT) * 10);
ObjectSetString("HighLabel_" + TimeToString(highPivotTime),
OBJPROP_TEXT, labelText);
ObjectSetInteger("HighLabel_" + TimeToString(highPivotTime),
OBJPROP_COLOR, clrRed);
ObjectSetInteger("HighLabel_" + TimeToString(highPivotTime),
OBJPROP_ANCHOR, ANCHOR_TOP);
}
}

//--- Find Swing Low


lowPivot = FindPivotLow(low, i, lookbackLeft, lookbackRight);
if(!isNaN(lowPivot))
{
lowPivotTime = time[i];
if(showLabels)
{
string labelText = "L";
ObjectCreate("LowLabel_" + TimeToString(lowPivotTime), OBJ_TEXT, 0,
lowPivotTime, lowPivot - SymbolInfoDouble(Symbol(), SYMBOL_POINT) * 10);
ObjectSetString("LowLabel_" + TimeToString(lowPivotTime), OBJPROP_TEXT,
labelText);
ObjectSetInteger("LowLabel_" + TimeToString(lowPivotTime),
OBJPROP_COLOR, clrGreen);
ObjectSetInteger("LowLabel_" + TimeToString(lowPivotTime),
OBJPROP_ANCHOR, ANCHOR_BOTTOM);
}
}
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Find Pivot High Function |
//+------------------------------------------------------------------+
double FindPivotHigh(const double &high[], int currentIndex, int lookbackLeft, int
lookbackRight)
{
for(int i = currentIndex - lookbackLeft; i <= currentIndex + lookbackRight; i++)
{
if(i == currentIndex)
continue;
if(i < 0 || i >= rates_total)
return NaN;
if(high[i] > high[currentIndex])
return NaN;
}
return high[currentIndex];
}
//+------------------------------------------------------------------+
//| Find Pivot Low Function |
//+------------------------------------------------------------------+
double FindPivotLow(const double &low[], int currentIndex, int lookbackLeft, int
lookbackRight)
{
for(int i = currentIndex - lookbackLeft; i <= currentIndex + lookbackRight; i++)
{
if(i == currentIndex)
continue;
if(i < 0 || i >= rates_total)
return NaN;
if(low[i] < low[currentIndex])
return NaN;
}
return low[currentIndex];
}
//+------------------------------------------------------------------+
//| Calculate Position Size Function |
//+------------------------------------------------------------------+
double CalculatePositionSize(double entryPrice, double stopPrice)
{
double riskAmount = AccountInfoDouble(ACCOUNT_EQUITY) * riskPercent / 100;
double priceDiff = MathAbs(entryPrice - stopPrice);
double posSize = riskAmount / priceDiff;
return posSize;
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
//+------------------------------------------------------------------+

You might also like