Price Data Components
Orders Execution
0
Views
0
Downloads
0
Favorites
cm_tral_stop_orders
//+------------------------------------------------------------------+
//| òðàë ñòîï îðäåðà ïî ñâå÷àì.mq4 |
//| Copyright © 2016, Vladimir Hlystov |
//| cmillion@narod.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016, Vladimir Hlystov"
#property link "cmillion@narod.ru"
#property version "1.00"
#property strict
#property show_inputs
//+------------------------------------------------------------------+
#property description "Ñêðèïò âûñòàâëÿåò ñòîï îðäåð íà òî ìåñòî êóäà åãî áðîñèëè èëè åñëè áëèæå äîïóñòèìîãî, òî íà áëèæàéøèé ýêñòðåìóì ñâå÷è"
#property description "BuyStop, åñëè êèíóëè Âûøå öåíû è SellStop åñëè íèæå"
#property description "Äàëåå ñêðèïò áóäåò òðàëèòü ýòîò îðäåð ïî ýêñòðåìóìàì ñâå÷åé äî ñðàáàòûâàíèÿ> ïîñëå ÷åãî çàêîí÷èò ñâîþ ðàáîòó."
#property description "Åñëè íå óêàçàí ëîò â ïàðàìåòðå Lot, òî ñêðèïò ñàì âû÷èñëÿåò ëîò êàê ðàçíèöó ñóììû ëîòîâ íàïðàâëåíèé (ëîê)"
//+------------------------------------------------------------------+
enum tf
{
m1=1, // M1
m5=5, // M5
m15=15, // M15
m30=30, // M30
h1=60, // H1
h4=240, // H4
d1=1440, // D1
t=0, // òåêóùèé
};
//--------------------------------------------------------------------*/
input tf TF = 0; // òàéìôðåéì
extern double Lot = 0.03; //ëîò, åñëè 0 òî ñêðèïò ñàì ðàññ÷èòûâàåò ëîê
extern int Delta = 10; //ìèíèìàëüíîå ðàññòîÿíèå îò öåíû
extern int Magic = 100; //óíèêàëüíûé íîìåð îðäåðà
//--------------------------------------------------------------------
void OnStart()
{
int i,OT,TicketBuyStop,TicketSellStop;
double LB=0,LS=0,PriceBuyStop=0,PriceSellStop=0;
if (Lot==0)
{
for (i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (OrderSymbol()==Symbol())
{
OT = OrderType();
if (OT==OP_BUY)
{
LB=OrderLots();
}
if (OT==OP_SELL)
{
LS=OrderLots();
}
}
}
}
}
//---
double StopLevel;
double Price = NormalizeDouble(WindowPriceOnDropped(),Digits);
while(!IsStopped())
{
StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL)*Point; if (StopLevel<Delta*Point) StopLevel=Delta*Point;
if(Price>=Ask)
{
if (Lot==0) Lot = LS-LB;
if (Lot<0) {Comment("Ëîê îðäåð â äàííîì íàïðàâëåíèè íå íóæåí, ñêðèïò çàêîí÷èë ñâîþ ðàáîòó");return;}
if (Price<NormalizeDouble(Ask+StopLevel,Digits))
{
for (i=1; i<100; i++)
{
Price=NormalizeDouble(iHigh(NULL,TF,i),Digits);
if (Price<NormalizeDouble(Ask+StopLevel,Digits)) continue;
else break;
}
if (Price<NormalizeDouble(Ask+StopLevel,Digits)) Price = NormalizeDouble(Ask+StopLevel,Digits);
}
if (OrderSend(Symbol(),OP_BUYSTOP,Lot,Price,0,0,0,"cmillion",Magic,0,CLR_NONE)==-1) Comment("Error ",GetLastError()," OrderSend BUYSTOP ->",DoubleToStr(Price,Digits)," Low ",StrPer(TF));
else {Comment("Send Order BUYSTOP->",DoubleToStr(Price,Digits)," High ",StrPer(TF));break;}
}
if(Price<=Bid)
{
if (Lot==0) Lot = LB-LS;
if (Lot<0) {Comment("Ëîê îðäåð â äàííîì íàïðàâëåíèè íå íóæåí, ñêðèïò çàêîí÷èë ñâîþ ðàáîòó");return;}
if (Price>NormalizeDouble(Bid-StopLevel,Digits))
{
for (i=1; i<100; i++)
{
Price=NormalizeDouble(iLow(NULL,TF,i),Digits);
if (Price>NormalizeDouble(Bid-StopLevel,Digits)) continue;
else break;
}
if (Price>NormalizeDouble(Bid-StopLevel,Digits)) Price = NormalizeDouble(Bid-StopLevel,Digits);
}
if (OrderSend(Symbol(),OP_SELLSTOP,Lot,Price,0,0,0,"cmillion",Magic,0,CLR_NONE)==-1) Comment("Error ",GetLastError()," OrderSend SELLSTOP ->",DoubleToStr(Price,Digits)," Low ",StrPer(TF));
else {Comment("Send Order SELLSTOP->",DoubleToStr(Price,Digits)," Low ",StrPer(TF));break;}
}
Sleep(500);
RefreshRates();
}
//---
double PriceB=0,PriceS=0;
while(!IsStopped())
{
StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL)*Point; if (StopLevel<Delta*Point) StopLevel=Delta*Point;
for (i=1; i<100; i++)
{
PriceB=NormalizeDouble(iHigh(NULL,TF,i),Digits);
if (PriceB<NormalizeDouble(Ask+StopLevel,Digits)) continue;
else break;
}
if (PriceB<NormalizeDouble(Ask+StopLevel,Digits)) PriceB = NormalizeDouble(Ask+StopLevel,Digits);
//---
for (i=1; i<100; i++)
{
PriceS=NormalizeDouble(iLow(NULL,TF,i),Digits);
if (PriceS>NormalizeDouble(Bid-StopLevel,Digits)) continue;
else break;
}
if (PriceS>NormalizeDouble(Bid-StopLevel,Digits)) PriceS = NormalizeDouble(Bid-StopLevel,Digits);
//---
TicketBuyStop=0;TicketSellStop=0;
for (i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (OrderSymbol()==Symbol() && Magic==OrderMagicNumber())
{
OT = OrderType();
if (OT==OP_BUYSTOP)
{
TicketBuyStop=OrderTicket();
PriceBuyStop = NormalizeDouble(OrderOpenPrice(),Digits);
if (PriceBuyStop > PriceB)
{
if (!OrderModify(TicketBuyStop,PriceB,0,0,0,clrMagenta)) Comment("Error ",GetLastError()," Order Modify BuyStop ",DoubleToStr(PriceBuyStop,Digits),"->",DoubleToStr(PriceB,Digits));
else Comment("Order ",TicketBuyStop," BuyStop Modify ",DoubleToStr(PriceBuyStop,Digits),"->",DoubleToStr(PriceB,Digits)," High ",StrPer(TF));
}
}
if (OT==OP_SELLSTOP)
{
TicketSellStop=OrderTicket();
PriceSellStop = NormalizeDouble(OrderOpenPrice(),Digits);
if (PriceSellStop < PriceS)
{
if (!OrderModify(TicketSellStop,PriceS,0,0,0,clrAqua)) Comment("Error ",GetLastError()," Order Modify SellStop ",DoubleToStr(PriceSellStop,Digits),"->",DoubleToStr(PriceS,Digits));
else Comment("Order ",TicketSellStop," SellStop Modify ",DoubleToStr(PriceSellStop,Digits),"->",DoubleToStr(PriceS,Digits)," Low ",StrPer(TF));
}
}
}
}
}
Sleep(500);
if (TicketBuyStop+TicketSellStop==0) {Comment("Ñêðèïò çàêîí÷èë ñâîþ ðàáîòó");return;}
RefreshRates();
}
}
//+------------------------------------------------------------------+
string StrPer(int per)
{
if (per == 0) per=Period();
if (per == 1) return("M1");
if (per == 5) return("M5");
if (per == 15) return("M15");
if (per == 30) return("M30");
if (per == 60) return("H1");
if (per == 240) return("H4");
if (per == 1440) return("D1");
if (per == 10080) return("W1");
if (per == 43200) return("MN1");
return("îøèáêà ïåðèîäà "+IntegerToString(per));
}
//+------------------------------------------------------------------+
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---