Order handling in signal / multiple signals

Read before posting.
andy4444
Posts: 25
Joined: 14 Nov 2013
Has thanked: 8 times
Been thanked: 2 times

Order handling in signal / multiple signals

Postby andy4444 » 11 Mar 2014

I wonder how others have approached the order handling of your signals? Especially if multiple signals on same instrument.
I paste my code below, and if others could do the same, it would be very much appreciated!
Any general comments on order handling issues you've ran into, or general order handling tips, are also appreciated.

I would ideally like to be able to apply multiple signals on the same instrument, but because of MY MAIN PRIORITY described below, it seems I will have to settle for running them simultaneously, BUT with only one of them being able to have a position open at each time.

MY MAIN PRIORITY is to never be left with an open position without stop order. That's why I always send related stoploss and take profit orders (OCO) at the same time as entry order. (That way I'm protected by native stoploss at broker in case of connectivity issues, order rejections, errors in my code generating multiple orders, etc.)

Below is the code of 2 separate signals (A and B), merged into one signal code. They function independently, but none of them can open a position if the other one is already in position. So far this is the best I've been able to do. Any comments very wellcome!

For those unfamiliar with the topic, here is a link to some basics:
http://www.multicharts.com/trading-soft ... Instrument

Code: Select all

vars:
intrabarpersist SignalAstation(0),
intrabarpersist SignalBstation(0);

[IntrabarOrderGeneration = True];

//SignalA///////////////////////////////////////

if SignalAstation=0 then begin
if close >10 {Just some random logic for the example} then SignalAstation=1;
end;

if SignalAstation=1 and marketposition=0 then begin
buy 1 contracts next bar market;
setstoploss(5);
setprofittarget(10);
SignalAstation=2;
end else SignalAstation=0;

if SignalAstation=3 then begin
if marketposition=0 then SignalAstation=0;
end;

//SignalB////////////////////////////////////////

if SignalBstation=0 then begin
if close <20 {Just some random logic for the example} then SignalBstation=1;
end;

if SignalBstation=1 and marketposition=0 then begin
sell 1 contracts next bar market;
setstoploss(5);
setprofittarget(10);
SignalBstation=2;
end else SignalBstation=0;

if SignalBstation=3 then begin
if marketposition=0 then SignalBstation=0;
end;

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

Re: Order handling in signal / multiple signals

Postby Andrew MultiCharts » 12 Mar 2014

Hello andy4444,
  • If you have 2 or more separate signal scripts applied to the same chart, they will work as one strategy. It means that if your signal A enters, signal B can exit.
  • If you have only 1 signal script applied to a chart, it will work as one strategy. If this signal doesn't have exiting logic, it won't exit any market position opened by itself.
  • If you have 2 or more separate signal scripts applied to different chart windows, they will work individually. It means that if your signal A enters on chart #1, signal B cannot exit this entry from chart #2. More details here.

andy4444
Posts: 25
Joined: 14 Nov 2013
Has thanked: 8 times
Been thanked: 2 times

Re: Order handling in signal / multiple signals

Postby andy4444 » 12 Mar 2014

Thanks Andrew.
If I write "if marketposition=0", am I right in assuming that this only checks whether the position on the particular chart is zero. ? I.e. if there is a missmatch between position on chart and position with broker, in this case the "marketposition" represents the strategy's position, not the brokers?

Would be interested to see anyone elses way of preventing order/position -hickups resulting from connectivity issues, order rejections, errors in your code generating multiple orders, etc.)

Thanks
Andy

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

Re: Order handling in signal / multiple signals

Postby Andrew MultiCharts » 13 Mar 2014

Let me explain how auto-trading in MC works so you will understand what keyword to use.

There are 3 separate positions when you auto-trade in MC:
  1. Broker position. This is net position for a traded security at your broker account that takes into consideration all trades.
  2. MC auto-trading engine position. This is inner position calculated for the strategy in the software.
  3. MC chart position. This is the position generated by orders visually executed on chart. It has nothing to do with open position horizontal drawing shown when you open chart trading panel.
When you auto trade in SA (sync. mode) the 2 and 3 are the same, what is you see on chart in synchronized with auto-trading engine. When you use AA (async. mode), you may have to deal with 3 different positions: what you have on broker, what you see on chart, what MC thinks it is.

Now the 3 market position checking keywords:
  1. MarketPosition_At_Broker. Returns broker position.
  2. MarketPosition_At_Broker_For_The_Strategy. Returns MC auto-trading engine position.
  3. MarketPosition. Returns MC chart position (only direction).
If you use SA mode, trade a security only from 1 chart and don't place manual orders, you can compare MarketPosition_At_Broker and MarketPosition_At_Broker_For_The_Strategy in your code to find out if strategy is ok.

andy4444
Posts: 25
Joined: 14 Nov 2013
Has thanked: 8 times
Been thanked: 2 times

Re: Order handling in signal / multiple signals

Postby andy4444 » 13 Mar 2014

Andrew.
I have read about most of the above, but had not gotten a full grasp.
Your post above explained it all, in a short and concise manner, yet giving a complete overview.
THANK YOU!

andy4444
Posts: 25
Joined: 14 Nov 2013
Has thanked: 8 times
Been thanked: 2 times

Multiple signals on the same instrument and broker

Postby andy4444 » 20 Mar 2014

I have the below questions to clear the last doubts on how to run 2 different signals on the same instrument.
The below example and questions, are all for ONE instrument and ONE account. Account has no positions or open orders when signals are initialized. Using SA (sync. mode).

Scenario 1 (only one script and one chart, to demonstrate how the script is intended to run):
Only Signal A (script below) is applied to a chart. Account has no positions or open orders when signal A is initialized. Market price is 9.
Price rises to 11, at which point Signal A’s logic moves from station 0 to station 1, buys 1 contract at market and sets stoploss to 5 and profit target to 10. Then logic moves to station 2 at which logic waits until marketposition is 0 (i.e. either stoploss or take profit was executed), at which point logic moves back to station 2 and repeats the loop when entry condition is met next time.

Scenario 2 (the questions refer to this scenario):
I have 2 signals, in 2 different scripts, called A and B. I apply each to a separate chart. I.e. Chart A and chart B. Signals scripts are identital, except for different entry-trigger price, and different take-profit amounts.
I initiate both signals when price is 9.
Signal A enters long when price >10, and sets profit target to 15 usd.
Signal B enters long when price >12, and sets profit target to 20 usd.

Questions:

1: When price has reached 11, and Chart A has an open long position, the “marketposition” script in CHART A will return the value “1”.
At this time script B still has not yet any open position. Will the “marketposition” script in CHART B return the value “1”, or “o”?

2: Once signal B enters long when price >12, and sets profit target to 20 usd, will the “setprofittarget(20);” in script B also change the take-profit amount on chart A to 20? Or will that remain 15?

3: At price 13 both strategies have one long position each. My broker account is now long 2 contracts. I come up with a manual trading idea (completely separate from Signals A and B) and start trading manually on the instrument in question directly via my Interactive Brokers Trader Work Station, e.g. I decide to manually place a sell short 2 contracts at market order that is filled (I have the intention of closing this manual short position when price falls to 3). So my Interactive Broker account is now flat, i.e. has no open position.
Will the “marketposition” script in CHART B return the value “1”, or “o”?
Will the stoploss and take profit orders of strategies A and B still remain open with the broker? (this is what I want...)

Code: Select all

//Signal A
vars:
intrabarpersist station(0);

[IntrabarOrderGeneration = True];

if station=0 then begin
if close >10 {“Just some random logic for the example”} then station=1;
end;

if station=1 and marketposition=0 then begin
buy 1 contracts next bar market;
setstoploss(5);
setprofittarget(15);
station=2;
end;

if station=2 then begin
if marketposition=0 then station=0;
end;

Code: Select all

//Signal B
vars:
intrabarpersist station(0);

[IntrabarOrderGeneration = True];

if station=0 then begin
if close >12 {“Just some random logic for the example”} then station=1;
end;

if station=1 and marketposition=0 then begin
buy 1 contracts next bar market;
setstoploss(5);
setprofittarget(20);
station=2;
end;

if station=2 then begin
if marketposition=0 then station=0;
end;

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

Re: Multiple signals on the same instrument and broker

Postby Andrew MultiCharts » 20 Mar 2014

andy4444,

Thank you for detailed and clear description.
1: When price has reached 11, and Chart A has an open long position, the “marketposition” script in CHART A will return the value “1”.
At this time script B still has not yet any open position. Will the “marketposition” script in CHART B return the value “1”, or “o”?
The marketposition keyword on the chart B will return 0 in this case.
2: Once signal B enters long when price >12, and sets profit target to 20 usd, will the “setprofittarget(20);” in script B also change the take-profit amount on chart A to 20? Or will that remain 15?
The setprofittarget(15) on the chart A will remain 15, no matter what happens on the chart B.
3: At price 13 both strategies have one long position each. My broker account is now long 2 contracts. I come up with a manual trading idea (completely separate from Signals A and B) and start trading manually on the instrument in question directly via my Interactive Brokers Trader Work Station, e.g. I decide to manually place a sell short 2 contracts at market order that is filled (I have the intention of closing this manual short position when price falls to 3). So my Interactive Broker account is now flat, i.e. has no open position.
Will the “marketposition” script in CHART B return the value “1”, or “o”?
Will the stoploss and take profit orders of strategies A and B still remain open with the broker? (this is what I want...)
The marketposition keyword on the chart A will return 1 in this case.
The stop loss and take profit orders of strategies A and B will remain pending at the broker. None of you strategies will "see" any manual orders you executed.

andy4444
Posts: 25
Joined: 14 Nov 2013
Has thanked: 8 times
Been thanked: 2 times

Re: Order handling in signal / multiple signals

Postby andy4444 » 20 Mar 2014

Great! Turns out everything works the way I would like it to.
Thank you for your excellent (as always!) help and patience Andrew!


Return to “MultiCharts FAQ”