Logic is simple , if price is above 50 days Volume SMA then it will plot colour based on candle closing , for example if bar is down then it will plot red colour and green for upbars. It shows high activities
But if volume is between 20-49sma then it will plot gray colour irrespective of bar whether it's up or down. It shows normal activities And lastly if volume is below 12sma then it will plot yellow colour irrespective of bar, it shows less activity
Logic is simple , if price is above 50 days Volume SMA then it will plot colour based on candle closing , for example if bar is down then it will plot red colour and green for upbars. It shows high activities
But if volume is between 20-49sma then it will plot gray colour irrespective of bar whether it's up or down. It shows normal activities And lastly if volume is below 12sma then it will plot yellow colour irrespective of bar, it shows less activity
Logic is simple , if price is above 50 days Volume SMA then it will plot colour based on candle closing , for example if bar is down then it will plot red colour and green for upbars. It shows high activities
But if volume is between 20-49sma then it will plot gray colour irrespective of bar whether it's up or down. It shows normal activities And lastly if volume is below 12sma then it will plot yellow colour irrespective of bar, it shows less activity
//@version=4
// To change this from a Strategy to a Indicator:-
// 1) uncomment the next line and comment out the strategy line.
// 2) at the end of the file comment out the last 2 lines
bool win = false // tracks if last trade was winning trade of losing trade.
float totalPrice = 0.0 // tracks total price, used for calculating avgPrice
float buyPrice = 0.0 // tracks the buy price of the last long position.
float avgPrice = 0.0 // tracks the avg price of all currently held long positions.
float nextBuyPrice = 0.0 // tracks the next buy price
float stopPrice = na // tracks the stop price
int totalBuys = 0 // tracks the total # of pyramid buys
bool inBuy = false // tracks if we are in a long position or not.
float l1LongPlot = highest(l1LongInput) // tracks the L1 price to display
float l2LongPlot = highest(l2LongInput) // tracks the L2 price to display
float n = atr(14) // tracks the n used to calculate stops and pyramid buys
string mode = 'L1' // tracks whether we are in a L1 position or L2 position.
bool fake = na // tracks if this is a fake trade, see comments below.
string longLevel = na // tracks where long positions, stops, pyramid buys occur.
// by default use the last value from the previous bar.
buyPrice := buyPrice[1]
totalBuys := totalBuys[1]
nextBuyPrice := nextBuyPrice[1]
stopPrice := stopPrice[1]
avgPrice := avgPrice[1]
totalPrice := totalPrice[1]
win := win[1]
// State to track if we are in a long positon or not.
inBuy := not inBuy[1] and (close > l1Long[1] or close > l2Long[1]) ? true : inBuy[1]
inBuy := inBuy[1] and close < stopPrice ? false : inBuy
inBuy := inBuy[1] and mode[1] == 'L1' and close < l1LongExit[1] ? false : inBuy
inBuy := inBuy[1] and mode[1] == 'L2' and close < l2LongExit[1] ? false : inBuy
// State to track if we are ia a fake trade. If the last trade was a winning, we need to skip the next trade.
// We still track it though as a fake trade (not counted against us). as the outcome determines if we can
// can take the next trade.
fake := not inBuy[1] and close > l1Long[1] and win[1] ? true : fake[1]
fake := not inBuy[1] and close > l1Long[1] and not win[1] ? false : fake
fake := close > l2Long[1] ? false : fake
// Series representing the l1 and l2 levels. If we break out above the l1 or l2 level, we want the
// line to stay at the breakout level, not follow it up.
l1LongPlot := iff(not inBuy or (inBuy and mode == 'L1' and fake),l1Long[1],l1LongPlot[1])
l2LongPlot := iff(not inBuy or (inBuy and mode == 'L1' and fake),l2Long[1],l2LongPlot[1])
// Variable in the series is only set when it happens. Possible values is L1, L2, SR
// (stopped out with a loss), SG (exited with a gain), and 'P' for pyramid buy.
longLevel := not inBuy[1] and close > l1Long[1] ? 'L1' : na
longLevel := not inBuy[1] and close > l2Long[1] ? 'L2' : longLevel
longLevel := not inBuy[1] and close > l1Long[1] and close > l2Long[1] and fake ? 'L2' : longLevel
// longLevel := longLevel == na and close > l2Long[1] and mode[1] != 'L2' ? 'L2' : longLevel
longLevel := longLevel == na and close > l2Long[1] and mode[1] == 'L1' ? na : longLevel // don't switch to L2 if we are already in a L1
longLevel := longLevel == na and close > l2Long[1] and mode[1] == 'L1' and fake[1] ? 'L2' : longLevel
// Either 'L1' or 'L2' depending on what breakout level we are in.
mode := longLevel == na ? mode[1] : longLevel
// Variables to track calculating avgPrice and nextBuyPrice for pyramiding.
if longLevel == 'L1' or longLevel == 'L2'
buyPrice = close
totalBuys := 1
totalPrice := close
avgPrice := buyPrice
stopPrice := close - (stopInput*n)
nextBuyPrice := high + (pyramidInput*n)
// Marks if we hit our next buy price, if so mark it with a 'P'
longLevel := inBuy[1] and close > nextBuyPrice and TradeDateIsAllowed() and totalBuys < 5 ? 'P' : longLevel
if longLevel == 'P'
buyPrice = close
totalBuys := totalBuys[1] + 1
totalPrice := totalPrice[1] + buyPrice
avgPrice := totalPrice / totalBuys
stopPrice := close - (stopInput*n)
nextBuyPrice := high + (pyramidInput*n)
// Tracks stops and exits, marking them with SG or SR
longLevel := longLevel == na and inBuy[1] and close < stopPrice and close >= avgPrice ? 'SG' : longLevel
longLevel := longLevel == na and inBuy[1] and close < stopPrice and close < avgPrice ? 'SR' : longLevel
longLevel := longLevel == na and mode[1] == 'L1' and inBuy[1] and (close < l1LongExit[1]) and close >= avgPrice ? 'SG' : longLevel
longLevel := longLevel == na and mode[1] == 'L2' and inBuy[1] and (close < l2LongExit[1]) and close >= avgPrice ? 'SG' : longLevel
longLevel := longLevel == na and mode[1] == 'L1' and inBuy[1] and (close < l1LongExit[1]) and close < avgPrice ? 'SR' : longLevel
longLevel := longLevel == na and mode[1] == 'L2' and inBuy[1] and (close < l2LongExit[1]) and close < avgPrice ? 'SR' : longLevel
// Tracks if the trade was a win or loss.
win := longLevel == 'SG' ? true : win
win := longLevel == 'SR' ? false : win
// Variables used to tell strategy when to enter/exit trade.
enterLong = (longLevel == 'L1' or longLevel == 'L2' or longLevel == 'P') and not fake and TradeDateIsAllowed()
exitLong = (longLevel == 'SG' or longLevel == 'SR') and not fake and TradeDateIsAllowed()