Help With Strategy

TylerYong

New member
I noticed certain Price Action pattern and would like to test out the strategy with the floating Pnl (like using the Strategy in the Strategies tab). But unfortunately, I couldnt write the code out nicely (even with the help of chatgpt). Hence, I hope to seek help from pro like you peeps.



The requirement:
Condition 1: the signal candle must be > 10% gain, meaning the close price / open price >= 10%
Condition 2 : signal candle' MACD value line >= the 0 line
Condition 3 (not sure if this is possible via coding) : the signal candle closing price must be above the highest point of the nearest 2 red candle.
Condition 4 (not sure if this is possible via coding) : the signal candle's volume >= sum of the volume of the nearest 2 red candle.

Buy at the open price when the conditions are met (meaning the next day after the signal candle).

Selling condition: sell at 5% profit or 5% stock loss.

Thanks & Regards
 
Solution
@TylerYong
Ruby:
def MACDV = reference MACD.Value;
def LastRedHigh = if close < open then high else LastRedHigh[1];
def NextLastRedHigh = if GetTime() <> GetTime()[1] then LastRedHigh[1] else NextLastRedHigh[1];
def HighestLastRedHigh = if LastRedHigh > NextLastRedHigh then LastRedHigh else NextLastRedHigh;

def LastRedVolume = if close < open then volume else LastRedVolume[1];
def NextLastRedVolume = if GetTime() <> GetTime()[1] then LastRedVolume[1] else NextLastRedVolume[1];
def TotalLastTwoRedVolume = LastRedVolume + NextLastRedVolume;

def Cond1 = close/open >= .1;
def Cond2 = MACDV >= 0;
def Cond3 = close > HighestLastRedHigh;
def Cond4 = volume >= TotalLastTwoRedVolume;

def Buy;
def BuyTarget;
def BuyStop;
def ExitPrice;

if...
@TylerYong
Ruby:
def MACDV = reference MACD.Value;
def LastRedHigh = if close < open then high else LastRedHigh[1];
def NextLastRedHigh = if GetTime() <> GetTime()[1] then LastRedHigh[1] else NextLastRedHigh[1];
def HighestLastRedHigh = if LastRedHigh > NextLastRedHigh then LastRedHigh else NextLastRedHigh;

def LastRedVolume = if close < open then volume else LastRedVolume[1];
def NextLastRedVolume = if GetTime() <> GetTime()[1] then LastRedVolume[1] else NextLastRedVolume[1];
def TotalLastTwoRedVolume = LastRedVolume + NextLastRedVolume;

def Cond1 = close/open >= .1;
def Cond2 = MACDV >= 0;
def Cond3 = close > HighestLastRedHigh;
def Cond4 = volume >= TotalLastTwoRedVolume;

def Buy;
def BuyTarget;
def BuyStop;
def ExitPrice;

if Buy[1] and high >= BuyTarget[1] {
Buy = 0;
BuyTarget = double.nan;
BuyStop = double.nan;
ExitPrice = BuyTarget[1];

}else if Buy[1] and low <= BuyStop[1] {
Buy = 0;
BuyTarget = double.nan;
BuyStop = double.nan;
ExitPrice = BuyStop[1];

}else if !Buy[1] && Cond1 && Cond2 && Cond3 && Cond4 {
Buy = 1;
BuyTarget = open[-1] + (open[-1] * .05);
BuyStop = open[-1] - (open[-1] * .05);
ExitPrice = double.nan;
}else {
Buy = Buy[1];
BuyTarget = BuyTarget[1];
BuyStop = BuyStop[1];
ExitPrice = ExitPrice[1];;
}

plot Signal = !Buy[1] && Cond1 && Cond2 && Cond3 && Cond4;
Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot BTPlot = BuyTarget;
plot BSPlot = BuyStop;

addOrder(OrderType.BUY_TO_OPEN, Buy, price = open[-1], tradesize = 1);
################################################################################################
#Use of negative offset in below exit order condition to simulate price hitting target or stop.#
addOrder(OrderType.SELL_TO_CLOSE, !Buy[-1], price = ExitPrice[-1], tradesize = 1);             #
################################################################################################
 
Last edited:
Solution
@TylerYong
Ruby:
def MACDV = reference MACD.Value;
def LastRedHigh = if close < open then high else LastRedHigh[1];
def NextLastRedHigh = if GetTime() <> GetTime()[1] then LastRedHigh[1] else NextLastRedHigh[1];
def HighestLastRedHigh = if LastRedHigh > NextLastRedHigh then LastRedHigh else NextLastRedHigh;

def LastRedVolume = if close < open then volume else LastRedVolume[1];
def NextLastRedVolume = if GetTime() <> GetTime()[1] then LastRedVolume[1] else NextLastRedVolume[1];
def TotalLastTwoRedVolume = LastRedVolume + NextLastRedVolume;

def Cond1 = close/open >= .1;
def Cond2 = MACDV >= 0;
def Cond3 = close > HighestLastRedHigh;
def Cond4 = volume >= TotalLastTwoRedVolume;

def Buy;
def BuyTarget;
def BuyStop;
def ExitPrice;

if Buy[1] and high >= BuyTarget[1] {
Buy = 0;
BuyTarget = double.nan;
BuyStop = double.nan;
ExitPrice = BuyTarget[1];

}else if Buy[1] and low <= BuyStop[1] {
Buy = 0;
BuyTarget = double.nan;
BuyStop = double.nan;
ExitPrice = BuyStop[1];

}else if !Buy[1] && Cond1 && Cond2 && Cond3 && Cond4 {
Buy = 1;
BuyTarget = open[-1] + (open[-1] * .05);
BuyStop = open[-1] - (open[-1] * .05);
ExitPrice = double.nan;
}else {
Buy = Buy[1];
BuyTarget = BuyTarget[1];
BuyStop = BuyStop[1];
ExitPrice = ExitPrice[1];;
}

plot Signal = !Buy[1] && Cond1 && Cond2 && Cond3 && Cond4;
Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot BTPlot = BuyTarget;
plot BSPlot = BuyStop;

addOrder(OrderType.BUY_TO_OPEN, Buy, price = open[-1], tradesize = 1);
################################################################################################
#Use of negative offset in below exit order condition to simulate price hitting target or stop.#
addOrder(OrderType.SELL_TO_CLOSE, !Buy[-1], price = ExitPrice[-1], tradesize = 1);             #
################################################################################################
Thanks alot buddy :)
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
349 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