Turn off a signal if it isn't valid on restart

Questions about MultiCharts and user contributed studies.
User avatar
MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 288 times

Turn off a signal if it isn't valid on restart

Postby MAtricks » 12 Mar 2015

Simplest example I can think of:

Code: Select all

if average(C, 10) crosses under average(C,20) then begin
Value1 = C ;
Condition1 = True ;
end ;

if Condition1 then
Sellshort next bar Value1 LIMIT ;

if MarketPosition<0 then
Condition1 = False ;

SetStopLoss( 100 ) ;
SetProfitTarget( 100 ) ;
Everything would work fine here until you decided to restart MC. On restart, MC would not know whether or not Condition1 should be False or not and would place the limit order in the market regardless if it should or not.

My question is: How do I write this in a way that MC would know what state Condition1 is supposed to be in after a restart of the strategy?
Last edited by MAtricks on 12 Mar 2015, edited 1 time in total.

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Turn off a signal if it isn't value on restart

Postby orion » 12 Mar 2015

You have to log changes to condition to persistent storage (file on disk) and have a once/begin block in your code that reads the condition from the file log on strategy restart.

User avatar
MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 288 times

Re: Turn off a signal if it isn't value on restart

Postby MAtricks » 12 Mar 2015

Thanks Orion :)

I really was hoping that this wasn't the way to solve my problem.

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Turn off a signal if it isn't value on restart

Postby orion » 12 Mar 2015

The file log method is the simplest; let me know if you need help with it. There is only one other way which is a bit more complicated; it requires implementing a daemon process outside of MC.

escamillo
Posts: 203
Joined: 25 Mar 2011
Has thanked: 23 times
Been thanked: 56 times

Re: Turn off a signal if it isn't value on restart

Postby escamillo » 12 Mar 2015

?
Last edited by escamillo on 18 Mar 2015, edited 1 time in total.

User avatar
MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 288 times

Re: Turn off a signal if it isn't value on restart

Postby MAtricks » 12 Mar 2015

Thank you Orion.

Escamillo,

I don't believe that your suggestion will work once a strategy is restarted. It doesn't know that the previous position was exited via PT or SL before the restart so the strategy will send out an order to sellshort at value1.

escamillo
Posts: 203
Joined: 25 Mar 2011
Has thanked: 23 times
Been thanked: 56 times

Re: Turn off a signal if it isn't valid on restart

Postby escamillo » 13 Mar 2015

.
Last edited by escamillo on 18 Mar 2015, edited 2 times in total.

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

Re: Turn off a signal if it isn't valid on restart

Postby furytrader » 13 Mar 2015

I'm confused - what do you mean by "restart" - do you mean actually shutting down MC and restarting it, or do you mean turning a system off and then on? Under both circumstances, wouldn't MC process all of the proceeding history up until today, thereby detecting the crossover that occurred historically?

orion
Posts: 250
Joined: 01 Oct 2014
Has thanked: 65 times
Been thanked: 104 times

Re: Turn off a signal if it isn't valid on restart

Postby orion » 13 Mar 2015

I'm confused - what do you mean by "restart" - do you mean actually shutting down MC and restarting it, or do you mean turning a system off and then on? Under both circumstances, wouldn't MC process all of the proceeding history up until today, thereby detecting the crossover that occurred historically?
This would be correct if only dependency was price action on chart but condition is also affected by market position which depends on actual fills at broker.

User avatar
MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 288 times

Re: Turn off a signal if it isn't valid on restart

Postby MAtricks » 13 Mar 2015

Imagine this situation:

1. A crossover happens so ShortCondition is turned on and a Limit order is placed at Value1
2. Value1 is reached so now we're short. ShortCondition is turned off because MP<0
3. The market goes away from you and you're stopped out
4. You turn OFF the strategy for whatever reason (I do daily platform restarts)
5. You turn the strategy back on. MP=0 and the crossover is logged so ShortCondition is now true again.
6. An order is reentered at an already used Value1 price -- which is exactly what I do not want.

What Orion said makes sense. To the best of my knowledge, this position needs to be logged to avoid reentry. If there is a better way of doing this, I'd love to know.

Jad
Posts: 92
Joined: 15 Jun 2014
Has thanked: 13 times
Been thanked: 21 times

Re: Turn off a signal if it isn't valid on restart

Postby Jad » 14 Mar 2015


1. A crossover happens so ShortCondition is turned on and a Limit order is placed at Value1
2. Value1 is reached so now we're short. ShortCondition is turned off because MP<0
3. The market goes away from you and you're stopped out
4. You turn OFF the strategy for whatever reason (I do daily platform restarts)
5. You turn the strategy back on. MP=0 and the crossover is logged so ShortCondition is now true again.
6. An order is reentered at an already used Value1 price -- which is exactly what I do not want.
No doubt your full process is more complex than your example. So, there are some unknowns that may make the solution more involved.

I haven't tried this but couldn't you maintain an Arw object and update its Date/Time/Price/Direction/Optional Text etc. only if your SL is hit (and/or PT)? That would continually move it forward every time a new signal's position is closed.

Then, after the restart, you could get the Arw's details and compare them with the latest signal generated. e.g. If they're the same Date/Time/Bar/Value 1 Price (or whatever), then you already opened and closed the position generated by this signal. If it's older, you didn't.

You don't mention whether you ever perform your daily platform restarts if you still have a filled order and working SL/PT but, if you do, then you might also have to turn the assign the market position at broker on or add a check for it (in the once/begin section) and set a flag to reference later in your code snippet to avoid sending a second, duplicate order.


Return to “MultiCharts”