#property strict #include CAppDialog OurInterface; #include CEdit LotSize,TP,SL; #include CLabel LotLabel,TPLabel,SLLabel; double TradeVolume = 1; double sl = 500; double tp = 400; double tsl= 0; double sd= 100; int start_hour = 11; int start_min = 50; int end_hour = 11; int end_min = 55; int second_start_hour = 18; // 18 is best int second_start_min = 0; int second_end_hour = 22; // 22:30 is best int second_end_min = 30; string OK_TO_TRADE= "NG"; string SymbolOrder="Cocoa_H5"; double MaxAccountBalance = AccountBalance(); double MinAccountBalance = 1000000; double InitialAccountBalance = AccountBalance(); const string LotName = "Lot_Volume"; const string TP_Value = "TP_Value"; const string SL_Value = "SL_Value"; int OnInit() { ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true); OurInterface.Create(0,"Cocoa Trading V3.0",0,180,70,400,200); LotLabel.Create(0,"LotLabel",0,10,10,50,30); OurInterface.Add(LotLabel); LotLabel.Text("Lot Size:"); LotSize.Create(0,LotName,0,90,10,160,30); OurInterface.Add(LotSize); LotSize.Text(string(TradeVolume)); TPLabel.Create(0,"TPLabel",0,10,40,50,60); OurInterface.Add(TPLabel); TPLabel.Text("TP:"); TP.Create(0,TP_Value,0,90,40,160,60); OurInterface.Add(TP); TP.Text(string(tp)); SLLabel.Create(0,"SLLabel",0,10,70,50,90); OurInterface.Add(SLLabel); SLLabel.Text("SL:"); SL.Create(0,SL_Value,0,90,70,160,90); OurInterface.Add(SL); SL.Text(string(sl)); OurInterface.Run(); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { EventKillTimer(); // Kill the timer OurInterface.Destroy(reason); } void OnTick() { datetime Today = StrToTime(StringConcatenate(Year(), ".", Month(), ".", Day())); datetime start_time = Today+start_hour*3600+start_min*60; datetime end_time = Today+end_hour*3600+end_min*60; datetime second_start_time = Today+second_start_hour*3600+second_start_min*60; datetime second_end_time = Today+second_end_hour*3600+second_end_min*60; if ((TimeCurrent()>start_time && TimeCurrent()second_start_time && TimeCurrent()sd) { double buy = price; // buy = NormalizeDouble(buy, _Digits); double slbuy = buy-sl; //buy-previousclose; // slbuy = NormalizeDouble(slbuy, _Digits); double tpbuy = buy+tp; // tpbuy = NormalizeDouble(tpbuy, _Digits); int orderbuy = OrderSend (SymbolOrder,OP_BUY,TradeVolume,buy,0,slbuy,tpbuy,NULL,100,0,clrBlue); } } if (OK_TO_TRADE=="OK" && CheckSellOpenOrders(200)==false) { double price = NormalizeDouble(MarketInfo(SymbolOrder,MODE_BID),4); if (previousclose-price>sd) { double sell = price; // sell = NormalizeDouble(sell, _Digits); double slsell = sell+sl; // sell+previousclose; // slsell = NormalizeDouble(slsell, _Digits); double tpsell = sell-tp; // tpsell = NormalizeDouble(tpsell, _Digits); int ordersell = OrderSend (SymbolOrder,OP_SELL,TradeVolume,sell,0,slsell,tpsell,NULL,200,0,clrBlue); } } double CurrentAccountBalance = AccountBalance(); if (MaxAccountBalance < AccountBalance()) MaxAccountBalance = AccountBalance(); if (MinAccountBalance > AccountBalance()) MinAccountBalance = AccountBalance(); Comment ("Initial Account Balance was ",InitialAccountBalance,", Minimum Balance was ",MinAccountBalance,", Max Balance was ",MaxAccountBalance,", Current Balance is ",CurrentAccountBalance," and Current Equity is ",AccountEquity(), ""); } // End of On Tick // Start of Event Handling void OnChartEvent (const int id,const long &lparam,const double &dparam,const string &sparam) { OurInterface.OnEvent(id,lparam,dparam, sparam); if (id == CHARTEVENT_OBJECT_ENDEDIT){ if(sparam == LotName) { string volumeText = ObjectGetString(0, LotName, OBJPROP_TEXT); TradeVolume = StringToDouble(volumeText); ObjectSetString(0, LotName, OBJPROP_TEXT, string(TradeVolume)); return; } if(sparam == TP_Value) { string volumeText = ObjectGetString(0, TP_Value, OBJPROP_TEXT); tp = StringToDouble(volumeText); ObjectSetString(0, TP_Value, OBJPROP_TEXT, string(tp)); return; } if(sparam == SL_Value) { string volumeText = ObjectGetString(0, SL_Value, OBJPROP_TEXT); sl = StringToDouble(volumeText); ObjectSetString(0, SL_Value, OBJPROP_TEXT, string(sl)); return; } } // End of CHARTEVENT_OBJECT_ENDEDIT // if an object was clicked } // End of Event Handling bool CheckBuyOpenOrders(int MagicBuy){ // We need to scan all the open and pending orders to see if there are any. // OrdersTotal returns the total number of market and pending orders. // What we do is scan all orders and check if they are of the same symbol as the one where the EA is running. for( int i = 0 ; i < OrdersTotal() ; i++ ) { // We select the order of index i selecting by position and from the pool of market/pending trades. int OrderBO = OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); // If the pair of the order is equal to the pair where the EA is running. if(OrderType()==OP_BUY && OrderMagicNumber()==MagicBuy) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); } bool CheckSellOpenOrders(int MagicSell){ // We need to scan all the open and pending orders to see if there are any. // OrdersTotal returns the total number of market and pending orders. // What we do is scan all orders and check if they are of the same symbol as the one where the EA is running. for( int i = 0 ; i < OrdersTotal() ; i++ ) { // We select the order of index i selecting by position and from the pool of market/pending trades. int OrderSO = OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); // If the pair of the order is equal to the pair where the EA is running. if(OrderType()==OP_SELL && OrderMagicNumber()==MagicSell) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); }