Stop Trading Logic

Questions about MultiCharts and user contributed studies.
tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Stop Trading Logic

Postby tony » 31 Jul 2013

I'm trying to have my strategy stop trading for a set period of time after a pre-determined loss and or after a SetStopLoss() condition is met.

No different than a trader saying I just took a big loss, let me step back for 20 minutes.

I'm using IntraBarOrder Generation = True which makes it a bit more difficult. Any suggestions would be most welcome. Thank You!

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: Stop Trading Logic

Postby ABC » 01 Aug 2013

You can monitor the trade performance using PosTradeProfit for example.
If you detect the last trade was a loss, either block new trades for a number of bars or a number of minutes. Depending on the bar you are working with you can use the bar time or need to use the computer time.

Regards,
ABC
I'm trying to have my strategy stop trading for a set period of time after a pre-determined loss and or after a SetStopLoss() condition is met.

No different than a trader saying I just took a big loss, let me step back for 20 minutes.

I'm using IntraBarOrder Generation = True which makes it a bit more difficult. Any suggestions would be most welcome. Thank You!

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Stop Trading Logic

Postby tony » 01 Aug 2013

Thanks for the tip but this would be in autotrading so I'm looking to write this logic into my script.

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: Stop Trading Logic

Postby ABC » 01 Aug 2013

I am not sure if I follow you, but this was meant to be the logic for a script. "PosTradeProfit" is a reserved word to monitor the performance of a position. Depending on what you need to use for the time, the reserved words you could use are "Time", "Time_s", "DateTime", "ComputerDateTime" and "CurrentTime_s".

Regards,
ABC

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Stop Trading Logic

Postby tony » 01 Aug 2013

I kind of figured this out and wanted to post for "the sake of the MC wiki." The more users can build up the wiki the better all of us are right.

I set up a variable (var1) to determine the equivalent of 5 minutes after entry time

var1 = calctime (entrytime, 5);

I then added this conditional statement that combines position profit and time duration to trigger in this case a sell.


if marketposition = 1 and currentcontracts = 20 and currenttime > var1 AND positionprofit > -500 then sell ( "time" ) 20 contracts next bar at market;

I need to play around with this but wanted to at least post. Hope this is of value.

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: Stop Trading Logic

Postby piranhaxp » 02 Aug 2013

if marketposition = 1 and currentcontracts = 20 and currenttime > var1 AND positionprofit > -500 then sell ( "time" ) 20 contracts next bar at market;
this sounds a little bit different to your entry in this thread .........
I'm trying to have my strategy stop trading for a set period of time after a pre-determined loss and or after a SetStopLoss() condition is met.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Stop Trading Logic

Postby tony » 02 Aug 2013

I was doing two different things, the prior posted "solution" was logic that said if you are in a trade and it is losing at a given rate then exit the trade. No different than a trader who puts a trade on and then immediately it goes against them.

As for the original quest to stop trading after x minutes when a SetStopLoss(0) is triggered, here is what looks to be working.

Var18 = 20 minutes after the exit of the last trade (and I'm not sure I have that calculated correctly so I'm not posting that piece).

I put the following above my entry criteria (MP=0) and then end; just above my exit criteria (MP=1, MP=-1).

C1 = IFF( positionprofit(1) > -9400, 1, 0 );
C2 = IFF( positionprofit(1) <= -9400 AND currenttime > var18, 1, 0);

If C1 = 1 or C2 = 1

then begin

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: Stop Trading Logic

Postby piranhaxp » 02 Aug 2013

Would :

Code: Select all

var18 = exittime_checked(1); // (1) defines last position ...
help you ?

Mike

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Stop Trading Logic

Postby tony » 02 Aug 2013

Would :

Code: Select all

var18 = exittime_checked(1); // (1) defines last position ...
help you ?

Mike
Thank you sir!

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: Stop Trading Logic

Postby piranhaxp » 02 Aug 2013

After a short review it is not what you are looking for. Without testing it I try to give you an idea with this signal :

Code: Select all

input: break1(20); // 20 minutes break

var: var1(0); // defines time of last exit
var: var2(0); // defines the minutes in fraction
var: var3(0); // defines currenttime in fraction
var: var4(0); // defines the last exit time in fraction
var: var5(0); // 0 to 1 ... if 1 go to action ...

if t = 900 then
begin
buy next bar at market;
end;

if t = 1000 then
begin
sell next bar at market;
var1 = exittime_checked(1);
end;

var2 = eltimetodatetime(break1);
var3 = eltimetodatetime(var1);
var4 = eltimetodatetime(time);

var5 = iff(var4 > (var2 + var3), 1, 0);

if var5 = 1 then buy next bar at market; // should buy at 10.21 am !?

setexitonclose;
Again, it is not tested and may some other guy has a better solution. I guess so, but I'm sure you get the idea when you see this signal working. So you go on with var18 as 0 or 1 switch like :

Code: Select all

C2 = IFF( positionprofit(1) <= -9400 AND var18 = 1, 1, 0);
Further I would set your stoploss as an INPUT and convert it into a variable to multiply it later with (-) for better optimizing way. If you wish it later on. From my experience working with var's is quicker than with inputs :

Code: Select all

input: sl(9400); // stoploss

var: var99(0); // converts input into variable

var99 = sl * (-1);

C2 = IFF( positionprofit(1) <= var99 AND var18 = 1, 1, 0);

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Stop Trading Logic

Postby tony » 02 Aug 2013

Thank you for the above code. I'll try it over the weekend. Will update what I find. Much appreciated!

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Stop Trading Logic

Postby tony » 03 Aug 2013

Piranahxp - no luck with the above code. I realized I could clean up my IFF statement as well to just


c1 = IFF( positionprofit[1] <= -9400 AND currenttime < var18, 1, 0);

If c1 = 0 then begin

I seem to be stuck with var18 which is a calculation that adds 20 minutes to the exit time of the last trade. I am trying the following but doesn't seem to work properly

c2 = exittime[1];
var18 = CalcTime ( c2, 20 );

I can change c1 to 1 and see how it impacts trading so it seems I have a problem in how I am calculating var18. Any help would be greatly appreciated.

Thank You!

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Stop Trading Logic

Postby tony » 07 Aug 2013

I'm getting closer solving this. Thanks to Dave at MC was able to learn that ExitTime is not the time of the exit of the position but the bar when the exit happened. So the key is to find when MP goes from 1 or -1 to 0. And then by placing the variable ET (exit time) inside the conditional, the value is not updated until the conditional changes. So here is what I have.

Now I'm just stuck converting ET which is in Julian Time so it can then compare to CurrentTime plus 20 minutes. The key which I was not aware of is the ability to have a variable hold its value.

Now the problem is ET converted to non-Julian time via DateTime2ELTime_s plus 20 minutes is not comparing correctly to CurrentTime. Which I even tried comparing it to ( CurrentDate + Currenttime).

ET (0),
intrabarpersist MP(0),

MP = MarketPosition;

If MP = 0 AND MP[1] <> 0

Then ET = ComputerDateTime;

c1 = IFF( PositionProfit(1) <= -10000 AND CurrentTime_s > DateTime2ELTime_s((ET + .0138888)), 1, 0);

c2 = IFF( PositionProfit(1) > -10000, 1, 0 );

If c1 = 1 or C2 = 1

Then Begin

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Stop Trading Logic

Postby tony » 07 Aug 2013

And here is a circuit breaker for the entire day if this is of value to anyone. YNP is a variable for Yesterday's Net Profit.

This piece of logic below says that if net losses exceed or are equal to 20000 to not trade for the remainder of the calendar day. The key once again is being able to set YNP to NetProfit for the close of the prior date and NOT recalculate for the current day.

If Date <> Date[1]

Then YNP = NetProfit;

If NetProfit - YNP >= -20000

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Stop Trading Logic

Postby tony » 07 Aug 2013

And here is a circuit breaker for the entire day if this is of value to anyone. YNP is a variable for Yesterday's Net Profit.

This piece of logic below says that if net losses exceed or are equal to 20000 to not trade for the remainder of the calendar day. The key once again is being able to set YNP to NetProfit for the close of the prior date and NOT recalculate for the current day.

If Date <> Date[1]

Then YNP = NetProfit;

If NetProfit - YNP >= -20000
Forgot to mention I have IOG=True so it is important to NOT have YNP as intrabarpersist but rather just declare the variable as if IOG=False.

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: Stop Trading Logic

Postby piranhaxp » 08 Aug 2013

Hmmm ....

1. Why you are working with numericals and fractions for timimg solutions in one place ?
2. Is your workstation set up for the exchange time of the contract you trade or are charts set up with local time to fit with your "ComputerDateTime" var(ET) ?

Regards.

Mike

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Stop Trading Logic

Postby tony » 08 Aug 2013

Hmmm ....

1. Why you are working with numericals and fractions for timimg solutions in one place ?
2. Is your workstation set up for the exchange time of the contract you trade or are charts set up with local time to fit with your "ComputerDateTime" var(ET) ?

Regards.

Mike
Fair question on 1. I'm just trying to get it to work right now so added in the Julian equivalent of 20 minutes (which I think is correct). But will use a variable to optimize once I know it works.

And on 2, great insight. Chart time and computer time do differ (I'm in Mountain time, using Chicago time for the charts). I'll take a look at that. Right now it seems like you may have nailed the answer because I am saying after a set loss wait 20 minutes after ET. But if computerdatetime is an hour ahead of chart time then computerdatetime is always an hour ahead of ET and therefore the script assumes the time conditional has been met. Nice catch! Thank You -

User avatar
piranhaxp
Posts: 241
Joined: 18 Oct 2005
Has thanked: 4 times
Been thanked: 30 times

Re: Stop Trading Logic

Postby piranhaxp » 08 Aug 2013

Check my proposal (post 10) again and work on your solution

Code: Select all

Then ET = ComputerDateTime;
c1 = IFF( PositionProfit(1) <= -10000 AND CurrentTime_s > DateTime2ELTime_s((ET + .0138888)), 1, 0);
and you will get it. I would prefer to stay and continue with the fraction solution. With DateTime2ELTime_s you are getting a numerical value of Time_s which is not wrong till you mix it up with a fraction of Time_s when you add .0138888 which is 0.20 am or 20min. It doesn't work. To mak eall perfect I would convert currenttime_s, computerdatetime and 20min in fractions and make the formula out of that ... Like I did in one of my past postings.

Keep on going to work with eltimetodatetime(xyz) or eltimetodatetime_s(xyz).

Mike


Return to “MultiCharts”