В этой теме 3 ответа, 2 участника, последнее обновление Вячеслав 9 года, 1 месяц назад.
Коллеги, подскажите, что не так с кодом.
Проблема — не выставляет StopLoss при открытии ордера.
И есть позиции, которые открывает с ошибкой.
Суть идеи — при пересечении Скользящей средней входим в сделку и тралим ее Стоплоссом.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
//+------------------------------------------------------------------+ //| MA AKI FixTP vol1.2.mq4 | //| Andrey Koldorkin | //+------------------------------------------------------------------+ #define MAGICMA 20131111 //--- Inputs input double Lots =0.1; input int MovingPeriod =40; input int MovingShift =5; extern int StopLoss = 200; double SL; ///+-----------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders(string symbol) { int buys=0,sells=0; //--- for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA) { if(OrderType()==OP_BUY) buys++; if(OrderType()==OP_SELL) sells++; } } //--- return orders volume if(buys>0) return(buys); else return(-sells); } //+------------------------------------------------------------------+ //| Check for open order conditions | //+------------------------------------------------------------------+ void CheckForOpen() { double ma; int res; //--- go trading only for first tiks of new bar if(Volume[0]>1) return; //--- get Moving Average ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0); //--- sell conditions if(Bid>ma) { res=OrderSend(Symbol(),OP_SELL,Lots,Bid,5,0,0,"",MAGICMA,0,Red); return; } //--- buy conditions if(Ask<ma) { res=OrderSend(Symbol(),OP_BUY,Lots,Ask,5,0,0,"",MAGICMA,0,Blue); return; } //--- } //+------------------------------------------------------------------+ //| TrallingStop and TrallingStep | //+------------------------------------------------------------------+ void TrallingStop () { double ma; double OMF; ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0); for (int i=0; i<OrdersTotal (); i++) { if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol () == Symbol () && OrderMagicNumber () == MAGICMA) { SL = NormalizeDouble (ma, Digits); if (OrderStopLoss() !=SL) OMF = OrderModify (OrderTicket (), OrderOpenPrice (), SL, 0, 0); } } } } //+------------------------------------------------------------------+ //| OnTick function | //+------------------------------------------------------------------+ void OnTick() { //--- check for history and trading if(Bars<100 || IsTradeAllowed()==false) return; //--- calculate open orders by current symbol if(CalculateCurrentOrders(Symbol())==0) CheckForOpen(); else TrallingStop(); //--- } //+------------------------------------------------------------------+ |
Поправил функцию, чтобы она выставляла начальный стоп-лосс:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
//+——————————————————————+ //| Check for open order conditions | //+——————————————————————+ void CheckForOpen() { double ma; int res; //— go trading only for first tiks of new bar if(Volume[0]>1) return; //— get Moving Average ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0); //— sell conditions if(Bid>ma) { double sellSL=0; //стоплосс для продажи if(StopLoss>0) sellSL=Bid+StopLoss*Point;; res=OrderSend(Symbol(),OP_SELL,Lots,Bid,5,sellSL,0,»»,MAGICMA,0,Red); return; } //— buy conditions if(Ask<ma) { double buySL=0; //стоплосс для покупки if(StopLoss>0) buySL=Ask-StopLoss*Point; res=OrderSend(Symbol(),OP_BUY,Lots,Ask,5,buySL,0,»»,MAGICMA,0,Blue); return; } //— } |
Исправил:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
//+------------------------------------------------------------------+ //| MA AKI FixTP vol1.2.mq4 | //| Andrey Koldorkin | //| http://www.quoteportal.ru | //+------------------------------------------------------------------+ #define MAGICMA 20131111 //--- Inputs input double Lots =0.1; input int MovingPeriod =40; input int MovingShift =5; extern int StopLoss = 200; double SL; ///+-----------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders(string symbol) { int buys=0,sells=0; //--- for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA) { if(OrderType()==OP_BUY) buys++; if(OrderType()==OP_SELL) sells++; } } //--- return orders volume if(buys>0) return(buys); else return(-sells); } //+------------------------------------------------------------------+ //| Check for open order conditions | //+------------------------------------------------------------------+ void CheckForOpen() { double buySL=0; //стоплосс для покупки double sellSL=0; //стоплосс для продажи if(StopLoss>0) {buySL=Ask-StopLoss*Point; sellSL=Bid+StopLoss*Point;} double ma; int res; //--- go trading only for first tiks of new bar if(Volume[0]>1) return; //--- get Moving Average ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0); //--- sell conditions if(Bid>ma) { res=OrderSend(Symbol(),OP_SELL,Lots,Bid,5,sellSL,0,"",MAGICMA,0,Red); return; } //--- buy conditions if(Ask<ma) { res=OrderSend(Symbol(),OP_BUY,Lots,Ask,5,buySL,0,"",MAGICMA,0,Blue); return; } //--- } //+------------------------------------------------------------------+ //| TrallingStop and TrallingStep | //+------------------------------------------------------------------+ void TrallingStop () { double ma; double OMF; ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0); for (int i=0; i<OrdersTotal (); i++) { if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol () == Symbol () && OrderMagicNumber () == MAGICMA) { SL = NormalizeDouble (ma, Digits); if (OrderStopLoss() !=SL) OMF = OrderModify (OrderTicket (), OrderOpenPrice (), SL, 0, 0); } } } } //+------------------------------------------------------------------+ //| OnTick function | //+------------------------------------------------------------------+ void OnTick() { //--- check for history and trading if(Bars<100 || IsTradeAllowed()==false) return; //--- calculate open orders by current symbol if(CalculateCurrentOrders(Symbol())==0) CheckForOpen(); else TrallingStop(); //--- } //+------------------------------------------------------------------+ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
///+-----------------------------------------------------------------+ //| TrallingStop and TrallingStep | ///+-----------------------------------------------------------------+ void TrallingStop () { double ma; double OMF; ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0); for (int i=0; i<OrdersTotal (); i++) { if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol () == Symbol () && OrderMagicNumber () == MAGICMA) { SL = NormalizeDouble (ma, Digits); double order_sl = OrderStopLoss(); if(OrderType() == OP_BUY) { if (order_sl != SL && Bid > order_sl && (Bid - order_sl) > StopLoss*Point) OMF = OrderModify (OrderTicket (), OrderOpenPrice (), SL, 0, 0); } else if(OrderType() == OP_SELL) { if (order_sl != SL && Ask < order_sl && (order_sl - Ask) > StopLoss*Point) OMF = OrderModify (OrderTicket (), OrderOpenPrice (), SL, 0, 0); } } } } } |
Вы должны авторизироваться для ответа в этой теме.
Оптимальное соотношение между доходностью и риском