//+------------------------------------------------------------------+
//---- input parameters; J1 w. G+ G$ a3 k8 W/ C8 H
extern double TakeProfit = 20;
extern double StopLoss = 30;
extern double Lots = 2;
extern double TrailingStop = 50;
extern int ShortEma = 5;
extern int LongEma = 60;
//+------------------------------------------------------------------+- {3 ^1 W& P5 E, F5 z* s% w
//| expert initialization function |) J0 m9 x5 b$ }' {& `' d3 B; M* _
//+------------------------------------------------------------------+& `& a4 e i- b: n' l5 r* `
int init()* x+ b2 `. |, D2 i( i0 K8 {0 x
{
//----
//----! n; ^& h Q4 L, }' }0 J
return (0);$ @) D" U q O6 t5 g7 t6 S9 l( P
}
//+------------------------------------------------------------------+) \6 o+ D2 b2 Y$ \. {8 D- X
//| expert deinitialization function | ^, F% }5 X* d+ a' b+ g/ n; z
//+------------------------------------------------------------------+
int deinit()
{
//----3 X7 Q8 g- r, J+ g n
//----
return (0);% z( U& M$ t3 [) ~5 `: X0 D* _3 S
}* Z9 N+ d1 r: L% P% l
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()7 B4 c5 c4 W4 a H
{
int cnt, ticket, total;% o& q/ s# Y4 A1 ?
double SEma, LEma;
//----5 w5 F$ ~ @ u( h
if (Bars < 100)! z& i' M1 v. ^% F' s. S
{* {" ^; N/ ^4 F$ E& I6 D* x6 O
Print("bars less than 100");/ H i0 B4 J: T0 W! K
return (0);. c4 b8 s# \7 V% B, N+ X
}
//----* E+ F, {# e6 P
if (TakeProfit < 10)" N& w/ C+ D' Y: i
{
Print("TakeProfit less than 10");
return (0); // check TakeProfit
}, @( k9 _( Y1 m, Q
//---- [4 e- I- Q$ h2 f6 g
SEma = iMA(NULL, 0, ShortEma, 0, MODE_EMA, PRICE_CLOSE, 0); {- t7 s0 L0 x9 b- Z; H. \
LEma = iMA(NULL, 0, LongEma, 0, MODE_EMA, PRICE_CLOSE, 0);
//----5 h) x9 d* y. X, x3 J/ t ?
static int isCrossed = 0;
isCrossed = Crossed(LEma, SEma);
//----
total = OrdersTotal();/ {% N, `" ]0 x2 F, c* [
if (total < 1)! K; ], z. c, l" ~5 Z* a0 H6 u
{
if (isCrossed == 1) // 满足空仓条件,开空仓+ [9 m' k7 z& E3 R5 [- ^
{7 E9 Z* t! u, m. v2 n8 G& W6 w3 ]
ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Bid + StopLoss * Point,
Bid - TakeProfit * Point, "EMA_CROSS", 12345, 0, Green);1 e' D# f' z0 A, |
if (ticket > 0)5 Z i, }% a* N: q$ L( [# }
{
if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
Print("SELL order opened : ", OrderOpenPrice());
} else
Print("Error opening SELL order : ", GetLastError());& |+ F! G( F8 ~: T' i! ^# J; K
return (0);
}8 w: z6 @0 @+ d# w9 }' W
if (isCrossed == 2) // 满足多仓条件,开多仓
{
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask - StopLoss * Point,5 J' N. k3 A3 I! H& y
Ask + TakeProfit * Point, "EMA_CROSS", 12345, 0, Red);
if (ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
Print("BUY order opened : ", OrderOpenPrice());
} else( u m" g% X% c" o; H+ M& A) @
Print("Error opening BUY order : ", GetLastError());4 o0 g A& s4 T* V3 u
return (0);4 u/ g& Y6 e9 B! h8 ~: J
}
return (0); Z3 S9 `; c" _( G
}
//---- 订单修改,实现动态止盈止损跟踪
for (cnt = 0; cnt < total; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderType() <= OP_SELL && OrderSymbol() == Symbol())
{5 J, ]$ A& s& B5 l# w$ O
if (OrderType() == OP_SELL) // long position is opened# Y$ M# z5 L' o* Q- B
{
// check for trailing stop! S: G# H0 l+ f- f
if (TrailingStop > 0)6 v' a3 j' R' Y f+ d
{7 j# \' j2 ?3 N0 G4 _5 S& D3 `
if (Bid - OrderOpenPrice() > Point * TrailingStop)
{
if (OrderStopLoss() < Bid - Point * TrailingStop)3 F4 }+ }0 {- B* `
{) Q5 f7 u& g: v
OrderModify(OrderTicket(), OrderOpenPrice(),& F! G- h: q3 L. W R1 C
Bid - Point * TrailingStop,
OrderTakeProfit(), 0, Green);
return (0);# H1 v7 Y$ v, Y$ o% K$ U
}) M( g v; {8 ^0 P& G( T) Y
}! o& ?) k0 i/ V+ k* T; S i5 x/ H
}8 ]. w0 B' A: u6 D7 d" C
} else // go to short position
{2 v4 b4 { x1 q' x# h7 s
// check for trailing stop
if (TrailingStop > 0) O1 ? d: g) |
{
if ((OrderOpenPrice() - Ask) > (Point * TrailingStop))) D8 e# V8 W2 s( g0 S$ r$ z" c/ ?, C
{
if ((OrderStopLoss() > (Ask + Point * TrailingStop)))8 {8 w3 f3 E! ?+ j6 a5 M. g" @/ Y
{2 a2 e8 e* |% \) g. l
OrderModify(OrderTicket(), OrderOpenPrice(),6 E8 Q- f$ l# ]" H# ?7 ]( } t
Ask + Point * TrailingStop,
OrderTakeProfit(), 0, Red);4 @2 L t0 _1 G4 p5 I8 ]( P
return (0);; ~# \7 {0 x$ t m4 S# s6 z/ `
}
}
}1 }. X& q% d/ G* e' h
}
}
}$ f. M# J4 g" S7 j
//----* [* z& i! }* g8 e2 m' d6 f9 b& b
return (0);
}- I. a* Q% M* t% F+ k, b
//+------------------------------------------------------------------+; H" |8 c4 ^! ^/ D8 r
// 移动平均线多空条件判断,
int Crossed(double line1, double line2)& m0 U! [2 U& U# k9 ~
{
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;
if (first_time == true)# w0 c: i6 \3 c/ {: B5 y) f
{
first_time = false;, q& b- N! m) B o5 [/ `
return (0);& Y3 ~. D; W, l1 p6 b# U1 g
}8 L" [) k7 \1 N! Y& P
//----
if (line1 > line2)/ _, J/ ~3 C' x
current_direction = 2; //up 多头市场 上穿做多
if (line1 < line2)3 M- v. c" U' Z: Y
current_direction = 1; //down 空头市场 下穿做空
//----
if (current_direction != last_direction) //changed 多空改变 {( R% u4 z2 k# h7 J
last_direction = current_direction;% T+ W, p% {7 U
return (last_direction);2 i( }; j! g5 Y; v4 S
else return (0); //not changed
}
欢迎光临 518财经网 (https://www.518waihui.com/) | Powered by Discuz! X3.5 |