Code Question: control named order

Questions about MultiCharts and user contributed studies.
User avatar
Henrik
Posts: 140
Joined: 13 Apr 2010
Has thanked: 25 times
Been thanked: 11 times

Code Question: control named order

Postby Henrik » 16 Nov 2011

Hello!

Is there a way to control an "order named" position?

With "marketposition" I can only see the summery pf all positions of the strategy on the same instrument.

But I have different positions:


Buy [("Entry1")] [Number of Shares/Contracts] [Order Action];
Buy [("Entry2")] [Number of Shares/Contracts] [Order Action];


OK, I can close both orders with

Sell [("Order Name")] [from entry ("Entry1")] [ Number of Shares [Total]]
[Execution Method];
Sell [("Order Name")] [from entry ("Entry2")] [ Number of Shares [Total]]
[Execution Method];


But how can I control these orders?
For example:

If marketposition(Order1) = 1 then....

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

Re: Code Question: control named order

Postby JoshM » 17 Nov 2011

But how can I control these orders?
For example:

If marketposition(Order1) = 1 then....
Don't have a clear solution for this, but what you could try..

1) Use variables that hold the position size for each order. Incredibly simplified, I mean something like this:

Code: Select all

if .... then begin
buy("EL mkt1") 2 contracts next bar at market;
PosSizeOrderOne = 2;
end;

if .... then begin
buy("EL mkt2") 1 contracts next bar at market;
PosSizeOrderTwo = 1;
end;

if PosSizeOrderOne > 0 then begin
Sell("XL mkt1") 1 contracts next bar at market;
PosSizeOrderOne = PosSizeOrderOne - 1;
end;
The biggest challenge here is to ensure that the if statements are only triggered at the appropriate moment, so that you 'PosSizeOrder' variable won't get updated with incorrect values. You could use the MarketPosition, CurrentEntries or CurrentContracts reserved words for that.

2) Use the new PosTradeSize(PosAgo, TradeNumber) for that, perhaps with the PosTradeEntryName/PosTradeExitName or PosTradeEntryCategory/PosTradeExitCategory for the filtering on entry/exit name or type of entry/exit order. Once again simplified, this could look like:

Code: Select all

if .... then begin
buy("EL mkt1") 2 contracts next bar at market;
end;

if .... then begin
buy("EL mkt2") 1 contracts next bar at market;
end;

if PosTradeSize(0, 0) > 0 then
Print("The first order of the open position has a size of ", PosTradeSize(0, 0));

if PosTradeSize(0, 1) > 0 then
Print("The second order is ", PosTradeSize(0, 1), " contracts big.");
The biggest challenge here is to test this thoroughly, since there some unknowns how this might work in real-time. For example, if you close the second position leg, would the third position leg become the second, or would the second read a position size of zero? (I suspect the latter, since the PosTrade* words are based on trades, not trade sizes, but I don't know for sure :) ).

Also see the documentation here and this explanation.

Regards,
Josh


Return to “MultiCharts”