EMA crossover stop and take profit

merc2226

Member
VIP
I have been working on this for days, I keep trying to adjust this but cant figure it out.
I love this study but would like to base entry on ema crossing and the stop and take profit on actual movement of the stock price.
Stop at -1 points from purchase and close at +2 points, something like that.

My strategy is simple, 5 crosses 50 ema, after candle close, trade box appears with my stop and my target. I close all but small amount at +10% or +20% but its options so I calculate how far it moves based on delta to come up with stock movement to = the %. I use this to help review trades and good visual when in a trade.

this script works perfect but I cant figure out how to get rid of ATR and just use actual stop loss based on the same price movement each time that I set.

here is a picture and script im' currently using that I would like to remove the ATR part.

Thank you for looking at it, hopefully its a easy fix.
IMG_5858.jpeg

#
# Trade Lines
# v1.0 by @mark.917
#

# Trade Lines will plot lines with and optional cloud showing where you entered your
# position and the location of your stop loss and profit target.
#
# To use, your code will need to set "your_buy_signal" or "your_sell_signal" to true.
# The profit target and stop loss are based on an ATR multplier. You may modify this
# to be whatever you would like.

declare upper;

#######################
# YOUR CODE STARTS HERE
#######################

# sample using moving average crossover for signals

plot FastMA = MovingAverage(AverageType.EXPONENTIAL, close, 5);
plot SlowMA = MovingAverage(AverageType.EXPONENTIAL, close, 50);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));

def buy_signal = FastMA crosses above SlowMA;
def sell_signal = FastMA crosses below SlowMA;

###################
# BEGIN TRADE LINES
###################

input show_trade_zone = yes;
input calc_mode = {default atr, fixed}; #hint mode: Select fixed values or atr values for target and stop levels.
input ATRTargetMultiple = 2.5;
input ATRStopMultiple = 1.5;
input FixedTarget = 1.50;
input FixedStop = .75;

def atr = ATR(14, averageType.EXPONENTIAL);

def long_signal = if buy_signal then 1 else 0;
def long_target = if long_signal == 1 then atr * ATRTargetMultiple else long_target[1];
def long_stop = if long_signal == 1 then atr * ATRStopMultiple else long_stop[1];
def long_entry = if long_signal == 1 then open else if (high[1] > long_entry[1] + long_target or low[1] < long_entry[1] - long_stop) then Double.NaN else long_entry[1];
def long_exit = if long_entry > 0 then 0 else 1;

plot long_pt = if show_trade_zone and !long_exit then long_entry + long_target else Double.NaN;
long_pt.SetDefaultColor(Color.GREEN);
plot long_ep = if show_trade_zone and !long_exit then long_entry else Double.NaN;
long_ep.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
long_ep.SetPaintingStrategy(PaintingStrategy.DASHES);
long_ep.SetDefaultColor(Color.WHITE);
plot long_sl = if show_trade_zone and !long_exit then long_ep - long_stop else Double.NaN;
long_sl.SetDefaultColor(Color.RED);

AddCloud(if show_trade_zone then long_pt else Double.NaN, long_ep, Color.GREEN);
AddCloud(if show_trade_zone then long_sl else Double.NaN, long_ep, Color.RED);

def short_signal = if sell_signal then 1 else 0;
def short_target = if short_signal == 1 then atr * ATRTargetMultiple else short_target[1];
def short_stop = if short_signal == 1 then atr * ATRStopMultiple else short_stop[1];
def short_entry = if short_signal == 1 then open else if (low[1] < short_entry[1] - short_target or high[1] > short_entry[1] + short_stop) then Double.NaN else short_entry[1];
def short_exit = if short_entry > 0 then 0 else 1;

plot short_pt = if show_trade_zone and !short_exit then short_entry - short_target else Double.NaN;
short_pt.SetDefaultColor(Color.GREEN);
plot short_ep = if show_trade_zone and !short_exit then short_entry else Double.NaN;
short_ep.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
short_ep.SetPaintingStrategy(PaintingStrategy.DASHES);
short_ep.SetDefaultColor(Color.WHITE);
plot short_sl = if show_trade_zone and !short_exit then short_ep + short_stop else Double.NaN;
short_sl.SetDefaultColor(Color.RED);

AddCloud(if show_trade_zone then short_ep else Double.NaN, short_pt, Color.GREEN);
AddCloud(if show_trade_zone then short_sl else Double.NaN, short_ep, Color.RED);

#################
# END TRADE LINES
#################
AddOrder(OrderType.BUY_AUTO, fastMA crosses above slowMA[1]);
AddOrder(OrderType.Sell_AUTO, fastMA crosses below slowMA[1]);
 
Solution
I have been working on this for days, I keep trying to adjust this but cant figure it out.
I love this study but would like to base entry on ema crossing and the stop and take profit on actual movement of the stock price.
Stop at -1 points from purchase and close at +2 points, something like that.

My strategy is simple, 5 crosses 50 ema, after candle close, trade box appears with my stop and my target. I close all but small amount at +10% or +20% but its options so I calculate how far it moves based on delta to come up with stock movement to = the %. I use this to help review trades and good visual when in a trade.

this script works perfect but I cant figure out how to get rid of ATR and just use actual stop loss based on the same...
I have been working on this for days, I keep trying to adjust this but cant figure it out.
I love this study but would like to base entry on ema crossing and the stop and take profit on actual movement of the stock price.
Stop at -1 points from purchase and close at +2 points, something like that.

My strategy is simple, 5 crosses 50 ema, after candle close, trade box appears with my stop and my target. I close all but small amount at +10% or +20% but its options so I calculate how far it moves based on delta to come up with stock movement to = the %. I use this to help review trades and good visual when in a trade.

this script works perfect but I cant figure out how to get rid of ATR and just use actual stop loss based on the same price movement each time that I set.

here is a picture and script im' currently using that I would like to remove the ATR part.

Thank you for looking at it, hopefully its a easy fix.
View attachment 20572
#
# Trade Lines
# v1.0 by @mark.917
#

# Trade Lines will plot lines with and optional cloud showing where you entered your
# position and the location of your stop loss and profit target.
#
# To use, your code will need to set "your_buy_signal" or "your_sell_signal" to true.
# The profit target and stop loss are based on an ATR multplier. You may modify this
# to be whatever you would like.

declare upper;

#######################
# YOUR CODE STARTS HERE
#######################

# sample using moving average crossover for signals

plot FastMA = MovingAverage(AverageType.EXPONENTIAL, close, 5);
plot SlowMA = MovingAverage(AverageType.EXPONENTIAL, close, 50);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));

def buy_signal = FastMA crosses above SlowMA;
def sell_signal = FastMA crosses below SlowMA;

###################
# BEGIN TRADE LINES
###################

input show_trade_zone = yes;
input calc_mode = {default atr, fixed}; #hint mode: Select fixed values or atr values for target and stop levels.
input ATRTargetMultiple = 2.5;
input ATRStopMultiple = 1.5;
input FixedTarget = 1.50;
input FixedStop = .75;

def atr = ATR(14, averageType.EXPONENTIAL);

def long_signal = if buy_signal then 1 else 0;
def long_target = if long_signal == 1 then atr * ATRTargetMultiple else long_target[1];
def long_stop = if long_signal == 1 then atr * ATRStopMultiple else long_stop[1];
def long_entry = if long_signal == 1 then open else if (high[1] > long_entry[1] + long_target or low[1] < long_entry[1] - long_stop) then Double.NaN else long_entry[1];
def long_exit = if long_entry > 0 then 0 else 1;

plot long_pt = if show_trade_zone and !long_exit then long_entry + long_target else Double.NaN;
long_pt.SetDefaultColor(Color.GREEN);
plot long_ep = if show_trade_zone and !long_exit then long_entry else Double.NaN;
long_ep.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
long_ep.SetPaintingStrategy(PaintingStrategy.DASHES);
long_ep.SetDefaultColor(Color.WHITE);
plot long_sl = if show_trade_zone and !long_exit then long_ep - long_stop else Double.NaN;
long_sl.SetDefaultColor(Color.RED);

AddCloud(if show_trade_zone then long_pt else Double.NaN, long_ep, Color.GREEN);
AddCloud(if show_trade_zone then long_sl else Double.NaN, long_ep, Color.RED);

def short_signal = if sell_signal then 1 else 0;
def short_target = if short_signal == 1 then atr * ATRTargetMultiple else short_target[1];
def short_stop = if short_signal == 1 then atr * ATRStopMultiple else short_stop[1];
def short_entry = if short_signal == 1 then open else if (low[1] < short_entry[1] - short_target or high[1] > short_entry[1] + short_stop) then Double.NaN else short_entry[1];
def short_exit = if short_entry > 0 then 0 else 1;

plot short_pt = if show_trade_zone and !short_exit then short_entry - short_target else Double.NaN;
short_pt.SetDefaultColor(Color.GREEN);
plot short_ep = if show_trade_zone and !short_exit then short_entry else Double.NaN;
short_ep.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
short_ep.SetPaintingStrategy(PaintingStrategy.DASHES);
short_ep.SetDefaultColor(Color.WHITE);
plot short_sl = if show_trade_zone and !short_exit then short_ep + short_stop else Double.NaN;
short_sl.SetDefaultColor(Color.RED);

AddCloud(if show_trade_zone then short_ep else Double.NaN, short_pt, Color.GREEN);
AddCloud(if show_trade_zone then short_sl else Double.NaN, short_ep, Color.RED);

#################
# END TRADE LINES
#################
AddOrder(OrderType.BUY_AUTO, fastMA crosses above slowMA[1]);
AddOrder(OrderType.Sell_AUTO, fastMA crosses below slowMA[1]);

This allows use to use the atr or fixed options by adding the missing code for the fixed option values input to work. The following code was added to define target and stop based upon the input at calc_mode. These were then substituted in long/short target/stop portions of the code. See the full code below the image, which is using the fixed option.

Code:
def target;
def stop;
switch (calc_mode) {
case atr:
    target = atr * ATRTargetMultiple;
    stop = atr * ATRStopMultiple;
case fixed:
    target = FixedTarget;
    stop = FixedStop;
}

The addorder portion of the code was commented out as it appears that you are not using the code as a TOS strategy type code.

Screenshot 2023-12-30 095610.png
Code:
#
# Trade Lines
# v1.0 by @mark.917
#

# Trade Lines will plot lines with and optional cloud showing where you entered your
# position and the location of your stop loss and profit target.
#
# To use, your code will need to set "your_buy_signal" or "your_sell_signal" to true.
# The profit target and stop loss are based on an ATR multplier. You may modify this
# to be whatever you would like.

declare upper;

#######################
# YOUR CODE STARTS HERE
#######################

# sample using moving average crossover for signals

plot FastMA = MovingAverage(AverageType.EXPONENTIAL, close, 5);
plot SlowMA = MovingAverage(AverageType.EXPONENTIAL, close, 50);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));

def buy_signal = FastMA crosses above SlowMA;
def sell_signal = FastMA crosses below SlowMA;

###################
# BEGIN TRADE LINES
###################

input show_trade_zone = yes;
input calc_mode = {default fixed, atr}; #hint mode: Select fixed values or atr values for target and stop levels.
input ATRTargetMultiple = 2.5;
input ATRStopMultiple = 1.5;
input FixedTarget = 1.50;
input FixedStop = .75;

def atr = ATR(14, AverageType.EXPONENTIAL);

def target;
def stop;
switch (calc_mode) {
case atr:
    target = atr * ATRTargetMultiple;
    stop = atr * ATRStopMultiple;
case fixed:
    target = FixedTarget;
    stop = FixedStop;
}

def long_signal = if buy_signal then 1 else 0;
def long_target = if long_signal == 1 then target else long_target[1];
def long_stop = if long_signal == 1 then stop else long_stop[1];
def long_entry = if long_signal == 1 then open else if (high[1] > long_entry[1] + long_target or low[1] < long_entry[1] - long_stop) then Double.NaN else long_entry[1];
def long_exit = if long_entry > 0 then 0 else 1;

plot long_pt = if show_trade_zone and !long_exit then long_entry + long_target else Double.NaN;
long_pt.SetDefaultColor(Color.GREEN);
plot long_ep = if show_trade_zone and !long_exit then long_entry else Double.NaN;
long_ep.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
long_ep.SetPaintingStrategy(PaintingStrategy.DASHES);
long_ep.SetDefaultColor(Color.WHITE);
plot long_sl = if show_trade_zone and !long_exit then long_ep - long_stop else Double.NaN;
long_sl.SetDefaultColor(Color.RED);

AddCloud(if show_trade_zone then long_pt else Double.NaN, long_ep, Color.GREEN);
AddCloud(if show_trade_zone then long_sl else Double.NaN, long_ep, Color.RED);

def short_signal = if sell_signal then 1 else 0;
def short_target = if short_signal == 1 then target else short_target[1];
def short_stop = if short_signal == 1 then stop else short_stop[1];
def short_entry = if short_signal == 1 then open else if (low[1] < short_entry[1] - short_target or high[1] > short_entry[1] + short_stop) then Double.NaN else short_entry[1];
def short_exit = if short_entry > 0 then 0 else 1;

plot short_pt = if show_trade_zone and !short_exit then short_entry - short_target else Double.NaN;
short_pt.SetDefaultColor(Color.GREEN);
plot short_ep = if show_trade_zone and !short_exit then short_entry else Double.NaN;
short_ep.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
short_ep.SetPaintingStrategy(PaintingStrategy.DASHES);
short_ep.SetDefaultColor(Color.WHITE);
plot short_sl = if show_trade_zone and !short_exit then short_ep + short_stop else Double.NaN;
short_sl.SetDefaultColor(Color.RED);

AddCloud(if show_trade_zone then short_ep else Double.NaN, short_pt, Color.GREEN);
AddCloud(if show_trade_zone then short_sl else Double.NaN, short_ep, Color.RED);

#################
# END TRADE LINES
#################
#AddOrder(OrderType.BUY_AUTO, FastMA crosses above SlowMA[1]);
#AddOrder(OrderType.SELL_AUTO, FastMA crosses below SlowMA[1]);

#
 
Solution
This allows use to use the atr or fixed options by adding the missing code for the fixed option values input to work. The following code was added to define target and stop based upon the input at calc_mode. These were then substituted in long/short target/stop portions of the code. See the full code below the image, which is using the fixed option.



The addorder portion of the code was commented out as it appears that you are not using the code as a TOS strategy type code.
I added the option to adjust the Ma's if one would like.
Code:
#
# Trade Lines
# v1.0 by @mark.917
#

# Trade Lines will plot lines with and optional cloud showing where you entered your
# position and the location of your stop loss and profit target.
#
# To use, your code will need to set "your_buy_signal" or "your_sell_signal" to true.
# The profit target and stop loss are based on an ATR multplier. You may modify this
# to be whatever you would like.

declare upper;

#######################
# YOUR CODE STARTS HERE
#######################

# sample using moving average crossover for signals
Input FastMaLength = 5;
Input SlowMaLength = 50;

def FastMA = MovingAverage(AverageType.EXPONENTIAL, close, FastMaLength);
def SlowMA = MovingAverage(AverageType.EXPONENTIAL, close, SlowMaLength);

plot fastMAPlot = FastMA;
fastMAPlot.SetDefaultColor(GetColor(1));

plot slowMAPlot = SlowMA;
slowMAPlot.SetDefaultColor(GetColor(2));

def buy_signal = FastMA crosses above SlowMA;
def sell_signal = FastMA crosses below SlowMA;

###################
# BEGIN TRADE LINES
###################

input show_trade_zone = yes;
input calc_mode = {default fixed, atr}; #hint mode: Select fixed values or atr values for target and stop levels.
input ATRTargetMultiple = 2.5;
input ATRStopMultiple = 1.5;
input FixedTarget = 1.50;
input FixedStop = .75;

def atr = ATR(14, AverageType.EXPONENTIAL);

def target;
def stop;
switch (calc_mode) {
    case atr:
        target = atr * ATRTargetMultiple;
        stop = atr * ATRStopMultiple;
    case fixed:
        target = FixedTarget;
        stop = FixedStop;
}

def long_signal = if buy_signal then 1 else 0;
def long_target = if long_signal == 1 then target else long_target[1];
def long_stop = if long_signal == 1 then stop else long_stop[1];
def long_entry = if long_signal == 1 then open else if (high[1] > long_entry[1] + long_target or low[1] < long_entry[1] - long_stop) then Double.NaN else long_entry[1];
def long_exit = if long_entry > 0 then 0 else 1;

plot long_pt = if show_trade_zone and !long_exit then long_entry + long_target else Double.NaN;
long_pt.SetDefaultColor(Color.GREEN);
plot long_ep = if show_trade_zone and !long_exit then long_entry else Double.NaN;
long_ep.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
long_ep.SetDefaultColor(Color.WHITE);
plot long_sl = if show_trade_zone and !long_exit then long_ep - long_stop else Double.NaN;
long_sl.SetDefaultColor(Color.RED);

AddCloud(if show_trade_zone then long_pt else Double.NaN, long_ep, Color.GREEN);
AddCloud(if show_trade_zone then long_sl else Double.NaN, long_ep, Color.RED);

def short_signal = if sell_signal then 1 else 0;
def short_target = if short_signal == 1 then target else short_target[1];
def short_stop = if short_signal == 1 then stop else short_stop[1];
def short_entry = if short_signal == 1 then open else if (low[1] < short_entry[1] - short_target or high[1] > short_entry[1] + short_stop) then Double.NaN else short_entry[1];
def short_exit = if short_entry > 0 then 0 else 1;

plot short_pt = if show_trade_zone and !short_exit then short_entry - short_target else Double.NaN;
short_pt.SetDefaultColor(Color.GREEN);
plot short_ep = if show_trade_zone and !short_exit then short_entry else Double.NaN;
short_ep.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
short_ep.SetDefaultColor(Color.WHITE);
plot short_sl = if show_trade_zone and !short_exit then short_ep + short_stop else Double.NaN;
short_sl.SetDefaultColor(Color.RED);

AddCloud(if show_trade_zone then short_pt else Double.NaN, short_ep, Color.GREEN);
AddCloud(if show_trade_zone then short_sl else Double.NaN, short_ep, Color.RED);

#################
# END TRADE LINES
#################
#AddOrder(OrderType.BUY_AUTO, FastMA crosses above SlowMA[1]);
#AddOrder(OrderType.SELL_AUTO, FastMA crosses below SlowMA[1]);;
 
Thanks again. I hate to ask for 1 more thing but this is great.
can a label be added based on the ema crossover chosen, take profit and stop loss to show you how many wins and how many losses closing the position once either is hit first. W take profit, loss hit stop loss.
last request, a trade time can be set for the totals.
I find you get less moves from 930 to 1200 eastern but better follow through so I stop at noon.
 
Thank you SleepyZ !!!!!!

The profit target and stop loss are based on an ATR multiplier is not exiting the trade in the Strategy and I don't know how to write the code

AddOrder(OrderType.BUY_AUTO, FastMA crosses above SlowMA[1]);
Insert line here:

AddOrder(OrderType.SELL_AUTO, FastMA crosses below SlowMA[1])
Insert line here:

The 2 codes above just buys or sells.

Please help.

Thank you SleepyZ !!!!!!
 
Thank you SleepyZ !!!!!!

The profit target and stop loss are based on an ATR multiplier is not exiting the trade in the Strategy and I don't know how to write the code

AddOrder(OrderType.BUY_AUTO, FastMA crosses above SlowMA[1]);
Insert line here:

AddOrder(OrderType.SELL_AUTO, FastMA crosses below SlowMA[1])
Insert line here:

The 2 codes above just buys or sells.

Please help.

Thank you SleepyZ !!!!!!

This changes the addorder to open/close, buy/sell orders using the standard TOS open[-1] entry price. This will not match the clouds as these are considered not able to be used/possible in realistic trading and may give a false impression of what is possible.

The following is a strategy script. Only one order will appear until closed even though the clouds will overlap and identify another possible trade in the opposite direction.

Screenshot 2024-01-01 080944.png
Code:
#
# Trade Lines
# v1.0 by @mark.917
#

# Trade Lines will plot lines with and optional cloud showing where you entered your
# position and the location of your stop loss and profit target.
#
# To use, your code will need to set "your_buy_signal" or "your_sell_signal" to true.
# The profit target and stop loss are based on an ATR multplier. You may modify this
# to be whatever you would like.

# Fixed input calc_mode with a switch code to provide both options
# Changed addorder from automatic to open/close method
 
declare upper;

#######################
# YOUR CODE STARTS HERE
#######################

# sample using moving average crossover for signals
input FastMaLength = 5;
input SlowMaLength = 50;

def FastMA = MovingAverage(AverageType.EXPONENTIAL, close, FastMaLength);
def SlowMA = MovingAverage(AverageType.EXPONENTIAL, close, SlowMaLength);

plot fastMAPlot = FastMA;
fastMAPlot.SetDefaultColor(GetColor(1));

plot slowMAPlot = SlowMA;
slowMAPlot.SetDefaultColor(GetColor(2));

def buy_signal = FastMA crosses above SlowMA;
def sell_signal = FastMA crosses below SlowMA;

###################
# BEGIN TRADE LINES
###################

input show_trade_zone = yes;
input calc_mode = {fixed, default atr}; #hint mode: Select fixed values or atr values for target and stop levels.
input ATRTargetMultiple = 3.5;
input ATRStopMultiple = 1.5;
input FixedTarget = 3;
input FixedStop = 1;

def atr = ATR(14, AverageType.EXPONENTIAL);

def target;
def stop;
switch (calc_mode) {
case atr:
    target = atr * ATRTargetMultiple;
    stop = atr * ATRStopMultiple;
case fixed:
    target = FixedTarget;
    stop = FixedStop;
}

def long_signal = if buy_signal then 1 else 0;
def long_target = if long_signal == 1 then target else long_target[1];
def long_stop = if long_signal == 1 then stop else long_stop[1];
def long_entry = if long_signal == 1 then open else if (high[1] > long_entry[1] + long_target or low[1] < long_entry[1] - long_stop) then Double.NaN else long_entry[1];
def long_exit = if long_entry > 0 then 0 else 1;

plot long_pt = if show_trade_zone and !long_exit then long_entry + long_target else Double.NaN;
long_pt.SetDefaultColor(Color.GREEN);
plot long_ep = if show_trade_zone and !long_exit then long_entry else Double.NaN;
long_ep.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
long_ep.SetDefaultColor(Color.WHITE);
plot long_sl = if show_trade_zone and !long_exit then long_ep - long_stop else Double.NaN;
long_sl.SetDefaultColor(Color.RED);

AddCloud(if show_trade_zone then long_pt else Double.NaN, long_ep, Color.GREEN);
AddCloud(if show_trade_zone then long_sl else Double.NaN, long_ep, Color.RED);

def short_signal = if sell_signal then 1 else 0;
def short_target = if short_signal == 1 then target else short_target[1];
def short_stop = if short_signal == 1 then stop else short_stop[1];
def short_entry = if short_signal == 1 then open else if (low[1] < short_entry[1] - short_target or high[1] > short_entry[1] + short_stop) then Double.NaN else short_entry[1];
def short_exit = if short_entry > 0 then 0 else 1;


plot short_pt = if show_trade_zone and !short_exit then short_entry - short_target else Double.NaN;
short_pt.SetDefaultColor(Color.GREEN);
plot short_ep = if show_trade_zone and !short_exit then short_entry else Double.NaN;
short_ep.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
short_ep.SetDefaultColor(Color.WHITE);
plot short_sl = if show_trade_zone and !short_exit then short_ep + short_stop else Double.NaN;
short_sl.SetDefaultColor(Color.RED);

AddCloud(if show_trade_zone then short_ep else Double.NaN, short_pt, Color.GREEN);
AddCloud(if show_trade_zone then short_sl else Double.NaN, short_ep, Color.RED);

#################
# END TRADE LINES
#################

AddOrder(OrderType.BUY_TO_OPEN, long_signal, name = "Long Open");
AddOrder(OrderType.SELL_TO_CLOSE, long_exit == 0 and IsNaN(long_exit[-1]), name = "Long Close");
AddOrder(OrderType.SELL_TO_OPEN, short_signal, name = "Short Open");
AddOrder(OrderType.BUY_TO_CLOSE, short_exit == 0 and IsNaN(short_exit[-1]), name = "Short Close");

#
 
Last edited:
Thank you, I cant figure out how to just trade form 930 to noon, what can I add to limit it to a certain time of trading for truer backtesting results.

This uses trade_entry_window to attempt to only initiate a trade during that timeframe. The time to close is not controlled. This was added to the strategy in the above post #7



Screenshot 2024-01-03 130026.png

Code:
#
# Trade Lines
# v1.0 by @mark.917
#

# Trade Lines will plot lines with and optional cloud showing where you entered your
# position and the location of your stop loss and profit target.
#
# To use, your code will need to set "your_buy_signal" or "your_sell_signal" to true.
# The profit target and stop loss are based on an ATR multplier. You may modify this
# to be whatever you would like.

# Fixed input calc_mode with a switch code to provide both options
# Changed addorder from automatic to open/close method
 
declare upper;

#######################
# YOUR CODE STARTS HERE
#######################

# sample using moving average crossover for signals
input FastMaLength = 5;
input SlowMaLength = 50;

def FastMA = MovingAverage(AverageType.EXPONENTIAL, close, FastMaLength);
def SlowMA = MovingAverage(AverageType.EXPONENTIAL, close, SlowMaLength);

plot fastMAPlot = FastMA;
fastMAPlot.SetDefaultColor(GetColor(1));

plot slowMAPlot = SlowMA;
slowMAPlot.SetDefaultColor(GetColor(2));

def buy_signal = FastMA crosses above SlowMA;
def sell_signal = FastMA crosses below SlowMA;

###################
# BEGIN TRADE LINES
###################

input show_trade_zone = yes;
input calc_mode = {fixed, default atr}; #hint mode: Select fixed values or atr values for target and stop levels.
input ATRTargetMultiple = 3.5;
input ATRStopMultiple = 1.5;
input FixedTarget = 3;
input FixedStop = 1;

def atr = ATR(14, AverageType.EXPONENTIAL);

def target;
def stop;
switch (calc_mode) {
case atr:
    target = atr * ATRTargetMultiple;
    stop = atr * ATRStopMultiple;
case fixed:
    target = FixedTarget;
    stop = FixedStop;
}

def long_signal = if buy_signal then 1 else 0;
def long_target = if long_signal == 1 then target else long_target[1];
def long_stop = if long_signal == 1 then stop else long_stop[1];
def long_entry = if long_signal == 1 then open else if (high[1] > long_entry[1] + long_target or low[1] < long_entry[1] - long_stop) then Double.NaN else long_entry[1];
def long_exit = if long_entry > 0 then 0 else 1;

plot long_pt = if show_trade_zone and !long_exit then long_entry + long_target else Double.NaN;
long_pt.SetDefaultColor(Color.GREEN);
plot long_ep = if show_trade_zone and !long_exit then long_entry else Double.NaN;
long_ep.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
long_ep.SetDefaultColor(Color.WHITE);
plot long_sl = if show_trade_zone and !long_exit then long_ep - long_stop else Double.NaN;
long_sl.SetDefaultColor(Color.RED);

AddCloud(if show_trade_zone then long_pt else Double.NaN, long_ep, Color.GREEN);
AddCloud(if show_trade_zone then long_sl else Double.NaN, long_ep, Color.RED);

def short_signal = if sell_signal then 1 else 0;
def short_target = if short_signal == 1 then target else short_target[1];
def short_stop = if short_signal == 1 then stop else short_stop[1];
def short_entry = if short_signal == 1 then open else if (low[1] < short_entry[1] - short_target or high[1] > short_entry[1] + short_stop) then Double.NaN else short_entry[1];
def short_exit = if short_entry > 0 then 0 else 1;


plot short_pt = if show_trade_zone and !short_exit then short_entry - short_target else Double.NaN;
short_pt.SetDefaultColor(Color.GREEN);
plot short_ep = if show_trade_zone and !short_exit then short_entry else Double.NaN;
short_ep.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
short_ep.SetDefaultColor(Color.WHITE);
plot short_sl = if show_trade_zone and !short_exit then short_ep + short_stop else Double.NaN;
short_sl.SetDefaultColor(Color.RED);

AddCloud(if show_trade_zone then short_ep else Double.NaN, short_pt, Color.GREEN);
AddCloud(if show_trade_zone then short_sl else Double.NaN, short_ep, Color.RED);

#################
# END TRADE LINES
#################
def trade_entry_window = secondsfromTime(0930)>=0 and secondsfromTime(1200)<=0;
AddOrder(OrderType.BUY_TO_OPEN, trade_entry_window and long_signal, name = "Long Open");
AddOrder(OrderType.SELL_TO_CLOSE, long_exit == 0 and IsNaN(long_exit[-1]), name = "Long Close");
AddOrder(OrderType.SELL_TO_OPEN, trade_entry_window and short_signal, name = "Short Open");
AddOrder(OrderType.BUY_TO_CLOSE, short_exit == 0 and IsNaN(short_exit[-1]), name = "Short Close");

#
 

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
312 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