CLOSE WHEN PROFIT OR LOSS IS REACH.

Questions about MultiCharts and user contributed studies.
RedBerlin
Posts: 74
Joined: 05 Dec 2014

CLOSE WHEN PROFIT OR LOSS IS REACH.

Postby RedBerlin » 09 Dec 2018

HI TO EVERYONE
I m posting this simple strategy to ask someone an advice or.
It is on supertrend indicator contrarian.
it works more o less and close all at 16.00but I m not able to setting to exit when the profit pr loss is 100 euro for example.
to me is just good if is reach 100 profit so ,if someone has some good advice is welcome.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Code: Select all

[LegacyColorValue = TRUE];

inputs:
ATRLength(1.5), ATRMult(4.9), UpColor(cyan), DnColor(magenta),
starttrade (0800),
endtrade(1600),
dailyprofit(500),
dailyloss(250);


vars:
ATR(0),
avg(0),
dn(0),
up(0),
trend(1),
flag(0),
flagh(0),
SuperTrend(0),
todaynet(0),
yesterdaynet(0);
if date <> date[1] then begin
yesterdaynet = NetProfit;
end;


ATR = AvgTrueRange(ATRLength) * ATRMult;
avg = (high + low)/2;
up = avg + ATR;
dn = avg - ATR;

if close > up[1] then
trend = 1
else if close < dn[1] then
trend = -1;

if trend < 0 and trend[1] > 0 then flag=1 else flag=0;
if trend > 0 and trend[1] < 0 then flagh = 1 else flagh = 0;

if trend > 0 and dn < dn[1] then dn=dn[1];
if trend < 0 and up > up[1] then up=up[1];

if flag = 1 then up = avg + ATR;
if flagh = 1 then dn = avg - ATR;

todaynet = NetProfit + openpositionprofit - yesterdaynet;

condition98 = (-dailyloss < todaynet and todaynet < dailyprofit) and (Time > starttrade and time < endtrade);

if condition98 then begin


if trend = 1 and trend[1]< 0 Then sellshort next bar at open;
if trend = -1 and trend[1]< 0 then buy next bar at open;


end;
if trend = 1 and time >endtrade then buy to cover this bar Close;
if trend = -1 and time >endtrade then sell this bar close;
thanks a lot

RedBerlin
Posts: 74
Joined: 05 Dec 2014

Re: CLOSE WHEN PROFIT OR LOSS IS REACH.

Postby RedBerlin » 12 Dec 2018

maybe the right input should be : strategy report exit ....but I believe it doesnt exist.

User avatar
ABC
Posts: 636
Joined: 16 Dec 2006
Has thanked: 120 times
Been thanked: 359 times
Contact:

Re: CLOSE WHEN PROFIT OR LOSS IS REACH.

Postby ABC » 12 Dec 2018

Hi RedBerlin,

the code you posted already tracks the profit you could use this within your exit conditions, too. For example modifying your exit checks from

Code: Select all

...and time >endtrade then...
to

Code: Select all

...and ( time >endtrade or todaynet >= dailyprofit ) then...
might work for you.

Regards,

ABC

RedBerlin
Posts: 74
Joined: 05 Dec 2014

Re: CLOSE WHEN PROFIT OR LOSS IS REACH.

Postby RedBerlin » 12 Dec 2018

Thanks alot ABC
just Im tryng in real time
but it doesnt work as I wait.
infact i think is calculated well profit, but not exit in bar!!!!!!!!!!!
it wait for: or setprofit or change direction from indicator and then close all.!!!
I d like it exit when strategy report reach my profit!!
now it stop all ,after change direction of trade from indicator or for various exit like profit,trailing brekeven o stoploss.!!
while for endtrade time it exit exacly .

User avatar
ABC
Posts: 636
Joined: 16 Dec 2006
Has thanked: 120 times
Been thanked: 359 times
Contact:

Re: CLOSE WHEN PROFIT OR LOSS IS REACH.

Postby ABC » 13 Dec 2018

Hi RedBerlin,

it might make sense for you to use the Print reserved word to check the values of todaynet and see why an order was or wasn't closed.
Your strategy is evaluated end of bar, so this should be kept in mind as it will not issue orders when the conditions are met intrabar.

The code snippet above would close out the trades when the daily profit is >= the dailyprofit input. Which is not a position based profit target, but a daily target exit. For a position based you can take a look at the built-in profit target for example.

Regards,

ABC

RedBerlin
Posts: 74
Joined: 05 Dec 2014

Re: CLOSE WHEN PROFIT OR LOSS IS REACH.

Postby RedBerlin » 13 Dec 2018

thanks ABC
I d like exit with dailyprofit input and not with setprofitarget.
but is possible to do it and needs to understand how itsnt work or tecnically is not possible?

User avatar
ABC
Posts: 636
Joined: 16 Dec 2006
Has thanked: 120 times
Been thanked: 359 times
Contact:

Re: CLOSE WHEN PROFIT OR LOSS IS REACH.

Postby ABC » 13 Dec 2018

Hi RedBerlin,

you will have to track down why the exit isn't triggering when you think it should and the print reserved word can be useful here to evaluate your variable values on every bar.

Regards,

ABC

RedBerlin
Posts: 74
Joined: 05 Dec 2014

Re: CLOSE WHEN PROFIT OR LOSS IS REACH.

Postby RedBerlin » 13 Dec 2018

thanks ABC FOR HELPING
i m triyng with setprofittarget and it seems to work.
of course it is not same thing because if i set profittarget 20 and dailyprofit 100 i need 5 profit filled but in meantime could be a loss and so, in case of run up the AND price touch my daily profit 100 , I have to wait for filled profit target.
but if doesnt happen and price go back my 100 daily profit will be not filled.
it is an example of difference ...small o big I dont know.
thanks and sorry for my English.

SUPER
Posts: 632
Joined: 03 Mar 2007
Has thanked: 105 times
Been thanked: 81 times

Re: CLOSE WHEN PROFIT OR LOSS IS REACH.

Postby SUPER » 14 Dec 2018

Have a look at this code it may provide some clues;

Code: Select all


{
Strategy Name: F_PLHit_StopTrading1
Posted by ruletrader7320
Date: 8/28/2009
Topic_ID=92850

Requirements:
For ExitIntraTrade input to be false:
Stop trading after normal position is closed even if profit or
stop targets are hit during the trade.
After a trade is exited, check for day proft or loss. Then, do
not trade for rest of day once the day profit target or day stop
loss has been hit in the previously closed trade.
For ExitIntraTrade input to be true;
Exit trades immediately when profit target or stop target is hit.

Exit any trades at the beginning of the last bar of the session.

Description: This code uses the strategy profit and loss, and does not
consider slippage, commissions, and account balances.
}

inputs:
numContracts(1), // number of contracts
ExitIntraTrade(true), // true: exit trades when if targets hit during trade
// false: check targets only when flat.
DayPTarget( 500 ), // day profit target per contract in dollars
DaySTarget( -250 ), // day stop loss target per contract in dollars
TradePTarget(200), // trade profit target per contract in dollars
TradeSTarget(-100), // trade stop loss target per contract in dollars
startTime(0831), // start time
endTime(1514), // end time
trace(true),
testDate(1090629);

variables:
NP( 0 ),
OPP( 0 ),
PLB4Today( 0 ),
ProfToday( 0 ),
MP(0),
tradeON(false),
myTradePTgtPts(0),
myTradeSTgtPts(0),
myDayPTarget(0),
myDaySTarget(0);

MP = MarketPosition;
NP = NetProfit ;
OPP = OpenPositionProfit ;

{**************************************
Initialize at beginning of strategy
**************************************}
once begin
{**************************
Convert dollars to points
***************************}
myTradePTgtPts = TradePTarget/BigPointValue;
myTradeSTgtPts = -1 * TradeSTarget/BigPointValue;

{****************************************
Multiply day profit target per contract
by the number of contracts
*****************************************}
myDayPTarget = numContracts * DayPTarget;
myDaySTarget = numcontracts * DaySTarget;
end;

{******************************************************
Initialize at beginning of day (for day chart)
NOTE: for 24 hour charts use "if date = startTime ..."
*******************************************************}
if date <> date[1] then begin
PLB4Today = NP[1] + OPP[1] ; // Profit before today
tradeON = true;
end;

{********************************************
At end of every bar calculate today's profit
*********************************************}
ProfToday = NP + OPP - PLB4Today ; // Profit today

{********************************************************
Turn off entries when a day profit or loss target it hit
*********************************************************}
if ExitIntraTrade then begin
{******************************************************
Check profit and loss on every bar and exit intraTrade
*******************************************************}
if ProfToday >= myDayPTarget or ProfToday <= myDaySTarget then begin
tradeON = false;
if MP = 1 then begin
sell ("PLHit_LX") next bar at market;
end
else if MP = -1 then begin
buyToCover ("PLHit_SX") next bar at market;
end;
end;
end
else begin
{****************************************
Check profit and loss only when flat
*****************************************}
If MP = 0 then begin
if ProfToday >= myDayPTarget or ProfToday <= myDaySTarget then begin
tradeON = false;
end ;
end;
end;

{******************************************************
Exits (REPLACE WITH YOUR STRATEGY EXITS)
*******************************************************}
if MP = 1 then begin
sell ("TradePTgt_LX") next bar at entryPrice + myTradePTgtPts limit;
sell ("TradeSTgt_LX") next bar at entryPrice - myTradeSTgtPts stop;
end
else if MP = -1 then begin
buyToCover ("TradePTgt_SX") next bar at entryPrice - myTradePTgtPts limit;
buyToCover ("TradeSTgt_SX") next bar at entryPrice + myTradeSTgtPts stop;
end;

{****************************************************
Entries (REPLACE WITH YOUR STRATEGY ENTRIES)
>>> BE SURE TO SURROUND ENTRIES WITH THE <<<
>>> "if tradeON ..." flag <<<
>>> that's what turns the entries off <<<
>>> when profit or stop targets are hit <<<
*****************************************************}
{ --- fast and slow exponential moving average setups --- }
variables: FastLength(8), SlowLength(16), FastAvg( 0 ), SlowAvg( 0 ) ;

FastAvg = XAverage( Close, FastLength ) ;
SlowAvg = XAverage( Close, SlowLength ) ;

if tradeON and time >= startTime and time < endTime then begin
{ --- long entry conditions for XMA cross over--- }
if CurrentBar > 1 and MP = 0 and FastAvg crosses over SlowAvg then begin
buy ("LE") next bar numContracts contracts at market;
end

{ --- short entry conditions for XMA cross under--- }
else if CurrentBar > 1 and MP = 0 and FastAvg crosses under SlowAvg then begin
sellShort ("SE") next bar numContracts contracts at market;
end;
end;

{*****************************************
Exit end of day
******************************************}
if time = endTime then begin // must be at least one bar less that last bar of session.
tradeON = false;
if MP = 1 then begin
sell ("ExitEOD_LX") next bar at market;
end
else if MP = -1 then begin
buyToCover ("ExitEOD_SX") next bar at market;
end;
end;

{ diagnostic }
if date = testDate then begin
print (File("c:\F_ProfitLossExit.txt"),date:8:0,"|",time :5:0,"|tradeON ",tradeON,"|ProfToday ",ProfToday,
"|NP ",NP,"|OPP ",OPP,"|PLB4Today ",PLB4Today,"|MP ",MP,"|myDayPTarget ",myDayPTarget);
end;




RedBerlin
Posts: 74
Joined: 05 Dec 2014

Re: CLOSE WHEN PROFIT OR LOSS IS REACH.

Postby RedBerlin » 14 Dec 2018

hi Super thank you a lot
about strategy it seems a big improvement than other one.
but i tried it shortly and sometimes when hit the DAY PROFIT close well sometimes when hit doesnt filled.
so I dont understand if is closed at closed of bar or intrabar.
as I said I have had no time today to test it.
for sure is a good but i havet to check where close.
I ll update later
thanks

RedBerlin
Posts: 74
Joined: 05 Dec 2014

Re: CLOSE WHEN PROFIT OR LOSS IS REACH.

Postby RedBerlin » 14 Dec 2018

maybe I want too much ,however I ve seen before in real time that with dayloss 8 point and dayiprrofit 8 points the
first trade with a run up of 9.23 points wasn t filled and changing direction the trade was loss and filled with 13 point.
so ,with ExitIntraTrade input why the trade didnt filled at 9.23 points ?

RedBerlin
Posts: 74
Joined: 05 Dec 2014

Re: CLOSE WHEN PROFIT OR LOSS IS REACH.

Postby RedBerlin » 14 Dec 2018

sorry if i insist
but another trade luckily in paper with dayprofit of 8 points and run up of 15 points still open .
i think it wait the close of bar.
so in this way to me it doesnt work.


Return to “MultiCharts”