NexusFi: Find Your Edge


Home Menu

 





Plot defaulColor based on a value


Discussion in ThinkOrSwim

Updated
    1. trending_up 1,394 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 1 posts
    2. attach_file 1 attachments




 
Search this Thread

Plot defaulColor based on a value

  #1 (permalink)
 Doggo 
Melbourne Beach FL USA
 
Experience: Intermediate
Platform: TOS, TWS
Broker: TDA
Trading: ES, FOREX, Stocks
Posts: 1 since Jan 2020
Thanks Given: 0
Thanks Received: 0

I want a line plotted across the chart that shows my average cost for the charted item. If the entry cost is less than the current price (I'm positive p/l) color the line green, else color it red.

The code below errors on the line:
posline.setDefaultColor(if price > (LastPrice) then Color.LIGHT_RED else Color.Green );

I have searched over two hours for a this simple requirement, to assign a color based on a decision, instead of hard coding the defaultcolor. I need it to be defaultcolor because I want the whole bar to be plotted the one color.

 
Code
def LastPrice = close(priceType = PriceType.LAST);
def price = GetAveragePrice();

plot posLine = HighestAll(if !IsNaN(price) and price > 0 then price else Double.NaN ) ;
posline.setDefaultColor(if price > (LastPrice) then Color.LIGHT_RED else  Color.Green );

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Trade idea based off three indicators.
Traders Hideout
REcommedations for programming help
Sierra Chart
ZombieSqueeze
Platforms and Indicators
Quant vue
Trading Reviews and Vendors
Better Renko Gaps
The Elite Circle
 
  #2 (permalink)
 JayC 
San Diego, CA
 
Experience: Beginner
Platform: TOS, Sierra
Trading: Emini ES, Crude CL
Posts: 55 since Mar 2019
Thanks Given: 9
Thanks Received: 43

Hi,

In order to apply conditional color to a plot you need to use AssignValueColor. As you pointed out, just using that with your previous logic resulted in a the color switching as last price changes across the chart. To avoid that, I've changed the logic to get the last price from the last bar. I hard coded the average price, but thats just to provide some test value instead of a real average cost. I think this achieves what you were looking for.

Regards,
JayC

 
Code
def LastBar = !IsNaN(open) and IsNaN(open [-1]);

def LastPrice = HighestAll(If LastBar then close(priceType = PriceType.LAST) else Double.NaN);
def price = 290;

plot posLine = HighestAll(if !IsNaN(price) and price > 0 then price else Double.NaN ) ;
posLine.SetDefaultColor(Color.GRAY);
posLine.AssignValueColor(if price > (LastPrice) then Color.LIGHT_RED else  Color.GREEN);

(Example)

Reply With Quote




Last Updated on May 18, 2020


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts