I need to write several buy conditions in a loop. Originally I had one buy
condition (and BuyPrice) outside the loop, and then several exit conditions in
the loop. this worked fine. Now I have added more buy conditions which I want
to test, and tried many times to code this, but I get bizzarre pricing, and I
give up!
Could someone please help by checking (the nasty looking) code sample below,
and pointing out what I'm doing wrong...
I know that there is a really easy way to write this without the loop, and even
nesting buy conditions using IIF, but I need to know how to loop through the
buy conditions, and assign a value to BuyPrice, if a certain buy condition is
met. I then need to test various exit conditions, using that BuyPrice...
// Basic Looping Buy and Sell exercise
//
M1 = Ref(MA(C,14),-1);
C1 = Ref(C,-1);
BuyPrice = 0;
SellPrice = 0;
Buy = 0;
Sell = 0;
Pt = 0.1; //fx AUDJPY
Sl = 0.2;
PriceAtBuy = 0; //Scalar
Exit = 0; //Scalar
for(i=0; i<BarCount; i++)
{
if(Buy[i]==0 AND PriceAtBuy==0 AND C1[i]>M1[i] AND L[i]<=M1[i])
{
Buy[i] = 1;
BuyPrice[i] = M1[i];
PriceAtBuy = BuyPrice[i];
}
else if(Buy[i]==1 AND PriceAtBuy>0) // Reset Buy[i] to zero here?
{
Buy[i] = 0;
BuyPrice = 0;
}
else if(PriceAtBuy>0)
{
if(Exit==0 AND H[i]>=PriceAtBuy+Pt)// why wont the system exit?
{
Sell[i] = 1;
SellPrice[i] = PriceAtBuy+Pt;
Exit = 1;
}
if(Exit==0 AND L[i]<=PriceAtBuy-Sl)
{
Sell[i] = 1;
SellPrice[i] = PriceAtBuy-Sl;
Exit = 1;
}
if(Exit>0) //Reset Scalars
{
Sell[i] = 0;
PriceAtBuy = 0;
Exit = 0;
}
}
}
SetPositionSize( 100000, spsValue );