LevelsTrade_2

Author: Candid
Price Data Components
Series array that contains open time of each bar
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
LevelsTrade_2
//+------------------------------------------------------------------+
//|                                               LevelsTrade_v2.mq4 |
//|                                         Copyright © 2010, Candid |
//|                                          http://candid.110mb.com |
//+------------------------------------------------------------------+
#property copyright "Candid"
#property link      "http://candid.110mb.com"

extern int LifeTime = 60;
extern int StopLevel = 20;
extern int Delta = 0;

bool StartEA;
double HalfPoint;
int LT;
double DLvl;
double RecDLvl;
double dStopLevel;
double dDelta;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {
  LT = LifeTime*60;
  StartEA = true;
  ObjectCreate("TopLevel",OBJ_HLINE,0,0,0);
  ObjectCreate("BotLevel",OBJ_HLINE,0,0,0);
  return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {
  ObjectDelete("TopLevel");
  ObjectDelete("BotLevel");
  return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() {
  if (StartEA) {
    StartEA = false;
    HalfPoint = 0.5*Point;
    DLvl = 1000*Point;
    RecDLvl = 1.0/DLvl;
    dStopLevel = StopLevel*Point;
    dDelta = Delta*Point;
    static bool WasUpCross = false;
    static bool WasDownCross = false;
    static bool TopJump = false;
    static double PreTopLvl;
    static double PreBotLvl;
    PreTopLvl = GetTopLevel(Bid);
    PreBotLvl = GetBotLevel(Bid);
Print("DLvl=",DoubleToStr(DLvl,Digits),", RecDLvl=",DoubleToStr(RecDLvl,Digits),", dStopLevel=",DoubleToStr(dStopLevel,Digits),", dDelta=",DoubleToStr(dDelta,Digits));    
  }
  double TopLvl = GetTopLevel(Bid);
  double BotLvl = GetBotLevel(Bid);

  ObjectSet("TopLevel",OBJPROP_PRICE1,TopLvl);
  ObjectSet("TopLevel",OBJPROP_WIDTH,2);
  ObjectSet("TopLevel",OBJPROP_COLOR,Blue);
  ObjectSet("BotLevel",OBJPROP_PRICE1,BotLvl);
  ObjectSet("BotLevel",OBJPROP_WIDTH,2);
  ObjectSet("BotLevel",OBJPROP_COLOR,Yellow);
  WindowRedraw();

  if (PreBotLvl-BotLvl > HalfPoint) {
    if (!TopJump) {
      WasDownCross = true;
      WasUpCross = false;
    }
  }
  PreBotLvl = BotLvl;
  if (TopJump) {
    if (Bid-PreTopLvl > HalfPoint) {
      WasDownCross = false;
      WasUpCross = true;
      PreTopLvl = TopLvl;
      TopJump = false;
    } else {
      if (PreTopLvl-Bid > HalfPoint) TopJump = false;
    }
  }
  if (TopLvl-PreTopLvl > HalfPoint) {
    if (Bid-PreTopLvl > HalfPoint) {
      WasDownCross = false;
      WasUpCross = true;
      PreTopLvl = TopLvl;
      TopJump = false;
    } else TopJump = true;
  }
  if (PreTopLvl-TopLvl > HalfPoint) PreTopLvl = TopLvl;

// Áëîê ñîïðîâîæäåíèÿ îðäåðîâ
  double LvlPrice;
  bool IsStopOrder = false;
  for(int ord=OrdersTotal();ord>=0;ord--) {
    if(OrderSelect(ord,SELECT_BY_POS,MODE_TRADES)==false) continue;
    switch(OrderType()) {
      case OP_BUY: 
        if (TimeCurrent()-OrderOpenTime() > LT) OrderClose(OrderTicket(),OrderLots(),Bid,5,Blue);
        break;
      case OP_SELL: 
        if (TimeCurrent()-OrderOpenTime() > LT) OrderClose(OrderTicket(),OrderLots(),Ask,5,Yellow);
        break;
      case OP_BUYSTOP: 
        IsStopOrder = true;
        LvlPrice = GetBuyStopLevel(TopLvl,dStopLevel);
        if (OrderOpenPrice()-LvlPrice > HalfPoint) OrderModify(OrderTicket(),NormalizeDouble(LvlPrice,Digits),0,0,0,Blue);
        break;
      case OP_SELLSTOP: 
        IsStopOrder = true;
        LvlPrice = GetSellStopLevel(BotLvl,dStopLevel);
        if (LvlPrice-OrderOpenPrice() > HalfPoint) OrderModify(OrderTicket(),NormalizeDouble(LvlPrice,Digits),0,0,0,Yellow);
        break;
    }  //  switch(OrderType())
  }  //  for(int ord=OrdersTotal();ord>=0;ord--)
  
//  Áëîê óñòàíîâêè îðäåðîâ  
  static int PreMnt;
  int Mnt = TimeMinute(TimeCurrent());
  if (Mnt != PreMnt) {
    PreMnt = Mnt;
    if (!IsStopOrder && WasDownCross) {
      if (TopLvl+HalfPoint > Bid+dStopLevel && Bid-HalfPoint > BotLvl) {
        OrderSend(Symbol(),OP_BUYSTOP,Lots(),NormalizeDouble(TopLvl+Ask-Bid,Digits),5,0,0,NULL,0,0,Blue);
        WasDownCross = false;
      }
    }
    if (!IsStopOrder && WasUpCross) {
      if (BotLvl+dStopLevel < Bid+HalfPoint) {
        OrderSend(Symbol(),OP_SELLSTOP,Lots(),NormalizeDouble(BotLvl,Digits),5,0,0,NULL,0,0,Yellow);
        WasUpCross = false;
      }
    }
  }
  return(0);
}
//+------------------------------------------------------------------+
double Lots() {
  return(0.1);
}
//+------------------------------------------------------------------+
double GetTopLevel(double Price) {
  int ILvl = (Price-dDelta+HalfPoint)*RecDLvl;
  return (DLvl*(ILvl+1)+dDelta);
}
//+------------------------------------------------------------------+
double GetBotLevel(double Price) {
  int ILvl = (Price-dDelta+HalfPoint)*RecDLvl;
  return (DLvl*ILvl+dDelta);
}
//+------------------------------------------------------------------+
double GetBuyStopLevel(double BuyStopLvl, double StopLvl) {
  if (Bid+StopLvl-HalfPoint > BuyStopLvl) BuyStopLvl += DLvl;
  return (BuyStopLvl+Ask-Bid);
}
//+------------------------------------------------------------------+
double GetSellStopLevel(double SellStopLvl, double StopLvl) {
  if (SellStopLvl > Bid-StopLvl+HalfPoint) SellStopLvl -= DLvl ;
  return (SellStopLvl);
}
//+------------------------------------------------------------------+

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 ---