Applying Static Variables to create a Stop loss

Can anyone help with my understanding of static variable as I assume it will help with my problem. (Im a newbie at static variable so please be gentle :slight_smile: this is a simple exercise for me to learn about static variables I hope.

I have a pairs trading system (autotrader with IB) , which calculates total unrealized PL (PLTotal) from the 2 underlying trades (PL1 and PL2). Once PLTotal < -1000 it will trigger a respective buy/sell of the underlying (i.e. close both positions).

BUT what I want is once it is triggered to stop trading going forward. which I assume I can do with a static variable.

My logic:
Start with static variable (SLTrigger) set to 1 (which allows all trades)
Once unrealized PLTotal < -1000 then the variable switches to zero and does not change until I reset the system (i.e. restart) . So even if a signal still exists to enter a trade I can use the Zero to block it. (buy or sell only if SLT == 1)

My problem: it is 0 when PLTotal <-1000 but then back to 1 otherwise, it does not stay zero (because the PLTotal goes to $0 once trades are closed) thus the variable goes back to 1.

PLTotal	= (PL1 + PL2);

SLT		= StaticVarGet("SLTrigger");
StaticVarSet("SLTrigger",1);
SLT		= IIf(PLTotal < -1000, 0, SLT);
StaticVarSet("SLTrigger",SLT);

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.