How to enter with Bar Magnifier?

Questions about MultiCharts and user contributed studies.
orjan
Posts: 12
Joined: 18 Jan 2015
Has thanked: 3 times

How to enter with Bar Magnifier?

Postby orjan » 19 Jan 2015

When previous days low has been taken out, I want to go long within the same bar directly after the price goes above the previous days high.

But my code buys, wrongly, at the high of the current bar. See pic, blue arrow. I use Intrabar Order Generation and Bar Magnifier (1 second) because enter and exit should be within the same bar. It seems like entry is taken at the last second bar high, but I don't know how to solve it. Any suggestions how to enter at correct price (1 tick above last days high)?

Code: Select all

Condition1= L<L[1] AND Marketposition = 0;
If Condition1 then Buyprice = H[1] + 0.0001;

If Condition1 Then buy ("Long") next bar at Buyprice stop;
Attachments
Outside entry.JPG
(10.25 KiB) Downloaded 865 times

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: How to enter with Bar Magnifier?

Postby Andrew MultiCharts » 19 Jan 2015

Hello orjan,

I recommend you to print order generation conditions and values related to it, as well as current bar values, to see at what particular moment the order was generated. You can find a good example of code how to do this right here.

orjan
Posts: 12
Joined: 18 Jan 2015
Has thanked: 3 times

Re: How to enter with Bar Magnifier?

Postby orjan » 19 Jan 2015

Thanks Andrew,

I did as you said and only one entry was plotted in the chart (2011-02-22) and I got this message in Output:

====================
The date is 2015-01-19,
time is 17:00:00,
bar # is 1315.00,
current bar open is 1.16,
current bar high is 1.16,
current bar low is 1.16,
current bar close is 1.16,
market position on chart is 1.00,
market position at broker is 0.00,
Condition1 is false, so no order is generated.
======================
But I dont understand how that can help me. I should say that I am backtestning, no live trading. Do you think it is something wrong with the code (Bar Magnifier 1 second), here is the complete code:

Code: Select all

[IntrabarOrderGeneration=True]
Inputs:
Losslong(0.0010),
PTlong(0.0010);

Vars: Buyprice(0);

Condition1= L<L[1] AND Marketposition = 0;
If Condition1 then Buyprice = H[1] + 0.0001;

If Condition1 AND (barssinceexit(1) >1 OR totaltrades <1) Then buy ("Long") next bar at Buyprice stop;

If Marketposition = 1 then sell ("PTlong") next bar from entry ("Long") at entryprice + PTlong or higher;
If Marketposition = 1 then Sell ("SLlong") next bar from entry ("Long") at entryprice - Losslong or lower;

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: How to enter with Bar Magnifier?

Postby JoshM » 20 Jan 2015

When previous days low has been taken out, I want to go long within the same bar directly after the price goes above the previous days high.

But my code buys, wrongly, at the high of the current bar. See pic, blue arrow. I use Intrabar Order Generation and Bar Magnifier (1 second) because enter and exit should be within the same bar. It seems like entry is taken at the last second bar high, but I don't know how to solve it. Any suggestions how to enter at correct price (1 tick above last days high)?
If I understand you correctly, your code looks correct to me, with an entry just a bit above the high of the previous bar:

Image

I did rewrote your code since that helps me spot errors, but didn't change its logic (besides adding an exit):

Code: Select all

[IntrabarOrderGeneration = true];

Variables:
enterLong(False),
buyPrice(0);

enterLong = (Low < Low[1]) and (MarketPosition(0) = 0);

if (enterLong) then begin

buyPrice = High[1] + 0.0001;
Buy ("Long") 1 contract next bar at buyPrice stop;

end;

if (BarStatus(1) = 2) and (MarketPosition(0) = 1) then
Sell ("XL") all contracts next bar at market;
Edit: was there a forum issue today? I'd swear that post #3 was not there when I replied in this topic. But that post's time stamp says it had to be there, even if I didn't saw it for some reason.
Attachments
scr.20-01-2015 12.18.34.png
(870 Bytes) Downloaded 992 times

orjan
Posts: 12
Joined: 18 Jan 2015
Has thanked: 3 times

Re: How to enter with Bar Magnifier?

Postby orjan » 20 Jan 2015

Andrew,

Yes, you understood my entry strategy. And the entry works now, thank you.

But I want to exit the trade in the same bar as I enter. The reason why I use "next bar" in the code is that I thought that "next bar" in conjunction with Bar Magnifier means next bar in the lower resolution (higher resolution in this case is daily, and lower resolution is 1 second).

Look at my two lines of sell code. The line for profit is essentially the same as the line for loss. But the strange thing is that loss is taken where I want (in the same bar as entry) but the profit is faulty taken in next DAILY bar. Se chart. Do you know how to code so also the profit is taken in the same daily bar as entry?

Code: Select all

[IntrabarOrderGeneration = true];

Inputs:
profittarget(0.0010),
stoploss(0.0010);

Variables:
enterLong(False),
buyPrice(0);

enterLong = (Low < Low[1]) and (MarketPosition(0) = 0);

if (enterLong) then begin

buyPrice = High[1] + 0.0001;
Buy ("Long") 1 contract next bar at buyPrice stop;

end;

if (BarStatus(1) = 2) and (MarketPosition(0) = 1) then
Sell ("XLPT") all contracts next bar at entryprice + profittarget or higher; // same daily bar as entry //
Sell ("XLSL") all contracts next bar at entryprice - stoploss or lower; // same daily bar as entry //
Attachments
Pic2.JPG
(13.98 KiB) Downloaded 859 times

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: How to enter with Bar Magnifier?

Postby Andrew MultiCharts » 21 Jan 2015

I did as you said and only one entry was plotted in the chart (2011-02-22) and I got this message in Output:

====================
The date is 2015-01-19,
time is 17:00:00,
bar # is 1315.00,
current bar open is 1.16,
current bar high is 1.16,
current bar low is 1.16,
current bar close is 1.16,
market position on chart is 1.00,
market position at broker is 0.00,
Condition1 is false, so no order is generated.
======================
But I dont understand how that can help me. I should say that I am backtestning, no live trading.
Look at my two lines of sell code. The line for profit is essentially the same as the line for loss. But the strange thing is that loss is taken where I want (in the same bar as entry) but the profit is faulty taken in next DAILY bar. Se chart. Do you know how to code so also the profit is taken in the same daily bar as entry?

Code: Select all

[IntrabarOrderGeneration = true];

Inputs:
profittarget(0.0010),
stoploss(0.0010);

Variables:
enterLong(False),
buyPrice(0);

enterLong = (Low < Low[1]) and (MarketPosition(0) = 0);

if (enterLong) then begin

buyPrice = High[1] + 0.0001;
Buy ("Long") 1 contract next bar at buyPrice stop;

end;

if (BarStatus(1) = 2) and (MarketPosition(0) = 1) then
Sell ("XLPT") all contracts next bar at entryprice + profittarget or higher; // same daily bar as entry //
Sell ("XLSL") all contracts next bar at entryprice - stoploss or lower; // same daily bar as entry //
If the stop loss is filled, then the only reason why the profit target is not filled at the same bar is that price never reached the profit target, or it reaches the stop loss first.

The idea of prints from the article was not to copy-paste the piece of code with prints and expect it to work right away, but to develop your own print code to check every variable, every condition, every value that may be involved in order generation and in order execution.

orjan
Posts: 12
Joined: 18 Jan 2015
Has thanked: 3 times

Re: How to enter with Bar Magnifier?

Postby orjan » 21 Jan 2015

- "If the stop loss is filled, then the only reason why the profit target is not filled at the same bar is that price never reached the profit target, or it reaches the stop loss first."

Yes. But in no trade at all in my backtest is profit filled in the entry bar. Despite the profit target price is reached in the entry bar several times after entry, in almost every trade, according to a manual 1 minute bar check. Instead the profit target is filled at the open of the next Daily bar.

Stop loss, on the other hand, is always filled at the right place (almost every time within the entry bar)

Conclusion:
The expression "next bar" in my code seems to mean next bar in the lower resolution in this line:

Code: Select all

Sell ("XLSL") all contracts next bar at entryprice - stoploss or lower;
And the expression "next bar" seems to mean next bar in the higher resolution in this line:

Code: Select all

Sell ("XLPT") all contracts next bar at entryprice + profittarget or higher;
I can not understand why.

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: How to enter with Bar Magnifier?

Postby Andrew MultiCharts » 22 Jan 2015

Could you send us the following files to support@multicharts.com?
  1. The workspace to reproduce the situation.
  2. Export of used symbols (with data) from QuoteManager in .qmd archive: https://www.multicharts.com/trading-sof ... rting_Data
  3. The exported scripts with all dependant functions that are used on the workspace: https://www.multicharts.com/trading-sof ... ng_Studies
  4. Step-by-step instruction how to reproduce the issue.
  5. Number of bar/bars, their date and time where the issue takes place.
  6. The screenshot demonstrating the issue.
After we receive the requested files, someone will study the case and get in touch with you as soon as we have any feedback from engineers.

orjan
Posts: 12
Joined: 18 Jan 2015
Has thanked: 3 times

Re: How to enter with Bar Magnifier?

Postby orjan » 26 Jan 2015

Andrew,

I have sent the files now

Regards
Orjan

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: How to enter with Bar Magnifier?

Postby Andrew MultiCharts » 27 Jan 2015

Sent you an email. There is no issue, everything was calculated and executed as expected according to your code logic and the following rules: https://www.multicharts.com/trading-sof ... Calculated

orjan
Posts: 12
Joined: 18 Jan 2015
Has thanked: 3 times

Re: How to enter with Bar Magnifier?

Postby orjan » 29 Jan 2015

Andrew,
Thank you very much for the email, I dont have time for the moment to work with the code, but I will next week.

Regards
Orjan


Return to “MultiCharts”