Steve,
OK....after all this time trying
to avoid loops I've now decided to tackle them. But I'm still unsure of how to
go about it. Can someone look over this code please and point out where
I'm amiss, as the results are not what I expected. Code is self
explanatory.....I think
Some comments in no
particular order:
This line of code
is invalid IMHO: BuyPrice =
ValueWhen(Buy,O) + spread;
You are using
ValueWhen(Buy,O) which looks into
the future. The most recent Buy would use ,1 and not ,0. Maybe you need this
for same bar exits, I don't know how that works. (See help on
ValueWhen.)
Possible
MIS-UNDESTANDING of ARRAY code: I see you have set BuyPrice and SellPrice BEFORE your loop begins in ARRAY code and then you modify
SellPrice within the
array. Amibroker executes these first 9 lines of code in your example ONCE and
NEVER LOOKS BACK, NEVER re-executes this code.
Be careful when
trying to set your own buyprice and sellprices. These are arrays in AB determined by AA-Settings OR your
code, such as BuyPrice = C;
The problem is
setting bogus prices. For example, your conditions are determined at the close
of the bar and you buy at the open or some arbitrary price you think you
should get. You may be doing this right as I'm not familiar with Forex data or
trading, just a word to the wise.
Now, inside your
loop you have several variables missing array subscripts. For example,
priceatbuy == 0 should be priceatbuy[i] == 0. You also refer to
SellProfit and
SellLoss with [i].
This is not allowed UNLESS you know the variables in question are NUMERIC and
not ARRAYS. This is clearly not the case with priceatbuy since you initialize it to 0 before your
loop begins. Spread is probably also an array, although used in the way it is,
it doesn't matter. You can force a numeric by using param statement as
in:
spread =
Param("Spread",0.0003,0.0002,0.0004,0.0001);
You are setting
Buy = BuyCond1; (which is undefined in your example). This is fine, but
it appears to serve no purpose. You could just have defined Buy in the first
place. Now, I use BuyCond1 as I believe (possibly incorrectly) that when you
actually set Buy = something, the backtester is invoked. If you later change
your Buy condition, then I don't really know how the backtester deals with
this. Maybe it just runs again. Somebody
please enlighten me on this point. Anyhow, to avoid any possible
problems, I define BuyCond1 and do whatever manipulations I want based on trade
conditions or whatever that my modify my BuyCond1. Then, when I'm all done messing around
and have my final BuyCond1 the way I want it, I set the Buy and Sell variables which
invoke the backtester just once.
You looping code
appears to be there mostly to set your buyprice and sellprice (see caution above). However, you are
setting Sell[i] =
True in this loop. This loop is invoked ONLY when you have a
Buy[i] == True. So,
you are selling on the same bar as you are buying. I have seen a check box to
allow a same bar trade so this might be OK, but again I need another's advice here. [20
minutes later…Reading and studying your
code further, you have ALREADY set your SellPrice as you desired with your IIF
statement before the loop for ALL BARS of data. The loop is only different buy
testing to see if you are also on a Buy first.]
You are first
setting priceatbuy to BuyPrice only if priceatbuy == 0 AND Buy[i] == True. Then you are testing
priceatbuy right
afterwards along with sella[i]. This is Okay, but redundant since the only way
priceatbuy > 0 is
if the first test is already true. No harm done except you don't really need
priceatbuy variable
at all, just wrap the sella and sellb tests inside {} so they are only
executed when the first conditions are true. Now that I wrote that,
priceatbuy will
ALWAYS be == 0 for your test since you initialize all bars of
priceatbuy to 0
before the loop.
I'd better stop
now. I hope all the above is coherent…and accurate!
spread =
0.0003;
//I believe
this will be defined as an array and not a numeric variable.
Buy =
Buycond1;
BuyPrice =
ValueWhen(Buy,O) + spread;
SellProfit =
BuyPrice + 0.0030
;// Take
Profit @ this level
SellLoss =
BuyPrice - 0.0015;// Stoploss @
this level
Sella =
Cross( H,SellProfit); //Sell when
30 pip in profit from entry price
Sellb =
Cross(SellLoss,L) ; //Sell when
15 pip loss from entry price
SellPrice =
IIf( Sella,
SellProfit, IIf(Sellb,SellLoss,Null));//Sellprice
depends on which condition was met
Priceatbuy =
0;
//I believe
this will be defined as an array and not a numeric variable.
for( i =
0; i <
BarCount; i++
)
{
if( priceatbuy
== 0 &&
Buy[ i ] )
priceatbuy = BuyPrice[ i
];
if( priceatbuy
> 0 &&
Sella[i] == 1)
{
Sell[ i ] = 1;
SellPrice[ i ] ==
SellProfit;
priceatbuy = 0;
}
if( priceatbuy
> 0 &&
Sellb[i] == 1)
{
Sell[ i ] = 1;
SellPrice[ i ] ==
SellLoss;
priceatbuy = 0;
}
else
Sell[ i ] = 0;
}