Skip to content

Instantly share code, notes, and snippets.

@currencysecrets
Last active July 27, 2020 20:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save currencysecrets/3083349 to your computer and use it in GitHub Desktop.
Save currencysecrets/3083349 to your computer and use it in GitHub Desktop.
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
* range.
* 2. Calculate PURCHASE & SALE amounts for each trade
* - Only will calculate closed trades during the financial year
* 3. Calculate charges: COMMISSIONS & SWAPS
* - generally these will already be converted into their BASE currency
* and therefore no conversion will be necessary
* - it will be assumed that COMMISSIONS are negative amounts
* - SWAPS will be listed in the Expenses area (even though they may
* have a positive value and therefore be income)
* 4. Calculate open and closing positions (stock)
* - positions that are open prior to the beginning of the year form
* the O/STOCK
* - positions that are open at the end of the year form the C/STOCK
* - the values of these are in the BASE currency
* 5. Output calculations.
* - at the moment it's just to Print
* - you should be able to double check the P&L value against your actual
* account's P&L value for the same financial year period
*/
//+------------------------------------------------------------------+
#property copyright "Ryan Sheehy, RobotTradingForex.com"
#property link "https://robottradingforex.com"
extern int startYearFY = 2019;
extern int startMonthFY = 7;
extern int startDayFY = 1;
string createDateTime(int y, int m, int d) {
return IntegerToString(y, 4, '0') + '.' + IntegerToString(m, 2, '0') + IntegerToString(d, 2, '0');
}
void OnInit() {
datetime startFY = StrToTime( createDateTime(startYearFY, startMonthFY, startDayFY) );
datetime endFY = StrToTime( createDateTime(startYearFY + 1, startMonthFY, startDayFY ));
double totalForexSales = 0;
double totalForexPurchases = 0;
double totalCommissions = 0;
double totalSwapIncome = 0;
double totalSwapExpenses = 0;
double tickVal, lotVal;
int tot, i;
tot = OrdersHistoryTotal();
// first we'll progress through the historical trades
for ( i = tot; i >= 0; i-- ) {
if ( OrderSelect( i, SELECT_BY_POS, MODE_HISTORY ) ) {
// exclude trades outside the FY
if (OrderCloseTime() >= endFY) continue;
if (OrderCloseTime() < startFY) continue;
lotVal = OrderLots() * MarketInfo(OrderSymbol(), MODE_LOTSIZE);
if ( OrderClosePrice() == OrderOpenPrice() ) {
tickVal = MarketInfo(OrderSymbol(), MODE_TICKVALUE);
} else if ( OrderType() == OP_BUY ) {
tickVal = OrderProfit() / (lotVal * (OrderClosePrice() - OrderOpenPrice()));
} else if ( OrderType() == OP_SELL ) {
tickVal = OrderProfit() / (lotVal * (OrderOpenPrice() - OrderClosePrice()));
}
if ( OrderType() == OP_BUY ) {
totalForexSales += (OrderClosePrice() * lotVal * tickVal);
totalForexPurchases += (OrderOpenPrice() * lotVal * tickVal);
} else if ( OrderType() == OP_SELL ) {
totalForexPurchases += (OrderClosePrice() * lotVal * tickVal);
totalForexSales += (OrderOpenPrice() * lotVal * tickVal);
}
}
// commissions should already be denominated in base currency
totalCommissions += OrderCommission();
// swap earnins should already be denominated in base currency
if (OrderSwap() > 0) {
totalSwapIncome += OrderSwap();
} else {
totalSwapExpenses += OrderSwap();
}
swap += OrderSwap();
}
Print( "Total SALES = $" + DoubleToStr(totalForexSales, 2));
Print( "Total Swap Income = $" + DoubleToStr( totalSwapIncome, 2 ) );
Print( "Total PURCHASES = $" + DoubleToStr( totalForexPurchases, 2 ) );
Print( "Total Commissions = $" + DoubleToStr( totalCommissions, 2 ) );
Print( "Total Swap Expenses = $" + DoubleToStr( totalSwapExpenses, 2 ) );
Print( "Total P&L = $" + DoubleToStr( totalForexSales + totalSwapIncome - totalForexPurchases - totalCommissions - totalSwapExpenses, 2 ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment