Skip to content

Instantly share code, notes, and snippets.

View currencysecrets's full-sized avatar

Ryan currencysecrets

View GitHub Profile
@currencysecrets
currencysecrets / getLotSize.mq4
Last active April 3, 2018 13:01
This function helps to calculate the lot size needed for any position where the entry price, initial stop loss price and amount being risked is all known. The function returns the amount according to the number of decimals allowed by the broker - generally speaking, if mini lots are allowed to be traded this means you can have 2 decimal places.
/*
* Calculates the position size of the trade based upon entry price, stop loss & amount risked.
* @param entryPrice double Entry price of the order
* @param iniStop double Initial stop loss of the order
* @param riskAmt double Risk per trade (in dollars of account's currency)
*
* @return amount in mini-lots
*/
double getLots( double entryPrice, double iniStop, double riskAmt ) {
string sym = Symbol();
@currencysecrets
currencysecrets / getLastSwingPrice.mq4
Created July 6, 2012 08:28
MQL4: Get Last Swing Price
/*
* Get the price of the Stop Loss according to last swing price.
* @param sym string Symbol of currency being analysed
* @param TF int TimeFrame of the currency being analysed
* @param ord int OrderType() = OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT, OP_SELLSTOP
* @param entry double Entry price of the order
* @return double Last swing point
*/
double getLastSwingPrice(string sym, int TF, int ord, double entry) {
int bars = iBars(sym,TF);
@currencysecrets
currencysecrets / checkManualOrders.mq4
Created July 7, 2012 04:39
MQL4: Check Manually Entered Pending Orders
// this procedure checks all manually placed orders
void checkManualOrders() {
for ( int i = 0; i < OrdersTotal(); i++ ) {
if ( OrderSelect(i, SELECT_BY_POS, MODE_TRADES) ) {
// the assumption will be that new manually placed pending orders have a MagicNumber of 0
// check with your MT4 broker that they allow MagicNumbers, some brokers do NOT!
// if your broker doesn't then you'll have to remove "OrderMagicNumer() == 0 &&"
if ( OrderMagicNumber() == 0 && OrderCloseTime() == 0 ) {
if ( OP_BUYLIMIT || OP_BUYSTOP || OP_SELLLIMIT || OP_SELLSTOP ) {
// okay you're now here on an active PENDING order, what do you want to do?
@currencysecrets
currencysecrets / myMoneyManagement.mq4
Created July 9, 2012 08:33
MQL4: My Money Management
//+------------------------------------------------------------------+
//| My Money Management.mq4 |
//| Ryan Sheehy, CurrencySecrets.com |
//| http://www.currencysecrets.com |
//+------------------------------------------------------------------+
#property copyright "Ryan Sheehy, CurrencySecrets.com"
#property link "http://www.currencysecrets.com"
//--- input parameters
extern double xRiskPerTrade=1; // $ amount of risked per trade
@currencysecrets
currencysecrets / automatedTrendLines.mq4
Created July 9, 2012 09:07
MQL4: Automated Trend Lines
//+------------------------------------------------------------------+
//| Automated Trend Lines.mq4 |
//| Ryan Sheehy, CurrencySecrets.com |
//| http://www.currencysecrets.com |
//+------------------------------------------------------------------+
#property copyright "Ryan Sheehy, CurrencySecrets.com"
#property link "http://www.currencysecrets.com"
/*
* This script automates the generation and plotting of sloping trend
@currencysecrets
currencysecrets / eofy.mq4
Last active July 27, 2020 20:49
If you need to calculate the total number of forex trading sales and purchases done in your financial year then use this script to help. For more information read our article on calculating tax returns here: https://robottradingforex.com/forex-trading-tax-australia/
//+------------------------------------------------------------------+
//| EoFY.mq4 |
//| Ryan Sheehy, RobotTradingForex.com |
//| https://robottradingforex.com |
/*
* DON'T FORGET TO SELECT "ALL HISTORY" FROM ACCOUNT HISTORY TAB!!!
* This script helps to calculate a summarised version of all trades
* done in a particular financial year. It converts all sales and
* purchase transactions into the base currency.
* 1. Loop through all HISTORICAL trades between a predetermined date
@currencysecrets
currencysecrets / rorAnalysis.mq4
Created July 17, 2012 21:00
MQL4: RoR Analysis
//+------------------------------------------------------------------+
//| Trades Analysis.mq4 |
//| Ryan Sheehy, CurrencySecrets.com |
//| http://www.currencysecrets.com |
//+------------------------------------------------------------------+
#property copyright "Ryan Sheehy, CurrencySecrets.com"
#property link "http://www.currencysecrets.com"
/*
* With this script we will be running through all trades that have
@currencysecrets
currencysecrets / calcPL.mq4
Created October 3, 2012 08:41
MQL4: Calculate Profit or Loss on Trade
/**
* This method calculates the profit or loss of a position in the home currency of the account
* @param string sym
* @param int type 0 = buy, 1 = sell
* @param double entry
* @param double exit
* @param double lots
* @result double profit/loss in home currency
*/
double calcPL(string sym, int type, double entry, double exit, double lots) {
@currencysecrets
currencysecrets / exitstrategytester.mq4
Last active February 4, 2017 19:36
MQL4: Exit Strategy Tester (Script)
//+------------------------------------------------------------------+
//| Breakout Strategy Tester.mq4 |
//| Copyright 2012, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
//
// With this script we will be doing the following:
// 1. Get all the trades (be wary of reversal trades!)
// 2.
#property copyright "Copyright 2012, MetaQuotes Software Corp."
@currencysecrets
currencysecrets / ArrayFunctions.mq4
Last active February 13, 2021 12:53
Array utility functions for MetaTrader 4. Helpful functions for doing some of the repetitive tasks in MQL.
/*
* clearIntArray
* This function deletes all items in array (sets it to 0) and resizes array according to size (default = 1).
* @param int& theArray - passing the array by reference
* @param int size - size of the array (default = 1)
* @return int blank array of size
*/
int clearIntArray( int& theArray[], int size = 0 ) {
ArrayResize( theArray, size );
if ( size > 0 ) { ArrayInitialize( theArray, 0 ); }