5 EMA strategy Pinescript needed - trading

if a 5-minute candle bar high is below 5 EMA and then the next candle exceeds the previous candle high, the script should generate a buy signal.
If a 5-minute candle bar low is above 5 EMA and then next candle closes below the previous candle low, the script should generate a sell signal.
Could someone please help me code the script?

Related

How to look back a certain number of days for an indicator and how to only calculate logic upto the last complete bar (i.e. not the current bar)

I'm relatively new to Pine and was having trouble with a couple of things.
So I've got a indicator that I want to look back for the last 5 full days of activity, however not each stock trades in the premarket/afterhours so I want to be able to look back say 5 days and then calculate the number of bars on the time frame (i.e. for a 5 min or hourly resolution). That way I can calculate the number of bars present on the current ticker for that time frame and adjust my indicator length (instead of just having a constant figure of 80 bars or whatever).
The second thing I want to do is only have the indicator update on the completion of a full bar (for example the hourly resolution). So I want it to only calculate up to x-1 bars. Right now I'm finding that it's updating on each new tick. ( I was able to figure this one out, just changed it from i= 0 to length -1 to i=1 to length).
Any help is greatly appreciated.
Thanks,
A

PineScript is exiting strategy and opening a double sized order on the same bar

Using the pinescript editor in TradingView, I've noticed a slightly strange occurrence. When there are triggers to exit a long trade (trailing stop) and a trigger to enter a new short trade on the same bar, both open, however, the short has 2x the position size than it should.
Here's the relevant code:
// When uptrend, use upper band as a stop market buy
if (uptrend)
strategy.entry("Long", strategy.long, stop = h)
// Exit longs with a trailing stop. Assume longStopPrice is calculated per bar
if (strategy.position_size > 0)
strategy.exit("Trailing Stop", "Long", stop = longStopPrice)
// When downtrend, use lower band as a stop market sell
if (downtrend)
strategy.entry("Short", strategy.short, stop = l)
// Exit shorts with a trailing stop. Assume shortStopPrice is calculated per bar
if (strategy.position_size < 0)
strategy.exit("Trailing Stop", "Short", stop = shortStopPrice)
Here is the output on the UI:
Notice that the long position size is 15.358918 and a few bars later, this is closed with a trailing stop size -15.358918. However a new short trade is also opened on the same bar size -30.70059 (It should not be -30 it should be -15).
A little bit later on this second trade is closed by trailing stop, size +15.34 (but -30 was opened).
The strategy leaves you net short -15 units for the entire duration of the backtest. Ideally i'd like the strategy.enter to only enter a single position size & trailing stop to close the entire position, not to enter a double position & close half.
Any ideas?
Its because your exit condition does not have the same id as the entry condition. For TV, the entry id has still not closed. So, change the exit id from 'Trailing Stop' to 'Long' and 'Short' as those are the ids used during the entry.
Theres some logic behind but this happens when you dont close a position before opening a new one, what helped me was just moving the close strategy above the open, so every loop also checks to confirm to always close a position before opening a new one
Btw, also dont forget that on one way mode position your 2nd trade must have to be 2x the previous one, since lets say you go +$25 long, then -$25 for short, you would end with $0 for the next trade. Then having 2x the previous one, you would go +$25 long, then -$50 for short, you would end with -$25 short for your next trade, then +$50 long again you would end +$25 next trade
add to if downtrend this--
and strategy.position_size == 0

how can I set take profit to double stop loss

I am setting up a pending order in the following way in my project:
int ticketSell = OrderSend(Symbol(),OP_SELLLIMIT,lotSize, bottom,0, top,0,"SellOrder",magicnumber,0,Red);
How can I make the take profit, double the pips for stop loss? In other words I want a 2:1 win ratio for my pending order.
Also how can I set the entry price which is bottom say, 6 pips below bottom's value?

How can I write an mt4 indicator that draws a rectangle and horizontal lines over first 6 hours of hourly candles

I am a quite new to mql4 coding. I would like to know how I write an indicator that does the following, based on the image below:
Draw a rectangle over the current day hour chart that covers the highest and lowest price points of the first 6 hours (candles)
Draw two horizontal lines along the highest and lowest points found 1.
Please note that 1. and 2. should be based strictly on the hourly period and shouldn't vary with the selected period.
I believe I'm suppose to be using ObjectCreate() with OBJ_RECTANGLE and OBJ_HLINE but the whole concept is quite new to me. I would really appreciate some assistance.

iOS find the average speed in core location [duplicate]

This question already has answers here:
iOS find average speed
(2 answers)
Closed 8 years ago.
I want to be able to calculate the average speed with core location. I already have the current speed:
- (void)locationUpdate:(CLLocation *)location {
speedLabel.text = [NSString stringWithFormat:#"%.2f", [location speed]*2.236936284];
And also I want to be able to show it in a label and reset it with a button.
I want it to start averaging when a button is pressed and update the average about every 5 seconds and then stop averaging after another button is pressed.
Thank you!
As defined: Average refers to the sum of numbers divided by n. That is, when dealing with an average, you usually have at least 2 or more observations from which you wish to calculate an average. The average of your current speed is just, well, your current speed divided by one. I'm sure that's not what you are looking for.
You will need to ask yourself for how long time you wish to average. Five seconds? 10 seconds? The shorter the interval (fewer observations), the more sensitive to significant changes your average will be.
Basically what you would do is to store your observations in a mutable collection, eg NSMutableArray, and then just sum them up and divide the sum by the array's count property, corresponding to the number of observations. Or, as NSHipster has a great article on, you could take advantage of valueForKeyPath:
NSMutableArray *speedObservations = [[NSMutableArray alloc] initWithCapacity:n];
...
CGFloat average = [speedObservations valueForKeyPath:#"#avg.floatValue"];
Hmm. I would do this differently.
Average speed is distance traveled from a start time to an end time. I would record the user's location when they start asking for average speed, along with the current date. Then I would periodically get a new location reading and calculate distance traveled from the old location to the new location divided by the elapsed time between the current location reading and the first location reading.

Resources