Get current price everytime it changes not the close, open, high, or low.

Kevin N

Member
Hello,

Has anyone come up with a way to get the current price of an instrument. I know you can reference the open, close, high, low price but I need the price at that current moment, between bars. I don't know of a direct way to do this, but it may be possible since referencing GetOpenPL() returns second by second changes to the current open P&L.

For example, the below is a handy little label I have at the top of my charts that give me my current P&L and it changes second by second, so it has to be getting the price. I have created a strategy that uses a stoploss based on points, but the problem I have is if the "low" is below the number of points I specified when a long trade is triggered, it's immediately throwing up a close signal.

Now, you may be wondering, "Why does this guy want to do this...." and you'd be right to wonder. I've been using a macro program to read label colors from my strategy to automatically submit buy, sell, flatten orders. So far, so good except I want to stop out based on pts (eg: 5pt stop on MES) instead of the low of the last bar, etc.

Code:
# Next line is a handy reference
#Colors for GetColor 0(violet), 1(baby blue), 2(pink), 3(gray), 4(yelllow), 5(red), 6(green), 7(dark gray), 8(pale yellow), 9(white)

declare hide_on_daily;

def price = close;
def averagePrice=GetAveragePrice();
def shareSize = GetQuantity();
def cost = averagePrice * shareSize;
def accountValue = (shareSize * close) - (shareSize * averagePrice);

plot AverageTradePrice=if(averagePrice > 0, averagePrice, double.NaN);
AverageTradePrice.SetDefaultColor(COLOR.GREEN);
AverageTradePrice.SetStyle(CURVE.SHORT_DASH);

# Plot the daily MA's
plot MA20 = MovingAverage(AverageType.EXPONENTIAL, close(period = aggregationPeriod.DAY), 20);
MA20.setdefaultColor(color.red);
MA20.setStyle(Curve.POINTS);

plot MA50 = MovingAverage(AverageType.SIMPLE, close(period = aggregationPeriod.DAY), 50);
MA50.setdefaultColor(color.green);
MA50.setStyle(Curve.POINTS);

plot MA100 = MovingAverage(AverageType.SIMPLE, close(period = aggregationPeriod.DAY), 100);
MA100.setdefaultColor(color.violet);
MA100.setStyle(Curve.POINTS);

plot MA200 = MovingAverage(AverageType.SIMPLE, close(period = aggregationPeriod.DAY), 200);
MA200.setdefaultColor(color.yellow);
MA200.setStyle(Curve.POINTS);
# End Daily MA's

input fastLength = 09;
input slowLength = 20;
input verySlowLength = 50;
input superSlowLength = 200;
input averageType = AverageType.EXPONENTIAL;


plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
plot VerySlowMA = MovingAverage(averageType, price, verySlowLength);
plot SuperSlowMA = MovingAverage(averageType, price, SuperSlowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));
VerySlowMA.SetDefaultColor(GetColor(6));
SuperSlowMA.SetDefaultColor(GetColor(4));

AddLabel(if (GetOpenPL() <> 0,1,0),
        "Avg: " + round(averagePrice) +
        ", Qty: " + shareSize +
        ", PnL: $" + GetOpenPL() +
        ", Gain: " + round(GetOpenPL() / AbsValue(cost) * 100) + "%" +
        ", Cost: $" + cost +
        ", Used: " + AsPercent(cost / GetNetLiq())
        , if accountValue > 0
            then Color.GREEN
            else if accountValue < 0
            then Color.PINK
            else Color.GRAY);

Regards, Kevin
 
Solution
Hello,

Has anyone come up with a way to get the current price of an instrument. I know you can reference the open, close, high, low price but I need the price at that current moment, between bars. I don't know of a direct way to do this, but it may be possible since referencing GetOpenPL() returns second by second changes to the current open P&L.

For example, the below is a handy little label I have at the top of my charts that give me my current P&L and it changes second by second, so it has to be getting the price. I have created a strategy that uses a stoploss based on points, but the problem I have is if the "low" is below the number of points I specified when a long trade is triggered, it's immediately throwing up a...
Hello,

Has anyone come up with a way to get the current price of an instrument. I know you can reference the open, close, high, low price but I need the price at that current moment, between bars. I don't know of a direct way to do this, but it may be possible since referencing GetOpenPL() returns second by second changes to the current open P&L.

For example, the below is a handy little label I have at the top of my charts that give me my current P&L and it changes second by second, so it has to be getting the price. I have created a strategy that uses a stoploss based on points, but the problem I have is if the "low" is below the number of points I specified when a long trade is triggered, it's immediately throwing up a close signal.

Now, you may be wondering, "Why does this guy want to do this...." and you'd be right to wonder. I've been using a macro program to read label colors from my strategy to automatically submit buy, sell, flatten orders. So far, so good except I want to stop out based on pts (eg: 5pt stop on MES) instead of the low of the last bar, etc.

Code:
# Next line is a handy reference
#Colors for GetColor 0(violet), 1(baby blue), 2(pink), 3(gray), 4(yelllow), 5(red), 6(green), 7(dark gray), 8(pale yellow), 9(white)

declare hide_on_daily;

def price = close;
def averagePrice=GetAveragePrice();
def shareSize = GetQuantity();
def cost = averagePrice * shareSize;
def accountValue = (shareSize * close) - (shareSize * averagePrice);

plot AverageTradePrice=if(averagePrice > 0, averagePrice, double.NaN);
AverageTradePrice.SetDefaultColor(COLOR.GREEN);
AverageTradePrice.SetStyle(CURVE.SHORT_DASH);

# Plot the daily MA's
plot MA20 = MovingAverage(AverageType.EXPONENTIAL, close(period = aggregationPeriod.DAY), 20);
MA20.setdefaultColor(color.red);
MA20.setStyle(Curve.POINTS);

plot MA50 = MovingAverage(AverageType.SIMPLE, close(period = aggregationPeriod.DAY), 50);
MA50.setdefaultColor(color.green);
MA50.setStyle(Curve.POINTS);

plot MA100 = MovingAverage(AverageType.SIMPLE, close(period = aggregationPeriod.DAY), 100);
MA100.setdefaultColor(color.violet);
MA100.setStyle(Curve.POINTS);

plot MA200 = MovingAverage(AverageType.SIMPLE, close(period = aggregationPeriod.DAY), 200);
MA200.setdefaultColor(color.yellow);
MA200.setStyle(Curve.POINTS);
# End Daily MA's

input fastLength = 09;
input slowLength = 20;
input verySlowLength = 50;
input superSlowLength = 200;
input averageType = AverageType.EXPONENTIAL;


plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
plot VerySlowMA = MovingAverage(averageType, price, verySlowLength);
plot SuperSlowMA = MovingAverage(averageType, price, SuperSlowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));
VerySlowMA.SetDefaultColor(GetColor(6));
SuperSlowMA.SetDefaultColor(GetColor(4));

AddLabel(if (GetOpenPL() <> 0,1,0),
        "Avg: " + round(averagePrice) +
        ", Qty: " + shareSize +
        ", PnL: $" + GetOpenPL() +
        ", Gain: " + round(GetOpenPL() / AbsValue(cost) * 100) + "%" +
        ", Cost: $" + cost +
        ", Used: " + AsPercent(cost / GetNetLiq())
        , if accountValue > 0
            then Color.GREEN
            else if accountValue < 0
            then Color.PINK
            else Color.GRAY);

Regards, Kevin

there is no 'price between bars.'
the current price is from close.
def x = close;
current bar: close is the current price
previous bars: close is the price at the end of the time period.
 
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

there is no 'price between bars.'
the current price is from close.
def x = close;
current bar: close is the current price
previous bars: close is the price at the end of the time period.
I understand how thinkscript works, I've built some pretty complex scripts, so I understand you can get the open, close, high, and low prices by referencing those in the script. I also know when you use the GetOpenPL() function such as in the script above, it's giving you a profit and loss tick by tick. I'm sure I can figure a way to do it, was just wondering if anyone else has already done it. I simply want to flash a label on my strategy as soon as I'm at a 15 pt loss. I'll post it here when I complete it.
 
I understand how thinkscript works, I've built some pretty complex scripts, so I understand you can get the open, close, high, and low prices by referencing those in the script. I also know when you use the GetOpenPL() function such as in the script above, it's giving you a profit and loss tick by tick. I'm sure I can figure a way to do it, was just wondering if anyone else has already done it. I simply want to flash a label on my strategy as soon as I'm at a 15 pt loss. I'll post it here when I complete it.
Any luck so far? Very interested.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
302 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top