Strategy positions reserved words behaviour

Questions about MultiCharts and user contributed studies.
RS
Posts: 39
Joined: 28 Sep 2007

Strategy positions reserved words behaviour

Postby RS » 19 Oct 2007

Marina,

I have some problems using the strategy position reserved words. My strategy uses al lot of opening orders with the "this bar on close" order action. In the same strategy code, after the code of these order statement(s) I want to use the strategy position reserved words: marketposition, barssinceentry, barssinceexit, entryprice and exitprice but they don't give me the right results. This simple code shows what I mean. In the code of "LE 1" I want the strategy results of the preceding buy but I get the wrong results. On the next bar the results are ok.

I studied very hard on all the EL documentation but I can't explain these results. Can you?

With regards,
Rob.

Code: Select all

if BarNumber = 1 then print(time, " ***");

if BarNumber = 2 then
begin
    buy this bar on Close;
    print(time, " LE 1. EntryPrice: ", EntryPrice, " ExitPrice: ", ExitPrice(1), " Open: ", Open, " Close: ", Close, " mp: ", marketposition, " Barssinceentry: ", barssinceentry, " Barssinceexit: ", barssinceexit(1));
end;

if BarNumber = 3 then print(time, " NO 2. EntryPrice: ", EntryPrice, " ExitPrice: ", ExitPrice(1), " Open: ", Open, " Close: ", Close, " mp: ", marketposition, " Barssinceentry: ", barssinceentry, " Barssinceexit: ", barssinceexit(1));

if BarNumber = 4 then begin sell 2 contracts this bar on close; print(time, " LX 3. EntryPrice: ", EntryPrice, " ExitPrice: ", ExitPrice(1), " Open: ", Open, " Close: ", Close, " mp: ", marketposition, " Barssinceentry: ", barssinceentry, " Barssinceexit: ", barssinceexit(1)); end;

if BarNumber = 5 then print(time, " NO 4. EntryPrice: ", EntryPrice, " ExitPrice: ", ExitPrice(1), " Open: ", Open, " Close: ", Close, " mp: ", marketposition, " Barssinceentry: ", barssinceentry, " Barssinceexit: ", barssinceexit(1));

if BarNumber = 6 then
begin
    sellshort 2 contracts this bar on close;
    print(time, " SE 5. EntryPrice: ", EntryPrice, " ExitPrice: ", ExitPrice(1), " Open: ", Open, " Close: ", Close, " mp: ", marketposition, " Barssinceentry: ", barssinceentry, " Barssinceexit: ", barssinceexit(1));
end;

if BarNumber = 7 then print(time, " NO 6. EntryPrice: ", EntryPrice, " ExitPrice: ", ExitPrice(1), " Open: ", Open, " Close: ", Close, " mp: ", marketposition, " Barssinceentry: ", barssinceentry, " Barssinceexit: ", barssinceexit(1));

if BarNumber = 8 then
begin
    buytocover 2 contracts this bar on close;
    print(time, " SX 7. EntryPrice: ", EntryPrice, " ExitPrice: ", ExitPrice(1), " Open: ", Open, " Close: ", Close, " mp: ", marketposition, " Barssinceentry: ", barssinceentry, " Barssinceexit: ", barssinceexit(1)); end;

if BarNumber = 9 then print(time, " NO 8. EntryPrice: ", EntryPrice, " ExitPrice: ", ExitPrice(1), " Open: ", Open, " Close: ", Close, " mp: ", marketposition, " Barssinceentry: ", barssinceentry, " Barssinceexit: ", barssinceexit(1));

User avatar
Marina Pashkova
Posts: 2758
Joined: 27 Jul 2007

Postby Marina Pashkova » 19 Oct 2007

Dear Rob,

What you are describing is not a mistake. In a nutshell the explanation for this behaviour is as follows:

until a new order is generated the values for those reserved words are taken from the previous bar. The order for the current bar cannot be generated until the indicator is calculated, which happens on close. After that the order will be generated - but will show on the next bar.

RS
Posts: 39
Joined: 28 Sep 2007

Postby RS » 19 Oct 2007

Marina,

Thanks for your answer. I will explain my problem in more detail. When I create an order, lets say a buy, together with an stop (loss) order, lets say a sell stop on bar 2 and that stop order is filled somewhere during the next bar (bar 3) the reserved words MarketPosition/BarsSince.... are never changed!
If I read your explanation well, MarketPosition etc. on bar 3 must be changed ( value 1), regardless of the stop order which is filled somewhere during bar 3. This code gives an example (the range of bar 3 must be around the close of bar 2, otherwise the stop is not filled).

Otherwise, do you have an alternative to check in the next bar after an entry with a stop order combination whether the stop is filled?


Thanks in advance,
Rob.

if BarNumber = 1 then print(time, " ***");
if BarNumber = 2 then begin buy this bar on close; sell next bar at close stop; print(time, " LE 1. EntryPrice: ", EntryPrice, " ExitPrice: ", ExitPrice(1), " Open: ", Open, " Close: ", Close, " mp: ", marketposition, " Barssinceentry: ", barssinceentry, " Barssinceexit: ", barssinceexit(1)); end;
if BarNumber = 3 then print(time, " NO 2. EntryPrice: ", EntryPrice, " ExitPrice: ", ExitPrice(1), " Open: ", Open, " Close: ", Close, " mp: ", marketposition, " Barssinceentry: ", barssinceentry, " Barssinceexit: ", barssinceexit(1));

User avatar
Marina Pashkova
Posts: 2758
Joined: 27 Jul 2007

Postby Marina Pashkova » 22 Oct 2007

Otherwise, do you have an alternative to check in the next bar after an entry with a stop order combination whether the stop is filled?



Yes, there is an alternative. You can check Exit Date(1) and Exit Time(1) and compare the values with those for the current bar. If the values coincide it would mean that the stop has been filled on the current bar.

if (exitdate (1) = date ) and (exittime (1) = time) then //Exit on this bar

Hope that answers your question

RS
Posts: 39
Joined: 28 Sep 2007

Postby RS » 24 Oct 2007

Marina,

Thanks! It works. As a (former) software engineer it is strange for me that the strategy position reserved words are only calculated after the strategy code is executed and not directly after a statement.

If I'm right the strategy position reserved words are calculated just before the (next) bar code execution so that all the (position) events during the bar are taken into account but not the events in the code itself.

With regards,
Rob.

User avatar
Marina Pashkova
Posts: 2758
Joined: 27 Jul 2007

Postby Marina Pashkova » 25 Oct 2007

Marina,

Thanks! It works. As a (former) software engineer it is strange for me that the strategy position reserved words are only calculated after the strategy code is executed and not directly after a statement.



Hi Rob,

The above could be done if only 1 script were allowed to generate orders. However, normally the program deals with multiple scripts - all of them interacting and influencing each other and generating multiple orders. It is hard to tell which orders will be eventually executed. That's why strategy position reserved words are calculated not directly after statements but after real order executions.

The mechanism works as follows:

1. Multiple scripts are calculated on bar1 at close.
2. As a result of these calculations multiple orders are generated. There can be 2 types of orders: at this bar and at next bar.
3. First, 'this-bar' orders are checked to see which of them can be executed and in what order this should be done (and at what price for price orders)
4. On bar2, the next group of orders ('next-bar' orders) is checked and executed.
5. During bar2 the orders are executed and market position is continuously changing as they are being executed. However, there is no way to see these changes.
6. At close of bar2 the script is calculated and the resultant market position can be seen.
7. The script at close of bar2 generates 2 types of orders 'this-bar' and 'next bar'. This bar orders weren't taken into account for bar2 market position. They will be taken into account later.
...etc

Hope that answers your question and gives you a general idea how order generation and position reserved words calculations interact.

RS
Posts: 39
Joined: 28 Sep 2007

Postby RS » 25 Oct 2007

Hope that answers your question and gives you a general idea how order generation and position reserved words calculations interact.


Marina,

Your explanation is very well. It's clear for me now.


Thank you!

Rob.

User avatar
Marina Pashkova
Posts: 2758
Joined: 27 Jul 2007

Postby Marina Pashkova » 26 Oct 2007

You're welcome Rob.

Whenever you have any quesitons just let us know and we'll try to help you.

Regards.

User avatar
MC_Prog
Posts: 330
Joined: 28 Feb 2007
Has thanked: 64 times
Been thanked: 33 times

Postby MC_Prog » 26 Oct 2007

Good dialog and good detail above.

I have a summary question:

Do the postion keywords in MC (MarketPosition, EntryPrice, ExitPrice, etc.) behave exactly as in TS, or not?

(The key word here is "exactly" ...) :?:

User avatar
Marina Pashkova
Posts: 2758
Joined: 27 Jul 2007

Postby Marina Pashkova » 29 Oct 2007

Hello MC_Prog,

There are a number of differences in the behaviour of position reserved words in MC and TS. The major differences are as follows:

contractprofit and maxcontractprofit:
in TS their default value is -1, in MC is equals 0;
in TS there is sometimes no access to history if contractprofit < 0.

in TS contractprofit =0; in MC contractprofic shows the actual value in position i.

in TS maxcontractprofit =0, in MC maxcontractprofit shows the actual value in position i.

In TS when calculating maxpostionprofit, maxpositionloss, maxcontractprofit, maxcontractloss exit commission isn't always taken into account, while in MultiCharts it is.


Return to “MultiCharts”