Help add ATR as USD on ThinkorSwim Forex Chart

B

Bigbagofspam

New member
I am new to ThinkScript, and trading Forex. I am trying to find a way to add a label to a chart that expresses Average True Range in USD even when USD is not on of the current charts currency pair. My current multi label script is a disaster lol. I have been making scripts for only 2 days so,... yeah its ugly.

Code:
# AA_Label_Fx_Tricolor_1_0
# 2019-07-16
# Matthew Jordan - Learning ThinkScript

# This script will add 4 labels to a Forex chart.
#    1. Current position in Lots.
#    2. Average True Range.
#    3. Average True Range converted to USD.(PER LOT)
#    4. Current Spread cost.
#
# These labels will be Ti-color coded,
# relating to the full range of the
# last (User set Length) bars on the
# current charts time frame.
#
#    Green    = The BEST 1/3 of that range.
#    Yellow   = The middle part of that range.
#    Red      = The WORST 1/3 of that range.        
# Green = Good, Yellow = Not Ideal & Red = Bad


# START User Defined Length in bars.
    input length = 252;
# END User Defined Length in bars.

# Start Define atrHi, atrLo, atrRange & atrPercentage.
    def atr
        = if IsNaN(atr())
        then atr[1]
        else atr();
    def atrHi
        = Highest(atr, length);
    def atrLo
        = Lowest (atr, length);
    def AtrRange
        = atrHi
        - atrLo;
    def atrPercentage
        = Round( 100
        * (atr() - atrLo)
        / AtrRange, 1) ;
# END Define atrHi, atrLo, atrRange & atr%.

# START Define BidPrice, AskPrice & FxSpread.
    def BidPrice
        = close(priceType = PriceType.BID);
    def AskPrice
        = close(priceType = PriceType.ASK);
    def FxSpread
        = AskPrice - BidPrice;
# END Define FxSpread.

# START Define FxSpread High, Lo, Range & Percentage.
   def SpreadHi
       = Highest(FxSpread, length);
   def SpreadLo
       = Lowest (FxSpread, length);
   def FxSpreadRange
       = SpreadHi
       - SpreadLo;
   def FxSpreadPerc
       = Round ((100 * FxSpread)
       / FxSpreadRange)
       / 100;
# END Define FxSpread High, Lo, Range & Percentage.

# START Define TrueATR & TickValue.
    def TrueATR
        = ATR
        - FxSpreadRange;
  def TickValue
        = if IsNaN(atr())
        then TickValue[1]
        else Tickvalue();
# END Define TrueATR & TickValue.

# START Define all 3 colors for Labels
    DefineGlobalColor("67%+", Color.RED);
    DefineGlobalColor("34-66%", Color.YELLOW);
    DefineGlobalColor("33%-", Color.GREEN);
# END Define all 3 colors for Labels

# START Add FxSpread label to chart in proper color.
    AddLabel(yes, "FxSpread " + FxSpread ,
    if FxSpreadPerc >66 then GlobalColor("67%+") else
    if FxSpreadPerc >33 then GlobalColor("34-66%") else
    GlobalColor("33%-"));
# END Add FxSpread label to chart in proper color.

# START Define TickSize & LotSize
     def TickSize
        = if IsNaN(tickSize())
        then tickSize[1] else
        tickSize();
     def LotSize = TickValue/Ticksize;
# END Define TickSize & Lotsize

# Start Add label for LotSize.
AddLabel(yes, "# Lots " + (LotSize / 100000));
# END Add label for LotSize.

# Start Add Tri-color label for ATR.
    AddLabel(yes, "ATR: " + atr ,
    if atrPercentage>66 then GlobalColor("33%-") else
    if atrPercentage>33 then GlobalColor("34-66%")else
    GlobalColor("67%+"));
# END Add Tri-color label for ATR.



# Start Add Tri-color label for ATR in USD.
    AddLabel(yes, "ATR= $"
    + (TickValue*LotSize)
    * TrueATR);
# END Add Tri-color label for ATR in USD.

# I highly suggest that you save as
#      AA_Label_Fx_Tricolor_1_0
[\CODE]
 
Last edited by a moderator:

Top