[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [trading] RE: [amibroker] Bitten the bullet on loops



PureBytes Links

Trading Reference Links

Terry & Jeff, thanks very much for your help. I'm still trying to get my head around your code Jeff, but it does look the way to go. Thanks again.
 
Terry, I copied the example given by Tomasz & tried to mangle it to suit my criteria.
 
"/* a sample low-level implementation of Profit-target stop in AFL: */
Buy = Cross( MACD(), Signal() );

priceatbuy=0;

for( i = 0; i < BarCount; i++ )
{
     if( priceatbuy == 0 && Buy[ i ] )
     priceatbuy = BuyPrice[ i ];

     if( priceatbuy > 0 && SellPrice[ i ] > 1.1 * priceatbuy )
     {
       Sell[ i ] = 1;
       SellPrice[ i ] = 1.1 * priceatbuy;
       priceatbuy = 0;
     }
     else
       Sell[ i ] = 0;
}"

In essence what I was trying to code was this:
if Buycond1 is met THEN Buy on Open NEXT bar
Sell conditions are based on one of 2 possibilities:
1) The High of this bar or subsequent bars exceeded my "Buyprice  + StopProfit  " threshold by a set amount of pips ( if 2nd condition wasn't met first)
2) The Low of this or subsequent bars ( if first sell condition not met) crossed my "Buyprice - my StopLoss" threshold 
 
Most of my entry/exits in my strats are based on entering *instantly* that a certain price is met & it goes the same for my exits. And often I need to know the LastValue of a variable as part of the Buy/Short & Sell/Cover conditions.
 And this is where I have difficulty as the general assumption is that you buy on a bars close or the next open and cover/sell on close or next open. I always try and make sure my Buycond is met on previous bar, so I'm never incorrectly buying on open when the Buycond wouldn't be known until close of that bar.
I'll study what's been you & Jeff have written and see if I can get any closer to mastering this.
cheers
----- Original Message -----
From: Terry
Sent: Friday, September 30, 2005 8:47 AM
Subject: [trading] RE: [amibroker] Bitten the bullet on loops

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!

--

Terry

 

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;

}



Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html





YAHOO! GROUPS LINKS