Multi Entries/Exits Strategies

Questions about MultiCharts and user contributed studies.
qweasz
Posts: 13
Joined: 15 Apr 2010
Has thanked: 6 times
Been thanked: 5 times

Multi Entries/Exits Strategies

Postby qweasz » 29 Aug 2011

Most of my Strategies use several Entries/Exits in the same direction, and I always get stuck at the same point when programming.
I manage to assign different tags to each Entry every time the variable currententries changes; the problem is currententries seems to just keep track of the Open entries in the position, so if an Exit and an Entry ocurrs at the same bar then currententries does not change and I am not able to handle the new Entry.
As far a I understand there is no way to know the number of total Entries in a position, just the open Entries. And there is no way to know the time/price of each Entry in a position, just the time/price of the first Entry in a position (EntryPrice EntryTime).
Am I right? Am I missing something?
Thank you for your help

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

Re: Multi Entries/Exits Strategies

Postby Dave Masalov » 29 Aug 2011

Dear qweasz,

There is a way to know the number of total Entries in a position. Please use PosTradeCount(positionsago) keyword. There can be only one open position or there is no open position at all, you may check the quantity of trades in position with PosTradeCount(0) keyword, including open and closed trades.

EntryPrice returns average Entry price, the price of every Entry is calculated with PosTradeEntryPrice(positions_ago, trade_number). All keywords beginning with “PosTrade…” return the necessary information regarding every trade of your strategy.

qweasz
Posts: 13
Joined: 15 Apr 2010
Has thanked: 6 times
Been thanked: 5 times

Re: Multi Entries/Exits Strategies

Postby qweasz » 29 Aug 2011

Thankyou very much, Dave!! All these PosTrade keywords are every thing I need and more.
They are not documented in my Powerlanguage editor (MC7); where can I get latest documentation?

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

Re: Multi Entries/Exits Strategies

Postby Dave Masalov » 30 Aug 2011

They are not documented in my Powerlanguage editor (MC7); where can I get latest documentation?
Dear qweasz,

We are working on the updated Power Language reference guide at the moment. it should be ready soon.

qweasz
Posts: 13
Joined: 15 Apr 2010
Has thanked: 6 times
Been thanked: 5 times

Re: Multi Entries/Exits Strategies

Postby qweasz » 30 Aug 2011

Thankyou Dave, PosTradeCount works great.
But there is something wrong with some PosTradeEntry keywords: they compile ok but produce runtime errors (STD exception : invalid argument).
I have used PosTradeEntryBar and PosTradeEntryPrice with very simple arguments to single out the problem and the result is always the same.
For instance, having next sentence in a Signal will produce the error

Code: Select all

aaa=PosTradeEntryPrice(0,1);

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

Re: Multi Entries/Exits Strategies

Postby Dave Masalov » 09 Sep 2011

Dear qweasz,

We failed to reproduce the issue. Please send us the signal that produces runtime errors or come to our Live Chat Mon-Fri between 6:30 AM and 1 PM EST so we can connect to your computer remotely for investigation.

qweasz
Posts: 13
Joined: 15 Apr 2010
Has thanked: 6 times
Been thanked: 5 times

Re: Multi Entries/Exits Strategies

Postby qweasz » 14 Sep 2011

Thank you Dave.
In my environement this silly signal will produce the error:

Code: Select all

[IntrabarOrderGeneration = false]

variables:
aaa(0);
aaa=PosTradeEntryPrice(0,1);
if currentbar=100 then buy this bar on close;
Signal properties:
Commission 0
Slippage 0
Init capital 100000
Interest rate 2
Max number of bars 50
Fixed shares/contracts 1

Changing this properties will not solve the problem.

I run MultiCharts Version 7.0 Release (Build 4510) on Windows Vista.

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

Re: Multi Entries/Exits Strategies

Postby TJ » 14 Sep 2011

Thank you Dave.
In my environement this silly signal will produce the error:

Code: Select all

[IntrabarOrderGeneration = false]

variables:
aaa(0);
aaa=PosTradeEntryPrice(0,1);
if currentbar=100 then buy this bar on close;
Signal properties:
Commission 0
Slippage 0
Init capital 100000
Interest rate 2
Max number of bars 50
Fixed shares/contracts 1

Changing this properties will not solve the problem.

I run MultiCharts Version 7.0 Release (Build 4510) on Windows Vista.
You cannot "buy this bar on close".
This is only usable in backtest.

qweasz
Posts: 13
Joined: 15 Apr 2010
Has thanked: 6 times
Been thanked: 5 times

Re: Multi Entries/Exits Strategies

Postby qweasz » 14 Sep 2011

Thank you TJ.
This was just a silly example. I never use "buy this bar on close" in real time, but I did not know that it was deprecated as it is documented in PowerLanguage.

Changing the sentence for

Code: Select all

if currentbar=100 then buy next bar at market;
will get the same runtime error.

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

Re: Multi Entries/Exits Strategies

Postby JoshM » 15 Sep 2011

Thank you TJ.
This was just a silly example. I never use "buy this bar on close" in real time, but I did not know that it was deprecated as it is documented in PowerLanguage.

Changing the sentence for

Code: Select all

if currentbar=100 then buy next bar at market;
will get the same runtime error.
True Qweasz, the same problem occurs here (MultiCharts Version 7.0 Release (Build 4510) on Windows 7 Home Premium).

The following code..

Code: Select all

[IntrabarOrderGeneration = false]

variables:
aaa(0);

aaa = PosTradeEntryPrice(0,1);
if currentbar = 100
then buy next bar at market;
compiles correctly..

Code: Select all

------ Build started: ------
Study: "TestStrategy" (Signal)
Please wait ....
15.09.11 09:13:48
------ Compiled successfully ------
..but generated an STD Exception after applying it to a chart with the default Strategy Properties settings:
Image

Don't know if this means anything, but just wanted to let you know Qweasz that, with this code, I also have the same error.

Regards,
Josh
Attachments
stdError.PNG
(5.53 KiB) Downloaded 2764 times

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

Re: Multi Entries/Exits Strategies

Postby JoshM » 15 Sep 2011

(Post deliberately emptied - asked a question that I later realized was needlessly, and gave an code example which further derived from the topic subject.)

qweasz
Posts: 13
Joined: 15 Apr 2010
Has thanked: 6 times
Been thanked: 5 times

Re: Multi Entries/Exits Strategies

Postby qweasz » 15 Sep 2011

Don't know if this means anything, but just wanted to let you know Qweasz that, with this code, I also have the same error.
Thank you very much Josh for taking the time to test the code. Now I know the problem is not a faulty installation on my part.

Regards

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

Re: Multi Entries/Exits Strategies

Postby JoshM » 15 Sep 2011

(This post, with further questions and partially working code, has been deliberately emptied to keep this thread tidy. The solution is provided further below.)
Last edited by JoshM on 15 Sep 2011, edited 2 times in total.

qweasz
Posts: 13
Joined: 15 Apr 2010
Has thanked: 6 times
Been thanked: 5 times

Re: Multi Entries/Exits Strategies

Postby qweasz » 15 Sep 2011

Josh, I am working on your code.

Your code sais:

Code: Select all

// Get number of positions
for tradeNumber = TotalTrades DownTo 1 begin
if EntryPrice(tradeNumber) > 0 then // Don't ask me why, but this filters out the multiple scale-ins
numberOfPositions = numberOfPositions + 1;
end;
I think EntryPrice is used with a "position number" argument, not a "trade number"; your code happens to give the right answer because it is counting the number of positions with entryprice>0

This code will do the same:

Code: Select all

variables: counter(0);

while EntryPrice(counter)<>0
begin
counter=counter+1;
end;
at the end counter is the number of positions.

In your code you are missing the current position because you do not check EntryPrice(0).

Regards

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

Re: Multi Entries/Exits Strategies

Postby JoshM » 15 Sep 2011

Solved! :) (Sorry, quite happy/satisfied now)
the price of every Entry is calculated with PosTradeEntryPrice(positions_ago, trade_number). All keywords beginning with “PosTrade…” return the necessary information regarding every trade of your strategy.
According to my understanding: PosTradeEntryPrice uses the position number, followed by the trade number relative for the position, starting at zero. So, if your first position scales in three times, and your second position 2 times, you can call this with:

Code: Select all

PosTradeEntryPrice(2, 0); // first position, first entry
PosTradeEntryPrice(2, 1); // first position, second entry
PosTradeEntryPrice(2, 2);
PosTradeEntryPrice(1, 0); // second position (i.e. 1 position ago), first entry
PosTradeEntryPrice(1, 1);
So, an error I made was to use the 'TotalTrades number' for the 'trade_number' input in PosTradeEntryPrice (perhaps this can be clearer communicated in the future, since we now have 3 three types of numbers, each different to another: position number != total trades number != trade number).

See for example the code below:

Code: Select all

if LastBarOnChart_s = true then begin

// Get number of positions
for tradeNumber = TotalTrades DownTo 1 begin
if EntryPrice(tradeNumber) > 0 then
numberOfPositions = numberOfPositions + 1;
end;

Print("Total positions: ", NumToStr(numberOfPositions, 0), ", totalTrades: ", totaltrades, NewLine);

tradeNumber = 1;

for positionNumber = numberOfPositions DownTo 1 begin

// Get the number of trades into the position
tradesIntoPosition = PosTradeCount(positionNumber);

// Print position data
Print("Position #", NumToStr(numberOfPositions - (positionNumber - 1), 0), Spaces(2),
"Trade #", NumToStr(tradeNumber, 0), Spaces(2),
"AvgEntryPrice: ", NumToStr(EntryPrice(positionNumber), 3), Spaces(2),
"ExitPrice: ", NumToStr(ExitPrice(positionNumber), 3),
Spaces(5), "Trades into position: ", NumToStr(tradesIntoPosition, 0));

// Print seperate entries data
for value5 = 0 to (tradesIntoPosition - 1) begin

Print(Spaces(3), "Entry", NumToStr(value5+1, 0), " @ ",
NumToStr(PosTradeEntryPrice(positionNumber, value5), 3), Spaces(2),
"TradeSize: ", NumToStr(PosTradeSize(positionNumber, value5), 0), Spaces(2),
"ExitPrice: ", NumToStr(PosTradeExitPrice(positionNumber, value5), 3)
);

end;

tradeNumber = tradeNumber + tradesIntoPosition;
end;
end;
Which gives here the following correct output, STD Exception free:

Code: Select all

Total positions: 63, totalTrades: 280.00

Position #1 Trade #1 AvgEntryPrice: 80.865 ExitPrice: 80.843 Trades into position: 8
Entry1 @ 80.865 TradeSize: 1000 ExitPrice: 80.843
Entry2 @ 80.870 TradeSize: 1000 ExitPrice: 80.843
Entry3 @ 80.850 TradeSize: 1000 ExitPrice: 80.843
Entry4 @ 80.858 TradeSize: 1000 ExitPrice: 80.843
Entry5 @ 80.844 TradeSize: 1000 ExitPrice: 80.843
Entry6 @ 80.850 TradeSize: 1000 ExitPrice: 80.843
Entry7 @ 80.861 TradeSize: 1000 ExitPrice: 80.843
Entry8 @ 80.844 TradeSize: 1000 ExitPrice: 80.843
Position #2 Trade #9 AvgEntryPrice: 80.843 ExitPrice: 80.850 Trades into position: 2
Entry1 @ 80.843 TradeSize: 1000 ExitPrice: 80.850
Entry2 @ 80.834 TradeSize: 1000 ExitPrice: 80.850
Position #3 Trade #11 AvgEntryPrice: 80.750 ExitPrice: 80.769 Trades into position: 2
Entry1 @ 80.750 TradeSize: 1000 ExitPrice: 80.769
Entry2 @ 80.757 TradeSize: 1000 ExitPrice: 80.769
Position #4 Trade #13 AvgEntryPrice: 80.773 ExitPrice: 80.834 Trades into position: 2
Entry1 @ 80.773 TradeSize: 1000 ExitPrice: 80.834
Entry2 @ 80.776 TradeSize: 1000 ExitPrice: 80.834
Position #5 Trade #15 AvgEntryPrice: 80.782 ExitPrice: 80.778 Trades into position: 10
Entry1 @ 80.782 TradeSize: 1000 ExitPrice: 80.778
Entry2 @ 80.797 TradeSize: 1000 ExitPrice: 80.778
Entry3 @ 80.793 TradeSize: 1000 ExitPrice: 80.778
Entry4 @ 80.804 TradeSize: 1000 ExitPrice: 80.778
Entry5 @ 80.800 TradeSize: 1000 ExitPrice: 80.778
Entry6 @ 80.801 TradeSize: 1000 ExitPrice: 80.778
Entry7 @ 80.806 TradeSize: 1000 ExitPrice: 80.778
Entry8 @ 80.818 TradeSize: 1000 ExitPrice: 80.778
Entry9 @ 80.825 TradeSize: 1000 ExitPrice: 80.778
Entry10 @ 80.815 TradeSize: 1000 ExitPrice: 80.778
Position #6 Trade #25 AvgEntryPrice: 81.141 ExitPrice: 81.106 Trades into position: 8
Entry1 @ 81.141 TradeSize: 1000 ExitPrice: 81.106
Entry2 @ 81.145 TradeSize: 1000 ExitPrice: 81.106
Entry3 @ 81.178 TradeSize: 1000 ExitPrice: 81.106
Entry4 @ 81.165 TradeSize: 1000 ExitPrice: 81.106
Entry5 @ 81.143 TradeSize: 1000 ExitPrice: 81.106
Entry6 @ 81.132 TradeSize: 1000 ExitPrice: 81.106
Entry7 @ 81.124 TradeSize: 1000 ExitPrice: 81.106
Entry8 @ 81.136 TradeSize: 1000 ExitPrice: 81.106
Position #7 Trade #33 AvgEntryPrice: 81.063 ExitPrice: 81.084 Trades into position: 1
Entry1 @ 81.063 TradeSize: 1000 ExitPrice: 81.084
Position #8 Trade #34 AvgEntryPrice: 80.870 ExitPrice: 80.843 Trades into position: 7
Entry1 @ 80.870 TradeSize: 1000 ExitPrice: 80.843
Entry2 @ 80.859 TradeSize: 1000 ExitPrice: 80.843
Entry3 @ 80.863 TradeSize: 1000 ExitPrice: 80.843
Entry4 @ 80.867 TradeSize: 1000 ExitPrice: 80.843
Entry5 @ 80.857 TradeSize: 1000 ExitPrice: 80.843
Entry6 @ 80.858 TradeSize: 1000 ExitPrice: 80.843
Entry7 @ 80.849 TradeSize: 1000 ExitPrice: 80.843
Position #9 Trade #41 AvgEntryPrice: 80.961 ExitPrice: 80.938 Trades into position: 5
Entry1 @ 80.961 TradeSize: 1000 ExitPrice: 80.938
Entry2 @ 80.954 TradeSize: 1000 ExitPrice: 80.938
Entry3 @ 80.958 TradeSize: 1000 ExitPrice: 80.938
Entry4 @ 80.960 TradeSize: 1000 ExitPrice: 80.938
Entry5 @ 80.942 TradeSize: 1000 ExitPrice: 80.938
Position #10 Trade #46 AvgEntryPrice: 80.938 ExitPrice: 80.961 Trades into position: 2
Entry1 @ 80.938 TradeSize: 1000 ExitPrice: 80.961
Entry2 @ 80.927 TradeSize: 1000 ExitPrice: 80.961
Position #11 Trade #48 AvgEntryPrice: 81.211 ExitPrice: 81.230 Trades into position: 2
Entry1 @ 81.211 TradeSize: 1000 ExitPrice: 81.230
Entry2 @ 81.216 TradeSize: 1000 ExitPrice: 81.230
Position #12 Trade #50 AvgEntryPrice: 81.230 ExitPrice: 81.215 Trades into position: 10
Entry1 @ 81.230 TradeSize: 1000 ExitPrice: 81.215
Entry2 @ 81.252 TradeSize: 1000 ExitPrice: 81.215
Entry3 @ 81.246 TradeSize: 1000 ExitPrice: 81.215
Entry4 @ 81.223 TradeSize: 1000 ExitPrice: 81.215
Entry5 @ 81.223 TradeSize: 1000 ExitPrice: 81.215
Entry6 @ 81.230 TradeSize: 1000 ExitPrice: 81.215
Entry7 @ 81.255 TradeSize: 1000 ExitPrice: 81.215
Entry8 @ 81.240 TradeSize: 1000 ExitPrice: 81.215
Entry9 @ 81.239 TradeSize: 1000 ExitPrice: 81.215
Entry10 @ 81.243 TradeSize: 1000 ExitPrice: 81.215
Position #13 Trade #60 AvgEntryPrice: 81.215 ExitPrice: 81.242 Trades into position: 2
Entry1 @ 81.215 TradeSize: 1000 ExitPrice: 81.242
Entry2 @ 81.210 TradeSize: 1000 ExitPrice: 81.242
Position #14 Trade #62 AvgEntryPrice: 81.266 ExitPrice: 81.276 Trades into position: 2
Entry1 @ 81.266 TradeSize: 1000 ExitPrice: 81.276
Entry2 @ 81.269 TradeSize: 1000 ExitPrice: 81.276
EntryPrice returns average Entry price, (..)
I don't mean this disrespectful, but I doubt that. See for example the output above - the EntryPrice(positions ago) returns the entry price for the first entry of the total position. Also other tests that I've done never returned the average entry price for the complete position. What am I doing wrong? :)

Regards,
Josh

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

Re: Multi Entries/Exits Strategies

Postby JoshM » 15 Sep 2011

(..)
I think EntryPrice is used with a "position number" argument, not a "trade number"; your code happens to give the right answer because it is counting the number of positions with entryprice>0

This code will do the same:

Code: Select all

variables: counter(0);

while EntryPrice(counter)<>0
begin
counter=counter+1;
end;
at the end counter is the number of positions.
(..)
Thanks, I did indeed overlook that. I've pasted the code snipped above in my strategy. :)

You probably already got it solved to return the number of entries for a position (the original question of this thread), but for other interested readers:

Code: Select all

while EntryPrice(numberOfPositions) <> 0 begin
numberOfPositions = numberOfPositions + 1;
end;

if LastBarOnChart_s = true then begin

for value10 = 1 to (numberOfPositions - 1) begin

Print("Position ago: ", value10, " Number of entries: ", PosTradeCount(value10));

end;

end;

Code: Select all

Position ago: 1.00 Number of entries: 2.00
Position ago: 2.00 Number of entries: 9.00
Position ago: 3.00 Number of entries: 2.00
Position ago: 4.00 Number of entries: 2.00
Position ago: 5.00 Number of entries: 2.00
Position ago: 6.00 Number of entries: 2.00
Position ago: 7.00 Number of entries: 10.00
Position ago: 8.00 Number of entries: 2.00
Position ago: 9.00 Number of entries: 2.00
Position ago: 10.00 Number of entries: 1.00
Position ago: 11.00 Number of entries: 2.00
Position ago: 12.00 Number of entries: 4.00
Position ago: 13.00 Number of entries: 2.00
Position ago: 14.00 Number of entries: 2.00
Position ago: 15.00 Number of entries: 10.00
Regards,
Josh

qweasz
Posts: 13
Joined: 15 Apr 2010
Has thanked: 6 times
Been thanked: 5 times

Re: Multi Entries/Exits Strategies

Postby qweasz » 15 Sep 2011

Great job Josh !!!
I managed to get an "STD exception free" signal by carefully handling the parameter ranges as you did, wich is tricky. This PosTrade functions are very unforgiving. No problem if you have other functions such as EntryPrice(100000) in your code (very out of range), but you can not check PosTradeEntryPrice(0,1) at the time there is no position yet; otherwise you get the runtime error.
Thank you !!

qweasz
Posts: 13
Joined: 15 Apr 2010
Has thanked: 6 times
Been thanked: 5 times

Re: Multi Entries/Exits Strategies

Postby qweasz » 16 Sep 2011

To further document this thread:

To count the number of positions you better use a code like

Code: Select all

numberOfPositions=1;
while EntryPrice(numberOfPositions) <> 0 begin
numberOfPositions = numberOfPositions + 1;
end;
Sentence "numberOfPositions=1;" is important because EntryPrice(0) is equal to 0 in the case there are no open positions at the time of counting. Some additional code will be required to control the case where numberOfPositions is really 0; something like

Code: Select all

numberOfPositions=1;
while EntryPrice(numberOfPositions) <> 0 begin
numberOfPositions = numberOfPositions + 1;
end;
if totaltrades=0 then numberOfPositions=0;

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: Multi Entries/Exits Strategies

Postby vking » 02 Oct 2011

Thanks Josh & qweasz for this thread.

- I am trying to determine the entry name & exit name for the position that is closed on the same bar. Is there any easier way to do that using new keywords?
- is there any efficient way to count the number of "closed" trades(not the individual entries in single trade but when the position is closed - it's a trade) and open trades?

thanks in advance!!

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

Re: Multi Entries/Exits Strategies

Postby JoshM » 03 Oct 2011

- I am trying to determine the entry name & exit name for the position that is closed on the same bar. Is there any easier way to do that using new keywords?
I guess you can use the new PosTradeExitName() keyword for that, but I don't know if that will be more easier than ExitName() with a MarketPosition check.
9. PosTradeExitName
Returns exit order name. Exit Name is indicated on the chart and in Order and Position Tracker Window

Usage
PosTradeExitName(PosAgo, TradeNumber)
Where: PosAgo - a numerical expression, specifying the position:
0 - open position;
1 - one position back (the last position closed);
2 - two positions back, etc.
TradeNumber - a numerical expression, specifying the number of trade (zero-based). To retrieve the total number of trades in specified position use PosTradeCount

Notes
This function can only be used in signals.
Example
PosTradeExitName(0,1) will return a value of “sell LX” for the second trade of the open position, if this trade was closed by the order with “sell LX” name.
Source.
- is there any efficient way to count the number of "closed" trades(not the individual entries in single trade but when the position is closed - it's a trade) and open trades?
The code from Qweasz in this post is pretty efficient I think, or do you mean something else? Btw, that code would return a value including open positions, but with OpenEntriesCount this can be corrected.
35. OpenEntriesCount
Same as CurrentEntries.
Hope this is of some help,

Regards,
Josh

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: Multi Entries/Exits Strategies

Postby vking » 03 Oct 2011

Thanks Josh. Appreciate it. I am going to check further and reply.

These new keywords are little tricky to use due to run-time exception errors :(


Return to “MultiCharts”