I think I have hit something solid. This might help someone. Here is what I have so far:
//+------------------------------------------------------------------+ //| start convertcurrency //+------------------------------------------------------------------+ // This is supposed to convert figure from a original currency to a target currency double convertcurrency(double originalamount,string originalcurrencysymbol, string intendedcurrencysymbol) { /* original price divided by quote price of the intended currency pair will be new price interms of intended currency. */ int asize1 = StringLen(originalcurrencysymbol); int asize2 = StringLen(intendedcurrencysymbol); string olstr = originalcurrencysymbol; if(asize1 > 3)olstr = StringSubstr(originalcurrencysymbol,asize1-3,3); string instr = intendedcurrencysymbol; if(asize2 > 3)instr = StringSubstr(intendedcurrencysymbol,asize2-3,3); StringToUpper(olstr); StringToUpper(instr); double bidp = -1; double askp = -1; double newprice = -1; string apair = ""; //use bid price if you have found pair with intended currency as quote apair = StringConcatenate(olstr,instr); bidp = MarketInfo(apair,MODE_BID); //use ask price if you have found pair with intended currency as base apair = StringConcatenate(instr,olstr); askp = MarketInfo(apair,MODE_ASK); if(bidp > 0) { newprice = originalamount*bidp; } else if(askp > 0) { newprice = originalamount/askp; } else if(olstr == instr) { newprice = originalamount; } else { Print("Failed to receive price rate for "+intendedcurrencysymbol+" inorder to covert from "+originalcurrencysymbol+DoubleToString(originalamount,8)+" to "+intendedcurrencysymbol+". Ensure that the symbol is available and supported."); } newprice = NormalizeDouble(newprice,4); return newprice; } //+------------------------------------------------------------------+ //| end convertcurrency //+------------------------------------------------------------------+
//+------------------------------------------------------------------+ //| OrderSwap2 //+------------------------------------------------------------------+ /* Get a position's swap(overnight charge/profit) in account currency. To use you must enter all three paramters: the symbol, ordertype and Lots. Or leave the symbol,ordertype,Lots blank and enter only Ticket#. */ double OrderSwap2( string aSymbol_Or_TicketAsString, int aOrderType=999999999, double PositionLots=999999999) { int aTicket=StrToInteger(aSymbol_Or_TicketAsString); string aSymbol = aSymbol_Or_TicketAsString; if(aOrderType == 999999999)aOrderType = -1; if(PositionLots == 999999999)PositionLots = -1; if(aTicket > 0) { if(OrderSelect(aTicket,SELECT_BY_TICKET) == true) { aSymbol = OrderSymbol(); aOrderType = OrderType(); PositionLots = OrderLots(); } else { Print("Cannot select order for Ticket#"+IntegerToString(aTicket)+" inside OrderSwap2() function."); return(0); } } else if(aSymbol == "" || aOrderType == -1 || PositionLots == -1) { Print("Error: aSymbol="+aSymbol+", aOrderType="+IntegerToString(aOrderType)+",PositionLots="+DoubleToStr(PositionLots)+". Correct this or simply enter the Ticket# ONLY."); return(0); } // now check swap type int swapType=(int)MarketInfo(aSymbol,MODE_SWAPTYPE);//SymbolInfoInteger(aSymbol, SYMBOL_SWAP_MODE); double swapRate=0,Swap=0; string accntcurrency_str = Account_Currency; double pointvalue_in_base_currency=(MarketInfo(aSymbol,MODE_POINT)/MarketInfo(aSymbol,MODE_BID)); //Swap calculation method. 0 - in points; 1 - in the symbol base currency; 2 - by interest; 3 - in the margin currency double Amt_in_AccntCurrency=0; if (aOrderType==OP_SELL) { swapRate=SymbolInfoDouble(aSymbol,SYMBOL_SWAP_SHORT); } else if(aOrderType==OP_BUY) { swapRate=SymbolInfoDouble(aSymbol,SYMBOL_SWAP_LONG); } if (swapType==0) { //swap = swap_long_points|swap_short_points * position_lots = swap in Quote Currency Swap = swapRate * PositionLots; Amt_in_AccntCurrency = convertcurrency(Swap,aSymbol,accntcurrency_str); } else if (swapType==1) { //swap = swap_long_points|swap_short_points * position_lots = swap in Base Currency Swap = swapRate * PositionLots; // get currency of symbol and need to convert to account currency string base_currency_Str=SymbolInfoString(aSymbol,SYMBOL_CURRENCY_BASE); Amt_in_AccntCurrency = convertcurrency(Swap,base_currency_Str,accntcurrency_str); } else if (swapType==2) { //swap = (swap_long_percent|swap_short_percent / 100)/365 * position_lots = swap in Account Currency // interest rate or percentage version Swap = (((swapRate/100)*MarketInfo(aSymbol, MODE_BID))/365) * PositionLots; Amt_in_AccntCurrency = Swap; } else if (swapType==3) { //swap = swap_long_points|swap_short_points * position_lots = swap in Account Currency Swap = swapRate * PositionLots; Amt_in_AccntCurrency = Swap; } return(Amt_in_AccntCurrency); } //+------------------------------------------------------------------+ //| end of OrderSwap2 //+------------------------------------------------------------------+
. The OrderSwap2 function should return an almost identical result to the OrderSwap function provided by MT4.
example1 - we want to know the swap if we short one lot of EURUSD before we open it:
string currentsymbol = "EURUSD"; int Ordertype = OP_SELL; double newposition = 1; //Here we want know what the swap cost/profit for a position before we send the order. double swap = OrderSwap2(currentsymbol,Ordertype,newposition);
example2 - we have already opened the order to short one lot of EURUSD and we wanted to check its swap:
//Here we already know the ticket number maybe from some previous use of OrderSend, which had returned 4 as the ticket number. int aTicket = 4; double swap = OrderSwap2( IntegerToString(aTicket) );// this is the same as doing OrderSwap2("4");
This is an attempt at a general approach to finding the swap, regardless of which instrument is used. It should work with CFDs also. Therefore, we can safely say "OrderSwap2 == OrderSwap" , with an accuracy of 4 decimal places.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi. I am trying to derive the formula used by OrderSwap(). Is it like so:
Orderswap = (MarketInfo(currentsymbol,MODE_TICKVALUE)/MarketInfo(currentsymbol,MODE_TICKSIZE))* MarketInfo(currentsymbol,MODE_POINT) * OrderLots();
?