2 Signals 1 Chart PosTrade Keywords TradeNumber

Questions about MultiCharts and user contributed studies.
User avatar
t-rader
Posts: 139
Joined: 02 Feb 2011
Location: Australia
Has thanked: 11 times
Been thanked: 27 times

2 Signals 1 Chart PosTrade Keywords TradeNumber

Postby t-rader » 21 Jul 2014

I have a working signal that I now wish to run 2 iterations of on the same instrument (i.e eur/usd) with different exit targets.

This is easy enough to do live as I simply setup another workspace and apply the code with the different exits via inputs but I want to see the performance report for when the 2 signals are combined. Unfortunately I cannot simply just apply these 2 signals to the chart and thus things get real tricky!

In my signal I'm using the PosTrade keywords i.e:

PosTradeEntryName(0,0)
PosTradeEntryName(1,0)
PosTradeExitBar(1,0)

I'm also started using PosTradeIsOpen to replace Marketposition keyword and PosTradeEntryPrice to replace EntryPrice.

How can I create a variable to replace the 'TradeNumber' input in the keyword PosTradeExitBar(PosAgo, TradeNumber)?


PS: This would not be an issue if Portfolio Backtester/trader could handle tick data, bar magnify and IntrabarOrderGeneration (IOG) :(

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: 2 Signals 1 Chart PosTrade Keywords TradeNumber

Postby ABC » 21 Jul 2014

t-rader,

I am afraid, but I don't understand your request regarding "How can I create a variable to replace the 'TradeNumber' input in the keyword PosTradeExitBar(PosAgo, TradeNumber)?".
What should the variable hold other than the PosTradeCount information?

How are you trying to get the perfomance report? Coding all entries into a third strategy and then checking this?

One idea that would come to mind in case both signals are running on the same chart interval (with different chart intervals it would still be doable, but a bit trickier) would be to simply print the equity at every bar (and other metrics you are interested in) to texts files. Read this into Excel, combine the results and chart them there.

Regards,
ABC

User avatar
t-rader
Posts: 139
Joined: 02 Feb 2011
Location: Australia
Has thanked: 11 times
Been thanked: 27 times

Re: 2 Signals 1 Chart PosTrade Keywords TradeNumber

Postby t-rader » 22 Jul 2014

Hi ABC,

Thank you for your reply. Let me try and explain myself better.

I have a simple signal as follows:

Code: Select all

input:
Profit1(600),
Loss1(300),
amount(100000);


if condition1 = true and PosTradeIsOpen(0,0) = false and PosTradeExitBar(1,0)<>currentbar and PosTradeExitBar(2,0)<>currentbar-2 then begin
buy ("trade1 long") next bar amount shares at market;
end;


if PosTradeIsOpen(0,0) = true and PosTradeEntryName(0,0) = ("trade1 long") then begin
if close >= (PosTradeEntryPrice(0,0) + Profit1 points) then sell ("trade1 long p") next bar at market;
if close <= (PosTradeEntryPrice(0,0) - Loss1 points) then sell ("trade1 long l") next bar at market;
end;
Now I want to run this signal twice but with different profit and loss targets. To do this live I will just use a separate workspace to keep it simple, but before I do this I want to know what the 2 signals look like from the Performance Report.

So I tried to apply this simple signal to the chart twice and just change the inputs for Profit1 and Loss1. Obviously this didn't work.

So now I'm trying to add a 2nd entry and 2nd exit into the 1 signal which different Profit and Loss targets.

The first trade that happens will enter at the same time thus making the second entry tradenumber 1 (zero-based) but due to the timing of the exits because they have different profit/loss targets the trade number might not be trade 1.

So how can I use the PosTrade(PosAgo,Tradenumber) keywords when Tradenumber could always be different depending on the timing of the entries. I also just realised that PosAgo will also be changing.

Hope this explains better what I'm trying to do.

To code this seems soo complex! and is reason why we need Portfolio Backtester/trader to be able to handle tick data, bar magnify and IOG. Just pleading my case on this one ;)

Any help on this is much appreciated. I've spent countless hours on trying to work this out and I'm almost just about to give up on coding this up.

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: 2 Signals 1 Chart PosTrade Keywords TradeNumber

Postby ABC » 22 Jul 2014

t-rader,

this shouldn't be that hard to do, but I can see that it can be a bit tricky. You have two entries that are done at the same time or can they come at different times?
First of all you should use different entry names. Then you tie the exits (stop and target) to each entry name.

Regards,
ABC

User avatar
t-rader
Posts: 139
Joined: 02 Feb 2011
Location: Australia
Has thanked: 11 times
Been thanked: 27 times

Re: 2 Signals 1 Chart PosTrade Keywords TradeNumber

Postby t-rader » 22 Jul 2014

Hi ABC,

Yes I've given the entries different names and have tied the exits to the entries using 'from entry ("trade1 long")'

The trades can and will happen at the same but will also happen at different times.

A more complex scenario that can and will happen is 'trade1 long' and 'trade2 long' enter at the same time but then 'trade2 long' will close and then reopen again while 'trade1 long' is still open. Then that 'trade2 long' will close and finally 'trade1 long' will close. This is when it gets tricky because the tradenumber reference in the PosTrade keywords has to be a variable? maybe even an array as I reference 1 and 2 positions ago.

So I was trying to do a counter to replace tradenumber used in the PosTrade keywords. I've tried numerous things but to give you an idea this is something I tried:

Code: Select all

[intrabarordergeneration=true];

input:
Profit1(600),
Loss1(300),
Profit2(200),
Loss2(100),
amount(100000);

vars:
intrabarpersist TradeCounter(-1),
intrabarpersist TradeCount1(-1),
intrabarpersist TradeCount2(-1);

// ENTRY 1
if condition1 = true and PosTradeIsOpen(0,TradeCount1) = false and PosTradeExitBar(1,TradeCount1)<>currentbar and PosTradeExitBar(2,TradeCount1)<>currentbar-2 then begin
buy ("trade1 long") next bar amount shares at market;
TradeCounter = TradeCounter + 1;
TradeCount1 = TradeCounter;
end;

// ENTRY 2
if condition1 = true and PosTradeIsOpen(0,TradeCount2) = false and PosTradeExitBar(1,TradeCount2)<>currentbar and PosTradeExitBar(2,TradeCount2)<>currentbar-2 then begin
buy ("trade2 long") next bar amount shares at market;
TradeCounter = TradeCounter + 1;
TradeCount2 = TradeCounter;
end;

// EXIT 1
if PosTradeIsOpen(0,TradeCount1) = true and PosTradeEntryName(0,TradeCount1) = ("trade1 long") then begin
if close >= (PosTradeEntryPrice(0,TradeCount1) + Profit1 points) then sell ("trade1 long p") from entry("trade1 long") next bar at market;
if close <= (PosTradeEntryPrice(0,TradeCount1) - Loss1 points) then sell ("trade1 long l") from entry("trade1 long") next bar at market;
end;

// EXIT 2
if PosTradeIsOpen(0,TradeCount2) = true and PosTradeEntryName(0,TradeCount2) = ("trade2 long") then begin
if close >= (PosTradeEntryPrice(0,TradeCount2) + Profit1 points) then sell ("trade2 long p") from entry("trade2 long") next bar at market;
if close <= (PosTradeEntryPrice(0,TradeCount2) - Loss1 points) then sell ("trade2 long l") from entry("trade2 long") next bar at market;
end;

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: 2 Signals 1 Chart PosTrade Keywords TradeNumber

Postby Henry MultiСharts » 29 Jul 2014

At the moment it is indeed tricky to make a flexible code for such scenario. Here is a sample code our programmer has created for you:

Code: Select all

[intrabarordergeneration=false];
once cleardebug;
input:
Profit1(600),
Loss1(300),
Profit2(200),
Loss2(100),
amount(100);

vars:
intrabarpersist TradeCounter(-1),
intrabarpersist TradeCount(0),
intrabarpersist TradeCount2(-1), avgep1(0), avgep2(0);

condition1 = true;

// ENTRY 1
if marketposition = 0 and condition1 = true then
begin
buy ("trade1 long") next bar amount shares at market;
end;

// ENTRY 2
if condition1 = true and marketposition > 0 then begin
buy ("trade2 long") next bar amount shares at market;
end;

avgep1 = PosTradeEntryPrice(0, 0);
avgep2 = PosTradeEntryPrice(0, 1);

if( currentshares = 2*amount ) then begin
if close >= (avgep1 + Profit1) then
sell ("trade1 limit") from entry("trade1 long") next bar at avgep1 + Profit1 limit;

if close <= (avgep1 - Loss1) then
sell ("trade1 stop") from entry("trade1 long") next bar at avgep1 - Loss1 stop;


if close >= ( avgep2 + Profit2) then
sell ("trade2 limit") from entry("trade2 long") next bar at avgep2 + Profit2 limit;

if close <= ( avgep2 - Loss2 ) then
sell ("trade2 stop") from entry("trade2 long") next bar at avgep2 - Loss2 stop;
end;


Return to “MultiCharts”