Issue with Global Variables  [SOLVED]

Questions about MultiCharts and user contributed studies.
Parker
Posts: 22
Joined: 18 May 2018
Has thanked: 7 times

Issue with Global Variables

Postby Parker » 26 Jun 2018

I'm having trouble using Global Variables.

I have imported the zip file found on the forum so I have the functions now available in PL editor. I have not been able to find any documentation on how to use them though. I've had a go using the following code but I have no values coming through in the GVGet functions.

This is in the sender signal code:

Code: Select all


//send data to global variables
GVSetDouble(1,EntryDate);
GVSetDouble(2,EntryTime);
GVSetString(3,EntryName);
GVSetDouble(4,MP);
GVSetDouble(5,Contracts);



This is in my receiver signal code:

Code: Select all


//pull data from global variables
GetEntryDate = GVGetDouble(1);
GetEntryTime = GVGetDouble(2);
GetEntryName = GVGetString(3);
GetMP = GVGetDouble(4);
GetContracts = GVGetDouble(5);



I get "-1" or missing for all values.

Am I missing something or using these functions incorrectly?

Thanks.

User avatar
TJ
Posts: 7151
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 988 times
Been thanked: 2041 times

Re: Issue with Global Variables

Postby TJ » 26 Jun 2018

Parker wrote:I'm having trouble using Global Variables.

I have imported the zip file found on the forum ...

Thanks.



The docs are in the zip file.
These users thanked the author TJ for the post:
Parker

Parker
Posts: 22
Joined: 18 May 2018
Has thanked: 7 times

Re: Issue with Global Variables

Postby Parker » 26 Jun 2018

Ok I have read the documentation. It sounds like my code is fine, but still getting errors.

It's not setting the values into the variables correctly. The following code gives me a result of "-1".

Code: Select all


Value1 = GVSetDouble(1,Close[1]);

Print(Value1);



Any idea what could be going wrong?

User avatar
TJ
Posts: 7151
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 988 times
Been thanked: 2041 times

Re: Issue with Global Variables

Postby TJ » 26 Jun 2018

If this is a coding question, you should post your codes.
I mean a complete set of codes that people can help you with, not a snippet here and there.

Parker
Posts: 22
Joined: 18 May 2018
Has thanked: 7 times

Re: Issue with Global Variables

Postby Parker » 26 Jun 2018

Ok, here is the full code.

I am working on a process for position recovery if auto trading is cut out for some reason. I am trying to write a function that my signals will call when auto trading is turned on. If there is an open trade on the symbol this function will check a csv file for the last market position, number of contracts, entry date/time, etc. This data is to be loaded into global variables and passed back to the signals for use in trade management. The read from file part is working fine. Just having trouble sending the values to global variables.

Code is a bit messy as is work in progress.

Code: Select all


inputs:
Filename(StringSimple);

variables:
ListC_ID(0),
FileRecords(0),
Date_ID(0),
Time_ID(0),
CurrentTime_ID(0),
Strategy_ID(0),
Magic_ID(0),
MP_ID(0),
mContracts_ID(0),
PrevMP_ID(0),
PrevmContracts_ID(0),
MPatBroker_ID(0),
MPatBrokerStrat_ID(0),
EntryName0_ID(0),
EntryDate0_ID(0),
EntryTime0_ID(0),
ExitName0_ID(0),
ExitDate0_ID(0),
ExitTime0_ID(0),
EntryName1_ID(0),
EntryDate1_ID(0),
EntryTime1_ID(0),
ExitName1_ID(0),
ExitDate1_ID(0),
ExitTime1_ID(0),
Date_("Date"),
Time_(""),
CurrentTime_(""),
Strategy_(""),
Magic_(""),
MP_(""),
mContracts_(""),
PrevMP_(""),
PrevmContracts_(""),
MPatBroker_(""),
MPatBrokerStrat_(""),
EntryName0_(""),
EntryDate0_(""),
EntryTime0_(""),
ExitName0_(""),
ExitDate0_(""),
ExitTime0_(""),
EntryName1_(""),
EntryDate1_(""),
EntryTime1_(""),
ExitName1_(""),
ExitDate1_(""),
ExitTime1_(""),

Recovered_EntryDate(0),
Recovered_EntryTime(0),
Recovered_EntryName(""),
Recovered_MP(0),
Recovered_Contracts(0)
;

if ELC.PathExists(FileName)=False THEN BEGIN;
Print("PositionHistory File Does Not Exist in Location ",FileName);
END

ELSE BEGIN

if CurrentBar = 1
//AND MarketPosition_at_Broker <> 0
 then BEGIN
   
ListC_ID = ListC.new;
Value1 = ListC.ReadFile(ListC_ID,Filename);

//virtual column names
Date_ID = ListC.Get(ListC_ID,1);
Time_ID = ListC.Get(ListC_ID,2);
CurrentTime_ID = ListC.Get(ListC_ID,3);
Strategy_ID = ListC.Get(ListC_ID,4);
Magic_ID = ListC.Get(ListC_ID,5);
MP_ID = ListC.Get(ListC_ID,6);
MPatBrokerStrat_ID = ListC.Get(ListC_ID,7);
MPatBroker_ID = ListC.Get(ListC_ID,8);
mContracts_ID = ListC.Get(ListC_ID,9);
PrevMP_ID = ListC.Get(ListC_ID,10);
PrevmContracts_ID = ListC.Get(ListC_ID,11);
EntryName0_ID = ListC.Get(ListC_ID,12);
EntryDate0_ID = ListC.Get(ListC_ID,13);
EntryTime0_ID = ListC.Get(ListC_ID,14);
ExitName0_ID = ListC.Get(ListC_ID,15);
ExitDate0_ID = ListC.Get(ListC_ID,16);
ExitTime0_ID = ListC.Get(ListC_ID,17);
EntryName1_ID = ListC.Get(ListC_ID,18);
EntryDate1_ID = ListC.Get(ListC_ID,19);
EntryTime1_ID = ListC.Get(ListC_ID,20);
ExitName1_ID = ListC.Get(ListC_ID,21);
ExitDate1_ID = ListC.Get(ListC_ID,22);
ExitTime1_ID = ListC.Get(ListC_ID,23);

//count number of rows in the file
FileRecords = ListS.Count(Date_ID);
Value2 = ListC.Count(ListC_ID);

//read only the last row of the file
Date_ = ListS.Get(Date_ID,FileRecords);
Time_ = ListS.Get(Time_ID,FileRecords);
CurrentTime_ = ListS.Get(CurrentTime_ID,FileRecords);
Strategy_ = ListS.Get(Strategy_ID,FileRecords);
Magic_ = ListS.Get(Magic_ID,FileRecords);
MP_ = ListS.Get(MP_ID,FileRecords);
MPatBrokerStrat_ = ListS.Get(MPatBrokerStrat_ID,FileRecords);
MPatBroker_ = ListS.Get(MPatBroker_ID,FileRecords);
mContracts_ = ListS.Get(mContracts_ID,FileRecords);
PrevMP_ = ListS.Get(PrevMP_ID,FileRecords);
PrevmContracts_ = ListS.Get(PrevmContracts_ID,FileRecords);
EntryName0_ = ListS.Get(EntryName0_ID,FileRecords);
EntryDate0_ = ListS.Get(EntryDate0_ID,FileRecords);
EntryTime0_ = ListS.Get(EntryTime0_ID,FileRecords);
ExitName0_ = ListS.Get(ExitName0_ID,FileRecords);
ExitDate0_ = ListS.Get(ExitDate0_ID,FileRecords);
ExitTime0_ = ListS.Get(ExitTime0_ID,FileRecords);
EntryName1_ = ListS.Get(EntryName1_ID,FileRecords);
EntryDate1_ = ListS.Get(EntryDate1_ID,FileRecords);
EntryTime1_ = ListS.Get(EntryTime1_ID,FileRecords);
ExitName1_ = ListS.Get(ExitName1_ID,FileRecords);
ExitDate1_ = ListS.Get(ExitDate1_ID,FileRecords);
ExitTime1_ = ListS.Get(ExitTime1_ID,FileRecords);


//convert data to correct formats
Recovered_EntryDate = StringToDate(EntryDate0_);
Recovered_EntryTime = StringToTime(EntryTime0_);
Recovered_EntryName = EntryName0_;
Recovered_MP = StrToNum(MP_);
Recovered_Contracts = StrToNum(mContracts_);

Print("Date_=",Date_," | Recovered_EntryDate=",Recovered_EntryDate," | Recovered_EntryTime=",Recovered_EntryTime," | Recovered_EntryName=",Recovered_EntryName," | Recovered_MP=",Recovered_MP," | Recovered_Contracts=",Recovered_Contracts);

//send data to global variables
Value3 = GVSetDouble(1,Recovered_EntryDate);
Value4 = GVSetDouble(2,Recovered_EntryTime);
Value5 = GVSetString(3,Recovered_EntryName);
Value6 = GVSetDouble(4,Recovered_MP);
Value7 = GVSetDouble(5,Recovered_Contracts);
Value8 = GVSetDouble(6,Close[1]);

//Test Print
Print("Sender Function >>> GetEntryDate=",Value3," | GetEntryTime=",Value4," | GetEntryName=",Value5," | GetMP=",Value6," | GetContracts=",Value7," | TEST=",Value8);

END;
END;


Parker
Posts: 22
Joined: 18 May 2018
Has thanked: 7 times

Re: Issue with Global Variables

Postby Parker » 27 Jun 2018

Does anyone have a working code that uses Global Variables that they would be willing to share?

I could then test run this in my platform and compare to my code and try to see where I am going wrong. I'm not sure if it's a coding issue or issue with the DLL installation.

Thanks.

User avatar
ABC
Posts: 626
Joined: 16 Dec 2006
Has thanked: 119 times
Been thanked: 354 times
Contact:

Re: Issue with Global Variables

Postby ABC » 27 Jun 2018

Hi Parker,

there should be sample indicators in the zip file that can be used to check if everything is working correctly.

Regards,

ABC
These users thanked the author ABC for the post:
Parker

Parker
Posts: 22
Joined: 18 May 2018
Has thanked: 7 times

Re: Issue with Global Variables  [SOLVED]

Postby Parker » 27 Jun 2018

Thanks ABC found the sample indicators.

I got to the bottom of it in the end. It was the "if CurrentBar = 1". By changing it to "LastBarOnChart" it started working. I'm not sure why to be honest as I was thinking the Current Bar being 1 is the bar we are on now, which is also the last bar on the chart. But the sample indicators use LastBarOnChart so I switched to this and have achieved what I want now. I come from a background trading with MT4 coding in MQL, so still trying to get my had around the keywords and functions in PL!

Thanks.
Parker


Return to “MultiCharts”