简体   繁体   English

如何在 TakeProfit 上触发事件

[英]How to trigger event on TakeProfit

I am working on this problem for several weeks now and I just can't find a proper way to solve it.我现在已经在这个问题上工作了几个星期,但我找不到合适的方法来解决它。 I am looking for help.我正在寻求帮助。

I want the following to happen:我希望发生以下情况:

  1. Place 2 orders, n and n+1.下 2 个订单,n 和 n+1。 n has a TP & SL level, n+1 has only SL level. n 有止盈和止损水平,n+1 只有止损水平。 SL levels are the same for both orders.两个订单的 SL 水平相同。

  2. If SL is hit, fine.如果SL被击中,很好。 Both orders are terminated.两个订单都被终止。

  3. If TP on order n is hit, modify order n+1 to have its SL moved to the price where order n hit TP.如果订单 n 的 TP 被击中,则修改订单 n+1 以使其 SL 移动到订单 n 达到 TP 的价格。

  4. After that I want to execute trailing stop loss and I have already programmed that successfully.之后我想执行追踪止损,我已经成功地编程了。

How is this done?这是怎么做到的? I tried with magic numbers, iterating over HistoryDealsTotal, HistoryOrdersTotal and a few more approaches but I just never get to the point where I am able to track an order inbetween many open orders and check if it just hit TP, then modify the other order that was initially created right after the first order.我尝试使用幻数,迭代 HistoryDealsTotal、HistoryOrdersTotal 和其他一些方法,但我从来没有达到能够跟踪许多未结订单之间的订单并检查它是否刚刚达到 TP,然后修改另一个订单的程度最初是在第一个订单之后创建的。

I would appreciate any help towards a solution, thanks!对于解决方案的任何帮助,我将不胜感激,谢谢!

After doing some research, I came up with a solution to the problem above.经过一番研究,我想出了解决上述问题的方法。 here is a breakdown.这是一个细分。

I looped through my order history and recorded the following;我遍历了我的订单历史记录并记录了以下内容;

  • OrderOpenTime();订单开放时间();
  • OrderSymbol();订单符号();
  • OrderType();订单类型();
  • OrderLots();订单手数();

Note: the data you record is up to you.注意:您记录的数据由您决定。

I then loop through the OrdersTotal with the data I have got for possible matches and then modify the match.然后,我使用我获得的数据循环遍历 OrdersTotal 以查找可能的匹配项,然后修改匹配项。

Hence;因此;

 datetime OrderDateTime=0; // variable to store the OrderOpenTime string orderSymbol=""; int orderType=0; double orderLot=0.0; int TotalHistoryOrders = OrdersHistoryTotal(),TotalOrders=OrdersTotal(); for(int h = TotalHistoryOrders - 1; h >= 0; h--) { if(OrderSelect(h, SELECT_BY_POS, MODE_HISTORY)) // checking through orderHistory { if(OrderSymbol() == _Symbol) { if(OrderType() == OP_SELL || OrderType() == OP_BUY) { if(OrderProfit() >= 0) // checking if the closed trade was in profit { //saving the needed data from the history data OrderDateTime = OrderOpenTime(); orderSymbol = OrderSymbol(); orderType = OrderType(); orderLot = OrderLots(); } for(int i = TotalOrders - 1; i >= 0; i--) { //checking opened trades for a possible match if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) if(OrderOpenTime() == OrderDateTime|| OrderOpenTime() == OrderDateTime+1) // +1 just incase the second order opens a second later { if(OrderSymbol() == orderSymbol) { if(OrderType() == orderType) { if(OrderLots() == orderLot) { // CHEKING IF STOP LOSS IS BELOW ONBAR_STOPLOSS if(OrderStopLoss() < Onbar_stoploss(OP_BUY)) // Onbar_stoploss is my stoploss function for Buy orders { double stop_loss = Onbar_stoploss(OP_BUY); // setting the stoploss //MODIFYING STOP LOSS bool ORDER_MORDIFY1 = OrderModify(OrderTicket(),OrderOpenPrice(), stop_loss, OrderTakeProfit(),0,clrNONE); } else if(OrderStopLoss() > Onbar_stoploss(OP_SELL))// Onbar_stoploss is my stoploss function for sell orders { double stop_loss = Onbar_stoploss(OP_SELL); // setting the stoploss //MODIFYING STOP LOSS bool ORDER_MORDIFY1 = OrderModify(OrderTicket(),OrderOpenPrice(), stop_loss, OrderTakeProfit(),0,clrNONE); } } } } } } } } } }

I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM