NexusFi: Find Your Edge


Home Menu

 





Multiple trades in Same Bar


Discussion in Platforms and Indicators

Updated
    1. trending_up 2,894 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 6 posts
    2. attach_file 5 attachments




 
Search this Thread

Multiple trades in Same Bar

  #1 (permalink)
raghavan
Bangalore + India
 
Posts: 13 since Jan 2015
Thanks Given: 3
Thanks Received: 2

I am trying to enter into multiple trades at same bar. In the attached image, I first Long at first bar and then first short in 5th bar and immediately Long in 5th bar. When I backtest, for the 5th bar, I just see Long trade entry and there is no entry for short trade that I've made in 5th bar. May I know why is this and how to get the entry of both short and Long trade details for the same bar in backtest result.

I tried using SetOption("AllowSameBarExit", True ); and SetOption("HoldMinBars", 1); but in vain.

Attached Thumbnails
Click image for larger version

Name:	MultipleTradesInABar.PNG
Views:	170
Size:	7.2 KB
ID:	185616  
Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Trade idea based off three indicators.
Traders Hideout
Quant vue
Trading Reviews and Vendors
About a successful futures trader who didn´t know anyth …
Psychology and Money Management
Better Renko Gaps
The Elite Circle
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 
  #2 (permalink)
 prouser 
Zurich/Switzerland
 
Posts: 79 since Oct 2014

In backtest settings 'General' tab choose 'Long and short' at the 'Positions' setting.

Reply With Quote
  #3 (permalink)
raghavan
Bangalore + India
 
Posts: 13 since Jan 2015
Thanks Given: 3
Thanks Received: 2


I've selected both Long and Short in Settings. I've attached two new images with Time mentioned.

At 09:59, I expect backtest to show me both short and Long trades entry. In the attached snippet(sorted by Date), at 09:59, it just shows Long trade and not the short trade. May I know why is that

Attached Thumbnails
Click image for larger version

Name:	MultipleTradesInABar.PNG
Views:	161
Size:	12.8 KB
ID:	185617   Click image for larger version

Name:	MultipleTradesInABar2.PNG
Views:	168
Size:	23.3 KB
ID:	185618  
Reply With Quote
  #4 (permalink)
 prouser 
Zurich/Switzerland
 
Posts: 79 since Oct 2014

It's some mistake on your end. Upload a project file or send it to the AmiBroker guys. They have a support channel for a reason.

Reply With Quote
  #5 (permalink)
raghavan
Bangalore + India
 
Posts: 13 since Jan 2015
Thanks Given: 3
Thanks Received: 2

Here's the code I am using

 
Code
_SECTION_BEGIN("Test");

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

Buy = Cross(MA(C, 5), MA(C, 20));
Short = Cross(MA(C, 20), MA(C, 5)) OR C<O;

Sell = short;
cover = buy;
PlotShapes(shapeUpArrow*Buy, colorBlue, 0, low);
PlotShapes(shapeDownArrow*Short, colorPink, 0, high);


dist = -12;
fnt = "Arial";
fntsize = 8;
txtdist = 25 - dist;
bi = Barindex();
fvb = FirstVisiblevalue( bi );
lvb = LastVisiblevalue( bi );
for( i = fvb; i <= lvb; i++ ) { // iterate through visible chart area only
        if( Buy[i] ) 
            PlotTextSetFont( StrFormat( "B@\n%g", BuyPrice[ i ] ), fnt, fntsize, i, L[ i ], colorBlue, colorDefault, -txtdist + fntsize );
       	if( Short[i] )
            PlotTextSetFont( StrFormat( "Sh@\n%g", ShortPrice[ i ] ), fnt, fntsize, i, H[ i ], colorYellow, colorDefault, txtdist);
}
_SECTION_END();

At 3:25:59, a short trade and Buy trade is placed. But, when i Backtest, the backtest result just shows the buy trade that I have placed. In addition, I have short trades at 3:18 PM and 3:24 PM and other times. Even they are not part of backtest results.

The last short trade as per backtest result is at 2:40:59 PM. After that, though I've many short trade entries, they are not shown in backtest. The next result after 2:40:59 PM is at 3:25:59 PM and that is where I enter into Buy trade again(which could be result of cover = Buy). Does it mean that Backtest doesnt show any short trades that i've made after 2:40:59 PM until the short trade I placed at 2:40:59 PM is covered back at 3:25:59 PM. Can't I enter into new short trade when I already have other short trade in open.? Please help clarifying this

Attached Thumbnails
Click image for larger version

Name:	MultipleTradesInABar3.PNG
Views:	141
Size:	8.6 KB
ID:	185663   Click image for larger version

Name:	MultipleTradesInABar4.PNG
Views:	169
Size:	88.2 KB
ID:	185664  
Reply With Quote
  #6 (permalink)
 prouser 
Zurich/Switzerland
 
Posts: 79 since Oct 2014

What about taking a look into the documention?
There for example you can see that there are six backtest modes available.
https://www.amibroker.com/guide/afl/setbacktestmode.html
Default one is backtestRegular.

As aside you should better use sections in your code by using status( "action" ) because otherwise you run redundant code in environments where it is not required to be run. This becomes more essential the longer/ the more resource hungry your entire code becomes.


Status action applied in your case:

 
Code
Buy = Cross(MA(C, 5), MA(C, 20));
Short = Cross(MA(C, 20), MA(C, 5)) OR C<O;

Sell = Short;
Cover = Buy;

if( Status( "action" ) == actionIndicator ) { // code for chart pane
	SetChartOptions(0,chartShowArrows|chartShowDates);
	_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
	
	Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

	PlotShapes(shapeUpArrow*Buy, colorBlue, 0, low);
	PlotShapes(shapeDownArrow*Short, colorPink, 0, high);

	dist = -12;
	fnt = "Arial";
	fntsize = 8;
	txtdist = 25 - dist;
	bi = Barindex();
	fvb = FirstVisiblevalue( bi );
	lvb = LastVisiblevalue( bi );
	for( i = fvb; i <= lvb; i++ ) { // iterate through visible chart area only
			if( Buy[i] ) 
				PlotTextSetFont( StrFormat( "B@\n%g", BuyPrice[ i ] ), fnt, fntsize, i, L[ i ], colorBlue, colorDefault, -txtdist + fntsize );
			if( Short[i] )
				PlotTextSetFont( StrFormat( "Sh@\n%g", ShortPrice[ i ] ), fnt, fntsize, i, H[ i ], colorYellow, colorDefault, txtdist);
	}
}

Reply With Quote
  #7 (permalink)
raghavan
Bangalore + India
 
Posts: 13 since Jan 2015
Thanks Given: 3
Thanks Received: 2

Thanks for your input on the status action part.

I went through the setbacktestmode documentation and related doc amibroker.com/guide/h_portfolio.html.

Multiple positions in a bar would be hold back along with exit signals if I use backtestRegularRaw2Multi. I have a below set of code added now.

SetBacktestMode(backtestRegularRaw2Multi); // I also tried with backtestRegularRawMulti
SetOption("AllowSameBarExit", True );
Setoption("holdminbars", 1);

But, Still I dont see two position details for the same bar in Backtest result. I am expecting my backtesting results to show something as below

BANKNIFTY Short 6/12/2015 3:25:59 PM 17521.3
BANKNIFTY Long 6/12/2015 3:25:59 PM 17521.3

May I know what I'm still missing.

Reply With Quote




Last Updated on June 26, 2015


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts