how to stop script when netprofit reaches targetprofit?  [SOLVED]

Questions about MultiCharts and user contributed studies.
treksis
Posts: 13
Joined: 18 Jul 2014
Has thanked: 4 times

how to stop script when netprofit reaches targetprofit?

Postby treksis » 02 Aug 2014

Hi, I'm testing a strategy and I would like the backtest to close all position and stop the script from running if my netprofit has reached x $.
Is there a function to stop the strategy completely when profittarget has been reaches?
I can't make that happen with setprofittarget(x$)

Also, is there an alternative to the function BarSinceEntry?
I'm using 1 second graph and if there are no trades on every second, each bar does not represent 1 second. I want to be able to close my orders if TimeSinceEntry >1800(seconds) and not BarSinceEntry>1800(bars xx).

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

Re: how to stop script when netprofit reaches targetprofit?  [SOLVED]

Postby JoshM » 02 Aug 2014

Hi, I'm testing a strategy and I would like the backtest to close all position and stop the script from running if my netprofit has reached x $.
Is there a function to stop the strategy completely when profittarget has been reaches?
I can't make that happen with setprofittarget(x$)
This will not literally "stop" a script, but any statement after `#return` will not be executed:

Code: Select all

if (NetProfit > 1000) then
#return;

// All statements that appear below
// return are not executed
If you want to stop a script completely, see Abort. That will, however, turn the strategy off so the backtest performance report will not be available then.
Also, is there an alternative to the function BarSinceEntry?
I'm using 1 second graph and if there are no trades on every second, each bar does not represent 1 second. I want to be able to close my orders if TimeSinceEntry >1800(seconds) and not BarSinceEntry>1800(bars xx).
You can use `PosTradeEntryDateTime` for that:

Code: Select all

Variables:
oneSecondDT(ELTimeToDateTime_s(1)),
timeSinceEntry(0);

timeSinceEntry = ComputerDateTime - PosTradeEntryDateTime(1, 0);

if (timeSinceEntry > (1800 * oneSecondDT)) then
// exit


Return to “MultiCharts”