自动止盈止损及追踪止损源码总结

  股票止损怎么定?想必是很股民在止损止赢过程中想要迫切知道的,有时止损止盈点设置的高低不太合适,对之后的追踪会有一定影响,那么如何才能将这个点设置的合理呢?其实有一种能够自动止盈止损,并且还可以追踪止损的源码可以解决你的困惑。

  //---- input parameters

  extern int 止赢=550;

  extern int 止损=320;

  extern int 追踪止损=150;

  extern int Magic=0;

  extern string 参数说明="建议不要小于10点";

  int start()

  {

  //----

  //追踪止损

  _MoveStop(Magic, 追踪止损,0);

  //设置止赢

  _TakeProfit(Magic,止赢);

  //设置止损

  _StopLoss(Magic,止损);

  //----

  return(0);

  }

  //+------------------------------------------------------------------+

  //| _MoveStop 移动止损函数 function |

  //+------------------------------------------------------------------+

  int _MoveStop(int MAGIC, int MOVE,int STEP)//_MoveStop(MAGIC, MOVE);

自动止盈止损及追踪止损

  {

  //----

  if (MOVE<=0) return(0);

  double MoveStopPrice;

  for ( int z = OrdersTotal() - 1; z >= 0; z -- )

  {

  if ( !OrderSelect( z, SELECT_BY_POS ) )

  {

  Print("OrderSelect(", z, ",SELECT_BY_POS) - Error #",GetLastError() );

  continue;

  }

  if (OrderSymbol()!=Symbol())continue;

  if (OrderMagicNumber() != MAGIC )continue;

  switch (OrderType())

  {

  case OP_BUY :

  {

  MoveStopPrice=NormalizeDouble(Bid-MOVE*Point,Digits);

  if (MoveStopPrice>OrderOpenPrice() && OrderStopLoss()

  {

  if(!OrderModify(OrderTicket(),OrderOpenPrice(),MoveStopPrice+STEP*Point,OrderTakeProfit(),OrderExpiration()))

  {

  Alert("MoveStop_OrderModify Error #",GetLastError());

  return(-1);

  }

  }

  continue;

  }

  case OP_SELL:

  {

  MoveStopPrice=NormalizeDouble(Ask+MOVE*Point,Digits);

  if (MoveStopPriceMoveStopPrice)

  {

  if(!OrderModify(OrderTicket(),OrderOpenPrice(),MoveStopPrice-STEP*Point,OrderTakeProfit(),OrderExpiration()))

  {

  Alert("MoveStop_OrderModify Error #",GetLastError());

  return(-1);

  }

  }

  continue;

  }

  default: continue;

  }

  }

  //----

  return(0);

  }

  //+------------------------------------------------------------------+

  //| _MoveStop 移动止损函数 function |

  //+------------------------------------------------------------------+

  int _TakeProfit(int MAGIC, int TP)//

  {

  //----

  if (TP<=0) return(0);

  double TakeProfit;

  for ( int z = OrdersTotal() - 1; z >= 0; z -- )

  {

  if ( !OrderSelect( z, SELECT_BY_POS ) )

  {

  Print("OrderSelect(", z, ",SELECT_BY_POS) - Error #",GetLastError() );

  continue;

  }

  if (OrderSymbol()!=Symbol())continue;

  if (OrderMagicNumber() != MAGIC )continue;

  switch (OrderType())

  {

  case OP_BUY :

  {

  TakeProfit=NormalizeDouble(OrderOpenPrice()+TP*Point,Digits);

  if (OrderTakeProfit()==0.0)

  {

  if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit,OrderExpiration()))

  {

  Alert("TakeProfit_OrderModify Error #",GetLastError());

  }

  }

  continue;

  }

  case OP_SELL:

  {

  TakeProfit=NormalizeDouble(OrderOpenPrice()-TP*Point,Digits);

  if (OrderTakeProfit()==0.0)

  {

  if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit,OrderExpiration()))

  {

  Alert("TakeProfit_OrderModify Error #",GetLastError());

  }

  }

  continue;

  }

  default: continue;

  }

  }

  //----

  return(0);

  }

  //+------------------------------------------------------------------+

  //| _MoveStop 移动止损函数 function |

  //+------------------------------------------------------------------+

  int _StopLoss(int MAGIC, int SL)//_MoveStop(MAGIC, MOVE);

  {

  //----

  if (SL<=0) return(0);

  double StopLoss;

  for ( int z = OrdersTotal() - 1; z >= 0; z -- )

  {

  if ( !OrderSelect( z, SELECT_BY_POS ) )

  {

  Print("OrderSelect(", z, ",SELECT_BY_POS) - Error #",GetLastError() );

  continue;

  }

  if (OrderSymbol()!=Symbol())continue;

  if (OrderMagicNumber() != MAGIC )continue;

  switch (OrderType())

  {

  case OP_BUY :

  {

  StopLoss=NormalizeDouble(OrderOpenPrice()-SL*Point,Digits);

  if (OrderStopLoss()==0.0)

  {

  if(!OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss,OrderTakeProfit(),OrderExpiration()))

  {

  Alert("StopLoss_OrderModify Error #",GetLastError());

  }

  }

  continue;

  }

  case OP_SELL:

  {

  StopLoss=NormalizeDouble(OrderOpenPrice()+SL*Point,Digits);

  if (OrderStopLoss()==0.0)

  {

  if(!OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss,OrderTakeProfit(),OrderExpiration()))

  {

  Alert("StopLoss_OrderModify Error #",GetLastError());

  }

  }

  continue;

  }

  default: continue;

  }

  }

  //----

  return(0);

  }

  //+------------------------------------------------------------------+

  自动止盈止损及追踪止损的方法,不仅可以及时进行止损,还可以在盈利之时将利润最大化处理,同时还能够规避掉一些不可预知的风险,这是股票止损技巧中最困难的,也是最难把握的地方,这种方法的出现无疑也给投资增加了一层防护膜。

  推荐阅读

  隔日超短线止损止盈点有何技巧?止损止盈设定范围!

  超短线止盈止损点怎么设置?有哪些止损止盈方法?

  最合理的止盈止损比例怎么设置?如何正确对待止损止盈?

关于作者:

交易自己的交易,制定自己的交易。