//+------------------------------------------------------------------+
//---- input parameters
extern double TakeProfit = 20;/ } R' E& m. f" j/ T
extern double StopLoss = 30;
extern double Lots = 2;' ~ k6 X7 R1 X- m2 H9 ~3 m
extern double TrailingStop = 50;
extern int ShortEma = 5;
extern int LongEma = 60;: ?1 v% T% U7 q( D) ]2 s9 P
//+------------------------------------------------------------------+3 _1 o0 J* J+ A# v- y5 v6 o8 C5 L
//| expert initialization function |
//+------------------------------------------------------------------+
int init(): d3 P+ i. Q" y
{$ Y5 d: Z3 ?) F$ Q
//----
//----' [2 P8 c+ t0 ~1 t+ r; B; B
return (0);/ O) i: V* \+ K" H* @
}
//+------------------------------------------------------------------+) r; H1 d6 u# P) |+ G2 t5 b
//| expert deinitialization function |' j( t: @ @ ~* ?
//+------------------------------------------------------------------+
int deinit()
{
//----! g8 o) d* @ f( z0 |# k. o3 @( E
//----$ Z7 i. N9 ^/ x; k# k" X1 A
return (0);$ |: K' y8 C, S ?' |* m4 H4 J
}0 E6 N4 ^. _3 I6 m9 b# O5 k+ t+ O
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+: W) u9 Q9 z" ?. F
int start()
{6 I# w* e. g' N# u/ d( N6 |' x
int cnt, ticket, total;. {+ L% m0 |1 J. n
double SEma, LEma;
//----( a) S0 b+ r* @+ h: e2 b' R
if (Bars < 100), `" z4 j' P( x
{
Print("bars less than 100");
return (0);
}
//----
if (TakeProfit < 10)
{ _9 _; Y, W( [3 u Z
Print("TakeProfit less than 10");' N1 e& ~. G. n, w& n7 z
return (0); // check TakeProfit# _% u _* W# P. I$ z0 P# @1 v
}3 @' i# x# B G: J
//----4 v. i( j: q! k9 m
SEma = iMA(NULL, 0, ShortEma, 0, MODE_EMA, PRICE_CLOSE, 0);
LEma = iMA(NULL, 0, LongEma, 0, MODE_EMA, PRICE_CLOSE, 0);2 \% i6 f' f' e K9 _3 M
//----
static int isCrossed = 0;
isCrossed = Crossed(LEma, SEma);
//----
total = OrdersTotal();
if (total < 1): h& ~( x. ?- @* h( j d
{7 ^- u* x) ]4 u/ T
if (isCrossed == 1) // 满足空仓条件,开空仓
{
ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Bid + StopLoss * Point,
Bid - TakeProfit * Point, "EMA_CROSS", 12345, 0, Green);
if (ticket > 0)+ Y! }3 V1 \8 _/ N2 r
{1 R& c. |& B# b* q, |4 S
if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
Print("SELL order opened : ", OrderOpenPrice());
} else
Print("Error opening SELL order : ", GetLastError());
return (0);
}
if (isCrossed == 2) // 满足多仓条件,开多仓: \- K9 E5 [8 j) a" y2 r
{
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask - StopLoss * Point,+ Z3 [ |! [# H$ E% N8 ]
Ask + TakeProfit * Point, "EMA_CROSS", 12345, 0, Red);
if (ticket > 0)+ ]) L i' J4 {) ~( `, F$ q1 Q5 z% j
{
if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)), h q- l: E* E! ~0 f* g- O6 b
Print("BUY order opened : ", OrderOpenPrice());
} else
Print("Error opening BUY order : ", GetLastError());0 O# E8 j+ C4 Q0 [
return (0);* K% U0 d# }9 G7 Q8 T7 Z" G+ O
}
return (0);/ D# _& m5 l; D' J. p) m
}
//---- 订单修改,实现动态止盈止损跟踪0 e; C0 N/ z# ]' \+ ?# I* h8 H5 s
for (cnt = 0; cnt < total; cnt++)
{! q2 B! e& s. Y* l# I. U8 r
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderType() <= OP_SELL && OrderSymbol() == Symbol()): z! I3 ~# e) W
{
if (OrderType() == OP_SELL) // long position is opened
{
// check for trailing stop
if (TrailingStop > 0)" I# O- A1 h* n5 y& E
{
if (Bid - OrderOpenPrice() > Point * TrailingStop)
{) ^1 `0 q6 g' S* l ~* g( k4 h
if (OrderStopLoss() < Bid - Point * TrailingStop)
{" L1 i, F7 g* |/ Y x
OrderModify(OrderTicket(), OrderOpenPrice(),
Bid - Point * TrailingStop,5 ]) H, L2 N& E; O2 i/ M1 B
OrderTakeProfit(), 0, Green);! Y3 C1 b+ g0 s" f% k
return (0);1 B; X/ l. U$ y0 B$ c3 E2 X; H
}. P |/ w+ r1 I
}
}: X3 p9 H1 M( v$ l8 X- \$ X0 D
} else // go to short position
{
// check for trailing stop; r& @ j5 i* A7 F
if (TrailingStop > 0)
{6 @1 W( H5 \0 G& u2 z
if ((OrderOpenPrice() - Ask) > (Point * TrailingStop))% g, C! Y: K9 V" h( q/ |* C! ?
{
if ((OrderStopLoss() > (Ask + Point * TrailingStop)))
{
OrderModify(OrderTicket(), OrderOpenPrice(),; A2 v- Z Y) r
Ask + Point * TrailingStop,
OrderTakeProfit(), 0, Red); n4 o7 v" b9 c. B
return (0);* t; r; |3 {& e: g0 D* D
}
}
}
}9 y/ R9 `. u4 P+ G& [3 l6 t. I
}# u6 P9 O8 {( m
}$ L9 O* h: J6 j( n# N; |
//----9 L! z* F4 _, k& |/ j
return (0);
}: ?; A( T+ w- J% \
//+------------------------------------------------------------------+: Q- y( i1 p: d7 r3 |5 L2 B( y
// 移动平均线多空条件判断,
int Crossed(double line1, double line2)/ M# p+ v# b( w( z4 Y$ l: r
{
static int last_direction = 0;
static int current_direction = 0;
//Don't work in the first load, wait for the first cross!
static bool first_time = true;( Z5 ^4 l8 G' S* e6 W
if (first_time == true)
{
first_time = false;
return (0);! _9 H9 A) G0 B. |3 J1 V8 t
}% o! V- { v" q5 j, H" S9 t
//----' d) @/ M9 K9 K/ h8 T
if (line1 > line2)
current_direction = 2; //up 多头市场 上穿做多
if (line1 < line2)
current_direction = 1; //down 空头市场 下穿做空0 H6 x8 k5 X/ J
//----
if (current_direction != last_direction) //changed 多空改变 {
last_direction = current_direction;' _/ R6 f% b& e7 l) H/ C
return (last_direction);! s( K1 h# W4 G5 t
else return (0); //not changed, Z2 u8 V* B: |; U+ |
}
| 欢迎光临 518外汇网 (https://www.518waihui.com/) | Powered by Discuz! X3.5 |