Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open ordersIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
Daydream01
/*-----------------------------+
|			       |
| 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();
   }
}
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---