Hi everybody, First posts so be sweet with me pls. I am looking to code an automated strategy for EURUSD trends. I need to add a feature where if the market moves in one direction with candles > x it trades, while if candles are < than x it stops. Any suggestions? Thanks, Edoardo
By “candles > x” do you mean:
- candles whose Opening/Closing/Highest/Lowest (which one?) is > x?
- candles whose range is > x?
- number of consecutive bullish/bearish candles greater than x?
Also, what does “candles are < than x it stops” mean? What should be stopped, the strategy, the current trade…?
Please post a real example, so we can try to be of some help.
Roberto
Hello Roberto
Thanks for offering help. I have an example below so that you can help further, but I also want to explain what I-m trying to do.
I have a strategy that buys into eurusd trends, it makes money when the market raises with more than 3 green 1 min candles or falls with more than 3 red 1 minute candles, but loses money when market does a red and a green candle or viceversa. I define this market absence of trend FLAPPING.
So I-d like to have a code, which I can-t program yet, that tells the auto trading system not to initiate a new trade if the previous difference between candles is small.
Here is what I have come up with
// Initialization of Amplitude
Amplitude = 0.001
// Initialization of detector
Detector = 0
// Definition of Flapping
IF ABS((Low – High [1]) / High [1]) < Amplitude THEN
// Behavior of the detector
Detector = 1
ENDIF
// Absence of Flapping
IF ABS((High – Low [1]) / Low [1]) > Amplitude THEN
// Behavior of the detector
Detector = -1
ENDIF
Thank you for your contributions, Edoardo
Please use the “Insert PRT code” button identified by <> on the grey bar when you are posting, to make code more readable and easier to understand, as follows:
// Initialization of Amplitude
Amplitude = 0.001
// Initialization of detector
Detector = 0
// Definition of Flapping
IF ABS((Low – High [1]) / High [1]) < Amplitude THEN
// Behavior of the detector
Detector = 1
ENDIF
// Absence of Flapping
IF ABS((High – Low [1]) / Low [1]) > Amplitude THEN
// Behavior of the detector
Detector = -1
ENDIF
A few questions:
- in your code you did not check whether 3 consecutive matching bars have elapsed or not (3 green bars or 3 red ones)
- 0.001 is the difference in price between two bars?
- a difference between candles/bars should be computed using which price (low,high,open,close) or maybe their range?
DEFPARAM CumulateOrders = False // No cumulative orders allowed.
DEFPARAM FlatBefore = 093000 // Cancel any pending orders, close any positions and prevent placement of additional orders.
DEFPARAM FlatAfter = 160000 // Cancel any pending orders, close any positions and prevent placement of additional orders.
StopLoss = 10 //stoploss in points
TakeProfit = 20 //takeprofit in points
// Initialization of Amplitude
Amplitude = 0.001
// Initialization of detector
Detector = 0
// Definition of Flapping
IF ABS((Low - High [1]) / High [1]) < Amplitude THEN
// Behavior of the detector
Detector = 1
ENDIF
// Absence of Flapping
IF ABS((High - Low [1]) / Low [1]) > Amplitude THEN
// Behavior of the detector
Detector = -1
ENDIF
// Go Long
IF close[1] > open [1] and Detector = -1 THEN
BUY AT MARKET
ENDIF
// Go Short
IF close[1] < open [1] and Detector = -1 THEN
SELLSHORT AT MARKET
ENDIF
// Close Long
IF LONGONMARKET and close < open THEN
SELL AT MARKET
ENDIF
// Close Short
IF SHORTONMARKET and close > open THEN
BUY AT MARKET
ENDIF
SET STOP PLOSS StopLoss
SET TARGET PPROFIT TakeProfit
Answers to your questions
- I-m not at the stage of defining the three candles yet (trend), so far I check only if the current close is higher or lower than the previous.
- yes is what I intended to use to define the range of the candle in %
- what I think is most relevant is the difference between the closes.
thanks for your help,
Edoardo
Your code works, though it seems not profitable at this stage.
What kind of help are looking for?
Hi Roberto,
Glad to hear you found no glitches in the SW. Do you think that the way I have designed it to stop trading in absence of trend works well. Is there a better way to do it in your opinion, thanks, Edoardo
You might use this code to detect whether your are in a range or not:
//************************************************************************
// variables to be initialized once before being used
ONCE RangeBars = 50 //50 bars
ONCE RangePips = 20 //20 pips
// check if it's arnge or not
hh = highest[RangeBars](high) //highest price in the last bars
ll = lowest[RangeBars](low) //lowest price in the last bars
OutOfRange = (hh - ll) > (RangePips * pipsize) //set OUTOFRANGE if enough pips have moved in the last bars
//************************************************************************
IF OutOfRange is true you will know you are not within a range (according to your settings, 50, 20 or whatever you choose).
You may try to find out other and better ways to determine if an instrument is trending or not. With the help of the serach box you may scan this forum for, say, the words TREND or RANGE. You may even search the web and then translate to PRT any way you may find worth while spending some time.