Buy Short within a 8 Bars from Signal

Dear Friend,

i am trouble code in the buy & Short condition.

first of all sorry to say my poor English.

when i getting Signal bar (buy or short), buy or short within 8 candle if not candle cross Signal buy or short condition failed, so ignore.

BarBuySig = BarsSince( BuySig );
BarSellSig = BarsSince( SellSig );

Buy 	=	IIf( BarsSince(BuySig)+1 <= BarBuySig AND BarsSince(BuySig)+8 >= BarBuySig, Cross(Close, BuySig ), Null) && TimeNum() <= 144500;
Sell	=	IIf( BarsSince(BuySig)+1 <= BarSellSig AND BarsSince(BuySig)+9 >= BarSellSig, Cross(SellSig, Close), Null) && TimeNum() <= 151000;
Buy		=	ExRem(Buy, Sell);
Sell	=	ExRem(Sell, Buy);

buy

Your code makes no sense...

But if you want to keep signal true for certain number of bars then you may use Hold() function.

BuySignal = /* your signal here */;

Buy = Hold(BuySignal, 8);

Or by looking at picture... if instead you look for higher highs for certain period of bars then use Sum() function.

bars = 8;
is_higher_high = H > Ref(H,-1);
// if there are N higher highs for N number of consecutive bars.
Buy = Sum(is_higher_high, bars ) == bars;

Or if you look for minimum one (or more) higher high occurrences in between certain period then e.g.

bars = 8;
is_higher_high = H > Ref(H,-1);
// if there are higher highs minimum N-times within certain period of bars.
Buy = Sum(is_higher_high, bars ) > 0;// replace '0' by other number

Or whatever you are looking for....

such as combining Hold() with Sum() condition via AND operator.


And as I said previously you need to take more time to create a post. (Optionally ask a friend whether he is better in English).

It really makes it difficult to decipher what you are actually asking.
As always recommended reading.

2 Likes

thanks for guide.

yes, i will better to increase step by step and on further post properly.

i tried to implement a condition, that it should be some logical condition find the candle (xHigh for buying or xLow for Short), which is i called base Nth bar. whatever it is buy & sell or short & cover entry and exit with in 9 candle from base Nth bar (xHigh or xLow). if unable to cross xBar, ignore Buy or short signal we wait fresh next xHigh or xLow.

now i write the Buy condition & Short condition.

Buy = Cross( H, xHigh); 
Short = Cross(xLow, L);

please see the picture better to understand.

buy1

You seem to be looking for Buy stop.
See here

You would just have to add Hold() function to Buy variable as in second example of KB limit order example and would have to modify BuyStopPrice.

/// based on limit order example of
/// @link http://www.amibroker.com/kb/2014/11/26/handling-limit-orders-in-the-backtester/
/// responded to in this thread
/// @link https://forum.amibroker.com/t/buy-short-within-a-8-bars-from-signal/16471/4
BuySignal = /* your Buy signal here */ Cross( Close, MA(Close, 100 ) );

// buy on the next bar
Buy = Ref( BuySignal, -1);
BuyStopPrice = ValueWhen(Buy, High) /* * 1.01 */;

// now we check if buystop was hit
Buy = Hold(Buy, 8) AND H > BuyStopPrice;

// if Open price is above the buystop, then we use Open for entry
BuyPrice = Max( Open, BuyStopPrice );

As for Short... look here.


As for exit within n-bars look at Applystop() function.

thanks for quick repose.:slightly_smiling_face:

i checked above code before read articles, and but signal will get it lately.

thanks to guide.

please see the below image fyr.
image

I will not respond to you anymore until you will eventually be able to create/write understandable post!
Your pics are useless as you show just pictures (no code to be seen). Post full used code but not just pictures without codes! Readers do not sit in front of your screen!

And the marked "High/Low above/below previous high/low" examples are not the only high/low having crossed previous one! What makes one different than other one? What is your exact entry rule?!

This is complete entry and exit code with plot:

Plot( C, "Price", colorDefault, styleBar );

BuySignal = Cross(Close, MA(Close, 100));

// buy on the next bar
Buy = Ref( BuySignal, -1);
BuyStopPrice = ValueWhen(Buy, High) /* * 1.01 */;

// plot initial signal
PlotShapes(Buy * shapeUpArrow, colorAqua, 0, L);

// now we check if buystop was hit
Buy = Hold(Buy, bars = 8) AND H > BuyStopPrice;

// if Open price is above the buystop, then we use Open for entry
BuyPrice = Max( Open, BuyStopPrice );

Sell = 0; 
// sell if some number of bars is reached
ApplyStop( stopTypeNBar, stopModeBars, bars, True );

eq = Equity(1, 0);
SellShape = Sell > 0;
PlotShapes( Buy * shapeUpArrow, colorGreen, 0, L);
PlotShapes( SellShape * shapeDownArrow, colorRed, 0, H); 

And Buy is triggered within n-bars after break of High of most recent start signal.
9

I've given recommendation to you to take your time to create a post and optionally ask friend or anyone else to formulate in better way (There are translation services out there just like there are coding services etc.).

So please post English sentences that can be understood unmistakably! "...poor English..." is not an excuse. You have plenty of time to learn it before writing. No one is rushing you.

"In this candle high cross after next within 8 candle if not cross high ignore signal"

is not English that can be understood. It is part of category "SMS style". It's a mix of words that can mean anything so it is rather English leading to lots of misunderstanding. Once again "...poor English.."* is poor excuse. You try to go easy route but I tell you once again: You have plenty of time to do it properly. Don't rush (as you do right now again) and please don't create posts making others wasting time. (for example, I do not like wasting time at all!)

Last but not least:
Use debug techniques! Learn how to fish!
This is not meant to be spoon feeding forum!!

You have got plenty of examples. Do some efforts yourself first.


1 Like