I’m currently doing custom signal processing in CBT mid-level.
When I’m evaluating each signal I need to calculate the position size of the signal in dollars (base currency).
Since I have symbols in different currencies, I need to get the Fxrate for symbol signal being processed.
From reading the manual it seems to me that only trade object has Fx rate available via GetPrice method.
Probably I’m missing something is there another way to achieve this?
Yes only Trade object (like open position) has direct access to FxRate. Before opening trade you need to use signal.Symbol , retrieve currency using GetFnDataForeign() and then use Foreign() to retrieve actual price.
An alternative, faster method is to store currency in static variable in first phase.
Something along these lines:
cur = GetFnData("Currency");
if( cur != "" AND cur != "USD" )
{
StaticVarSet( "Fx" + cur, Foreign( cur + "USD", "C" ) );
}
SetCustomBacktestProc("");
if (Status("action") == actionPortfolio)
{
bo = GetBacktesterObject(); // Get backtester object
bo.PreProcess(); // Do pre-processing (always required)
for (i = 0; i < BarCount; i++) // Loop through all bars
{
for (sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
{
cur = GetFnDataForeign( "Currency", sig.Symbol );
entryfxrate = 1;
if( cur != "" AND cur != "USD" )
{
fxrates = StaticVarGet( "Fx" + cur );
entryfxrate = fxrates[ i ];
}
// do your custom signal processing
}
bo.ProcessTradeSignals( i ); // Process trades at bar (always required)
}
bo.PostProcess(); // Do post-processing (always required)
}
My code is intended for original poster experience level (i.e. Advanced).
In the code I store currency data in static variables for speed so they can be later retrieved faster. So say store EURUSD rates in “FxEUR” static. Static variables are orders of magnitude faster than Foreign call.
But if you are beginner you should keep things simple and don’t use such things.
Thomaz
I have to use fixed FX rates in Preferences > Currencies as I use multiple databases some without FX data others with differing symbols for the same FX rate.
Is it possible to retrieve these fixed FX Rates from the preferences table in AFL or do I need to replicate this table as Variables within the AFL code?