NexusFi: Find Your Edge


Home Menu

 





buy next bar... and set stop immediately and not for the bar after


Discussion in EasyLanguage Programming

Updated
      Top Posters
    1. looks_one Nicolas11 with 4 posts (6 thanks)
    2. looks_two Jura with 2 posts (8 thanks)
    3. looks_3 Big Mike with 1 posts (2 thanks)
    4. looks_4 Speculationist with 1 posts (0 thanks)
      Best Posters
    1. looks_one Jura with 4 thanks per post
    2. looks_two Big Mike with 2 thanks per post
    3. looks_3 ejtrader with 2 thanks per post
    4. looks_4 Nicolas11 with 1.5 thanks per post
    1. trending_up 18,306 views
    2. thumb_up 20 thanks given
    3. group 7 followers
    1. forum 13 posts
    2. attach_file 7 attachments




 
Search this Thread

buy next bar... and set stop immediately and not for the bar after

  #1 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,769

Hello,

I am facing the following difficulty with Easy Language in MultiCharts.

Typically, I have the following code which buys and sets a stop-loss (on EUR.GBP 5 min):

If MarketPosition = 0 AND condition1 then
Buy next bar at xxx stop;
If Market Position = 1 then
Sell ("stop loss") next bar at yyy stop;


This work fines except when the "buy" bar is a long bear candlestick and when the stop-loss level is reached within this "buy" bar. In such case, the stop is not triggered within the "buy" bar, since the stop-loss is only activated on the bar after the "buy" bar.

Such behaviour seems consistent with the code, but is more conservative than in real life (where the stop order could have been placed just after the buy, and the stop actually triggered in the same 5-minute window).

Do you confirm that there is no way to avoid this, or am I missing something?

( I think that Bar Magnifier does not help. With Bar Magnifier (1 min), MultiCharts could see that the stop-loss level would have been reached after the "buy", within the same "buy" 5 minutes bar. So he could trigger it... but the code just activates the stop loss for the next 5-minute bar. )

Thanks in advance for your help! :-)

Nicolas

Attached Thumbnails
Click image for larger version

Name:	Image1.jpg
Views:	442
Size:	34.4 KB
ID:	54242  
Visit my NexusFi Trade Journal Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
ZombieSqueeze
Platforms and Indicators
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
Trade idea based off three indicators.
Traders Hideout
Could it be that currency futures are way easier to trade?
Currencies
Better Renko Gaps
The Elite Circle
 
  #3 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,460 since Jun 2009
Thanks Given: 33,234
Thanks Received: 101,655


Bar Magnifier takes non-tick level bar types (minute, daily) and adds tick accuracy to them behind the scenes. But for tick level dataseries bar types like Range or Volume, you cannot use Bar Magnifier (pointless).

IntraBarOrderGeneration is what allows you to place and manage orders intrabar. By default it is off, you need to turn it on in the Format section of your strategy. When on, the strategy is evaluated on a tick-by-tick basis and when your conditions are met new orders are generated.

With regards to backtesting, MultiCharts by default assumes a worst case scenario. As you know, when testing using minute data (without bar magnifier), the order of the OHLC is not known. So in your scenario, it is not known if you bought before or after the low (and thus, your stop) was made. But MultiCharts assumes the worst case already. Be careful, as other platforms assume the best case.

But with IOG and Bar Magnifier, you can be assured backtest results are as accurate as possible.

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #4 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,769

Thanks Mike and sorry for this late reply.

Let's suppose that I work with 5-minute bars.

I would like the calculation (value of condition1) to be done on the close of the bar only, and not during the bar.
However, I would like the stop order to be sent immediately after MarketPosition = 1 during the same 5-minute bar as the "buy", and not waiting for the next bar, in order that the stop could be trigerred (if necessary) during the same bar as the "buy".

I have the feeling that these requests are contradictory with respect to IBOG and/or Bar Magnifier.

Has some one an opinion on this subject?

Thanks in advance for any help!

Nicolas

Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #5 (permalink)
 kzaorski 
Warsaw, Poland
 
Experience: Beginner
Platform: MultiCharts
Broker: IB, IQFeed
Trading: TF
Posts: 5 since Aug 2011
Thanks Given: 13
Thanks Received: 6

Hi,
You can try this:

setstopcontract;
setprofittarget(target$$$);
setstoploss(loss$$$);

Reply With Quote
Thanked by:
  #6 (permalink)
 
Jura's Avatar
 Jura   is a Vendor
 
Posts: 775 since Apr 2010
Thanks Given: 2,352
Thanks Received: 690


Nicolas11 View Post
I would like the calculation (value of condition1) to be done on the close of the bar only, and not during the bar.
However, I would like the stop order to be sent immediately after MarketPosition = 1 during the same 5-minute bar as the "buy", and not waiting for the next bar, in order that the stop could be trigerred (if necessary) during the same bar as the "buy".

I have the feeling that these requests are contradictory with respect to IBOG and/or Bar Magnifier.

Don't know if I follow you completely, but if you submit a buy order for the next bar (which gets executed on the first tick of that bar), and also submit a stop order for that bar, you'll get a stop submitted on the "right bar" and not the bar after entry? (There's no need for the Bar Magnifier for that as far as I know)

In other words, buy next bar and submit stop for next bar will not (in my opinion) give you the scenario pictured in your first post (buying on bar 1 and that the stop gets triggered on bar 2).

Btw, since you want to do the calculations on the last tick of the bar, per definition the buy/sell orders need to be submitted for the next bar, because if the last tick is evaluated by MultiCharts, you can't trade that bar any more (since it's the last tick). So, the buy and sell orders need to be submitted for the next bar, and if the stop orders are also submitted for the next bar, then the stop would get executed on the same bar that the trade was entered.

Perhaps a picture says its better:


Code:
 
Code
[IntraBarOrderGeneration = TRUE]    

Variables: maFast(0), maSlow(0), longCondition(False), shortCondition(False), posSize(10000);

maFast = XAverage(Close, 5);
maSlow = XAverage(Close, 15);

longCondition     = maFast > maSlow;
shortCondition = maFast < maSlow;

// Only on close of bar
if BarStatus(1) = 2 then begin

    if longCondition = True then    
        Buy ("EL") posSize contracts next bar at market;

        
    if shortCondition = True then     
        SellShort ("SE") posSize  contract next bar at market;
        
end;

// Transmit stop orders
if MarketPosition(0) = 1 then
    Sell ("XL Stop") posSize contracts next bar at (Low - 0.0001) stop;
    
if MarketPosition(0) = -1 then
    BuyToCover ("XS stop") posSize contracts next bar at (High + 0.0001) stop;
Warning: the code above was written to generate a order for every bar. Don't apply it to a chart with a lot of bars, since the calculation will take up a lot of CPU and can make MultiCharts unresponsive.

Regards,
Jura

Reply With Quote
Thanked by:
  #7 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,769

Thanks @ Jura and sorry for this late answer.

I have thought again about this issue...

My rephrased question is the following: After having entered "at market" (= at the open), is it possible to have a stop loss triggered on the same bar as the said entry?

My conclusion is: it depends on the way the stop loss is coded.

1. With a manual stop loss, answer is NO.

I take the example of a (stupid) signal which enters long at market after an inside bar, with a 5 ticks stop loss.

 
Code
Variables:
    TickSize    ( MinMove / PriceScale );
    
if MarketPosition = 0 and H < H[1] and L > L[1] {inside bar} then begin
    Buy ("Entry LONG") next bar at market;
end;

if MarketPosition = 1 then begin
    Sell ("Stop Loss LONG") next bar at EntryPrice - 5 * TickSize or lower;
    Sell ("Take Profit LONG") next bar at EntryPrice + 5 * TickSize or higher;
    if BarsSinceEntry >= 3 then sell ("Time Exit LONG") next bar at market;
end;
The stop loss begins to be considered only at the bar following the entry:


2. With a built-in stop loss (SetStopLoss), answer is YES.

 
Code
Variables:
    TickSize    ( MinMove / PriceScale );
    
if MarketPosition = 0 and H < H[1] and L > L[1] {inside bar} then begin
    Buy ("Entry LONG") next bar at market;
    SetStopLoss(5 * TickSize * BigPointValue);
end;

if MarketPosition = 1 then begin
    Sell ("Take Profit LONG") next bar at EntryPrice + 5 * TickSize or higher;
    if BarsSinceEntry >= 3 then sell ("Time Exit LONG") next bar at market;
end;
With such built-in stop loss, stop loss can be triggered as soon as the entry bar:


Note. It is consistent with Easy Language documentation.

"SetStopLoss function is evaluated intra-bar and not only on close of a bar, and can exit within the same bar as the entry."

"Normally strategies generate orders on the close of the bar for execution on the next bar, SetStopLoss allows you to generate orders and exit on the same bar as the bar of entry, this is especially useful when working with longer during bars, (e.g. 30-min, 60-min, daily, weekly, monthly)."

Any comment welcome!

Nicolas

Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #8 (permalink)
 ejtrader 
Portland, OR
 
Experience: Intermediate
Platform: Sierra Chart
Trading: ES
Posts: 269 since Jan 2011
Thanks Given: 327
Thanks Received: 180

For the first case - can you try to set the option "allow unlimited entries"? If I remember correctly it should work fine.

Signal -> format

- for the entry code - you would have to make sure you don't enter it one more time ( probably need to use barssinceentry to filter out the re-entry trades for the bar).

Attached Thumbnails
Click image for larger version

Name:	MC_Unlimited_Entries.jpg
Views:	259
Size:	41.6 KB
ID:	61130  
Reply With Quote
Thanked by:
  #9 (permalink)
 
Jura's Avatar
 Jura   is a Vendor
 
Posts: 775 since Apr 2010
Thanks Given: 2,352
Thanks Received: 690


Nicolas11 View Post
(..)
My conclusion is: it depends on the way the stop loss is coded.

1. With a manual stop loss, answer is NO.

2. With a built-in stop loss (SetStopLoss), answer is YES.

Hi Nicolas,

Your conclusion is correct, but there is a third way:

3. With a manual stop loss, and IntrabarOrderGeneration set to True, the answer is Yes.

Let's walk through an example: with your 'inside bar stop loss code'-strategy, I used this code...

 
Code
Variables:
    TickSize    ( MinMove / PriceScale );
    
if MarketPosition = 0 and H < H[1] and L > L[1] {inside bar} then begin
    Buy ("Entry LONG") next bar at market;
end;

if MarketPosition = 1 then begin
    Sell ("Stop Loss LONG") next bar at EntryPrice - 1 * TickSize or lower;
    Sell ("Take Profit LONG") next bar at EntryPrice + 5 * TickSize or higher;
    if BarsSinceEntry >= 3 then sell ("Time Exit LONG") next bar at market;
end;
(only changed the stop loss to one tick since I'm testing it on another instrument)

...and got the following result:



(Which is exactly what you've got in your post).

However, if we use IntrabarOrderGeneration set to true (meaning that on every tick the strategy logic is evaluated and on every tick orders can be submitted) we'll get the following code:

 
Code
[IntrabarOrderGeneration = TRUE]

Variables:
    TickSize    ( MinMove / PriceScale );
    
if (BarStatus(1) = 2) and // only submit buy orders at the close of the bar
    (MarketPosition = 0) and H < H[1] and L > L[1] {inside bar} then begin
       
        Buy ("Entry LONG") next bar at market;

end;

// In the code below, there is no BarStatus check; these orders are submitted on a tick by tick basis.
//        So if the buy is executed on tick 1 of the bar, the stop loss should be submitted on tick 2 of the bar.
if (MarketPosition = 1) then begin 
    Sell ("Stop Loss LONG") next bar at EntryPrice - 1 * TickSize or lower;
    Sell ("Take Profit LONG") next bar at EntryPrice + 5 * TickSize or higher;
    if BarsSinceEntry >= 3 then sell ("Time Exit LONG") next bar at market;
end;
...this gives the following result:




PS: No need to apologize for a late answer.

Reply With Quote
  #10 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,769


Thanks a lot, Jura!

This "BarStatus" was exactly what I was missing in order the entry order to be triggered on the close, but the exit order to be triggered any time (in the context of IntrabarOrderGeneration = true).

And I love this feeling of having learnt a new thing!

Thanks again,

Nicolas

Visit my NexusFi Trade Journal Started this thread Reply With Quote
Thanked by:




Last Updated on November 11, 2019


© 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