It's a simple Expert Advisor written in MQL4 for MetaTrader 4.
The EA opens and manages long only trades and only one trade at a time.
A buy stop order is placed at the highest high of a specified number of previous candles with stoploss at the lowest low of another specified number of previous candles.
The Buy stop order's entry price and stop loss is modified at the close of every candle to match the current highest high and lowest low of the specified number of bars. After the trade has been opened, the stop loss level of the trade will be modified accordingly at the close of every candle.
The details for the code is given below:
Extern Variables (the user of the EA specifies them before running the EA)
X (integers like 5,10)
Y (integers like 5,10)
G (float variables like 0.001)
S (float variables like 5.7)
C1 = opening of a new bar.
C2 = Buy Stop Order Exists
C3 = open trade exists.
If C1=True, C2=True and C3=False, then Modify Buy stop order price to (the highest high of last X bars + Spread + G) and modify the Buy Order’s Stoploss to (the lowest low of last Y bars ??" G). Keep the existing lot size of the Buy stop order and don’t change it.
If C1=True,C2=False and C3=True, then modify the open trade’s stoploss to (the lowest low of the last Y bars ??" G).
If C1=true,C2=False and C3=False, then send a buy stop order at the price equal to (the highest high of last X bars + Spread + G) with Stoploss at (the lowest low of last Y bars ??" G). In this case the lot size will be equal to the extern variable S.
For all other cases, do nothing.
This operation should be run every time at the opening of a new candle. If you have more questions please ask me through the messaging system at rentacoder.