Storing moving average price in a variable upon entry

lane_parsons

New member
Hi folks! I'm back testing a strategy and I'm trying to store the value of a moving average in a variable at the time of entry so I can use it to calculate my take profit and stop loss orders. The moving average value I'm trying to store is the midline of a Bollinger band.

Would anyone be able to help me write the variable to store the value of this midline at the time of entering the trade?

Thanks so much!
 
Solution
In general it works like this:
Code:
def entry = if my_entry_condition then 1 else 0;
def entry_price = if entry[1] == 1 then open else entry_price[1];
you can then do things with the entry_price variable. It will change again the next time the entry condition is met (this can be a monkey wrench if your strategy has multiple buy signals before the first sell signal and the entry_price is reset every time the condition is met even though you're already in a trade and the entry hasn't really changed).

If you can't figure it out from here, post some code back and we'll see if we can get you across the finish line.

Trade Happy,
mashume
In general it works like this:
Code:
def entry = if my_entry_condition then 1 else 0;
def entry_price = if entry[1] == 1 then open else entry_price[1];
you can then do things with the entry_price variable. It will change again the next time the entry condition is met (this can be a monkey wrench if your strategy has multiple buy signals before the first sell signal and the entry_price is reset every time the condition is met even though you're already in a trade and the entry hasn't really changed).

If you can't figure it out from here, post some code back and we'll see if we can get you across the finish line.

Trade Happy,
mashume
 
Solution
In general it works like this:
Code:
def entry = if my_entry_condition then 1 else 0;
def entry_price = if entry[1] == 1 then open else entry_price[1];
you can then do things with the entry_price variable. It will change again the next time the entry condition is met (this can be a monkey wrench if your strategy has multiple buy signals before the first sell signal and the entry_price is reset every time the condition is met even though you're already in a trade and the entry hasn't really changed).

If you can't figure it out from here, post some code back and we'll see if we can get you across the finish line.

Trade Happy,
mashume

@mashume - thank you so much for chiming in here! I definitely understand the concept you're going for, but I think I might need a little more help if you're able to get it integrated into my code. I put my code below, and detailed my issue on line 65, under the #LONG PROFIT#. Once I see the logic for the long, I can get it incorporated into the short which would be good practice for me too ;)

I'm still very new to this, so if you see any other noob mistakes, I'm very open to feedback :)

Thanks again!!


Code:
#__________INPUTS__________#
input tradeSize = 100;

    ##___BOLLINGER BANDS INPUTS___##
input price_boll = close;
input displace = 0;
input length_boll = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType_boll = AverageType.EXPONENTIAL;

        #BOLLLINGER BANDS PLOT#
def sDev = StDev(data = price_boll[-displace], length = length_boll);

plot MidLine = MovingAverage(averageType_boll, data = price_boll[-displace], length = length_boll);
plot LowerBand = MidLine + Num_Dev_Dn * sDev;
plot UpperBand = MidLine + Num_Dev_up * sDev;

LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

    ##___RSI___##
input length_RSI = 14;
input over_Bought = 70;
input over_Sold = 30;
input price_RSI = close;
input averageType_RSI = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;


#__________DEFINE__________#

    ##___BOLLINGER BANDS___##
def BollingerBands = BollingerBands(price_boll, displace, length_boll, Num_Dev_Dn, Num_Dev_up, averageType_boll);

def boll_upper_cross = close is greater than UpperBand;
def boll_lower_cross = close is less than LowerBand;

    ##___RSI LOWER & UPPER CROSS___##
def RSI = RSI(length_RSI, over_Bought, over_Sold, price_RSI, averageType_RSI, showBreakoutSignals);
def RSI_upper_cross = RSI is greater than over_Bought;
def RSI_lower_cross = RSI is less than over_Sold;


#__________CONDITIONS__________#

    ##___LONG CONDITION___##
def buySignalLong = boll_lower_cross from 1 bars ago and RSI_lower_cross from 1 bars ago and close is greater than LowerBand;

    ##___SHORT CONDITION___##
def sellSignalShort = boll_upper_cross from 1 bars ago and RSI_upper_cross from 1 bars ago and close is less than UpperBand;



#__________SALES__________#

    ##___LONG ENTRY___#

        #LONG OPEN#
AddOrder(OrderType.BUY_TO_OPEN, buySignalLong, open[-1], tradeSize, Color.CYAN, Color.CYAN);

        #LONG PROFIT#

##This order below is where I'm struggling. What I would like is to have my take profit be equal to the midline (moving average) of the bollinger band on the day that I entered and my stop loss be calculated as 50% of the distance between my entry price and the moving average. So for example if the day I entered my entry price was 40, and the midline was at 50, I'd like the take profit to be set at 50 and the stop loss to be set at 35.

##AddOrder(OrderType.SELL_TO_CLOSE, longProfit, longProfit, tradeSize, Color.GREEN, Color.GREEN);

        #LONG LOSS#

##AddOrder(OrderType.SELL_TO_CLOSE, EntryPrice() - (MidLine - EntryPrice()), EntryPrice() - (MidLine - EntryPrice()), tradeSize, Color.RED, Color.RED);

    ##___SHORT ENTRY___##
##AddOrder(OrderType.SELL_TO_OPEN, sellSignalShort, open[-1], tradeSize, Color.BLUE, Color.BLUE);

        #SHORT PROFIT#
##AddOrder(OrderType.BUY_TO_CLOSE, exitGoodShort, EntryPrice() * percMultShortProfit + EntryPrice(), tradeSize, Color.GREEN, Color.GREEN);

        #SHORT LOSS#
##AddOrder(OrderType.BUY_TO_CLOSE, exitBadShort, EntryPrice() * percMultShortLoss + EntryPrice(), tradeSize, Color.RED, Color.RED);



#__________PLOTS__________#
 

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