Compare buy price after a crossover with previous days EMA

Buy when Close crosses above EMA of (Close,7) at Buyprice of Ref(EMA(Close,7),-1).

Buy = Cross((Close),(EMA(Close,7)));//when Close crosses above EMA of (Close,7)
Prevday EMA = Ref(EMA(Close,7),-1);
Sell =.................

Assume Close crosses above EMA of (Close,7) and at that time buyprice is 1000. The expectation is buyprice will go down further upto 995 which is
equal to Previous day EMA. I would like to buy at buyprice 995. Could you please help me to build coding for it?

@av.ragavan I think you are looking to place a Limit Order. You need to review this Knowledge Base article and identify the price you are looking to enter at.

http://www.amibroker.com/kb/2014/11/26/handling-limit-orders-in-the-backtester/

2 Likes

Thanks. Is this correct?

SetTradeDelays( 1, 1, 0, 0 );
Buysignal = Cross((Close),(EMA(Close,7)));
Buylimitprice1 = Ref(EMA(Close,7),-1);
Buylimitprice = Buylimitprice1 - (0.0050*Buylimitprice1);
Buy = Buysignal AND L < BuyLimitPrice;
BuyPrice = BuyLimitPrice; 

Sellsignal = Cross(EMA(Close,7),(Close));
Selllimitprice1 = Ref(EMA(Close,7),-1); 
Selllimitprice = Selllimitprice1;
Sell = Sellsignal AND H > Selllimitprice;
SellPrice = Selllimitprice;

@av.ragavan no that is not what is written in the article I asked you to read.

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

Also you are now changing what you originally were asking. You wrote that your limit price was going to be the previous bar's moving average,

but in your solution you have

Buylimitprice = Buylimitprice1 - (0.0050*Buylimitprice1);

So that is not the previous days value for the moving average.

I suggest you study how to debug your own code and make an Exploration or run a detailed log backtest to examine what is happening.

I have corrected it now, please check the code below and let me know If I miss anything.

Assume Close crosses above EMA of (Close,7) and at that time buyprice is 1000. The expectation is buyprice will go down further upto 995 which is
equal to (Previous day EMA - (0.005*Previous day EMA)). Buy on next days Open or Buylimitprice whichever is minimum.

SetTradeDelays( 1, 1, 0, 0 );
Buysignal = Cross((Close),(EMA(Close,7)));
Buylimitprice1 = Ref(EMA(Close,7),-1);
Buylimitprice = Buylimitprice1 - (0.0050*Buylimitprice1);
Buy = Buysignal AND L < BuyLimitPrice;
BuyPrice = Min(Open,BuyLimitPrice); 

Sellsignal = Cross(EMA(Close,7),(Close));
Selllimitprice1 = Ref(EMA(Close,7),-1); 
Selllimitprice = Selllimitprice1;
Sell = Sellsignal AND H > Selllimitprice;
SellPrice = Max(Open,Selllimitprice);

@av.ragavan Did you make an attempt to debug your code? Have you run a backtest and examined the detailed log? Let us see those results? Have you coded an Exploration and looked at the signals and prices? Are they the results that you expected? If not, what was different? I have not done that for you so I am not certain what the answer to your question is.

I doubt that you have done all of the above because you have not even read the 5 lines of code that I first referenced. You must carefully read the Knowledge Base article. Slowly, carefully. Why did you not include this line?

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

It does not appear to me that you are making any serious effort at solving your problem and learning how to use AmiBroker.

Good luck, perhaps someone else will hold your hand and write all of your code for you.