int init() { return; } int start() { crossMA("GAZP"); return; } ///////// ///------ ///------функция построения четырех средних, для стратегии. Стратегия пересечения четырех средних. ///------ ///////// int iOrdLongCount = 0; int iOrdShortCount = 0; void crossMA(string sFinInstr) { int iLongPeriod1 = 9; int iLongPeriod2 = 30; int iLongATRPeriod = 3; int iLongKoef = 1.5; int iShortPeriod1 = 6; int iShortPeriod2 = 40; int iShortATRPeriod = 5; int iShortKoef = 1.5; double dLongStopPrice = 0; double dShortStopPrice = 0; bool bBuy = false; bool bSell = false; bool bShort = false; bool bCover = false; // установка сигналов по условиям // для длинных позиций double dLongMA1 = iMA(sFinInstr, 0, iLongPeriod1, 0, MODE_SMA, PRICE_CLOSE, 0); double dLongMA2 = iMA(sFinInstr, 0, iLongPeriod2, 0, MODE_SMA, PRICE_CLOSE, 0); double dLongMA1_1 = iMA(sFinInstr, 0, iLongPeriod1, 0, MODE_SMA, PRICE_CLOSE, 1); double dLongMA2_2 = iMA(sFinInstr, 0, iLongPeriod2, 0, MODE_SMA, PRICE_CLOSE, 1); Alert("Prosto long1 = ",dLongMA1, "long 2 = ", dLongMA2, "long3 = ", dLongMA1_1, "long4 = ", dLongMA2_2); if ((dLongMA1 > dLongMA2) && (dLongMA1_1 <= dLongMA2_2)) { bBuy = true; Alert("long1 = ",dLongMA1, "long 2 = ", dLongMA2, "long3 = ", dLongMA1_1, "long4 = ", dLongMA2_2); } if ((dLongMA1 < dLongMA2) && (dLongMA1_1 >= dLongMA2_2)) bSell = true; // для коротких позиций double dShortMA1 = double iMA(sFinInstr, 0, iShortPeriod1, 0, MODE_SMA, PRICE_CLOSE, 0); double dShortMA2 = double iMA(sFinInstr, 0, iShortPeriod1, 0, MODE_SMA, PRICE_CLOSE, 0); double dShortMA1_1 = double iMA(sFinInstr, 0, iShortPeriod1, 0, MODE_SMA, PRICE_CLOSE, 1); double dShortMA2_2 = double iMA(sFinInstr, 0, iShortPeriod1, 0, MODE_SMA, PRICE_CLOSE, 1); double dLongATR = iATR(sFinInstr, 0, iLongATRPeriod, 1); double dShortATR = iATR(sFinInstr, 0, iShortATRPeriod, 1); if ((dShortMA1 < dShortMA2) && (dShortMA1_1 >= dShortMA2_2)) bShort = true; if ((dShortMA1 > dShortMA2) && (dShortMA1_1 <= dShortMA2_2)) bCover = true; if (iOrdLongCount != 0) { OrderSelect(iOrdLongCount, SELECT_BY_TICKET); dLongStopPrice = OrderOpenPrice() - dLongATR * iLongKoef; if (Bid > dShortStopPrice) bCover = true; } if (iOrdShortCount != 0) { OrderSelect(iOrdShortCount, SELECT_BY_TICKET); dShortStopPrice = OrderOpenPrice() + dShortATR * iShortKoef; if (Bid > dShortStopPrice) bCover = true; } if (iOrdLongCount == 0) { if (bBuy == true) { //source.Positions.BuyAtMarket(bar + 1, 1, "LN"); iOrdLongCount++; OrderSend(sFinInstr, OP_BUY, 1, Ask, 3, Bid-25*Point, Ask * iLongKoef, "My order" + iOrdLongCount, iOrdLongCount, 0, Green); } else { if (bSell == true) { iOrdLongCount--; OrderClose(0, 1, Bid, 3, Red); } } } if (iOrdShortCount == 0) { if (bShort == true) { //source.Positions.BuyAtMarket(bar + 1, 1, "LN"); iOrdShortCount++; OrderSend(sFinInstr, OP_SELL, 1, Bid, 3, 0, 0, "My order" + iOrdShortCount, iOrdShortCount, 0, Red); } else { if (bCover == true) { iOrdShortCount--; OrderClose(0, 1, Ask, 3, Green); } } } //================================================================ }