Skip to content

Instantly share code, notes, and snippets.

View currencysecrets's full-sized avatar

Ryan currencysecrets

View GitHub Profile
@currencysecrets
currencysecrets / usingAccountBalance.mq4
Created February 19, 2014 19:57
Be careful of using AccountEquity in your risk calculations, here's a better alternative.
double EXT_RISK_DIV = 10;
double EXT_RISK_MINAMT = 50;
double riskAmt = MathMax( getCashBalance() / EXT_RISK_DIV, EXT_RISK_MINAMT );
// this function returns the actual cash balance of all positions if they were to close now
double getCashBalance() {
return( AccountBalance() + getOpenPL() );
}
// this function will calculate the total profit/loss of all open positions if all stops are hit
double getOpenPL() {
int tot = OrdersTotal();
@currencysecrets
currencysecrets / getRiskPerTrade.mq4
Last active October 28, 2019 17:01
This function calculates the risk per trade
extern double EXT_RISK_MINAMT = 50; // minimal amount of money to risk per trade
extern int EXT_RISK_MULT = 15; // maximum number of consecutive losses needed to wipeout account
// calculates the amount of money to risk per trade
double getRiskAmount() {
return( MathMax( EXT_RISK_MINAMT, ( AccountBalance() + getStopPL() ) / EXT_RISK_MULT ) );
}
// this function returns the profit/loss if the stop is hit
double profitAtStop( string sym, double open, double stop, double lots, int type ) {
@currencysecrets
currencysecrets / arrayToString.ab
Created January 13, 2014 08:33
Amibroker utility function on converting array to string - helpful for debugging just what's in those arrays. Needs the countArray functions.
EnableScript("JScript");
<%
function getArrayLength( a ) {
x = VBArray( a ).toArray();
for ( y = 0; y < x.length; y += 1 ) {
if ( x[y] == undefined ) break;
}
return y;
}
@currencysecrets
currencysecrets / countArray.ab
Last active December 18, 2020 20:20
Count Array length in Amibroker. One of the most frustrating aspects when starting out with AmiBroker is being able to count an array's length. To execute this function simply paste the two functions into your code and whenever you want to count the length of an array simply call arrCount( yourArray );
EnableScript("JScript");
<%
function getArrayLength( a ) {
x = VBArray( a ).toArray();
for ( y = 0; y < x.length; y += 1 ) {
if ( x[y] == undefined ) break;
}
return y;
}
@currencysecrets
currencysecrets / importForexData.ab
Created January 4, 2014 07:08
Importing ASCII data from forexDataDownloader into AmiBroker automatically (i.e. without running the Import Data)
EnableScript("JScript");
<%
function importForexQuotes() {
var files = [
"Z:\\Dropbox\\DATA\\OANDA\\D1\\AUDCAD.csv",
"Z:\\Dropbox\\DATA\\OANDA\\D1\\AUDCHF.csv",
"Z:\\Dropbox\\DATA\\OANDA\\D1\\AUDJPY.csv",
@currencysecrets
currencysecrets / forexDataDownloader.mq4
Last active January 1, 2016 16:09
Forex MetaTrader Data Downloader - this script is an enhancement to my previously designed script. It will download data to the respective location on your computer and will continually append new data to it as it becomes available. Dependencies: needs WinFile.mqh
//+------------------------------------------------------------------+
//| Example of using WinFile.mqh for reading/writing files |
//| anywhere on the hard disk. WinFile.mqh needs to be present |
//| in the experts\include directory, and "Allow DLL imports" |
//| needs to be turned on. |
//+------------------------------------------------------------------+
#include <WinFile.mqh>
#property copyright "Copyright © 2013 Currency Secrets.com"
#property link "http://www.currencysecrets.com"
@currencysecrets
currencysecrets / auto-trendlines.afl
Last active December 14, 2022 13:38
Amibroker version of automated trend lines.
/*
* Name: Automatic Trend Lines
* This Amibroker script is designed to automate the process of drawing:
* 1. Trend lines (sloping)
* 2. Support & resistance lines (horizontal)
* 3. Channel lines (parallel lines to trend lines & horizontal lines)
* This script is not a recommendation to buy or sell anything. It's to be
* used as is without any guarantees or warranties on its reliability.
* Author: Ryan Sheehy
* Version: v2.0.2 (20160816)
@currencysecrets
currencysecrets / metatrader-ea-framework.mq4
Created December 18, 2013 09:02
This is how my MetaTrader Expert Advisor framework formed throughout 2013. I found myself storing much information into variables and would then recalibrate those variables depending upon fixed time periods.
//+------------------------------------------------------------------+
//| MetaTrader EA Framework.mq4
//| Copyright 2013, Ryan Sheehy
//| http://www.currencysecrets.com
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, Ryan Sheehy"
#property link "http://www.currencysecrets.com"
//--- global variables (keep capitalised)
string VERSION = "1.0.0";
@currencysecrets
currencysecrets / getTrend.mq4
Created December 1, 2013 07:17
"Trade with the trend!" Here's my method on determining the ***weekly*** trend. The numbers returned correspond to the following detail: ++ A positive number is how many bars the instrument has been in an UP trend ++ A negative number is how many bars the instrument has been in a DOWN trend ++ A zero denotes that the instrument is currently NOT …
int getWeeklyTrend( string sym ) {
int per = PERIOD_W1,
b = iBars( sym, per ) - 3,
isLong, pk0, pk1, pk2, tr0, tr1, tr2, i;
for ( i = b; i > 0; i -= 1 ) {
if ( isLong <= 0 ) {
if ( tr1 > 0 && iHigh( sym, per, i ) > iHigh( sym, per, pk0 ) && iLow( sym, per, tr0 ) > iLow( sym, per, tr1 ) ) {
isLong = i;
} else if ( iHigh( sym, per, i ) > iHigh( sym, per, pk0 ) ) {
isLong = 0;
@currencysecrets
currencysecrets / trendStrength.mql4
Last active May 13, 2017 20:25
Determining the strength of a trend can be utilised with this simple function. More details of this indicator found here: http://www.currencysecrets.com/882/identifying-weakest-strongest-currencies-measuring-trend
double trendStrength( int b = 52, int per = 0, string sym = "" ) {
if ( sym == "" ) { sym = Symbol(); }
if ( b * 2 < iBars( sym, per ) ) { b = MathFloor( iBars( sym, per ) * 0.5 ); }
double maDiff = iMA( sym, per, b, 0, MODE_SMA, PRICE_CLOSE, 0 ) - iMA( sym, per, b, 0, MODE_SMA, PRICE_CLOSE, b );
double strVal = maDiff / iATR( sym, per, b, 0 );
double result = strVal * MarketInfo( sym, MODE_TICKVALUE );
return ( result );
}