Daydream01
Download scriptThis trading algorithm, titled "Daydream," is designed to automate trade entries and exits based on price action relative to a dynamic price channel. It is specifically structured to identify moments where the market breaks out of recent established boundaries, allowing it to capture trend reversals or extensions.
Core Trading Logic
The script operates by monitoring the highest and lowest prices reached over a specific period (the "Channel Period"). It uses these two points to create a boundary for the current market movement.
Entry Conditions:
- Buy Orders (Long): The system looks for instances where the current price closes below the lowest point of the recent channel. When this happens, it interprets the move as a signal to enter a "Buy" position, assuming a potential rebound or trend shift.
- Sell Orders (Short): Conversely, if the current price closes above the highest point of the recent channel, the system identifies a "Sell" signal, entering a short position to profit from a downward move.
Trade Management: Whenever a new signal is identified, the script automatically manages existing positions:
- If a Buy signal occurs while a Sell position is still open, it will close the Sell position before opening the new Buy.
- If a Sell signal occurs while a Buy position is still open, it will close the Buy position before opening the new Sell.
Profit Protection and Exit Strategy
Beyond simple entry signals, the script includes an automated "Trailing Profit" mechanism to help lock in gains as the market moves in your favor:
- Dynamic Target Adjustment: As the price moves, the script constantly recalculates the target profit level. For a Buy position, it attempts to keep the profit target at a fixed distance ahead of the current price; for a Sell position, it keeps it at a fixed distance below the current price.
- Automatic Execution: Once the market reaches this dynamically updated profit target, the script will automatically close the trade.
Key Operational Features
- Frequency Control: The script is programmed to ensure it only opens one trade per candle (time period), preventing it from entering multiple redundant positions during a single price fluctuation.
- Precision: It accounts for "Slippage," which helps manage the execution of orders in fast-moving markets where the exact requested price might change slightly between the signal and the actual execution.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| Daydream by Cothool |
//| Recommended: USD/JPY 1H |
//+------------------------------------------------------------------+
#define MAGIC_NUM 48213657
//+------------------------------------------------------------------+
extern double Lots = 1;
extern int ChannelPeriod = 25;
extern int Slippage = 3;
extern int TakeProfit = 15;
//+------------------------------------------------------------------+
double LastOrderTime = 0;
double CurrentDirection = 0;
double CurrentTakeProfitPrice = 0;
//+------------------------------------------------------------------+
void OpenLong()
{
if (Time[0] == LastOrderTime)
return;
if (CurrentDirection != 0)
return;
OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0,
"Daydream", MAGIC_NUM, 0, Blue);
LastOrderTime = Time[0];
CurrentDirection = 1;
}
//+------------------------------------------------------------------+
void OpenShort()
{
if (Time[0] == LastOrderTime)
return;
if (CurrentDirection != 0)
return;
OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0,
"Daydream", MAGIC_NUM, 0, Red);
LastOrderTime = Time[0];
CurrentDirection = -1;
}
//+------------------------------------------------------------------+
void CloseLong()
{
int i;
if (Time[0] == LastOrderTime)
return;
if (CurrentDirection != 1)
return;
for (i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() &&
OrderMagicNumber() == MAGIC_NUM && OrderType() == OP_BUY)
{
OrderClose(OrderTicket(), OrderLots(), Bid, 3, White);
LastOrderTime = Time[0];
CurrentDirection = 0;
}
}
}
//+------------------------------------------------------------------+
void CloseShort()
{
int i;
if (Time[0] == LastOrderTime)
return;
if (CurrentDirection != -1)
return;
for (i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() &&
OrderMagicNumber() == MAGIC_NUM && OrderType() == OP_SELL)
{
OrderClose(OrderTicket(), OrderLots(), Ask, 3, White);
LastOrderTime = Time[0];
CurrentDirection = 0;
}
}
}
//+------------------------------------------------------------------+
void start()
{
double HighestValue;
double LowestValue;
HighestValue = High[Highest(NULL, 0, MODE_HIGH, ChannelPeriod, 1)];
LowestValue = Low[Lowest(NULL, 0, MODE_LOW, ChannelPeriod, 1)];
// BUY
if (Close[0] < LowestValue)
{
CloseShort();
OpenLong();
CurrentTakeProfitPrice = Bid + TakeProfit * Point;
}
// SELL
if (Close[0] > HighestValue)
{
CloseLong();
OpenShort();
CurrentTakeProfitPrice = Ask - TakeProfit * Point;
}
// Trailing Profit Taking for Long Position
if (CurrentDirection == 1)
{
if (CurrentTakeProfitPrice > Bid + TakeProfit * Point)
CurrentTakeProfitPrice = Bid + TakeProfit * Point;
if (Bid >= CurrentTakeProfitPrice)
CloseLong();
}
// Trailing Profit Taking for Short Position
if (CurrentDirection == -1)
{
if (CurrentTakeProfitPrice < Ask - TakeProfit * Point)
CurrentTakeProfitPrice = Ask - TakeProfit * Point;
if (Ask <= CurrentTakeProfitPrice)
CloseShort();
}
}
//+------------------------------------------------------------------+
Profitability Reports
Expected payoff
Average expected payoff across the available backtests for each symbol.
Comments