Help creating a "switch" in code

Questions about MultiCharts and user contributed studies.
User avatar
ZaphodB
Posts: 64
Joined: 12 Feb 2019
Has thanked: 20 times
Been thanked: 2 times

Help creating a "switch" in code

Postby ZaphodB » 19 Jun 2019

Greetings community, I have been spending free time whenever I can working on learning EL/MC. I am getting stuck on storing data in a variable for future bars and with setting trailing stop ENTRIES. I spent many hours scouring the internet and manuals trying to figure this out and I am new to programming, so please be gentle :-)

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;

User avatar
ZaphodB
Posts: 64
Joined: 12 Feb 2019
Has thanked: 20 times
Been thanked: 2 times

Re: Help creating a "switch" in code

Postby ZaphodB » 19 Jun 2019

Also, if I'm understanding correctly, I'm going to have a problem with "ZaphodLow" where the value will be the Low of the current bar and won't stay at the lowest low of recent bars (which is what I'm intending to do).

This also is part of my original query of how I can get a variable to store a value rather than refresh on every bar. This is like trying to learn Spanish just by reading a book haha. Helps if there's some interaction with people who speak the language.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Help creating a "switch" in code

Postby TJ » 19 Jun 2019

What is your chart resolution?

Can you draw a diagram to illustrate your thoughts?

What are the "What-if" scenarios you will encounter?

User avatar
ZaphodB
Posts: 64
Joined: 12 Feb 2019
Has thanked: 20 times
Been thanked: 2 times

Re: Help creating a "switch" in code

Postby ZaphodB » 19 Jun 2019

What is your chart resolution?
1 hour chart
Can you draw a diagram to illustrate your thoughts?
I think I can. Will that help you answer my question if I do?
What are the "What-if" scenarios you will encounter?
Not sure what you mean by what-if scenarios. This is intended as an exercise. My goal is to learn how to use EasyLanguage. I kept getting hung up on storing values so I simplified everything and still couldn't figure it out. I read about arrays but I'm not sure if those refresh on every bar or if I can get them to store values from previous bars. Thanks for any help

User avatar
ZaphodB
Posts: 64
Joined: 12 Feb 2019
Has thanked: 20 times
Been thanked: 2 times

Re: Help creating a "switch" in code

Postby ZaphodB » 19 Jun 2019

I may have found a way to do this but not sure if my understanding of arrays is correct. I'm not sure if the variables have to be initialized as an array or just using these style brackets [ ] turns a variable into an array whilly-nilly. I attempted below to get the low price of the last 5 bars IF the required number of negative standard deviations has been tripped. More notes in code... Am I doing this the hard way?

Code: Select all

Inputs: Price (close), Length (20), multi(1.005); //multiplier for trailing entry Variables: smaValue (0), ZaphodDev (0), NumberDevs (0), ZaphodLow5(9999999), ZaphodLow4(9999999), ZaphodLow3(9999999), ZaphodLow2(9999999), ZaphodLow1(9999999), ZaphodLow0(9999999), ZaphodUltimateLow (9999999) ; smaValue = AverageFC (Price, Length) ; ZaphodDev = StandardDev (Price, Length, 1) ; NumberDevs = (Price-smaValue) / ZaphodDev ; // If Number of Std. Deviations is lower than -2.0 then consider Low for trailing entry If NumberDevs[5]<-2 then ZaphodLow5 = Low[5] else ZaphodLow5 = 9999999 ; If NumberDevs[4]<-2 then ZaphodLow4 = Low[4] else ZaphodLow4 = 9999999 ; If NumberDevs[3]<-2 then ZaphodLow3 = Low[3] else ZaphodLow3 = 9999999 ; If NumberDevs[2]<-2 then ZaphodLow2 = Low[2] else ZaphodLow2 = 9999999 ; If NumberDevs[1]<-2 then ZaphodLow1 = Low[1] else ZaphodLow1 = 9999999 ; If NumberDevs <-2 then ZaphodLow0 = Low else ZaphodLow0 = 9999999 ; // The below is intended to figure out which bar of past 5 bar had lowest Low, assuming they met the std deviation requirement If ZaphodLow1 < ZaphodLow2 then ZaphodUltimateLow = ZaphodLow1 ; If ZaphodLow3 < ZaphodUltimateLow then ZaphodUltimateLow = ZaphodLow3 ; If ZaphodLow4 < ZaphodUltimateLow then ZaphodUltimateLow = ZaphodLow4 ; If ZaphodLow5 < ZaphodUltimateLow then ZaphodUltimateLow = ZaphodLow5 ; // Now I figure out if current Low is lower than past 5 bars If ZaphodLow0 < ZaphodUltimateLow then ZaphodUltimateLow = ZaphodLow0 ; //ORDER EXECUTION // Buy Orders If ZaphodUltimateLow < 9999999 // Default value is 9999999 if required number deviations hasn't been tripped in last 5 bars and marketposition = 0 and Price >= ZaphodUltimateLow * multi then buy next bar at market; // Sell Orders If marketposition>0 and NumberDevs>2 then sell next bar at market;
Last edited by ZaphodB on 19 Jun 2019, edited 3 times in total.

User avatar
ZaphodB
Posts: 64
Joined: 12 Feb 2019
Has thanked: 20 times
Been thanked: 2 times

Re: Help creating a "switch" in code

Postby ZaphodB » 19 Jun 2019

Crap, this doesn't solve it because I still have issue with storing deviation exceedance over prior bars but I think I just need to add another array variable. A job for tomorrow.

MAZINGUER
Posts: 75
Joined: 06 Jan 2017
Has thanked: 9 times
Been thanked: 25 times

Re: Help creating a "switch" in code

Postby MAZINGUER » 20 Jun 2019

Hi,
This also is part of my original query of how I can get a variable to store a value rather than refresh on every bar
Perhaps the MRO and LRO functions could be useful to you

User avatar
ZaphodB
Posts: 64
Joined: 12 Feb 2019
Has thanked: 20 times
Been thanked: 2 times

Re: Help creating a "switch" in code

Postby ZaphodB » 20 Jun 2019

Hi,

Perhaps the MRO and LRO functions could be useful to you
That is supremely helpful. I could compare the MRO of deviation exceedance to MRO of buy signal to create the switch I think. Thank you!

On another note, is there a way to get the lowest or highest value of an array of a specified lookback without having to hard code all of it?

MAZINGUER
Posts: 75
Joined: 06 Jan 2017
Has thanked: 9 times
Been thanked: 25 times

Re: Help creating a "switch" in code

Postby MAZINGUER » 20 Jun 2019


is there a way to get the lowest or highest value of an array of a specified lookback without having to hard code all of it?
Functions: EXTREMESARRAY, HIGHESTARRAY, LOWESTARRAY

User avatar
ZaphodB
Posts: 64
Joined: 12 Feb 2019
Has thanked: 20 times
Been thanked: 2 times

Re: Help creating a "switch" in code

Postby ZaphodB » 20 Jun 2019

Thank you! I also found the Highest and Lowest functions through reading an old post on another forum. I was starting to worry that I hit a roadblock with capability of EL/PL and it would negatively affect my performance. Seems like I can reference historical data sufficiently and what I'm trying will probably be do-able.

I also was able to create some indicators to see if using [ ] brackets worked like I thought they did. According to the displays I'm getting on my charts, they do. Appears that you don't need to initialize the variable as an array, just using those brackets gets the old value x bars back.

I'll try to rewrite the above exercise code with the new knowledge so if someone else comes along it might help them.


Return to “MultiCharts”