using System; using System.ComponentModel; using System.Windows.Media; using Alveo.Interfaces.UserCode; using Alveo.UserCode; using Alveo.Common; using Alveo.Common.Classes; namespace Alveo.UserCode { [Serializable] [Description("")] public class HMA_Strategy : ExpertAdvisorBase { #region Properties Array UpTrend; Array DownTrend; Array ExtMapBuffer; int price = 0; bool bConsiderTradingLong = false; bool bConsiderTradingShort = false; string stg="waiting"; #endregion public HMA_Strategy() { // Basic EA initialization. //indicator_buffers = 2; //indicator_chart_window = true; period = 21; method = 3; //indicator_width1 = 2; //indicator_width2 = 2; //indicator_color1 = Colors.Blue; //indicator_color2 = Colors.Red; UpTrend = new Array(); DownTrend = new Array(); ExtMapBuffer = new Array(); copyright = ""; link = ""; } [Category("Settings")] [DisplayName("MA Period")] public int period { get; set; } [Category("Settings")] [DisplayName("Method")] public int method { get; set; } //+------------------------------------------------------------------+" //| expert initialization function |" //+------------------------------------------------------------------+" protected override int Init() { // ENTER YOUR CODE HERE //IndicatorBuffers(2); //SetIndexBuffer(0, UpTrend); //SetIndexArrow(0, 159); //SetIndexBuffer(1, DownTrend); //SetIndexArrow(1, 159); //SetIndexStyle(0, DRAW_LINE, STYLE_SOLID); //SetIndexLabel(0, "HMA(" + period + ").Bull"); //SetIndexStyle(1, DRAW_LINE, STYLE_SOLID); //SetIndexLabel(1, "HMA(" + period + ").Bear"); //IndicatorShortName("Hull Moving Average("+period+")"); return 0; } //+------------------------------------------------------------------+" //| expert deinitialization function |" //+------------------------------------------------------------------+" protected override int Deinit() { // ENTER YOUR CODE HERE; return 0; } protected double WMA(int x, int p) { return(iMA(null, 0, p, 0, method, price, x)); } //+------------------------------------------------------------------+" //| expert start function |" //+------------------------------------------------------------------+" protected override int Start() { int x = 0; double p = MathSqrt(period); int e = Bars - period + 1; double dSto = NormalizeDouble(iStochastic(Symbol(), Period(),5,3,3,MODE_SMA,1,0,1),2); double dHMA1 = NormalizeDouble(iHMA(Symbol(), Period(), 89, 3,0,1),5); double dHMA2 = NormalizeDouble(iHMA(Symbol(), Period(), 89, 3,0,2),5); Array vect = new Array(); Array trend = new Array(); Random rnd = new Random(); int dice = rnd.Next(1, 7); // creates a number between 1 and 6 if(e > Bars) e = Bars; ArrayResize(vect, e); ArraySetAsSeries(vect, true); ArrayResize(trend, e); ArraySetAsSeries(trend, true); ArrayResize(ExtMapBuffer, e); ArraySetAsSeries(ExtMapBuffer, true); for(x = e; x >= 0; x--) { vect[x] = 2*WMA(x, period/2) - WMA(x, period); } for(x = 0; x < e-period; x++) { ExtMapBuffer[x] = iMAOnArray(vect, 0, (int)p, 0, method, x); } for(x = e-period; x >= 0; x--) { trend[x] = trend[x+1]; if (ExtMapBuffer[x]> ExtMapBuffer[x+1]) trend[x] =1; if (ExtMapBuffer[x]< ExtMapBuffer[x+1]) trend[x] =-1; if (trend[x]>0) { UpTrend[x] = ExtMapBuffer[x]; if (trend[x+1]<0) { UpTrend[x+1]=ExtMapBuffer[x+1]; } DownTrend[x] = EMPTY_VALUE; } else { if (trend[x]<0) { DownTrend[x] = ExtMapBuffer[x]; if (trend[x+1]>0) { DownTrend[x+1]=ExtMapBuffer[x+1]; } UpTrend[x] = EMPTY_VALUE; } } } if (AccountNumber()!=271968 && TimeHour(TimeCurrent())<23 && TimeHour(TimeCurrent())>=1 /*&& TimeCurrent()Convert.ToDateTime("2018/05/30 15:00:00")*/ ){ // Trade Identification for Long trades if (trend[0]>1 && dSto<30 && !bConsiderTradingLong) { bConsiderTradingLong = true; Print("Consider Buying ", Symbol()); stg="Consider Buying it"; } // Triggering Long trades if (trend[0]>1 && dSto>30 && bConsiderTradingLong) { bConsiderTradingLong = false; Print("Buying: ", Symbol()); string sSymbol = Symbol(); double pipPos = OrderSymbol().EndsWith("JPY") ? 0.01 : 0.0001; Sleep(dice*1000); OrderSend(Symbol(),OP_BUY,0.1,MODE_ASK,1,0,0,"HMA: Long "+sSymbol); stg="It has been bought"; } // Trade Identification for short trades if (trend[0]<1 && dSto>70 && !bConsiderTradingShort) { bConsiderTradingShort = true; Print("Consider Selling ", Symbol()); stg="Consider Selling it"; } // Triggering short trades if (trend[0]<1 && dSto<70 && bConsiderTradingShort) { bConsiderTradingShort = false; Print("Selling: ", Symbol()); string sSymbol = Symbol(); double pipPos = OrderSymbol().EndsWith("JPY") ? 0.01 : 0.0001; Sleep(dice*1000); OrderSend(Symbol(),OP_SELL,0.1,MODE_BID,1,0,0,"HMA: short "+sSymbol); stg="It has been sold"; } }//end of if account number double dvalhighest=WindowPriceMax(); ObjectCreate("stat2", OBJ_TEXT, 0, Time[WindowBarsPerChart()-20],dvalhighest); SetObjectTime1("stat2", Time[WindowBarsPerChart()-30]); SetObjectPrice1("stat2", dvalhighest); ObjectSet("stat2",OBJPROP_COLOR, White); ObjectSet("stat2",OBJPROP_WIDTH, 11); ObjectSetText("stat2","Sto: "+dSto+"\ntrend[0]: "+trend[0]+"\nUpTrend[0]: "+dHMA1+"\nDownTrend[0]: "+dHMA2+"\n"+stg); //Money Management Behavior int iOpenOrders=0; int OrderFilled = OrdersTotal(); for (int k = 0 ; k < OrderFilled ; k++) { OrderSelect(k,SELECT_BY_POS,MODE_TRADES); double pipPos = OrderSymbol().EndsWith("JPY") ? 100 : 10000; //Closing orders if HMA values switches if (AccountNumber()!=271968) { if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && trend[1]<0) { Print("Consider closing ", OrderSymbol(), " Type: Buy, because it changed condition"); Sleep(dice*1000); OrderClose(OrderTicket(),0.1,OrderOpenPrice(),1); stg="Closed and changed condition, no more buying"; } if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && trend[1]>0) { Print("Consider closing ", OrderSymbol(), " Type: Sell, because it changed condition"); Sleep(dice*1000); OrderClose(OrderTicket(),0.1,OrderOpenPrice(),1); stg="Closed and changed condition, no more selling"; } } } return 0; } } }