Stop Trading After Daily Loss Is Reached  [SOLVED]

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 After Daily Loss Is Reached

Postby tony » 06 Oct 2013

I swear I had this working now today it is not. I am trying to have my script trade only if it has not exceeded a daily allowable loss for that day. Below is what I wrote to achieve this. MaxDailyLoss is an input value and YNP is defined as a variable.

This works once a MaxDailyLoss is exceeded but then never starts trading again on the next day.

Code: Select all

IF Date <> Date[1]

Then

YNP = NetProfit;

IF NetProfit - YNP >= MaxDailyLoss

Then Begin

( enter buy and sell short logic )

End;
I would imagine I am missing something really simple here but like I said it was working for me before now seems to no longer work. Before it would shut down for the day then start back up on the next trading day. Thanks for any help.

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

Re: Stop Trading After Daily Loss Is Reached

Postby ABC » 06 Oct 2013

Hi tony,

what is the exact value for MaxDailyLoss?
My point is that it should be a negative value in your context.

Regards,
ABC

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

Re: Stop Trading After Daily Loss Is Reached

Postby tony » 06 Oct 2013

It is a negative value. I swear this was working and now it is not. Wasn't sure if placement of "end;" was in the wrong spot but it appears to be correct after trying a different placement. I am using IOG=True and have IntraBarPersist on all variables including YNP. Once the circuit breaker triggers the script never runs again.

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Stop Trading After Daily Loss Is Reached

Postby furytrader » 07 Oct 2013

This is weird. To debug it, I would place the following code at the end:

Code: Select all

Print(Date," ",Time," ",NetProfit," ",YNP," ",NumToStr(NetProfit - YNP,4)," ",MaxDailyLoss);
... this is going to generate a series of values for each bar in the Output window in the PowerLanguage Editor, each bar identified marked by the day and time of that bar. I would check each of these values, especially when the date switches over, and see what happens. This may give you a clue as to what is driving the program logic.

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

Re: Stop Trading After Daily Loss Is Reached

Postby tony » 01 Nov 2013

After scouring then putting this problem on the side I came back and found what appears to be a conflict. I had this day circuit breaker working just fine. And then I added the following line at the top of my script to specify when the script runs


IF Time is > 0920 AND Time is < 1600


Now if I remove that above line, the daily circuit breaker works. So it appears there is some conflict going on. The "end;" for this above line is placed next to the "end;" for the circuit breaker, both of which are placed after the conditionals to take a position but before the conditionals to exit the position.

I appreciate any insight anyone may have. Thank You!

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

Re: Stop Trading After Daily Loss Is Reached

Postby tony » 01 Nov 2013

Well the fix appears to be pretty simple. I just moved the logic for the time in which the script can run to below the logic for the daily circuit breaker and it works fine. I'd love to know why reversing the two causes the daily circuit breaker not to work though. But for now it appears just reversing the order so that the daily circuit breaker logic is before the time logic enables both to work properly.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Stop Trading After Daily Loss Is Reached

Postby Henry MultiСharts » 04 Nov 2013

Hello tony,

Based on the information you have provided I can assume the following:
The date of the bars inside this condition IF Time is > 0920 AND Time is < 1600 is the same.
That is why IF Date <> Date[1] logic did not trigger.

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

Re: Stop Trading After Daily Loss Is Reached

Postby tony » 04 Nov 2013

You are correct in your assumption. Can you elaborate further why the order of the two (time the script trades and date<>date[1]) matters. I'm not following the logic behind that. Thank You -

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Stop Trading After Daily Loss Is Reached  [SOLVED]

Postby Henry MultiСharts » 05 Nov 2013

If you are using intraday resolution and date<>date[1] is the first condition then it will trigger only once, on the first bar of the day, when the currentbar date is different from the previous bar date.
The following condition compares the bars that have the same date between 9.20 and 16.00:

Code: Select all

IF Time is > 0920 AND Time is < 1600
That is why IF Date <> Date[1] logic does not trigger when it comes after the Time condition.

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

Re: Stop Trading After Daily Loss Is Reached

Postby tony » 05 Nov 2013

I understand. Thanks for the help Henry. Much appreciated!


Return to “MultiCharts”