EA8-SAR止损系统

extern double 手数 = 0.1;
extern int 四小时止损 = 30;
extern int 日线止损 = 60;
extern int 滑点 = 3;
extern int 均线周期 = 208;
extern int KD高分界线 = 55;
extern int KD低分界线 = 45;
extern int 魔术号码 = 1314;
int 最后价1 = 0;
int 最后价2 = 0;
int init()
{
ObjectCreate("", OBJ_LABEL, 0, 0, 0);
ObjectSetText("", "海豚小弟", 10, "宋体", SteelBlue);
ObjectSet("", OBJPROP_XDISTANCE, 10);
ObjectSet("", OBJPROP_YDISTANCE, 20);
ObjectCreate("", OBJ_LABEL, 0, 0, 0);
ObjectSetText("", "12345", 10, "宋体", SteelBlue);
ObjectSet("", OBJPROP_XDISTANCE, 10);
ObjectSet("", OBJPROP_YDISTANCE, 40);
return(0);
}
int deinit()
{
GetLastError();
return(0);
}
int start()
{
double 均线,
KDJ主0,
KDJ信0,
KDJ主1,
KDJ信1;
均线 = iMA(Symbol(), 0, 均线周期, 0, MODE_EMA, PRICE_MEDIAN, 1);
KDJ主0 = iStochastic(Symbol(), 0, 80, 5, 3, MODE_SMA, 0, MODE_MAIN, 1);
KDJ信0 = iStochastic(Symbol(), 0, 80, 5, 3, MODE_SMA, 0, MODE_SIGNAL, 1);
KDJ主1 = iStochastic(Symbol(), 0, 80, 5, 3, MODE_SMA, 0, MODE_MAIN, 2);
KDJ信1 = iStochastic(Symbol(), 0, 80, 5, 3, MODE_SMA, 0, MODE_SIGNAL, 2);
if ((High[1]+Low[1])/2>均线 && KDJ主0>KDJ信0 && KDJ信0<=KD低分界线 && Time[0]>最后价1)
{
if (KDJ主1< KDJ信1)
{
OrderSend(Symbol(),
OP_BUY,
手数,
Ask,
滑点,
获得止损子(OP_BUY),
0,
"",
魔术号码,
0,
Blue);
最后价1 = Time[0];
return (0);
}
}
else if ((High[1]+Low[1])/2<均线 && KDJ主0=KD高分界线 && Time[0]>最后价1)
{
if (KDJ主1>KDJ信1)
{
OrderSend(Symbol(),
OP_SELL, // 开卖
手数,
Bid,
滑点,
获得止损子(OP_SELL),
0,
"",
魔术号码,
0,
Red);
最后价1 = Time[0];
return (0);
}
}
if (单总数子(魔术号码)>0)
{
if (Time[0]>最后价2)
{
跟踪止损子(魔术号码);
最后价2 = Time[0];
}
for (int i=0; i=80 && KDJ主1>KDJ信1)
OrderClose(OrderTicket(),
OrderLots(),
Bid,
滑点,
Pink);
}
if (OrderType()==OP_SELL)
{
if (KDJ主0>KDJ信0 && KDJ信0<=20 && KDJ主1<KDJ信1)
OrderClose(OrderTicket(),
OrderLots(),
Ask,
滑点,
Pink);
}
}
return(0);
}
int 单总数子(int 魔术号)
{
int 总数=0;
for (int i=0; i<OrdersTotal(); i++){
OrderSelect(i,SELECT_BY_POS);
if (OrderMagicNumber()==魔术号){
总数++;
}
}
return(总数);
}
double 获得止损子(int 买卖类型)
{
int 默认止损;
if (Period()==PERIOD_H4) 默认止损=四小时止损;
else 默认止损=日线止损;
if (买卖类型==OP_BUY)
{
if (Low[2]默认止损Point) return (Low[2]); else if (Ask>(Low[1]+默认止损Point))
return (Low[1]);
else
return (NormalizeDouble(Ask-默认止损*Point, Digits));
}
else if (买卖类型==OP_SELL)
{
if (High[2]>High[1] && (High[2]-Bid)>默认止损Point) return (High[2]); else if ((High[1]-Bid)>默认止损Point)
return (High[1]);
else
return (NormalizeDouble(Bid+默认止损*Point, Digits));
}
}
void 跟踪止损子(int 魔术号){
for (int k=0; kiSAR(Symbol(), 0, 0.02, 0.2, 1)) {
if (!OrderModify(OrderTicket(),
OrderOpenPrice(),
iSAR(Symbol(), 0, 0.02, 0.2, 1),
0,
0,
Blue)){
Print("Buy跟踪止损修改出错 :"+GetLastError());
}
}
if ( OrderType()==OP_SELL
&& OrderStopLoss()>iSAR(Symbol(), 0, 0.02, 0.2, 1)
&& Bid<iSAR(Symbol(), 0, 0.02, 0.2, 1)) {
if (!OrderModify(OrderTicket(),
OrderOpenPrice(),
iSAR(Symbol(), 0, 0.02, 0.2, 1),
0,
0,
Red)){
Print("Sell单跟踪止损修改出错 :"+GetLastError());
}
}
}
}
}