I need to know what is the entire swap at rollover for this position, so I constructed the following tools to assist me:
updated on 4.4.19
//+------------------------------------------------------------------+//| GetPipValue //+------------------------------------------------------------------+/*
Get a symbol's pip value in terms of Account currency.
*/double GetPipValue(string SymbolPair, double Position_Lots, double AccountCurrency_Exchange_Rate)
{
double pvalue = 0;
double pip = 0.0001;
double askp = MarketInfo(SymbolPair,MODE_ASK);
string BaseCurrency = SymbolInfoString(SymbolPair,SYMBOL_CURRENCY_BASE);
double contractsize = SymbolInfoDouble(SymbolPair,SYMBOL_TRADE_CONTRACT_SIZE);
if(askp > 0)
{
pvalue = (pip/askp) * Position_Lots * contractsize;//this is in Base Currency
pvalue = pvalue / AccountCurrency_Exchange_Rate;
}
else
{
Print("Failed to get ASK price for the symbol "+SymbolPair+" for GetPipValue. Please ensure the symbol is listed in MarkeWatch and that you are connected to the trade server.");
}
return pvalue;
}
//+------------------------------------------------------------------+//| end of GetPipValue //+------------------------------------------------------------------+//+------------------------------------------------------------------+//| GetSwapRateInAccountCurrency //+------------------------------------------------------------------+/*
Get a symbols swap rate in account currency.
*/double GetSwapRateInAccountCurrency(string aSymbol,int aOrderType, double ExchangeRate)
{
// now check swap typeint swapType=MarketInfo(aSymbol, MODE_SWAPTYPE);
double swapRate=0;
// get out the tick value to calculate swap pointsdouble tickValue=MarketInfo (aSymbol, MODE_TICKVALUE); // tick value in deposit currency (typically USD)string accntcurrency_str = AccountInfoString(ACCOUNT_CURRENCY);
//Swap calculation method. 0 - in points; 1 - in the symbol base currency; 2 - by interest; 3 - in the margin currencydouble AccntCurrency=0;
if (aOrderType==OP_SELL)
{
swapRate=SymbolInfoDouble(aSymbol,SYMBOL_SWAP_SHORT);
}
elseif(aOrderType==OP_BUY)
{
swapRate=SymbolInfoDouble(aSymbol,SYMBOL_SWAP_LONG);
}
if (swapType==0)
{
AccntCurrency = swapRate/ExchangeRate;
}
elseif (swapType==1)
{
// need to convert to account currency // get currency of symbol string base_currency_Str=SymbolInfoString(aSymbol,SYMBOL_CURRENCY_BASE);
AccntCurrency = swapRate/ExchangeRate;
}
elseif (swapType==2)
{
// interest rate or percentage version, e.g. bitcoin. Use %tage of price but returned down to a day. Probably wrong...
AccntCurrency=((swapRate/100)*MarketInfo(aSymbol, MODE_BID))/365;
}
elseif (swapType==3)
{
// this is easiest. Amount is in margin currency which = account currency
AccntCurrency=swapRate;
}
return(AccntCurrency);
}
//+------------------------------------------------------------------+//| end of GetSwapRateInAccountCurrency //+------------------------------------------------------------------+
Inorder to calculate swap we need to get the value for the following variables to plug into the formula above:
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
I need to know what is the entire swap at rollover for this position, so I constructed the following tools to assist me:
Inorder to calculate swap we need to get the value for the following variables to plug into the formula above:
PipValue = ?
Lots = ?
Swaprate = ?
NumberOfNights = ?
Let's do this in mql4:
The above evaluates to(The values below are not real):
Thus, Swap = 3 * 1 * 8 * 1 = 24$USD.
Maybe someone could correct/change/confirm.