Australian (ASX) Stock Market Forum

Preventing a daily bar from becoming both a Buy and a Sell Signal

Joined
12 April 2009
Posts
16
Reactions
4
Hello All ,
Using Amibroker AFL
If the stratagy opens a trade when the daily bar hits the limit buy order and closes the trade on the first up tick bar ( C > O )
sometimes the bar that creates the open of a trade is an up tick bar ( C > O ) .

And so takes on being both a buy signal and sell signal bar because it is an up tick bar ( C > ) )

Would it be possible using the AFL code to prevent this from happening .

All feedback , suggestions and comments would be very helpful

Thanks in advance

michael Bethell
 
Are you trading long or short?
I’m interested in why you’d be selling a long trade on the first uptick bar
it seems counter intuitive?
 
Hello All ,
Using Amibroker AFL
If the stratagy opens a trade when the daily bar hits the limit buy order and closes the trade on the first up tick bar ( C > O )
sometimes the bar that creates the open of a trade is an up tick bar ( C > O ) .

And so takes on being both a buy signal and sell signal bar because it is an up tick bar ( C > ) )

Would it be possible using the AFL code to prevent this from happening .

All feedback , suggestions and comments would be very helpful

Thanks in advance

michael Bethell
Hi Michael

If you’re just starting your trading system journey I would highly recommend looking at Realtest as backtesting software. I used AB for 5+ years and it is good but the learning curve on RT is a easier space to navigate. You will also have better support and the code is more beginner friendly in terms of making errors

Just some food for thought. There’s a thread about it somewhere
 
Hello All ,
Using Amibroker AFL
If the stratagy opens a trade when the daily bar hits the limit buy order and closes the trade on the first up tick bar ( C > O )
sometimes the bar that creates the open of a trade is an up tick bar ( C > O ) .

And so takes on being both a buy signal and sell signal bar because it is an up tick bar ( C > ) )

Would it be possible using the AFL code to prevent this from happening .

All feedback , suggestions and comments would be very helpful

Thanks in advance

michael Bethell
I know most of my system have that code in AB:

// removing buys you don't want
for( i = 0; i < BarCount; i++ )
{
if( Sell[ i ] )
{
Buy[ i ] = False; // remove buy if sell also requested this bar
}
}
Is that what you are talking about?
 
Hi All ,
Thanks for the wonderful feed bac .
Tech/A , its a long only mean reversion system , low exposure , smothes out the equity curve , i can trades this with same capital as the longer term system , thanks

Roller_1 need to stay with Amibroker for the time being thanks

qldfrog thanks , I am very much a learner with all this code , stuff

if its ok I may need to go over the code in english , so lets see if I can kock my brain into gear

cheers

michael bethell
 
Hi All ,
Just to illustrate the situation

cheers

michael bethell
 

Attachments

  • sell bar 1.png
    sell bar 1.png
    29.9 KB · Views: 6
  • sell bar 2.png
    sell bar 2.png
    26.7 KB · Views: 6
Hi All ,


Algozero , thanks for comment already in code , as all exits are next day

SetOption("AllowSameBarExit",False); // Disable same bar exit as the strategy exits open of the following day
cheers

michael bethell
 
You need to explain what you are trying to prevent.

Are you coding a backtest?
Can you show a backtest report where the # bars field shows 1 bar only, when you have set SameBarExit to 0 ?

Or are you talking about exploration?
If you explore for buys and sells at the same time, then it will happen. Why not explore separately?

It seems that none of us understand the problem.
 
Hi All ,
Thanks for comments and suggestions
In an ideal world this is the process I would love to happen every trade

Only Long trades taken , buy to open , sell to close , there are no Short trades , daily bars only , all trades are opened with the daily bar hitting the buy limit order , all open trades are closed if the next day daily bar is an up tic bar ( C > O ) . All trades are closed the next day after the sell signal bar ( an uptick bar ( C>O ) has occurred at the open . No trades are closed at the end of the day.

So # one , # two , # three daily bars

# one daily bar .............. Limit buy price hit trade opened

# two daily bar ........... Close is greater than the open = up tick bar = sell ( close the trade )

# three daily bar ......... trade is closed at the open

I am not suggesting this is good , bad right or wrong , it just is . Of course there are many ways and time frames to close this trade and a stop loss level is monitored .

BUT at present the # one daily bar can take on two signals , if it is an up tick bar ( C > O ) , it can become both a buy signal bar and a sell signal bar

So I was wondering how this might be prevented from occuring using the AFL code.

Thanks to all for reading and commenting

cheers

michael bethell
 
O.k., a standard mean-reversion system, something like this:


PositionScore = [it's up to you] ; // careful, must use Ref( [], -1) or you get a future leak

MaxPos = 5;
TradeSize = 10000;

SetOption("AccountMargin", 100);
SetOption("InitialEquity", 100000);
SetOption("MaxOpenPositions", MaxPos);
SetPositionSize( TradeSize, spsValue);
SetOption("CommissionMode", 2);
SetOption("CommissionAmount", 5); // CAREFUL !!! this is very cheap brokerage, not CommSec
SetOption("AllowSameBarExit", False);

SetTradeDelays (0,0,0,0);

BuyCandidate = [some condition, optional]
AND RSI(2) < 15 // oversold, maybe
AND C > MA(C, 200) // in some sort of an up-trend
AND MA(C * V, 20) > 10000000 // daily turnover 10 Million
;

BuyLimit = Ref(C, -1) * [optional discount, e.g., * 0.99]; //1% lower than yesterday's close

Buy = Ref(BuyCandidate, -1) AND L < BuyLimit ;
BuyPrice = Min(Open, BuyLimit);

Sell = C > O; // this is your up-bar
SellPrice = Close; // I would sell right here while the going is good

but you want to sell on next Open when bad things may happen over night
in that case you set

SellPrice = Ref(O, 1); // that's a future leak but innocent in this backtest


I haven't tested this, simply typed it off the cuff. There may be typos.
Obviously, this will not just run in AmiBroker - lots of syntax errors.

The strategy you have described is likely to lose money hand over fist.
Mean-reversion in equities works with very short holding periods, like intra-day to a few days at most. Howard Bandy always recommended 1 or 2 days.

Here is one like that, it should run in AmiBroker as is:

////////////

PositionScore = Ref(C, -1) / O ; // slight future leak but using Random() also works, just not as profitable

PosQty = 5;
PosSize = 10000;

SetOption("AccountMargin", 100);
SetOption("InitialEquity", 100000);
SetOption("MaxOpenPositions", PosQty);
SetPositionSize( PosSize, spsValue);
SetOption("CommissionMode", 2);
SetOption("CommissionAmount", 5); // CAREFUL !!! Marketech brokerage rate
SetOption("AllowSameBarExit", True); // not what you wanted, I know

SetTradeDelays (0,0,0,0);

BuySig = L < Ref(L, -2)
AND C > MA(C, 250)
AND MA(C * V, 20) > 5000000 // 5 Million here
;

Buy = Ref(BuySig, -1) AND O < Ref(C, -1) ; // buy if Open is less than previous Close
// IMPORTANT, must cancel order immediately after Open if not filled
BuyPrice = Open ;

Sell = 1;
SellPrice = Close ; // sell same day, definitely not what you want

/////////////////////

just an example that works and is profitable
difficult to execute but can be done
I have traded this but it's too much hard work
 
Hi All ,
Thanks for the many comments and suggestions .
Of course , it was me all along , I only had this LimitBuy = ExRem(LimitBuy,ExitSignal) in the code
but i should of had both ExitSignal = ExRem(ExitSignal,LIMITbuy); , my bad

cheers michael b
 
Top