![Smile :-)](./images/smilies/icon_smile.gif)
I am trying to code a basic strategy that sets a trailing entry order when the Close is lower than 2 standard deviations below the SMA. I am trying to figure out how to code "ZaphodSwitchA" below. There are a few notes to the right of // that explain further what I'm trying to do. Any help would be appreciated.
Code: Select all
Inputs:
Price (close),
Length (20),
multi(1.005); //multiplier for trailing entry
Variables:
smaValue (0),
ZaphodDev (0),
NumberDevs (0),
ZaphodSwitchA (0), //The part I need help with, more details in notes below
ZaphodLow (9999999) ;
smaValue = AverageFC (Price, Length) ;
ZaphodDev = StandardDev (Price, Length, 1) ;
NumberDevs = (Price-smaValue) / ZaphodDev ;
If ZaphodLow > Low then ZaphodLow = Low ;
If (NumberDevs < -2.0) then set ZaphodSwitchA to 1 and leave it at 1 for all future bars until I have bought a position ; // I don't know how to code this part so I left it in English :-)
//ORDER EXECUTION
If ZaphodSwitchA = 1
and marketposition = 0
and Price >= ZaphodLow * multi
then buy next bar at market;
If marketposition>0
and NumberDevs>2
then sell next bar at market;