///////////////////////////////////////////////////////////////////////////////////////// // // CoinFlip_v03.mq4 // Derk wehler // ///////////////////////////////////////////////////////////////////////////////////////// #property copyright "Derk Wehler" #property link "" #include #include extern string S1 = "-- MONEY MANAGEMENT SETTINGS --"; extern double Lots = 1.0; extern string S2 = " "; extern string S3 = "-- NEWS SETTINGS --"; extern int MinsPrior = 30; extern int MinsSince = 30; extern string S4 = " "; extern string S5 = "-- OTHER SETTINGS --"; extern int SL = 15; extern int TP = 30; extern int Slippage = 1; extern string Name_Expert = "ACoinFlip"; extern int MagicNumber = 8675309; static int PrevBars = -1; static int PrevMinute = -1; int init() { return(0); } int deinit() { return(0); } int start() { bool direction; double lotsMM = Lots; // Return if this is NOT a new candle if (Bars == PrevBars) return(0); PrevBars = Bars; // Only check once per minute... No reason to call FFCal more // than once per minute since "one minute" is it's finest resolution if (Minute() == PrevMinute) return(0); PrevMinute = Minute(); if (CheckOpenPositions() == 0) { if (IsNewsTime()) return(0); direction = Flip(); if (direction) { OrderSendReliable(Symbol(), OP_BUY, lotsMM, Ask, Slippage, Ask - SL*Point, Ask + TP*Point, Name_Expert, MagicNumber, 0, CLR_NONE); } else { OrderSendReliable(Symbol(), OP_SELL, lotsMM, Bid, Slippage, Bid + SL*Point, Bid - TP*Point, Name_Expert, MagicNumber, 0, CLR_NONE); } } return (0); } ///////////////////// FUNCTIONS ////////////////////// //============================================================================= // Flip // Return, randomly, whether to go long or short in a trade ("flip a coin") //============================================================================= bool Flip() { MathSrand(LocalTime()); int rand = MathRand(); if (rand % 2 == 1) return (TRUE); return (FALSE); } //============================================================================= // CheckOpenPositions // Count the number of position this EA has open //============================================================================= int CheckOpenPositions() { int cnt, NumPositions; int NumBuyTrades, NumSellTrades; // Number of buy and sell trades in this symbol NumBuyTrades = 0; NumSellTrades = 0; for(cnt=OrdersTotal()-1; cnt >= 0; cnt--) { OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != MagicNumber) continue; if (OrderType() == OP_BUY ) NumBuyTrades++; if (OrderType() == OP_SELL ) NumSellTrades++; } NumPositions = NumBuyTrades + NumSellTrades; return (NumPositions); } bool IsNewsTime() { int minsSince, minsUntil; int minutesSincePrevEvent = iCustom("EURUSD", 0, "FFCal_v05", true, true, false, true, true, 1, 0); int minutesUntilNextEvent = iCustom("EURUSD", 0, "FFCal_v05", true, true, false, true, true, 1, 1); Print("minutesSinceEURUSD === ", minutesSincePrevEvent, " minutesUntilEURUSD === ", minutesUntilNextEvent); int minutesSincePrevEvent2 = iCustom("USDJPY", 0, "FFCal_v05", true, true, false, true, true, 1, 0); int minutesUntilNextEvent2 = iCustom("USDJPY", 0, "FFCal_v05", true, true, false, true, true, 1, 1); Print("minutesSinceUSDJPY === ", minutesSincePrevEvent2, " minutesUntilUSDJPY === ", minutesUntilNextEvent2); int minutesSincePrevEvent3 = iCustom("GBPUSD", 0, "FFCal_v05", true, true, false, true, true, 1, 0); int minutesUntilNextEvent3 = iCustom("GBPUSD", 0, "FFCal_v05", true, true, false, true, true, 1, 1); Print("minutesSinceGBPUSD === ", minutesSincePrevEvent3, " minutesUntilGBPUSD === ", minutesUntilNextEvent3); minsSince = MathMin(minutesSincePrevEvent, MathMin(minutesSincePrevEvent2, minutesSincePrevEvent3)); minsUntil = MathMin(minutesUntilNextEvent, MathMin(minutesUntilNextEvent2, minutesUntilNextEvent3)); Print("MINIMUM OF ALL: minutesSince === ", minsSince, " minutesUntil === ", minsUntil); if (minsSince <= MinsSince) { Print(".....MinsSince event < Since setting, so IS news time, returning TRUE"); return(true); } if (minsUntil <= MinsPrior) { Print(".....MinsUntil event < Until setting, so IS news time, returning TRUE"); return(true); } Print(".....is NOT news time, returning FALSE"); return(false); }