Buy at limit of average of last 2 candles


please help me code this

i want to buy when Rsi(close,18) > 30 but

the buy price should be the limit price of average of high of previous candles ref(high,-1) and close of previous to previous candle ref(close,-2)

sell when Rsi(close,18) < 30

acutly i want to use robo trader to automate.But my order are being punched at Market price .

thank you

@jass17, for best results (i.e. help), please include what you have done so far.

Show us your work, explain what you can’t get working, and we can usually help you out.

NOTE 1: Always use the Code Tags when posting code.

NOTE 2: Use the SEARCH feature (magnifying glass up in top right by your avatar) to see if you can find something similar already discussed.

NOTE 3: Use the Members Site and Search there for MANY solutions.

Good Luck.

1 Like

Buysignal = Rsi(close,18) > 30 AND Ref(close,-1) < 30;

buylimitprice = (Ref(high,-1) + Ref(close, -2)) / 2 ;

BUY = iif ( buysignal AND Buylimitprice , Min (open , buylimitprice, Null) ;

But what it is doing is buying at the open price and also at limit price.
so please help to optimize it

what i want is if Rsi(18) crosses above 30 the buy on nxt candle at price of average of last 2 candles

HI snoopy

thanks for ur reply man.Below is my full code

The code you’ve shown never sets the BuyPrice variable. In addition, your Buy statement is incorrect. Try something like this:

Buy = Ref(buySignal,-1) AND L < buyLimitPrice;
BuyPrice = IIf(Buy, Min (open , buyLimitPrice), -1);

Translation: Only generate an actual Buy signal when your entry conditions were met yesterday AND today’s price falls below the limit price. Set the entry price for the trade (BuyPrice) to be the limit price unless the open gaps down below the limit, in which case use the open instead of the limit as your entry price. If there was no Buy signal today, then just set the BuyPrice to -1.



Hi mradtke,
thanks for your time man

i have done as you suggested, But still m getting 2 buy orders
First on open of new candle after buysignal
and second when price goes below limitprice

i dont knw if its due to code Or Auto trader :-/

The code that I gave you should work for a simple back test on EOD data where you’re not concerned with having competing orders or having more setups (limit orders) than you have available capital to fund. If you’re doing real-time scans or back tests and trying to hook that up to some type of auto trader, then you’ll need to put some more thought into it.

My advice would be to test the AB output in isolation first. When you 100% comfortable that it’s behaving as you want, THEN add the auto trading to the mix.

Actualy i use 10 mins chart.
And strange thing is on back test the code is working fine.The buying price in Backtest is Average price

But on Bar replay on is buying on open and limit both

I am using paid Auto trader.So need not to worry about its code. And they had said it buys on market price

Just because you paid for your auto trader software doesn’t guarantee that it works perfectly, and it most certainly doesn’t imply that you’re actually using it correctly. My advice remains the same: make sure you understand how AmiBroker works, and that your logic is producing the results you expect. Then do the same with the Auto Trader. If it’s being driven by a chart showing real-time data, then how does it behave when your chart gets redrawn but you’re still on the same 10-minute bar?

For future reference, if you want people to help you solve a problem it would be best if you provided as many details up front as possible. In other words, try to make it easy for others to help you. When you give a few more details each time you post another message, it means you’ve been expecting people to guess what you’re trying to do which is counter-productive.

i swear that 10 mins was the last thing i forgot to mention :joy::rofl::joy::sweat_smile:

Rest my code and what i am trying to do is mentioned in my 2nd msg .

Whole thing m asking is

My code is buying at Openprice and Limit price.
How to rectify it to buy it on limit price :neutral_face:

Hi @Tomasz

Sorry for bothering you.But can you please help me with this.You are my last hope.


@mradtke answered you already. Also there is article in the KB that covers that subject and couple of threads in this forum if you search for “limit order”

hey, thanks everyone for your time to revert
But what i noticed is
Buy = Ref(buySignal,-1) AND L < buyLimitPrice

the above statement does not throw at limit price but

Buy = (Ref(buySignal,-1) =1) AND L < buyLimitPrice

this statement throws price at limit

what is the difference b/w the two?

thanks for this.. has been a gud help.