//+------------------------------------------------------------------+5 q" ` y( Z, {/ q: S
//---- input parameters
extern double TakeProfit = 20;
extern double StopLoss = 30;$ }+ |+ s; K# }3 m
extern double Lots = 2;1 }7 V1 E# a/ X
extern double TrailingStop = 50;
extern int ShortEma = 5;
extern int LongEma = 60;; V* q( e' X% ^# m+ ~6 f
//+------------------------------------------------------------------+5 z6 l( X6 e: q# Q
//| expert initialization function |
//+------------------------------------------------------------------+
int init()" N8 }' y/ F- W% L; L2 Z
{
//----
//---- ~! h7 g6 [2 \ k, z1 x
return (0);
}8 a7 b2 D$ e9 N! r' P4 J6 T
//+------------------------------------------------------------------+# \* d: q( e O. o
//| expert deinitialization function |- g" p* @5 J2 d5 [8 s2 [1 s( i5 e5 E
//+------------------------------------------------------------------+
int deinit()
{) t* m, X* u5 N9 X1 i, B% F
//----# h8 }" B9 j* i/ @, U L* l2 d
//----
return (0);
}
//+------------------------------------------------------------------+
//| expert start function |6 S9 M" y( w$ u" f, t$ ^* b
//+------------------------------------------------------------------+; ^4 J0 A3 r+ F% d
int start(); m9 `& D9 }3 p5 T, D3 F
{
int cnt, ticket, total;
double SEma, LEma;( b; K8 I, x5 Y: \' ?
//----. v; g8 L/ G `
if (Bars < 100)0 q1 e+ H0 N& E" O! ]$ S
{" N6 [8 z2 O' G. T0 V* |0 Q
Print("bars less than 100");
return (0);! L- A1 t/ U( v9 \+ |: I+ n8 W$ S
}0 @# ]; B- L2 u
//----- [2 U; y$ e! {3 L
if (TakeProfit < 10)
{
Print("TakeProfit less than 10");5 J5 L6 h; }; [) m
return (0); // check TakeProfit
}
//----. }+ X% z! k" X1 l8 X& E' Q
SEma = iMA(NULL, 0, ShortEma, 0, MODE_EMA, PRICE_CLOSE, 0);' f) W" J8 t; B6 x. c& [! I
LEma = iMA(NULL, 0, LongEma, 0, MODE_EMA, PRICE_CLOSE, 0);- ?- R7 k0 t* P
//----
static int isCrossed = 0;' n! i; ]" R" f: G; S) `# z d8 B
isCrossed = Crossed(LEma, SEma);0 H( n& D+ N. ~# I8 {* k
//----
total = OrdersTotal();8 f$ o) F0 q! q f% ^% c% p+ E, `
if (total < 1)1 j2 e3 B" u1 ~8 g0 J2 C
{
if (isCrossed == 1) // 满足空仓条件,开空仓
{
ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Bid + StopLoss * Point,3 \5 ^3 J- l+ w# f
Bid - TakeProfit * Point, "EMA_CROSS", 12345, 0, Green);
if (ticket > 0)
{8 n( p' q( x$ Q. }
if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))1 q, h' F! f) Y+ N: Z8 I
Print("SELL order opened : ", OrderOpenPrice());
} else
Print("Error opening SELL order : ", GetLastError());
return (0);
}
if (isCrossed == 2) // 满足多仓条件,开多仓
{6 s* m+ z1 F& j4 X7 ^8 t# h
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask - StopLoss * Point,; |3 G) o5 P" v+ E/ d2 I
Ask + TakeProfit * Point, "EMA_CROSS", 12345, 0, Red);( S7 v# X3 M$ y
if (ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
Print("BUY order opened : ", OrderOpenPrice());
} else( X6 M* V! H2 V
Print("Error opening BUY order : ", GetLastError());2 ?& U/ a& n. {& C1 q3 j5 \
return (0);
}
return (0);
}
//---- 订单修改,实现动态止盈止损跟踪
for (cnt = 0; cnt < total; cnt++)
{3 n( ^, h/ x; _, ^. ~
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);: Q3 V" q; j. j
if (OrderType() <= OP_SELL && OrderSymbol() == Symbol())( L, I1 K9 P" J( u* x7 y( d
{' ~! g: C A: w
if (OrderType() == OP_SELL) // long position is opened2 q: @( O% B; g
{
// check for trailing stop
if (TrailingStop > 0)
{+ u& Z. o0 b( g- ~
if (Bid - OrderOpenPrice() > Point * TrailingStop)4 [2 O' r$ e+ I" q7 A& f8 F: T
{; c. A+ G; b( Q7 o+ b
if (OrderStopLoss() < Bid - Point * TrailingStop)
{& G% t- \1 b1 x5 r1 P; R7 K
OrderModify(OrderTicket(), OrderOpenPrice(),- q$ D0 `. p2 T* L6 U5 F5 w
Bid - Point * TrailingStop,
OrderTakeProfit(), 0, Green);4 Z# o& I% G3 S! B
return (0);0 F' w$ z8 u$ Q+ _. N% d
}% Y$ d ~/ C$ E5 ^; A) b
}
}/ Z2 k: P" c+ ^! C$ M# H- d
} else // go to short position* ^' w* t, R! H9 q, K, `
{+ e+ [$ s- z0 w6 H6 G" Y' Y
// check for trailing stop
if (TrailingStop > 0)
{
if ((OrderOpenPrice() - Ask) > (Point * TrailingStop))9 }+ }/ e/ t3 _- ~, e& A! I- S, G3 x
{( O; z2 R" j8 ~6 U
if ((OrderStopLoss() > (Ask + Point * TrailingStop)))# G5 D9 a# N* m% h% w/ d. x9 X
{4 d) b! v3 K, s/ E
OrderModify(OrderTicket(), OrderOpenPrice(),( D9 q9 R" X+ K, X' a7 }
Ask + Point * TrailingStop,
OrderTakeProfit(), 0, Red);
return (0);( e: [ @$ R: K9 D
}
}: I1 O& F$ A' l4 ^6 u4 E7 O; t y. r
}
}
}& n9 `/ E) _9 i. `
}
//----
return (0);
}
//+------------------------------------------------------------------+
// 移动平均线多空条件判断,, J( a9 @4 P/ g3 _, |
int Crossed(double line1, double line2)
{
static int last_direction = 0;. O/ X8 s- n5 ?$ f% y" i
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)4 h9 M$ v0 D/ y' |+ V: {
{
first_time = false;
return (0);
}1 P% R: K- \' F4 L! Q
//----
if (line1 > line2)
current_direction = 2; //up 多头市场 上穿做多
if (line1 < line2)
current_direction = 1; //down 空头市场 下穿做空
//----0 n% p# _6 Y/ G$ ~
if (current_direction != last_direction) //changed 多空改变 {. k2 p% X& X5 i* n' q+ q
last_direction = current_direction;$ E+ {# g C, v* N' Y
return (last_direction);
else return (0); //not changed
}
欢迎光临 518外汇网 (https://www.518waihui.com/) | Powered by Discuz! X3.5 |