Limit IOG to either buy or sell per bar

Questions about MultiCharts and user contributed studies.
geoff1983
Posts: 12
Joined: 21 Feb 2016
Has thanked: 2 times

Limit IOG to either buy or sell per bar

Postby geoff1983 » 02 Mar 2016

Hi,

I am fairly new to MC so sorry for the rudimentary question. I have a strategy that buys or sells at the open of the next bar when a simple cross occurs. I need to use IOG so that when the signal is tripped, the buy or sell occurs immediately at the open of the next bar. In backtesting this strategy works great. (Historical pic attached...IOG is operational on the left and turned off on the right)

Today I ran the program in a live simulator and a problem occurred....as they often do in real life. The buy order generation worked great when the signal was tripped. However, because the signal was in "IOG mode", it often tripped the sell order and was therefore just canceling out the trade. (Simulated trading pic attached...IOG is operational on the left and turned off on the right)

Is there a way to have the signal for the sell only occur on the next bar and not IOG? Or that only a buy OR a sell can occur in one bar?

Thanks a ton

Code: Select all

[IntrabarOrderGeneration = TRUE]

inputs:
PriceN( Close data2), //Price for Numerator
PriceD( Close data3), //Price for Denominator
LengthF( 1 ), //Average Period
Opening_Momentum (0), //Buy Signal
Closing_Momentum (0); //Sell Signal

variables:
var_Numerator( 0 ), //Averege Numerator Price
var_Denominator( 0 ), //Averege Denominator Price
var_Ratio( 0 ), //Indicator Ratio
var_Ratio_Momentum( 0 ), //Indicator Momentum
tradesize(0);


tradesize = (10000 / close);
tradesize = tradesize +(Netprofit/close);


var_Numerator = Average( PriceN, LengthF ) ;
var_Denominator = Average( PriceD, LengthF ) ;
var_Ratio = var_Numerator / var_Denominator ;
var_Ratio_Momentum = Momentum( var_Ratio, LengthF ) ;


condition1 = var_Ratio_Momentum < Opening_Momentum ;
condition2 = var_Ratio_Momentum > Closing_Momentum ;


if condition1 then
Buy ( "BUY" ) tradesize shares next bar at open ;

if condition2 then
Sell ( "SELL" ) next bar at Open;
Attachments
AAPL simulated trading 15 minutes.PNG
Live trading feed
(95.28 KiB) Downloaded 903 times
AAPL historical 15 minutes.PNG
Backtesting data
(117.14 KiB) Downloaded 907 times

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Limit IOG to either buy or sell per bar

Postby TJ » 02 Mar 2016

Without IOG, all the signals are calculated at the end of bar (EOB).

ie. these 2 lines have the same effect:

Code: Select all

BUY next bar at OPEN;

BUY next bar at MARKET;

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Limit IOG to either buy or sell per bar

Postby TJ » 02 Mar 2016

Hi,

I am fairly new to MC so sorry for the rudimentary question.
::

Code: Select all

::

if condition2 then
Sell ( "SELL" ) next bar at Open;

Look up the keyword

SELLSHORT

geoff1983
Posts: 12
Joined: 21 Feb 2016
Has thanked: 2 times

Re: Limit IOG to either buy or sell per bar

Postby geoff1983 » 02 Mar 2016

Thanks for the reply TJ. Is there a way to set up the "sell" condition so it cannot occur in the same bar as a "buy"?

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Limit IOG to either buy or sell per bar

Postby tony » 02 Mar 2016

When you declare your variables you need to declare them as intrabarpersist var1(0), etc

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Limit IOG to either buy or sell per bar

Postby tony » 02 Mar 2016

Thanks for the reply TJ. Is there a way to set up the "sell" condition so it cannot occur in the same bar as a "buy"?
By sell do you mean sellshort? If so, yes, add a conditional for each entry of if MP = 0, therefore you'll only buy or sell short if flat. You can also specify how many trade entries per bar are allowed in settings.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Limit IOG to either buy or sell per bar

Postby TJ » 02 Mar 2016

When you declare your variables you need to declare them as intrabarpersist var1(0), etc
Can you explain further on what you mean?

Can you give examples of various scenario where this is applicable?

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Limit IOG to either buy or sell per bar

Postby tony » 02 Mar 2016

When you declare your variables you need to declare them as intrabarpersist var1(0), etc
Can you explain further on what you mean?

Can you give examples of various scenario where this is applicable?
His script is using IOG=True so variables are declared as intrabarpersist var1(0) versus if IOG=False it would be var1(0).

https://www.multicharts.com/trading-sof ... BarPersist

geoff1983
Posts: 12
Joined: 21 Feb 2016
Has thanked: 2 times

Re: Limit IOG to either buy or sell per bar

Postby geoff1983 » 02 Mar 2016

Tony, adjusting the number of trades will not work because the same issue could occur in the next bar where the indicator is tripped during the interbar movement (var_Ratio_Momentum > 0). So even if I set the max trade at 0, this might not be true at the end.

Also, I am only trying to sell, not sellshort.


The way the code should work is the following:

Bar 0: at close, indicator is tripped if it goes below 0.
Bar 1: at open signal is received to buy and buys.

The issue is with IOG the indicator keeps moving around during the period and if it is tripped a sell order is executed.

IOG needs to be on because if not, all this is delayed one period since I think the indicator actually comes just after bar 1 has started. (look at the pictures with IOG "on" on the left and "off" on the right)

Hope this helps a little. Also, do you know if TS has this issue?

geoff1983
Posts: 12
Joined: 21 Feb 2016
Has thanked: 2 times

Re: Limit IOG to either buy or sell per bar

Postby geoff1983 » 02 Mar 2016

the indicator is the bottom chart in the pictures.

geoff1983
Posts: 12
Joined: 21 Feb 2016
Has thanked: 2 times

Re: Limit IOG to either buy or sell per bar

Postby geoff1983 » 02 Mar 2016

is there a way to reference the previous indicator outputs? Since these are based off close pricing, I could add a condition 3 (see below) and have this be part of the "sell" condition. If so, how would you write "condition 3"? Thanks again.

[IntrabarOrderGeneration = TRUE]

inputs:
PriceN( Close data2), //Price for Numerator
PriceD( Close data3), //Price for Denominator
LengthF( 1 ), //Average Period
Opening_Momentum (0), //Buy Signal
Closing_Momentum (0); //Sell Signal

variables:
var_Numerator( 0 ), //Averege Numerator Price
var_Denominator( 0 ), //Averege Denominator Price
var_Ratio( 0 ), //Indicator Ratio
var_Ratio_Momentum( 0 ), //Indicator Momentum
tradesize(0);


tradesize = (10000 / close);
tradesize = tradesize +(Netprofit/close);


var_Numerator = Average( PriceN, LengthF ) ;
var_Denominator = Average( PriceD, LengthF ) ;
var_Ratio = var_Numerator / var_Denominator ;
var_Ratio_Momentum = Momentum( var_Ratio, LengthF ) ;


condition1 = var_Ratio_Momentum < Opening_Momentum ;
condition2 = var_Ratio_Momentum > Closing_Momentum ;
condition3 = var_Ratio_Momentum [n] < Opening_Momentum ;

if condition1 then
Buy ( "BUY" ) tradesize shares next bar at open ;

if condition2 and condition3 then
Sell ( "SELL" ) next bar at Open;

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Limit IOG to either buy or sell per bar

Postby tony » 02 Mar 2016

You can use more than one script / signal for your strategy. You could have one for entry that uses IOG=True and an exit signal that uses IOG=False if you need something like that. I didn't read through your script in detail but wanted to at least share one option for you.

geoff1983
Posts: 12
Joined: 21 Feb 2016
Has thanked: 2 times

Re: Limit IOG to either buy or sell per bar

Postby geoff1983 » 02 Mar 2016

Thanks Tony.

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

Re: Limit IOG to either buy or sell per bar

Postby JoshM » 03 Mar 2016

Is there a way to have the signal for the sell only occur on the next bar and not IOG? Or that only a buy OR a sell can occur in one bar?
Besides the programming suggestions made by others, you can also limit the buy and sells with a manual setting:

Image

You find that setting with a right-click on the chart and selecting 'Format Signals'. Then selecting one of the signals and pressing 'Format', and then go to the 'Properties' tab.

Sometimes the manual setting is more convenient, since it allows you to configure the same signal differently on multiple charts.
Attachments
multicharts-iog-entry-exit-settings.png
(28.03 KiB) Downloaded 968 times


Return to “MultiCharts”