Hi Grant (or anyone),
Your code further below, calculates the fxRate based on the Close of
the last bar. Since I have yet to live trade and have only been
involved in backtesting, I am unsure how a broker actually computes
the exchange rate during a real trade. Is their rate typically
"dynamic" and set according to the "last bar" of one's trade?
For example, if I am trading GBP JPY intraday on 15-min bars, with a
base account in USD, and I execute a trade at 09:15 am, will the
broker calculate the exchange rate of JPY USD based on what the rate
was at 09:00 am (i.e. "one bar ago") or will the broker use a rate
based on the close of the previous day? If the latter, how do we modify
your code to accomdate that fact?
Also, I have modified a section of your code to account for the fact
that my historical Forex database might have the USDJPY pair but not
the reverse, JPYUSD. Does this look like the right approach?:
ccy1 = StrLeft(Name(),3);
// eg. EURJPY gives EUR
ccy2 = StrRight( Name(), 3 );
// eg. EURJPY gives JPY
TickSize = IIf( ccy2 == "JPY", 0.01, 0.0001 ); // the
minimum price move of symbol
RoundLotSize = 1;
// whole contracts only
contracts = 1;
SetPositionSize( contracts, spsShares );
// Calculate the exchange rate, "fxRate" against our base currency
// For e.g., if we are trading GBYCHF with base of USD, we search our
symbols database for either
// CHFUSD or USDCHF, and then find last bar's Close in order to get the
correct multiplier:
if( IsNull(Foreign(StrFormat( ccy2 + base ), "Close") ) )
{
fxRateInversed = IIf( ccy2 == base, 1, Ref( Foreign( StrFormat( base
+ccy2 ), "Close" ), -1 ) );
fxRate = 1 / fxRateInversed;
}
else
fxRate = IIf( ccy2 == base, 1, Ref( Foreign( StrFormat( ccy2 + base ),
"Close" ), -1 ) );
pv = lotsize * fxRate;
md = pv * Close * 1 / leverage;
Thanks for any feedback.
--- In [email protected], Grant Noble <[EMAIL PROTECTED]> wrote:
>
> Quit your whining. It took me the best part of 2 years to get accurate
profit & margin calculations.
> It didn't help that there was a bug in fx profit calculations
involving leverage but Thomasz fixed
> that a few releases ago (ta :)). Along the way I realised that not all
brokers calculate margin the
> same way - my broker actually has three different calculations
depending on the type of stop used.
> Some base margin on CCY1, some on CCY2. Also margin, as a symbol
information property, is defined
> statically whereas I needed to define it dynamically at order time
based on entry price. Therefore I
> needed CBT code which I was able to create with the help of another
participant on this forum (g'day
> Aron). You too will probably need something like this. Amibroker
support is tip-top but working out
> how to set backtest preferences for your broker is your job.
>
> I'll attach some code I use. I #include it in all my systems. Note
that is uses ATC so you'll have
> to retrieve this data within your system code. Note also that this
code is partially obsolete (since
> that bug was sorted out). Will it do exactly what you want? Probably
not, you'll need to tweak
> depending upon your use. I need to do more work on it but I've been
busy with twin babies. Hope it
> helps. G
>
> /*
> Grant's FX preferences
> */
> SetOption("FuturesMode", True); // = use MarginDeposit and
PointValue in calculations
> SetOption("InitialEquity", 10000 ); // Initial equity $
> SetOption("MaxOpenPositions", 4 ); // Max number of positions
> SetOption("CommissionMode", 3); // 3 = $ per share/contract
> SetOption("CommissionAmount", 0); // Commission amount
> SetOption("AccountMargin", 100); // Account margin, 100 = no
margin
> SetOption("PriceBoundChecking", True ); // Price to stay in bar
range ?
> SetOption("UsePrevBarEquityForPosSizing", True ); // Use last known
bar for position sizing ?
> SetOption("ActivateStopsImmediately", False); // Intraday stops ?
> SetOption("Allowsamebarexit", True); // Allow same bar exit for
profit stops ?
> SetOption("Allowpositionshrinking", True); // Take partial trades
if equity available ?
> SetOption("InterestRate",0); // Set interest rate earned for
free cash, zero to evaluate system
> SetOption("UseCustomBacktestProc", True); // Use Custom backtest for
reporting ?
> SetOption("MinShares", 1); // Min number shares one
> SetOption("MinPosValue", 0); // Min position value to make trade
worthwhile, 0 = no limit
> SetTradeDelays(0,0,0,0); // Trade delays applied by the
backtester
>
> lotsize = StrToNum( ParamList("Lot size", "100000|10000|1000", 0) );
// default lot 100000
> leverage = Param("Leverage rate", 50, 1, 100, 1); // default
leverage 50:1
> base = ParamList("Base currency","AUD|CAD|CHF|EUR|GBP|USD", 0); //
default base currency AUD
>
> ccy2 = StrRight( Name(), 3 ); // eg. EURJPY gives JPY
> TickSize = IIf( ccy2 == "JPY", 0.01, 0.0001 ); // the minimum price
move of symbol
> RoundLotSize = 1; // whole contracts only
> contracts = 1; // contracts to acquire
> SetPositionSize( contracts, spsShares ); // spsShares = size
expressed in shares/contracts
>
> fxRate = IIf( ccy2 == base, 1, Ref( Foreign( StrFormat( ccy2 + base ),
"Close" ), -1 ) ); // eg.
> close of JPYAUD yesterday
> pv = lotsize * fxRate; // the amount of profit generated by one
contract for a one point increase
> in price
> md = pv * Close * 1 / leverage; // the amount of money required to
open single contract position [IG
> margin - no stop]
>
> // note: ATC code produces future leak warning when code is Checked by
editor..
> AddToComposite( pv, "~spec_" + Name(), "O", atcFlagDeleteValues +
atcFlagCompositeGroup +
> atcFlagEnableInExplore + atcFlagEnableInBacktest ); // store daily
point value in composite
> AddToComposite( md, "~spec_" + Name(), "C", atcFlagCompositeGroup +
atcFlagEnableInExplore +
> atcFlagEnableInBacktest ); // store daily margin deposit in composite
>
> SetCustomBacktestProc(""); // no external backtest procedure
> if ( Status("action") == actionPortfolio) {
> bo = GetBacktesterObject(); // get backtester object
> //
> // set correct MarginDeposit & PointValue on entry signal days
> //
> bo.PreProcess(); // do pre-processing (always required)
> for (i = 1; i < BarCount; i++) // loop through all bars
> {
> for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i)) //
loop through all signals at
> this bar
> {
> if( sig.IsEntry() ) // is it an entry signal (buy/short) ?
> {
> pv = Foreign( "~spec_" + sig.Symbol, "O" ); // entry day point
value
> md = Foreign( "~spec_" + sig.Symbol, "C" ); // entry day margin
deposit
> _TRACE( StrFormat( "Ticker=" + sig.Symbol + " PointValue=%1.0f
pv=%1.0f MarginDeposit=%1.0f
> md=%1.0f", sig.PointValue, pv[i], sig.MarginDeposit, md[i] ) );
> sig.PointValue = pv[i]; // set symbol PointValue
> sig.MarginDeposit = md[i]; // set symbol MarginDeposit
> }
> }
> bo.ProcessTradeSignals(i); // process trades at bar (always
required)
> }
> bo.PostProcess(); // do post-processing (always required)
> }
> //
>
> Ýlhan Ketrez wrote:
> > Dear friends,
> >
> > Before registering the mailgroup, I was quite frustated regarding
the
> > customer support for Amibroker. Now I observe that Tomasz is very
busy
> > with the new technological developements of the software and replies
the
> > related questions mostly. I appreciate his great effort and magical
> > software.
> >
> > In my recent e-mails, I believe that I emphasize an important point
> > about forex backtesting in Amibroker. Currently, I see that it is
> > impossible to truly backtest multiple forex pairs with the standard
> > backtester interface. Below, I am trying to explain in detail.
> >
> > - For EURUSD and XXXUSD pairs everything is OK.
> > - When the second unit is not USD, problem arises. I'm taking the
EURJPY
> > as my example. Brokers calculate the profit-loss of these trades in
JPY
> > (using the pointvalue * pricechange formula) and then converts to
USD,
> > using the USDJPY pair as you know.
> > - In Amibroker, when we set the currency as JPY and indicate USDJPY
in
> > the settings, it calculates the profit-losses correctly.
> > - In this case, it takes also the margin deposit scalar in JPY.
> > - Hence, all the traded contract quantities increase to extreme
values
> > and all the backtest becomes incorrect.
> >
> > How can we (do you) overcome this hindrance?
> >
> > Thanks in advance and best regards,
> > Ilhan
> >
> >
> >
> >
> >
> >
> > ---------- Yönlendirilmiþ ileti ----------
> > Kimden: *Ýlhan Ketrez* [EMAIL PROTECTED]
> > <mailto:[EMAIL PROTECTED]>
> > Tarih: 27 Haziran 2008 Cuma 15:28
> > Konu: Re: [amibroker] Portfolio backtest in forex
> > Kime: [email protected] <mailto:[email protected]
> >
> >
> >
> > Thanks Tomasz,
> >
> > Curreny setting in the symbol information window is OK.
> >
> > The settings in the Tools->Preferences->Currencies windos are also
OK.
> >
> > When we change the currency, the currency of the margin deposit also
> > changes. That is the problem.
> > In forex accounts all of the margins are (Say) 1000 USD.
> > Hence, in the current situation it becomes 1000 CHF. (Small Problem,
> > average in last years is around 1.4)
> > If the pair is USDJPY, it becomes 1000 JPY. (Greater problem average
in
> > last years is around 110)
> >
> > As a result, almost all of the results become insignificant,
starting
> > with exposure, total profit/loss calculations.
> >
> >
> > Please note that, unit profit-loss calculation is correct. Infact
> > everything else seem to be correct.
> >
> > I understand the need for setting the margindeposit currency to the
> > active currency to make trade in different markets.
> >
> > One temporary solution can be making an approach by adjusting the
> > position size accordingly But the real, exact solution can only be
> > achieved by defining a seperate currency setting for the margin
deposit.
> >
> > I am waiting for your comments.
> >
> > Best regards,
> > Ilhan
> >
> >
> > 2008/6/27, Tomasz Janeczko [EMAIL PROTECTED]
> > <mailto:[EMAIL PROTECTED]>:
> >
> > Hello,
> >
> > You need to define CURRENCY (symbol->information) for that pair
to
> > match the deposit currency.
> > You also need to setup currency tables in
Tools->Preferences->Currencies
> >
> > Best regards,
> > Tomasz Janeczko
> > amibroker.com <http://amibroker.com/>
> >
> > ----- Original Message -----
> > *From:* Ýlhan Ketrez <mailto:[EMAIL PROTECTED]
> > *To:* [email protected]
<mailto:[email protected]
> > *Sent:* Thursday, June 26, 2008 4:24 PM
> > *Subject:* [amibroker] Portfolio backtest in forex
> >
> >
> > Hello,
> >
> > While the currency for the margin deposit is not USD, how
can we
> > perform a correct portfolio backtest in XXXYYY where YYY is
not
> > USD such as EURCHF?
> >
> > Thanks in advance.
> > Ilhan
> >
> >
> >
> >
> >
> >
> >
> >
------------------------------------------------------------------------
> >
> >
> > No virus found in this incoming message.
> > Checked by AVG.
> > Version: 8.0.101 / Virus Database: 270.4.3/1527 - Release Date:
6/30/2008 6:07 PM
>