How to code this exit strategy?

Questions about MultiCharts and user contributed studies.
serioustrader
Posts: 5
Joined: 27 Dec 2016
Has thanked: 4 times

How to code this exit strategy?

Postby serioustrader » 06 Jan 2017

Hi
I would like to code a strategy based on barssinceentry:
exit the position while barssinceentry is larger than 30 bars, if the position is at a loss.
I tried to write it but got a bit stuck, since I just started learning Easy Language.

this is my previous version:

Code: Select all

input: positiontime(30);

condition1 = barssinceentry = positiontime;
condition2 = contractprofit < 0;

if condition1 and condition2 and MarketPosition <> 0 then begin;

Sell ("Time Exit LX") all contracts next bar at market;
Buytocover ("Time Exit SX") all contracts next bar at market;
end;
say I have a long position,
However if it was at a profit *at* the 30th bar, it would not sell, and the code would not sell after the 30th bar even at a loss.


Any help would be appreciated!
Last edited by serioustrader on 07 Jan 2017, edited 2 times in total.

MAZINGUER
Posts: 57
Joined: 06 Jan 2017
Has thanked: 5 times
Been thanked: 21 times

Re: How to code this exit strategy?

Postby MAZINGUER » 06 Jan 2017

Hi seriustrader,

I'm not an expert but you can try this:

If BarsSinceEntry>30 and GetPositionOpenPL<0 then....

Regards and happy new year

User avatar
TJ
Posts: 7250
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 991 times
Been thanked: 2058 times

Re: How to code this exit strategy?

Postby TJ » 06 Jan 2017

See post #1 & #2
viewtopic.php?t=11713

serioustrader
Posts: 5
Joined: 27 Dec 2016
Has thanked: 4 times

Re: How to code this exit strategy?

Postby serioustrader » 07 Jan 2017

See post #1 & #2
viewtopic.php?t=11713
Edited post, thanks

serioustrader
Posts: 5
Joined: 27 Dec 2016
Has thanked: 4 times

Re: How to code this exit strategy?

Postby serioustrader » 09 Jan 2017

Hi seriustrader,

I'm not an expert but you can try this:

If BarsSinceEntry>30 and GetPositionOpenPL<0 then....

Regards and happy new year
Hi MAZINGUER,

Thanks, would this work when purely backtesting?
the function GetPoisitionOpenPL asks for an account number, does it only work in live trading?
Thanks.

MAZINGUER
Posts: 57
Joined: 06 Jan 2017
Has thanked: 5 times
Been thanked: 21 times

Re: How to code this exit strategy?

Postby MAZINGUER » 10 Jan 2017

Thanks, would this work when purely backtesting?
the function GetPoisitionOpenPL asks for an account number, does it only work in live trading?
That's right, serioustrader.
Watch this link
viewtopic.php?f=1&t=46385&p=103788&hili ... nt#p103788

regards

serioustrader
Posts: 5
Joined: 27 Dec 2016
Has thanked: 4 times

Re: How to code this exit strategy?

Postby serioustrader » 11 Jan 2017

Thanks, would this work when purely backtesting?
the function GetPoisitionOpenPL asks for an account number, does it only work in live trading?
That's right, serioustrader.
Watch this link
viewtopic.php?f=1&t=46385&p=103788&hili ... nt#p103788

regards
Thanks.
I would like to build it to work with backtesting though..
I accomplished a part of it using my code posted in post 1.
Tt would run a check of the position P&L once, at 30 bars since entry.
However I would want to make it keep looping....so that even after 30 bars, if the position is at a loss it would exit as well....
any ideas how to code it?

MAZINGUER
Posts: 57
Joined: 06 Jan 2017
Has thanked: 5 times
Been thanked: 21 times

Re: How to code this exit strategy?

Postby MAZINGUER » 11 Jan 2017

The powerlenguage programming language reads the codes from top to bottom and for each bar. For each bar it means that if you have a variable, the code calculates its value for each bar.
It is for this reason that if you want to know if a position is positive or negative in each bar, you do not need any loop. You simply need to calculate the benefit of the position and powerlanguage will give you this value for each bar.
If you also want to let 30 bars pass since you entered the position to do this calculation because the code would look something like:

Code: Select all

If MarketPosition=1 and BarsSinceEntry>30 and openpositionprofit<0 then
sell ("Time Exit SX") all contracts next bar at market;

If MarketPosition=-1 and BarsSinceEntry>30 and openpositionprofit<0 then
Buytocover ("Time Exit SX") all contracts next bar at market;
I really do not know if this is what you mean.
Keep in mind that in 30 bars many things can happen and with this code there are 30 bars that you do not know where the price will be
In addition, if you just wait for the position to be negative to close an open position, the code will always close losing money. This code would be like a stop loss.
Regards


Return to “MultiCharts”