Page 1 of 1

How to code this exit strategy?

Posted: Jan 06 2017
by serioustrader
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!

Re: How to code this exit strategy?

Posted: Jan 06 2017
by MAZINGUER
Hi seriustrader,

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

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

Regards and happy new year

Re: How to code this exit strategy?

Posted: Jan 06 2017
by TJ
See post #1 & #2
viewtopic.php?t=11713

Re: How to code this exit strategy?

Posted: Jan 07 2017
by serioustrader
See post #1 & #2
viewtopic.php?t=11713
Edited post, thanks

Re: How to code this exit strategy?

Posted: Jan 09 2017
by serioustrader
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.

Re: How to code this exit strategy?

Posted: Jan 10 2017
by MAZINGUER
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

Re: How to code this exit strategy?

Posted: Jan 11 2017
by serioustrader
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?

Re: How to code this exit strategy?

Posted: Jan 11 2017
by MAZINGUER
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