Orders Execution
Miscellaneous
0
Views
0
Downloads
0
Favorites
#AETicks
//+------------------------------------------------------------------+
//| _AEPeriods.mq4 |
//| JDP |
//| |
//+------------------------------------------------------------------+
#property copyright "JDP"
#property link ""
extern bool SetTP=false;
extern bool SetSL=false;
extern bool DoData=false;
extern bool DoCloseAll=false;
extern double Lots=0.1;
extern double SL=0.001;
extern double TP=0.0001;
extern int magic = 1000;
int LastAction = 0;
int LastTm=0;
double Balance = 0;
double buyprice,sellprice,buyprofit,sellprofit,buylots,selllots;
int buycnt = 0,sellcnt = 0;
int ticket;
double V;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//Print(Digits);
Open_Trades();
Get_Signal();
Do_Trade();
Set_TP();
Comment("#Ticks (" + DoubleToStr(V,4) + " : " + DoubleToStr(buyprice,4) + " : " + DoubleToStr(buylots,2) + ") (" + DoubleToStr(sellprice,4) + " : " + DoubleToStr(selllots,2) + ") (" + DoubleToStr(buyprofit,2) + " : " + DoubleToStr(sellprofit,2) + " : " + DoubleToStr(buyprofit+sellprofit,2) + ")");
if (DoCloseAll==True) {
Close_All();
}
if (DoData==true) {
if (LastTm < Time[0]) {
Do_Data();
LastTm = Time[0];
}
}
return(0);
}
void Do_Trade() {
double VTP,VSL;
int Ticket = 0;
if (V==1) {
if (LastAction!=1) {
//Print(DoubleToStr(Ask,5)," : ",DoubleToStr(VSL,5)," : ",DoubleToStr(VTP,5));
if (buylots==0) {
Ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,NULL,NULL,"",magic,0,Blue);
} else {
Ticket = OrderSend(Symbol(),OP_BUY,buylots,Ask,3,NULL,NULL,"",magic,0,Blue);
}
//if (Ticket>0) {
//
// if (OrderSelect(Ticket,SELECT_BY_TICKET)==true) {
// VTP = OrderOpenPrice() + TP;
// VSL = OrderOpenPrice() - SL;
// VTP = NormalizeDouble(VTP,5);
// VSL = NormalizeDouble(VSL,5);
// OrderModify(Ticket,OrderOpenPrice(),VSL,VTP,0);
// }
//}
LastAction = 1;
}
} else {
if (V==-1) {
if (LastAction!=-1) {
//Print(DoubleToStr(Bid,5)," : ",DoubleToStr(VSL,5)," : ",DoubleToStr(VTP,5));
if (selllots==0) {
Ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,NULL,NULL,"",magic,0,Red);
} else {
Ticket = OrderSend(Symbol(),OP_SELL,selllots,Bid,3,NULL,NULL,"",magic,0,Red);
}
//if (Ticket>0) {
// if (OrderSelect(Ticket,SELECT_BY_TICKET)==true) {
// VTP = OrderOpenPrice() - TP;
// VSL = OrderOpenPrice() + SL;
// VTP = NormalizeDouble(VTP,5);
// VSL = NormalizeDouble(VSL,5);
// OrderModify(Ticket,OrderOpenPrice(),VSL,VTP,0);
// }
//}
LastAction = -1;
}
} else {
LastAction = 0;
}
}
}
void Get_Signal() {
V = GlobalVariableGet("TV");
}
int Set_TP() {
int i = 0,total = OrdersTotal();
int cTotal = 0;
int cTicket,cnt,tcnt;
bool busy;
double VTP;
busy = true;
tcnt = total;
for (i=0;i<total;i++) {
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) {
if (OrderMagicNumber()==magic) {
if (Symbol()==OrderSymbol()) {
if (OrderType()==OP_BUY) {
VTP = buyprice + TP;
VTP = NormalizeDouble(VTP,Digits);
if (OrderTakeProfit()!=VTP) {
//Print(cTicket);
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),VTP,0);
}
}
if (OrderType()==OP_SELL) {
VTP = sellprice - TP;
VTP = NormalizeDouble(VTP,Digits);
if (OrderTakeProfit()!=VTP) {
//Print(cTicket);
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),VTP,0);
}
}
}
}
}
}
return(cTotal);
}
int Set_SL() {
int i = 0,total = OrdersTotal();
int cTotal = 0;
int cTicket,cnt,tcnt;
bool busy;
busy = true;
tcnt = total;
for (i=0;i<total;i++) {
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) {
if (Symbol()==OrderSymbol()) {
RefreshRates();
if (OrderStopLoss()!=SL) {
OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0);
}
}
}
}
return(cTotal);
}
int Do_Data() {
double cIP;
int handle=FileOpen(Symbol() + "_" + Period() + ".csv", FILE_CSV|FILE_READ|FILE_WRITE, ',');
cIP = iCustom(Symbol(),0,"#cIPTrendv2",Symbol(),1,0,1);
//Print(handle);
if(handle>0) {
FileSeek(handle, 0, SEEK_END);
FileWrite(handle,Symbol(), TimeToStr(Time[1]),Open[1],Close[1],High[1],Low[1],cIP);
FileClose(handle);
}
}
int Close_All() {
int i = 0,total = OrdersTotal();
int cTotal = 0;
int cTicket,cnt,tcnt;
bool busy;
busy = true;
tcnt = total;
while (busy==true) {
for (i=0;i<total;i++) {
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) {
//if (Symbol()==OrderSymbol()) {
cTicket = OrderTicket();
cTotal = cTotal + 1;
//}
}
}
if (cTicket>0) {
if (OrderSelect(cTicket,SELECT_BY_TICKET,MODE_TRADES)==true) {
RefreshRates();
if (OrderType()==OP_BUY) {
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),10);
}
if (OrderType()==OP_SELL) {
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),10);
}
if ((OrderType() == OP_BUYLIMIT) || (OrderType() == OP_SELLLIMIT) || (OrderType()==OP_BUYSTOP) || (OrderType()==OP_SELLSTOP)) {
//OrderDelete(OrderTicket());
DeleteOrder(OrderTicket());
busy = true;
i = OrdersTotal();
}
}
} else {
busy = false;
}
cnt = cnt + 1;
if (cnt >= tcnt) {
busy = false;
}
}
return(cTotal);
}
void DeleteOrder(int tTicket) {
int j;
for (j=0;j< ObjectsTotal();j++) {
if (StringFind(ObjectName(j),DoubleToStr(tTicket,0),0) >= 0) {
ObjectDelete(ObjectName(j));
}
}
OrderDelete(tTicket);
}
double Get_Lots(double tlots,string CurPair) {
double lots = 0;
int i;
double base = 0;
double cPrice = 0;
double tPrice = 0;
double cPers = 0;
string s1,s2;
s1 = StringSubstr(CurPair,3,3);
if (s1=="USD") {
base = 1;
} else {
s2 = s1 + "USDm";
tPrice = MarketInfo(s2,MODE_BID);
if (tPrice==0.0) {
s2 = "USD" + s1 + "m";
tPrice = MarketInfo(s2,MODE_BID);
if (tPrice>0) {
base = 1/tPrice;
}
} else {
base = tPrice;
}
//Print(s2 + " : " + tPrice + " : " + base);
}
}
void Open_Trades() {
double tmp2;
int pPoint,itmp;
int total=OrdersTotal();
buyprice = 0;
sellprice = 0;
buyprofit = 0;
sellprofit = 0;
buylots = 0;
selllots = 0;
buycnt = 0;
sellcnt = 0;
for (int pos=0;pos<total;pos++) {
if (OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)==true) {
if (OrderSymbol() == Symbol()) {
//if (OrderMagicNumber() == magic) {
if (OrderType()== OP_BUY) {
buylots = buylots + OrderLots();
buyprice = buyprice + (OrderOpenPrice()* OrderLots());
buyprofit = buyprofit + OrderProfit();
buycnt = buycnt + 1;
}
if (OrderType()== OP_SELL) {
selllots = selllots + OrderLots();
sellprice = sellprice + (OrderOpenPrice()* OrderLots());
sellprofit = sellprofit + OrderProfit();
sellcnt = sellcnt + 1;
if (OrderMagicNumber() > 0) {
ticket = OrderTicket();
}
}
//}
}
}
}
if (buylots > 0) {
tmp2 = 1/buylots;
buyprice = buyprice * tmp2;
if (ObjectFind("LBuy") != 0) {
ObjectCreate("LBuy",OBJ_HLINE,0,Time[0],buyprice);
ObjectSet("LBuy",OBJPROP_COLOR,Blue);
} else {
ObjectSet("LBuy",OBJPROP_PRICE1,buyprice);
}
} else {
if (ObjectFind("LBuy") >= 0) {
ObjectDelete("LBuy");
}
}
if (selllots > 0) {
tmp2 = 1/selllots;
sellprice = sellprice * tmp2;
if (ObjectFind("LSell") != 0) {
ObjectCreate("LSell",OBJ_HLINE,0,Time[0],sellprice);
ObjectSet("LSell",OBJPROP_COLOR,Red);
} else {
ObjectSet("LSell",OBJPROP_PRICE1,sellprice);
}
} else {
if (ObjectFind("LSell") >= 0) {
ObjectDelete("LSell");
}
}
if (Ask >= 100) {pPoint = 100;} else {pPoint = 10000;}
buyprice = buyprice * pPoint;
itmp = buyprice;
buyprice = itmp;
buyprice = buyprice / pPoint;
sellprice = sellprice * pPoint;
itmp = sellprice;
sellprice = itmp;
sellprice = sellprice / pPoint;
}
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
---