about stop trading when meeting daily profit target

Questions about MultiCharts and user contributed studies.
User avatar
johnmok
Posts: 85
Joined: 18 Oct 2011
Has thanked: 30 times
Been thanked: 8 times

about stop trading when meeting daily profit target

Postby johnmok » 09 Nov 2011

part of my code is shown below, the concept is that i want the program to stop trading whenever the daily profit meet $800 or loss $800.

i use a dailyprofit counter to detect the daily total profit.

at the end of the day, the dailyprofit counter will set zero and works another round the next day...

but the program is trading the same as without the counter....

am i making any mistake? any easier method to do so? thanks a lot!

Code: Select all

[intrabarordergeneration = true];

input: NetProfitLimit(800), NetLossLimit(-800);
var: var0(0), var1(0), var2(0), var3(0), DailyProfit(0), tradeprofit(0);

tradeprofit = PosTradeProfit(0,0);

condition1 = CurrentBar > 2 and var2 cross under varX ;
condition2 = CurrentBar > 2 and var2 cross over varY ;
condition3 = DailyProfit <= NetProfitLimit and DailyProfit >= NetLossLimit;

if marketposition = 0 and condition3 and condition1 then
Buy next bar market
else if marketposition = -1 and condition3 and condition1 then
begin
DailyProfit = DailyProfit + tradeprofit;
Buy next bar market;
end;;

if marketposition = 0 and condition3 and condition2 then
Sell Short next bar market
else if marketposition = 1 and condition3 and condition2 then
begin
DailyProfit = DailyProfit + tradeprofit;
Sell Short next bar market;
end;;

input: CloseTime(1615);

if (Time = CloseTime) then
Begin
RecalcLastBarAfter(1);
DailyProfit = 0;
sell next bar market;
buytocover next bar market;
End;

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: about stop trading when meeting daily profit target

Postby TJ » 09 Nov 2011

try this:

Code: Select all

if d > d[1] then
DailyProfit = 0;


if DailyProfit < 800
and DailyProfit > -800
then
begin

{---- put your trading code here -----}

end;

User avatar
johnmok
Posts: 85
Joined: 18 Oct 2011
Has thanked: 30 times
Been thanked: 8 times

Re: about stop trading when meeting daily profit target

Postby johnmok » 09 Nov 2011

hi TJ, i've tried your code, it shows error message

"tried to reference back more bars(51) than allowed by the current maxbarsback setting.
please increase the maxbarsback setting."

the script should just compare the date difference, nothing related to moving average sort of things that need to reference a lot of bars. it confused me.

any ideas? thanks :-)

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: about stop trading when meeting daily profit target

Postby TJ » 09 Nov 2011

hi TJ, i've tried your code, it shows error message

"tried to reference back more bars(51) than allowed by the current maxbarsback setting.
please increase the maxbarsback setting."

the script should just compare the date difference, nothing related to moving average sort of things that need to reference a lot of bars. it confused me.

any ideas? thanks :-)
can't tell you more without looking at the code

User avatar
johnmok
Posts: 85
Joined: 18 Oct 2011
Has thanked: 30 times
Been thanked: 8 times

Re: about stop trading when meeting daily profit target

Postby johnmok » 09 Nov 2011

thanks TJ, i modified the code as you said and the max bar error disappear.

but the problem is that, no matter the daily netprofit exceed 800 or below -800, the system keeps on trading.... the limit trade function of the strategy still not work..

let me show you the full code of my strategy....thanks!

Code: Select all

[intrabarordergeneration = true];

input: NetProfitLimit(800), NetLossLimit(-800);
var: DailyProfit(0), tradeprofit(0);

if d > d[1] then
DailyProfit = 0;

{Stochastic Entry}
inputs: Length( 14 ), OverSold( 20 ) ;
variables: var0( 0 ), var1( 0 ), var2( 0 ), var3( 0 ) ;

Value1 = Stochastic( H, L, C, Length, 3, 3, 1, var0, var1, var2, var3 ) ;

condition1 = CurrentBar > 2 and var2 crosses over var3 and var2 < OverSold ;
if condition1 then
Buy ( "StochLE" ) next bar at market ;

if DailyProfit <= NetProfitLimit and DailyProfit >= NetLossLimit then
begin
if marketposition = 0 and condition1 then
Buy ( "STO_LE" ) next bar market
else if marketposition = -1 and condition1 then
begin
DailyProfit = DailyProfit + tradeprofit;
Buy ( "STO_C+LE" ) next bar market;
end;;

if marketposition = 0 and condition2 then
Sell Short ( "STO_SE" ) next bar market
else if marketposition = 1 and condition2 then
begin
DailyProfit = DailyProfit + tradeprofit;
Sell Short ( "STO_C+SE" ) next bar market;
end;;
end;

{Take Profit}
input: profit_target(400);

if tradeprofit >= profit_target then
begin
DailyProfit = DailyProfit + tradeprofit;
buytocover("Profit_SX") next bar market;
sell("Profit_LX") next bar market;
end;

{Stop Loss base on close price}
input: stoploss_value(400);

if tradeprofit <= -stoploss_value then
begin
DailyProfit = DailyProfit + tradeprofit;
buytocover("StopLoss_SX") next bar market;
sell("StopLoss_LX") next bar market;
end;

{EOS Exit and Reset Counter}
input: CloseTime(1615);

if (Time = CloseTime) then
Begin
RecalcLastBarAfter(1);
sell ("EOD_LX") next bar market;
buytocover("EOD_SX") next bar market;
End;
Last edited by johnmok on 13 Nov 2011, edited 1 time in total.

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: about stop trading when meeting daily profit target

Postby TJ » 09 Nov 2011

You can debug by adding PRINT statements to the code.

You will need to know if DailyProfit is actually at the value you expected.



ps. what is your chart resolution?

User avatar
johnmok
Posts: 85
Joined: 18 Oct 2011
Has thanked: 30 times
Been thanked: 8 times

Re: about stop trading when meeting daily profit target

Postby johnmok » 09 Nov 2011

You can debug by adding PRINT statements to the code.

You will need to know if DailyProfit is actually at the value you expected.



ps. what is your chart resolution?
hi TJ, i use the strategy on 5 min chart :-)

User avatar
johnmok
Posts: 85
Joined: 18 Oct 2011
Has thanked: 30 times
Been thanked: 8 times

Re: about stop trading when meeting daily profit target

Postby johnmok » 10 Nov 2011

after checking the print dailyprofit value, i find that the strategy i wrote can just save the current position profit to the dailyprofit variable, rather than do what i want, accumulate the result. just as my statement write, dailyprofit = dailyprofit + tradeprofit.

i suspect that the dailyprofit variable keep on set zero before the statement runs, so that the dailyprofit variable can just record down the current tradeprofit?

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: about stop trading when meeting daily profit target

Postby TJ » 10 Nov 2011

after checking the print dailyprofit value, i find that the strategy i wrote can just save the current position profit to the dailyprofit variable, rather than do what i want, accumulate the result. just as my statement write, dailyprofit = dailyprofit + tradeprofit.

i suspect that the dailyprofit variable keep on set zero before the statement runs, so that the dailyprofit variable can just record down the current tradeprofit?
this is in the wrong place:

Code: Select all

DailyProfit = DailyProfit + tradeprofit;
try it at the end of the code, outside of the loop.


Return to “MultiCharts”