Skip to content

Instantly share code, notes, and snippets.

View currencysecrets's full-sized avatar

Ryan currencysecrets

View GitHub Profile
@currencysecrets
currencysecrets / drawdownLimits.mq4
Last active August 29, 2023 07:00
How to set your drawdown limits to prevent excessive loss of capital in your MetaTrader script
//+------------------------------------------------------------------+
//| My Automated Trading EA.mq4
//| Copyright 2013, Ryan Sheehy
//| http://www.currencysecrets.com
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, Ryan Sheehy"
#property link "http://www.currencysecrets.com"
//--- input parameters
@currencysecrets
currencysecrets / arrayUtility.mq4
Last active February 28, 2021 20:52
Handy Array Utility functions for MetaTrader 4
void flushArray( int& arr[] ) {
ArrayInitialize( arr, 0 );
ArrayResize( arr, 0 );
}
// append new element to end of int array
void intArrayPush( int& arr[], int elem ) {
int size = ArraySize( arr );
ArrayResize( arr, size + 1 );
arr[ size ] = elem;
@currencysecrets
currencysecrets / WinFile.mqh
Created April 27, 2014 19:25
The famous WinFile.mqh file from MTIntelligence.com
//+------------------------------------------------------------------+
//| Copyright © 2009 MTIntelligence.com |
//| http://www.mtintelligence.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009 MTIntelligence.com"
#property link "http://www.mtintelligence.com"
// *************************************************************************************
//
string VERSION = "TL BO 1.0";
// external global variables
extern int EXT_SWINGS = 2; // swing level, 0 is lowest, 2 is most common
extern int EXT_NO_SWING_LMT = 12; // don't consider the latest X bars as swing points to draw trend lines
extern int EXT_ATR = 300; // ATR(x)
extern double EXT_ATR_MAX_SLOPE = 0.05; // multiple of ATR that would be considered maximum slope
extern double EXT_ATR_BUFFER = 0.05; // multiple of ATR to add to low/high for it to be considered a "touch"
extern int EXT_NULL_CLOSES = 0; // number of closes beyond trend line to nullify it as a valid trend line
extern int EXT_NUM_TOUCHES = 3; // (inclusive) number of touches needed to be considered a valid trend line
@currencysecrets
currencysecrets / exitNow.mq4
Last active October 31, 2016 07:58
Exit at market strategy
// this function exits at market, but needs the following params:
// sym = currency symbol
// tkt = trades' ticket number
// lots = trades' lot size
// p = price to exit at
bool exitNow( string sym, int tkt, double lots, double p ) {
// check if it's okay to exit trade
int trade = checkIsTradeAllowed();
// place trade if ok to place order
if ( trade > 0 ) {
@currencysecrets
currencysecrets / trendline-orders.mq4
Last active August 22, 2018 13:10
Entries and exits now in one script
string VERSION = "TL BO 1.0";
extern int EXT_ATR = 300; // ATR(x)
extern double EXT_ATR_INI_STOP = 0.1; // additional buffer to add beyond the initial stop
extern int EXT_LOOKBACK = 3; // number of bars to find highest high/lowest low
extern double EXT_RISK_MINAMT = 50; // dollar value of the minimum amount to risk per trade
extern double EXT_RISK_DIVISOR = 10; // AccountBalance/X = risk per trade
extern int EXT_MAX_SLIP = 10; // maximum points of slippage allowed for order 10 = 1 pip
extern double EXT_ATR_TRAILSTOP = 2; // multiple for trailing the market ATR(EXT_ATR) * X
@currencysecrets
currencysecrets / getTrailingStop.mq4
Last active October 31, 2016 07:59
Trailing stop method.
// this function requires the following parameters:
// sym = currency
// op = opening price of the current trade
// sl = stop loss price of the current trade
// ot = opening datetime of the current trade
// per = active chart's period - for calculating ATR distance properly
// type = order type of the current trade
double getTrailingStop( string sym, double op, double sl, datetime ot, int per, int type ) {
double temp;
// make the result for this function initially equal the stop loss
@currencysecrets
currencysecrets / trendline-orders.mq4
Last active October 31, 2016 07:59
Trend line ordering demo
string VERSION = "TL BO 1.0";
extern int EXT_ATR = 300; // ATR(x)
extern double EXT_ATR_INI_STOP = 0.1; // additional buffer to add beyond the initial stop
extern int EXT_LOOKBACK = 3; // number of bars to find highest high/lowest low
extern double EXT_RISK_MINAMT = 50; // dollar value of the minimum amount to risk per trade
extern double EXT_RISK_DIVISOR = 10; // AccountBalance/X = risk per trade
extern int EXT_MAX_SLIP = 10; // maximum points of slippage allowed for order 10 = 1 pip
// global variables
@currencysecrets
currencysecrets / newTrendline.mq4
Last active October 31, 2016 07:59
New trend line script, returns the closest upper trend line and lowest trend line
extern int EXT_SWINGS = 2; // swing level, 0 is lowest, 2 is most common
extern int EXT_NO_SWING_LMT = 12; // don't consider the latest X bars as swing points to draw trend lines
extern int EXT_ATR = 300; // ATR(x)
extern double EXT_ATR_MAX_SLOPE = 0.05; // multiple of ATR that would be considered maximum slope
extern double EXT_ATR_BUFFER = 0.05; // multiple of ATR to add to low/high for it to be considered a "touch"
extern int EXT_NUM_CONSEC_CLOSES = 4; // number of consecutive closes beyond trend line to break
extern int EXT_NUM_TOUCHES = 3; // (inclusive) number of touches needed to be considered a valid trend line
extern int EXT_RES_COLOR = IndianRed; // horizontal resistance line http://docs.mql4.com/constants/colors
extern int EXT_SUP_COLOR = PaleGreen; // horizontal support line http://docs.mql4.com/constants/colors
extern int EXT_SLOPE_RES_COLOR = LightSalmon; // sloping resistance line http://docs.mql4.com/constants/colors
@currencysecrets
currencysecrets / getCurrencyStrength.mq4
Last active February 9, 2018 09:42
This function helps to determine which of the currencies listed in the ccyList. It uses the metric you've defined in your function and returns a score for the currency you're chart is currently active on.
extern int EXT_METRIC = 30; // bars for metric
extern int EXT_METRIC_MIN = 1; // minimum metric value needed for each individual currency
// this function returns the active currencies "strength" value
// it displays the strength value on the active chart's upper left portion of the screen (using Comment function)
double getTrend() {
// get the active chart's symbol
string sym = Symbol();