Open trades @ limit instead of close

#1
Hello,

help greatly appreciated, thanks in advance.

I am new to AmiBroker and come from TradeStation, WealthLab and Metastock. I am currently trying out AmiBroker since the above three are either getting outdated and unsuported or not available in my region without a brokerage account.

Never have been a programming wizard so would appreciate some help. I have put all my steps in this post so you know what I did and hopefully can point me to the mistakes I made.

I tried to write some basic script that will buy at XX price and will sell at a set profit target.

I started out with the following script:

---------------------------------------------

SetOption("ActivateStopsImmediately", True );

PositionSize =-1;

BuyStop_1 = 14.00;
SellStop_1 = 15.00;

Buy = (BuyStop_1);
Sell = (SellStop_1);

---------------------------------------------

Unfortunatly that did not work. Apperantly AmiBroker is not that simple :)

After some research I found that the following works a little better...

---------------------------------------------
SetOption("ActivateStopsImmediately", True );

PositionSize =-1;

BuyStop_1 = 14.00;
SellStop_1 = 15.00;


Buy = Cross(BuyPrice, BuyStop_1);
Sell = Cross(SellPrice, SellStop_1);
---------------------------------------------

The problem I am left with with this script is that AmiBroker now does buy, but it buys on the close after the crossing has happened.

I however want the buy's and sells to be limit buys and sells that can be triggered intraday. (ofcourse, if the open is lower than the limit, we buy @ open)

I found part of the solution (entry now works) and ended up with the below script but... for the exit, I am still searching for the solution....

-------------------------------------------

BuySignal = (L>14.01);

// buy on the next bar
Buy = Ref( BuySignal, -1);

BuyLimitPrice = 14.00;
SellLimitPrice = 15.00;


// now we check if limit was hit
Buy = Buy AND L < BuyLimitPrice;

// if Open price is below the limit, then we use Open for entry
BuyPrice = Min( Open, BuyLimitPrice );

Sell = Cross( EMA( Close, 45 ), Close );

------------------------------------------

Buy's now happen on the limit (or better) but I can't find a way to prefent the sells from happening on the close.


If anyone can help, thanks in advance !
 
#3
Yes, thanks, that is where the code comes from indeed.

Unfortunatly it only talks about opening trades and not about closing them. I asume they use the fixed exits... as you can see in the code, I want to use my own exit.
 
#5
Sounds so easy :)

Think I got it. Lots of trial and error but I have it trading on my limits now.

-----------------------------------

BuySignal = (L>14.01);
SellSignal = (L<14.00);

Buy = Ref( BuySignal, -1);
Sell = Ref( SellSignal, -1);

BuyLimitPrice = 14.00;
SellLimitPrice = 15.00;


Buy = Buy AND L < BuyLimitPrice;
Sell = Sell AND H > SellLimitPrice;

// if Open price is below (above) the limit, then we use Open for entry (exit)
BuyPrice = Min( Open, BuyLimitPrice );
SellPrice = Max(Open, SellLimitPrice);

--------------------------------------------

Hope I am doing everyting correct.... can't find anything wrong in the trades that are generated but maybe someone sees something that I have done wrong... Would love any info.

Now I face a bigger problem....

Is there a way to incorporate more positions on different levels ?

I would also like to buy @ 13.00 (and sell that position again @ 14) and buy @ 12 (and sell @ 13) etc. all the way down...

Can you "name" positions so correct positions are closed ?
 

Similar threads