Translating code from Traderstudio What are GValues?

Discussion in 'Trading Software' started by Crassius, Mar 2, 2011.

  1. Crassius

    Crassius

    I am working on porting an indicator in 2008 issue of S&C to NinjaTrader because the two versions of it I have found online are not returning valid values.

    In looking through the TradersStudio code, he uses something called GValues.... they look like variables, but he doesn't declare them... or assign an initial value. He just starts using them.

    First he declares then sets 0 as the value in a handful of arrays:

    For N = 0 To 60
    I[N] = 0
    OldI[N] = 0
    OlderI[N] = 0
    Q[N] = 0
    OldQ[N] = 0
    OlderQ[N]=0
    Real[N] = 0
    OldReal[N] = 0
    OlderReal[N] = 0
    Imag[N] = 0
    OldImag[N] = 0
    OlderImag[N] = 0
    Ampl[N] = 0
    OldAmpl[N] = 0
    DB[N] = 0
    OldDB[N] = 0
    color[N] = 0
    Next

    Then he sets a couple other variables:

    price = (H+L)/2
    delta = 0.1
    fuzzRGB = 190
    lineRGB = 255
    TScurrentBar = CurrentBar + 1
    myDate = FormatDateTime(Date)

    Then this block appears:

    I = GValue201
    OldI = GValue202
    OlderI = GValue203
    Q = GValue204
    OldQ = GValue205
    OlderQ = GValue206
    Real = GValue207
    OldReal = GValue208
    OlderReal = GValue209
    Imag = GValue210
    OldImag = GValue211
    OlderImag = GValue212
    Ampl = GValue213
    OldAmpl = GValue214
    DB = GValue215
    OldDB = GValue216

    But he hasn't declared these GValues, so am confused what they are.

    Then the calcs are done, and at the end, last code block before End Function, This block appears:

    GValue201 = I
    GValue202 = OldI
    GValue203 = OlderI
    GValue204 = Q
    GValue205 = OldQ
    GValue206 = OlderQ
    GValue207 = Real
    GValue208 = OldReal
    GValue209 = OlderReal
    GValue210 = Imag
    GValue211 = OldImag
    GValue212 = OlderImag
    GValue213 = Ampl
    GValue214 = OldAmpl
    GValue215 = DB
    GValue216 = OldDB

    Which looks like he is resetting the arrays.

    The program would then start over again on the next bar.

    So WTH are GValues? He declares and assigns values to all other variables, but they sure look like variables.
     
  2. MGJ

    MGJ

    I think if you perform the search shown in the image below, and read each of the pages it finds, you may decide that this is a reasonable guess:
    • GValues are global variables which are common across all stocks / forexpairs / futures / ETFs in the portfolio. They provide a mechanism for the instruments in the portfolio to talk to one another. There is only one GValue37; there aren't different GValue37's for Intel and Ford. Both Intel and Ford can read it, Both can write it.

    [​IMG]
     
  3. Crassius

    Crassius

    I read all that... I had searched GValue over there, as well as GValueXXX that is in the code, but nothing comes up.. ...so thanks for suggesting GValue1 search.

    OK I agree they look like Global Variables....

    At the start of the code, he has to run through a For loop to set all the elements of the arrays to 0.

    For N = 0 To 60
    I[N] = 0
    OldI[N] = 0
    OlderI[N] = 0
    Q[N] = 0
    OldQ[N] = 0
    OlderQ[N]=0
    Real[N] = 0
    OldReal[N] = 0
    OlderReal[N] = 0
    Imag[N] = 0
    OldImag[N] = 0
    OlderImag[N] = 0
    Ampl[N] = 0
    OldAmpl[N] = 0
    DB[N] = 0
    OldDB[N] = 0
    color[N] = 0
    Next

    That's how you would have to do it in C#...

    then you get that block

    I = GValue201
    OldI = GValue202
    OlderI = GValue203
    Q = GValue204
    OldQ = GValue205
    OlderQ = GValue206
    Real = GValue207
    OldReal = GValue208
    OlderReal = GValue209
    Imag = GValue210
    OldImag = GValue211
    OlderImag = GValue212
    Ampl = GValue213
    OldAmpl = GValue214
    DB = GValue215
    OldDB = GValue216

    How come he doesn't have to run through a loop? What element of those arrays is he setting to GValueXXX?

    Each time his code runs, as a last setp, he takes the current value of those array variables... OldAmpl or OldDB for example, and assigns the value of each to a global variable.

    GValue201 = I
    GValue202 = OldI
    GValue203 = OlderI
    GValue204 = Q
    GValue205 = OldQ
    GValue206 = OlderQ
    GValue207 = Real
    GValue208 = OldReal
    GValue209 = OlderReal
    GValue210 = Imag
    GValue211 = OldImag
    GValue212 = OlderImag
    GValue213 = Ampl
    GValue214 = OldAmpl
    GValue215 = DB
    GValue216 = OldDB

    At the start of the next bar, he assigns the value of the global variables to ..... to what ?

    I = GValue201
    OldI = GValue202
    OlderI = GValue203
    Q = GValue204
    OldQ = GValue205
    OlderQ = GValue206
    Real = GValue207
    OldReal = GValue208
    OlderReal = GValue209
    Imag = GValue210
    OldImag = GValue211
    OlderImag = GValue212
    Ampl = GValue213
    OldAmpl = GValue214
    DB = GValue215
    OldDB = GValue216

    They are 61 element arrays.... he had to use a loop and an index number to assign 0 to them at the head of the code... now he just uses their name and no index number?


    In Ehlers notes on the code he states "First the entire code is completed for each bar and the value of the variables is retained from bar to bar. However the value of arrays are not automatically indexed to each price bar."

    So maybe that is what he is doing here... retaining the value of the variable for use in the next bar's calcs...

    But if that's the case... how come he doesn't need index numbers? How come he doesn't need a loop to set all 61 elements, or address a certain element with an index number?

    My translation of this, without resetting the arrays to anything bar to bar, in C# NinjaTrader works, but only becomes stable after a huge number of bars... like 180....
     
  4. Murray Ruggiero

    Murray Ruggiero ET Sponsor

    GValue1..GValue255 are global variables. They are used so that normal variable non bar arrays can be saved from bar to bar. GValue variables can store any type of variable including arrays.


    Here is an example usage.

    ' TradersStudio(r) (C) 2004-2010 All Rights Reserved
    ' Custom year by year breakdown report.
    Sub MarketBreakDownYR()
    Dim AveTrade As Double
    Dim Cellrow As Integer
    Dim OpenProfit As Double,ClosedProfit As Double
    Dim ThisYr As Integer,YearProfit As Double
    Dim DD As Double

    'If BarNumber=FirstBar And MomSeries=1 Then
    If GValue7 = 0 Then
    GValue12 = 4 'Current Row
    SetTextValue("Symbol",3,1,15)
    SetTextValue(" Closed Profit",3,2,12)
    SetTextValue(" Open Profit",3,3,12)
    SetTextValue(" Total Profit",3,4,12)
    SetTextValue(" # Trades",3,5,8)
    SetTextValue(" Win %",3,6,8)
    SetTextValue(" Ave Trade",3,7,8)
    SetTextValue(" Drawdown",3,8,12)
    SetTextValue(" Ave Return",3,9,8)
    SetTextValue(" Final Yr $",3,10,12)
    SetTextValue("Trade dates",1,1,15)
    End If

    ThisYr = Year(Date)
    YearProfit = GetNetProfit(1)
    If ThisYr <> GValue7 Then
    If MarketPosition[1] <> 0 Then
    YearProfit = YearProfit + (Close[1] - EntryPrice[1]) * MarketPosition[1] * BigPointValue
    End If
    If GValue7 = 0 Or ThisYr < GValue7 Then
    SetTextValue(FormatDateTime(Date),1,2,12)
    End If
    GValue7 = ThisYr
    GValue8 = YearProfit
    End If

    If BarNumber = FirstBar Then
    GValue10 = 0 'Commodity max drawdown
    GValue11 = 0
    End If
    If MarketPosition <> 0 Then
    If GValue11 = 0 Then GValue11 = BarNumber
    If BarsSinceEntry > 0 Then
    'If the max drawdown occurs on either the entry day or the exit day,
    'this will not be computed correctly.
    If MarketPosition > 0 Then
    DD = (Low - EntryPrice) * BigPointValue
    Else
    DD = (EntryPrice - High) * BigPointValue
    End If
    If DD < GValue10 Then
    GValue10 = DD
    End If
    End If
    End If

    If BarNumber = LastBar Then
    'Cellrow = MomSeries + 3
    Cellrow = GValue12
    GValue12 = GValue12 + 1
    If OpenPositionProfit > -9999998 Then OpenProfit = OpenPositionProfit
    SetTextValue(getactivesymbol(0),Cellrow,1,15)
    SetTextValue(" Session",Cellrow+1,1,15)
    ClosedProfit = NetProfit - OpenPositionProfit
    If NoTrades <> 0 Then
    AveTrade = (ClosedProfit/NoTrades)
    Else
    AveTrade = 0
    End If
    SetCurrencyValue(ClosedProfit,Cellrow,2,12)
    GValue3 = GValue3 + ClosedProfit
    SetCurrencyValue(GValue3,Cellrow+1,2,12)
    SetCurrencyValue(OpenProfit,Cellrow,3,12)
    SetCurrencyValue(OpenProfit+ClosedProfit,Cellrow,4,12)
    GValue4 = GValue4 + OpenProfit
    SetCurrencyValue(GValue4,Cellrow+1,3,12)
    SetCurrencyValue(GValue3+GValue4,Cellrow+1,4,12)
    SetLongValue(Notrades,Cellrow,5,8)
    GValue5 = GValue5 + NoTrades
    SetLongValue(GValue5,Cellrow+1,5,8)
    SetPercentValue(percentprofitable/100,Cellrow,6,8)
    GValue6 = GValue6 + NoTrades*PercentProfitable
    SetcurrencyValue(AveTrade,Cellrow,7,8)
    If GValue5 > 0 Then
    SetPercentValue(GValue6/(100*GValue5),Cellrow+1,6,8)
    SetCurrencyValue(GValue3/GValue5,Cellrow+1,7,8)
    End If
    SetCurrencyValue(Drawdown,Cellrow,8,12)
    If Date > GValue2 Then
    SetTextValue(FormatDateTime(Date),1,3,12)
    GValue2 = Date
    End If
    If Drawdown < 0 Then
    SetPercentValue((OpenProfit+ClosedProfit)*250/(-Drawdown*(BarNumber-GValue11)),Cellrow,9,8) 'Avg 250 trading days per Year
    End If
    SetCurrencyValue(NetProfit-GValue8,Cellrow,10,12)
    GValue9 = GValue9 + NetProfit - GValue8
    SetCurrencyValue(GValue9,Cellrow+1,10,12)
    End If

    End Sub
     
  5. Please, can you show us how the market variables work and also show us a good use for them?
     
  6. What is the difference between GValue variables and MarketVar ("Variable Name") in terms of global variable, why do we have both?
     
  7. Murray Ruggiero

    Murray Ruggiero ET Sponsor

    GValue and MarketVar are very related there is one major difference MarketVar can communicate between sessions and tradeplans and even be accessed from macro's. GValue variables can only be accessed from Session level.
     
  8. Thanks for the reply. So can we say they are like the public (MarketVar) and private (GValue) variables of the programming languages?
     
  9. Murray Ruggiero

    Murray Ruggiero ET Sponsor

    GvalueXX variables only work at the run session level. MarketVar's can be accessed from Session level as well the market,session,tradeplan level in a tradeplan or macro.
     
  10. Thanks Mr. Murray, for the explanation. :)
     
    #10     May 16, 2011