Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Calculate lot size for each Instrument for a fixed amount
 Post subject: Calculate lot size for each Instrument for a fixed amount Post rating: 0   New post Posted: Thu 22 Dec, 2011, 12:06 

User rating: 0
Joined: Mon 19 Dec, 2011, 13:56
Posts: 3
Location: France,
Hello,


In order to apply a risk management, I'm trying to calculate lot value for each instrument in my account currency. As for example, for a CHF account, how to calculate how many CHF will I invest in a 1 lot EURUSD SELL order ?

I define the amount I want to invest per order in my account currency, and then I calculate how many lots I have to trade on a given pair to open a position for this amount. Therefore I'm coding a strategy which will display lot value each time I open a graph, in my account currency.

I searched the board and the Wiki, I found how to code a strategy to get pip value in account currency for an open position but yet I can't find a way to calculate lot value in account currency for each instrument.

Help will be really appreciated :)

Regards,
Jean Coiron


 
 Post subject: Re: Calculate lot size for each Instrument for a fixed amoun Post rating: 0   New post Posted: Thu 22 Dec, 2011, 20:47 

User rating: 0
Joined: Mon 19 Dec, 2011, 13:56
Posts: 3
Location: France,
The MQL4 code I made with another borker was as follow :

//+------------------------------------------------------------------+
//|                                                 EA2JLotValue.mq4 |
//|                                 2010, Jimmy Rolland, Jean Coiron |
//+------------------------------------------------------------------+
#property copyright "2010, Jimmy Rolland, Jean Coiron"

#property indicator_chart_window

int    Cur_DIGITS,             Cur_LotDiv;
   
double Cur_LOTSIZE,            Cur_TICKVALUE,             
       Cur_POINT,              Cur_LotValue,
       Cur_MinLots,
       IndDefTradValue;
   
string Cur_AccountCurrency,    Cur_Symbol;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   
    Cur_AccountCurrency       = AccountCurrency();
   
    Cur_Symbol                = Symbol();
   
    Cur_LOTSIZE               = MarketInfo(Cur_Symbol, MODE_LOTSIZE);
    Cur_TICKVALUE             = MarketInfo(Cur_Symbol, MODE_TICKVALUE);
    Cur_DIGITS                = MarketInfo(Cur_Symbol, MODE_DIGITS);
    Cur_POINT                 = MarketInfo(Cur_Symbol, MODE_POINT);
    Cur_MinLots               = MarketInfo(Cur_Symbol, MODE_MINLOT);
   
    if      (Cur_DIGITS == 3) Cur_LotDiv = 1;
    else if (Cur_DIGITS == 4) Cur_LotDiv = 10;
    else if (Cur_DIGITS == 5) Cur_LotDiv = 100;
    else                      Cur_LotDiv = 100;
   
    Cur_LotValue          = Cur_TICKVALUE / Cur_POINT / Cur_LotDiv;
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   //int    counted_bars=IndicatorCounted();
//----
    bool   GlobVarExists;
    double Cur_TradeDefValue;
    string TextDefTradeValue;
   
    Cur_Symbol                = Symbol();
   
    Cur_LOTSIZE               = MarketInfo(Cur_Symbol, MODE_LOTSIZE);
    Cur_TICKVALUE             = MarketInfo(Cur_Symbol, MODE_TICKVALUE);
    Cur_DIGITS                = MarketInfo(Cur_Symbol, MODE_DIGITS);
    Cur_POINT                 = MarketInfo(Cur_Symbol, MODE_POINT);
    Cur_MinLots               = MarketInfo(Cur_Symbol, MODE_MINLOT);
    if      (Cur_DIGITS == 3) Cur_LotDiv = 1;
    else if (Cur_DIGITS == 4) Cur_LotDiv = 10;
    else if (Cur_DIGITS == 5) Cur_LotDiv = 100;
    else                      Cur_LotDiv = 100;
   
    Cur_LotValue          = Cur_TICKVALUE / Cur_POINT / Cur_LotDiv;
   
    GlobVarExists = GlobalVariableCheck("gTradeDefaultVal");
    if ( GlobVarExists == true )
    {
        IndDefTradValue = GlobalVariableGet("gTradeDefaultVal");
        Cur_TradeDefValue = IndDefTradValue / Cur_LotValue;
        TextDefTradeValue = " Default lots: " + DoubleToStr(Cur_TradeDefValue,2) + " = " + DoubleToStr(IndDefTradValue, 2) + " " + Cur_AccountCurrency;
       
    }
    else
    {
        IndDefTradValue = 0.0;
        TextDefTradeValue = "";
    }

    Comment("Lot Value: ", Cur_LotValue, " ", Cur_AccountCurrency, " MinLot: ", Cur_MinLots, TextDefTradeValue);
//----
   return(0);
  }
//+------------------------------------------------------------------+


 
 Post subject: Re: Calculate lot size for each Instrument for a fixed amoun Post rating: 1   New post Posted: Tue 27 Dec, 2011, 16:40 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
One option could be to first calculate the lot in your account currency and then convert it to desired trading currency.
Current exchange rates can be obtained from history:
history.getLastTick(Instrument.EURUSD).getAsk();

or
history.getLastTick(Instrument.EURUSD).getBid();


 
 Post subject: Re: Calculate lot size for each Instrument for a fixed amoun Post rating: 0   New post Posted: Wed 28 Dec, 2011, 16:12 

User rating: 0
Joined: Mon 19 Dec, 2011, 13:56
Posts: 3
Location: France,
Are the following statements corrects for a EUR account for a 1 lot short (sell) position on GPBUSD ?

When I open a short position :
  1. My EUR are first translated into GBP
  2. Then My GBP are translated into USD and my position is opened
When I close this position :
  1. My USD are translated into GBP
  2. My GBP are translated into EUR

So when I open a short position on GBPUSD I buy USD using GBP. Do I buy 1 lot of USD ?
For a 1 lot short position GBPUSD do I buy 1 000 000 USD ?
The lot value is :
LotValue = 1000000 / history.getLastTick(Instrument.EURUSD).getBid();
?

And for a 1 lot Long position on GBPUSD :
LotValue = 1000000 / history.getLastTick(Instrument.EURGBP).getBid();
?

What about a EURNOK, EURSEK, or USDNOK buy and sell positions for instance when my account currency is CHF ? (which will be the case actually)
There is no CHFNOK nor CHFSEK instrument to get the lot value ?
[Edit]Well, I can use intermediate instruments, for EURNOK or USDNOK I can use EURCHF or USDCHF to calculate CHFNOK[/edit]


 
 Post subject: Re: Calculate lot size for each Instrument for a fixed amoun Post rating: 0   New post Posted: Mon 02 Jan, 2012, 12:23 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
JCoiron wrote:
Are the following statements corrects for a EUR account for a 1 lot short (sell) position on GPBUSD ?

When I open a short position :
My EUR are first translated into GBP
Then My GBP are translated into USD and my position is opened
When I close this position :
My USD are translated into GBP
My GBP are translated into EUR
Yes
JCoiron wrote:
So when I open a short position on GBPUSD I buy USD using GBP. Do I buy 1 lot of USD ?
Technically you buy a lot of an instrument GBP/USD (not a currency), the amount of which is expressed in USD. The distinction between currency and instrument is important when you consider application of overnights (see: https://www.dukascopy.com/swiss/english/ ... overnight/) - for instance, AUD/USD and GBP/USD rates differ.
JCoiron wrote:
So when I open a short position on GBPUSD The lot value is :
LotValue = 1000000 / history.getLastTick(Instrument.EURUSD).getBid();
?

And for a 1 lot Long position on GBPUSD :
LotValue = 1000000 / history.getLastTick(Instrument.EURGBP).getBid();
?

What about a EURNOK, EURSEK, or USDNOK buy and sell positions for instance when my account currency is CHF ? (which will be the case actually)
There is no CHFNOK nor CHFSEK instrument to get the lot value ?
The conversion algorithm is rather involved, since you have to find subscribed inter-instruments, for instance, to convert from NZD/CHF to GBP/USD at least one of the instruments GBP/NZD, NZD/USD, GBP/CHF, USD/CHF have to be subscribed (see in JForex wiki Strategy API/Practices/Subscribe to an instrument) and active. Plus, you have to consider the direction of the currencies in instruments. The conversion is available with JForex API version 2.6.45 which is available in Standalone API (see in JForex wiki Get Started/Standalone JForex API/Use in Eclipse):
https://www.dukascopy.com/client/javadoc/com/dukascopy/api/JFUtils.html#convert(com.dukascopy.api.Instrument,%20com.dukascopy.api.Instrument,%20double,%20int,%20com.dukascopy.api.OfferSide)


 
 Post subject: Re: Calculate lot size for each Instrument for a fixed amoun Post rating: 0   New post Posted: Tue 03 Jan, 2012, 16:31 
User avatar

User rating: 6
Joined: Thu 19 May, 2011, 11:14
Posts: 235
Location: South Africa,
I haven't read this whole thread carefully, but I think I ran into a similar problem a while back.
viewtopic.php?f=65&t=37571


 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com