Orders not generating from code

Questions about MultiCharts and user contributed studies.
User avatar
MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 279 times

Orders not generating from code

Postby MAtricks » 30 Aug 2012

I’m having issues with converting some of my stops to only engage at the close of a bar…. I converted the strategy, but it won’t trade. Here’s one of my stops that I rewrote to only use the close:


if marketposition = 1 then
sell ( "L-D" ) next bar at Longstop stop ;
if marketposition = -1 then
buytocover ( "S-D" ) next bar at shortstop stop ;
I changed that code to this:

if marketposition = 1 and close < Longstop then
sell ( "L-D" ) this bar on close ;
if marketposition = -1 and close > shortstop then
buytocover ( "S-D" ) this bar on close ;

Am I doing something wrong? It won’t exit a trade and these are the only changes I made.

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 485 times

Re: Orders not generating from code

Postby Dave Masalov » 31 Aug 2012

Hello MAtricks,

Apparently the conditions are not met. Please use Print keyword for debugging.

I would recommend to try the following signal with Intra-Bar Order Generation on:

Code: Select all

if marketposition = 1 and close < Longstop then
sell ( "L-D" ) next bar on close;
if marketposition = -1 and close > shortstop then
buytocover ( "S-D" ) next bar on close;

User avatar
MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 279 times

Re: Orders not generating from code

Postby MAtricks » 31 Aug 2012

Thanks for the reply.

I didn't have intra-bar order generation set to true because the whole idea of this test is because I do not want intra-bar orders to happen.

The code is from a strategy that I use regularly. It generates orders perfectly. When I change the 4 lines of code to force it to only use the close of the bar, the strategy places no orders.

... I just thought I was somehow missing something

User avatar
JoshM
Posts: 2080
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1483 times
Contact:

Re: Orders not generating from code

Postby JoshM » 31 Aug 2012

MAtricks wrote:The code is from a strategy that I use regularly. It generates orders perfectly. When I change the 4 lines of code to force it to only use the close of the bar, the strategy places no orders.

If you're referring to real-time behaviour (instead of backtest), keep in mind the following:
this bar on Close: Market order on the close of this bar, generally used for historical backtesting purposes only. This will not generate a market on close order.

Source & more info.

There's a feature request to change that behaviour here, but it would be simpler to just change your code to something like:

Code: Select all

if marketposition = 1 and close < Longstop then
   sell ( "L-D" ) next bar at market;
   
if marketposition = -1 and close > shortstop then
   buytocover ( "S-D" ) next bar at market;


(Yes, then you'd need to use IOG again to make sure that the stop loss can also be triggered on the entry bar)

Btw, if you don't want orders to trigger intrabar but still want to trigger stop-losses intrabar, use BarStatus to achieve more control over an IOG strategy.

User avatar
MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 279 times

Re: Orders not generating from code

Postby MAtricks » 31 Aug 2012

JoshM,

Thank you for your suggestions. I appreciate any help.

The fix was longstop[1] and shortstop[1] because this was calculated at the close of the bar, we had to look at the last bar instead of this bar :)


Return to “MultiCharts”