Skip to content

Instantly share code, notes, and snippets.

View currencysecrets's full-sized avatar

Ryan currencysecrets

View GitHub Profile
@currencysecrets
currencysecrets / breakouts.md
Created August 9, 2013 12:43
Breakout Principles. What are the underlying principles behind a breakout bar?

Setups

  1. Price range must exceed average price range.
  2. Price must break into an uncommon area.
  3. Price must open near one extreme and close near the other.
  4. Volume must exceed average volume.

Confirmations

  • Price continues to advance in the direction of the breakout.
/**
v2.0
This function checks to determine that a breakout candle has occurred.
A breakout candle contains the majority of it's body in the candle (no long tails).
*/
bool isBO( double o, double h, double l, double c, double volt, bool isLong ) {
if ( MathAbs( o - c ) > volt ) {
if ( MathAbs( o - c ) / ( h - l ) > 0.8 ) {
if ( c > o && isLong ) {
return( true );
@currencysecrets
currencysecrets / getPinBar.mq4
Created July 7, 2013 10:13
This function checks to see whether the last "lookBack" bars when added together form a pin bar. If so it will return the high (for bullish patterns) or the low (for bearish patterns) of the pin bar - if no pin bar was formed a 0 will be returned. Parameters: + int per = period of price to obtain (eg. PERIOD_H4) + int lookBack = number of bars t…
/* v1.0
* this will loop through the last X bars to determine whether there has
* been a pin bar (it tests only the last bar = n-1, and folds the last two and three)
*/
int getPinBar( int per, int lookBack, bool dir ) {
string sym = Symbol();
int h, l;
if ( lookBack <= 1 ) {
lookBack = 1;
}
@currencysecrets
currencysecrets / isPinBar.mq4
Last active December 19, 2015 10:49
This function returns a boolean result on passing in an open, high, low and close price as well as the direction (true = bullish; false = bearish).
/** v6.0
* A pin bar is a hammer like formation where the body of the candle
* is at one extreme of the entire candle and the wick making up the
* remainder of the candle. The body of the candle should be no more
* than 1/3 of the entire candle, with the remaining wick taking up
* no more than 2/3 of the remainder of the body.
*/
bool isPin( double o, double h, double l, double c, bool isLong ) {
double hx, lx, rng = h - l, pct = 1/3;
if ( isLong ) {
@currencysecrets
currencysecrets / isTradeAllowed.mq4
Created May 1, 2013 10:04
This script slightly modifies the _IsTradeAllowed function provided by http://articles.mql4.com/141
//* ////////////////////////////////////////////////////////////////////////////////
// int _IsTradeAllowed( int MaxWaiting_sec = 30 )
//
// the function checks the trade context status. Return codes:
// 1 - trade context is free, trade allowed
// 0 - trade context was busy, but became free. Trade is allowed only after
// the market info has been refreshed.
// -1 - trade context is busy, waiting interrupted by the user (expert was removed from
// the chart, terminal was shut down, the chart period and/or symbol was changed, etc.)
// -2 - trade context is busy, the waiting limit is reached (MaxWaiting_sec).
@currencysecrets
currencysecrets / getErrorDetail.mq4
Created April 17, 2013 23:32
This function returns the error message returned by MetaTrader. Obtained from: http://www.forexfactory.com/showthread.php?p=4435889#post4435889 The way I use this function is within my alertMe function, where I wrap err_msg( GetLastError() ).
//+------------------------------------------------------------------+
string err_msg(int e)
//+------------------------------------------------------------------+
// Returns error message text for a given MQL4 error number
// Usage: string s=err_msg(146) returns s="Error 0146: Trade context is busy."
{
switch (e) {
case 0: return("Error 0000: No error returned.");
case 1: return("Error 0001: No error returned, but the result is unknown.");
case 2: return("Error 0002: Common error.");
@currencysecrets
currencysecrets / removeOldOrders.mq4
Created April 17, 2013 01:40
Removing pending orders when they do not match the new trading system's version number. It does assume that the system tag is placed in the OrderComment area of a new order.
void removeOldOrders( string sym, string systemTag ) {
int tot = OrdersTotal();
for( int i = 0; i < tot; i++ ) {
if ( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) ) {
if ( OrderSymbol() == sym ) {
if ( OrderType() != OP_BUY || OrderType() != OP_SELL ) {
if ( OrderComment() != systemTag ) { // don't forget your new order must contain the systemTag!
OrderDelete( OrderTicket() );
}
}
@currencysecrets
currencysecrets / alertMe.mq4
Last active December 16, 2015 06:58
Using the SendEmail function to alert me of potential problems in my MetaTrader live code.
// This function provides a boilerplate type template for sending email alerts about your MetaTrader code.
// param subject (optional) => Usually a short description of what the email is alerting you about
// param body (optional) => As the email already contains many bits of information I just use the body for detailing a reason
// param systemTag (optional) => The name/tag of your expert advisor
//
// Example usage within my EA:
// alertMe( "CLOSE long order failed", "Reason: Closing a pending buystop order in function XYZ has failed", "My Sys (v1.0)" );
void alertMe( string subject = "", string body = "", string systemTag = "" ) {
SendMail( subject + " " + Symbol() + " " + systemTag ,
@currencysecrets
currencysecrets / getSpread.mq4
Created April 13, 2013 07:29
This function returns the spread in double digit form, eg 0.00030 rather than 30.
// this function returns the spread of the currency in double form
double getSpread( string sym ) {
return( MarketInfo( sym, MODE_SPREAD ) * MarketInfo( sym, MODE_POINT ) );
}
@currencysecrets
currencysecrets / getOpenBar.mq4
Last active December 16, 2015 04:29
This function returns the bar number according to the currency and datetime being passed in. This function is good for trailing stop methods that need to know when the entry of the bar occurred.
int getOpenBar( string sym, datetime openTime ) {
// each bar has the opening time at the start of the bar
// eg if openTime is 12:01PM we will know at 4:00PM (when the
// bar is greater than the opening time of trade)
for ( int i = 0; i < Bars; i++ ) {
if ( openTime >= Time[i] ) {
return( i );
}
}
}