Scope of variable  [SOLVED]

Questions about MultiCharts and user contributed studies.
Sylpha
Posts: 29
Joined: 14 Apr 2015
Has thanked: 1 time

Scope of variable

Postby Sylpha » 31 Jul 2020

Code: Select all

inputs: DonchianChannelLength(6); variables: currentDateTime(0), tradeDateBeginTime(915), tradeDateEndTime(1610), morningSessionBeginTime(915), morningSessionEndTime(1155), morningSessionFirstEntyTime(945), morningSessionLastEntyTime(1130), afternoonSessionBeginTime(1300), afternoonSessionEndTime(1610), afternoonSessionFirstEntyTime(1330), afternoonSessionLastEntyTime(1545), aDonchianChannel(0), ClosePrice( Close ), aDCHighPrice(High), aDCLowPrice(Low), MidPrice(0), entryCondition1(false), entryCondition2(false), entryCondition3(false), exitCondition1(false), exitCondition2(false), exitCondition3(false), exitCondition4(false), isTradeable(False); [INTRABARORDERGENERATION=false] currentDateTime = el_datetodatetime(date) + el_timetodatetime(time); if time > 945 and time <= 1130 then begin print("====================="); print("Current bar is: ", DateTimeToString(el_datetodatetime(date) + el_timetodatetime(time)), ", Open: ", Open, ", High: ", High, ", Low: ", Low , ", Close: ", Close ); //print("Highest(High, 6): " , Highest(High, 6)); //print("Highest(High): ", Highest[0](High, 6)); aDonchianChannel = DonchianChannel(DonchianChannelLength, MidPrice , aDCHighPrice, aDCLowPrice); If High > aDCHighPrice[1] then begin entryCondition1 = true; print("High: " , High); print("aDCHighPrice: ", aDCHighPrice[1]); end; print("entryCondition1: ", entryCondition1); If entryCondition1 = true then begin Buy ("BO Entry") next bar market; //entryCondition1 = false; end; if time > 950 then begin //print("HighestBar(High): ", HighestBar(High, 6)); //print("Highest(High, 6): ", Highest(High, 6)); // on bar close, find the location of the higest bar including current bar, so the oldest bar is current bar - 5. if aDCHighPrice > aDCHighPrice[1] then begin exitCondition1 = false; end else begin if aDCHighPrice = aDCHighPrice[1] then begin exitCondition1 = false; end else begin exitCondition1 = true; end; end; print("aDCHighPrice: ", aDCHighPrice); print("aDCHighPrice[1]: ", aDCHighPrice[1]); end; print("exitCondition1: ", exitCondition1); if exitCondition1 and marketposition > 0 then begin Sell ("BO Exit") next bar from entry("BO Entry") at market; end; print("=====================", NewLine); end; if time >= morningSessionEndTime and time < afternoonSessionBeginTime then begin if marketposition > 0 then sell next bar at market; //if marketposition < 0 then buytocover next bar at market; end;
I found that the variable entryCondition1 once set to true by the code, it will stay as true value forever.



The console log (at 10:40:00 AM, the High is 24675 which is lower than 24693, but the value of entryCondition1 is still true). Is it a bug or something? I think the variable should be re initiate on the new bar every time. Thanks.


=====================
Current bar is: 29/7/2020 10:35:00 AM, Open: 24655.00, High: 24693.00, Low: 24650.00, Close: 24668.00
High: 24693.00
aDCHighPrice: 24686.00
entryCondition1: TRUE
aDCHighPrice: 24693.00
aDCHighPrice[1]: 24686.00
exitCondition1: FALSE
=====================

=====================
Current bar is: 29/7/2020 10:40:00 AM, Open: 24667.00, High: 24675.00, Low: 24641.00, Close: 24645.00
entryCondition1: TRUE
aDCHighPrice: 24693.00
aDCHighPrice[1]: 24693.00
exitCondition1: FALSE
=====================

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

Re: Scope of variable

Postby TJ » 31 Jul 2020

>>The console log (at 10:40:00 AM, the High is 24675 which is lower than 24693, but the value of entryCondition1 is still true). Is it a bug or something? I think the variable should be re initiate on the new bar every time. Thanks.

Yes it is a bug -- your bug.

The variables are initialized when you load the strategy.

After that, the variable is controlled by your code.

The variable will not change until you tell it to do so.

Sylpha
Posts: 29
Joined: 14 Apr 2015
Has thanked: 1 time

Re: Scope of variable  [SOLVED]

Postby Sylpha » 31 Jul 2020

Thanks TJ.

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

Re: Scope of variable

Postby TJ » 31 Jul 2020

You can modify your code this way:

Code: Select all

If High > aDCHighPrice[1] then begin entryCondition1 = true; print("High: " , High); print("aDCHighPrice: ", aDCHighPrice[1]); end ELSE entryCondition1 = FALSE;


Return to “MultiCharts”