- //+------------------------------------------------------------------+
- //|
- //| Copyright 2024,TradingVUE Ltd. |
- //| https://www.tradingvue.net |
- //+------------------------------------------------------------------+
- #property copyright "Copyright 2024,TradingVUE Ltd."
- #property link "https://www.tradingvue.net"
- #property version "1.00"
- #include <Expert\Money\MoneyFixedRisk.mqh>
- #include <Trade\SymbolInfo.mqh>
- #include <Trade\Trade.mqh>
- #include <Trade\PositionInfo.mqh>
- CSymbolInfo m_symbol;
- CTrade m_trade;
- CPositionInfo m_position;
- CMoneyFixedRisk m_money;
- input int maLength = 233; // 均线长度
- input int Inp_src = PRICE_MEDIAN; // 均线源值
- input double fib1 = 1.618; // 裴波那契水平1
- input double fib2 = 2.618; // 裴波那契水平2
- input double fib3 = 4.236; // 裴波那契水平3
- input int kcMultiplier = 2 ; // 肯特纳倍数
- input int kcLength = 89; // 肯特纳长度
- input double Inp_lot = 0.1; // 下单手数
- input int inp_magic = 5555; // 幻数
- input bool CloseAll = true; // 全平
- input bool CloseAllProfit = false; // 平盈利
- input bool CloseAllLoss = false; // 平亏损
- int handle_ma, handle_tr, handle_atr, handle_rsi;
- //+------------------------------------------------------------------+
- //| Expert initialization function |
- //+------------------------------------------------------------------+
- int OnInit()
- {
- //---
- m_symbol.Name(Symbol());//初始化商品品种
-
- int dig = 1;
- if(m_symbol.Digits() == 3 || m_symbol.Digits() == 5)
- dig = 10;
- if(m_symbol.Digits() == 2)
- dig = 100;
- m_money.Init(GetPointer( m_symbol), PERIOD_CURRENT, m_symbol.Point() * dig );
- handle_ma = iMA(m_symbol.Name(), PERIOD_CURRENT, maLength, 0, MODE_EMA, Inp_src);//获取指标
- handle_tr = iATR(m_symbol.Name(), PERIOD_CURRENT, 1);//获取指标
- handle_atr = iMA(m_symbol.Name(), PERIOD_CURRENT, kcLength, 0, MODE_SMA, handle_tr);//获取指标
- handle_rsi = iRSI(m_symbol.Name(), PERIOD_CURRENT,14, Inp_src);
- ChartIndicatorAdd(ChartID(), 0, handle_ma);//画图。 将指标可视化
-
-
- //m_trade.SetExpertMagicNumber(inp_magic);//幻数
- //m_trade.SetMarginMode();
-
- //滑点
- //m_trade.SetTypeFillingBySymbol(m_symbol.Name());
- //m_trade.SetDeviationInPoints(10);
- //---
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Expert deinitialization function |
- //+------------------------------------------------------------------+
- void OnDeinit(const int reason)
- {
- //---
-
- }
- //+------------------------------------------------------------------+
- //| Expert tick function |
- //+------------------------------------------------------------------+
- void OnTick()
- {
- //---
- SetTime();
-
-
- if(!isNewBar())
- {
- return;
- }
-
- Refresh();
-
- // 获取指标中的数据--
- double kcAverageTrueRange[], ma[], rsi[];//数组,存放指标数据
-
- //Copy方式 获取数值
- ArraySetAsSeries(kcAverageTrueRange, true);//倒序排列,形成时间序列排序
- ArraySetAsSeries(ma, true);
- ArraySetAsSeries(rsi, true);
-
- //读取指标数据
- int to_copy=2;
- GetArray(handle_atr, 0, 0, to_copy, kcAverageTrueRange);
- GetArray(handle_ma, 0, 0, to_copy, ma);
- GetArray(handle_rsi, 0, 0, to_copy, rsi);
-
- double kcUpper[2],kcLower[2];
- //double fbUpper1[2],fbLower1[2];
- //double fbUpper2[2],fbLower2[2];
- double fbUpper3[2],fbLower3[2];
- for(int i=0; i<2 ; i++)
- {
- kcUpper[i] = ma[i] + kcMultiplier * kcAverageTrueRange[i];
- kcLower[i] = ma[i] - kcMultiplier * kcAverageTrueRange[i];
-
- // Calculate the Fibonacci Bands
- //fbUpper1[i] = ma[i] + fib1 * (kcUpper[i] - ma[i]);
- //fbUpper2[i] = ma[i] + fib2 * (kcUpper[i] - ma[i]);
- fbUpper3[i] = ma[i] + fib3 * (kcUpper[i] - ma[i]);
- //fbLower1[i] = ma[i] - fib1 * (ma[i] - kcLower[i]);
- //fbLower2[i] = ma[i] - fib2 * (ma[i] - kcLower[i]);
- fbLower3[i] = ma[i] - fib3 * (ma[i] - kcLower[i]);
-
- }
-
-
- //开多单
- if(Close(0)<fbLower3[0] && Close(1)>fbLower3[1] && rsi[0]<40)
- {
- m_trade.Buy(Inp_lot);
- //OpenSell(Inp_lot, sl, tp);
- }
- //开空单
- if(Close(0)>fbUpper3[0] && Close(1)<fbUpper3[1] && rsi[0]>60)
- {
- m_trade.Sell(Inp_lot);
- //OpenBuy(Inp_lot, sl, tp);
- }
-
- if(PositionsTotal() == 0 ) return;
- // 基线交叉时全部平仓
- if((Close(0)<ma[0] && Close(1)>ma[1]) || (Close(0)>ma[0] && Close(1)<ma[1]))
- {
- for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of current positions
- if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
- {
- if(CloseAll) // m_trade.PositionClose(m_position.Symbol());
- ClosePosition(m_position.Symbol()); // close a position by the specified symbo
- else if(CloseAllProfit)
- {
- if(m_position.Commission()+m_position.Swap()+m_position.Profit()>0.0)
- ClosePosition(m_position.Symbol()); // close a position by the specified symbo
- }
- else if(CloseAllLoss)
- {
- if(m_position.Commission()+m_position.Swap()+m_position.Profit()<0.0)
- ClosePosition(m_position.Symbol()); // close a position by the specified symbo
- }
- }
- }
-
-
- }
- //+------------------------------------------------------------------+
- //刷新函数
- bool Refresh(void)
- {
- if(!m_symbol.RefreshRates())
- {
- Print("refresh error");
- return (false);
- }
- if(m_symbol.Ask() == 0 || m_symbol.Bid() == 0)
- {
- Print("refresh error");
- return (false);
- }
-
- return(true);
- }
- //买入函数
- bool OpenBuy(double lot, double sl = 0.0, double tp = 0.0)
- {
- if (sl > 0 && tp > 0)
- {
- sl = m_symbol.NormalizePrice(sl);//返回规范价格形式
- tp = m_symbol.NormalizePrice(tp);
- }
-
- if(m_trade.Buy(lot, m_symbol.Name(), m_symbol.Ask(), sl, tp))
- {
- return(true);
- }
- else
- {
- Print("Error:", GetLastError());
- return(false);
- }
- }
- //卖出函数
- bool OpenSell(double lot, double sl = 0.0, double tp = 0.0)
- {
- if (sl > 0 && tp > 0)
- {
- sl = m_symbol.NormalizePrice(sl);//返回规范价格形式
- tp = m_symbol.NormalizePrice(tp);
- }
-
- if(m_trade.Sell(lot, m_symbol.Name(), m_symbol.Bid(), sl, tp))
- {
- return(true);
- }
- else
- {
- Print("Error:", GetLastError());
- return(false);
- }
- }
- //获取开盘价格
- double Open(const int idx)
- {
- return iOpen(m_symbol.Name(), PERIOD_CURRENT, idx);//品种,周期,K线
- }
- //获取收盘价格
- double Close(const int idx)
- {
- return iClose(m_symbol.Name(), PERIOD_CURRENT, idx);
- }
- //获取最低价格
- double Low(const int idx)
- {
- return iLow(m_symbol.Name(), PERIOD_CURRENT, idx);
- }
- //获取最高价格
- double High(const int idx)
- {
- return iHigh(m_symbol.Name(), PERIOD_CURRENT, idx);
- }
- //检查是不是新K线
- bool isNewBar()
- {
- static datetime last_time = 0;
- datetime lastbar_time = datetime(SeriesInfoInteger(Symbol(), Period(), SERIES_LASTBAR_DATE));
- if(last_time == 0)
- {
- last_time = lastbar_time;
- return(false);
- }
- if(last_time != lastbar_time)
- {
- last_time = lastbar_time;
- return(true);
- }
- return(false);
- }
- //显示交易时间
- void SetTime(void)
- {
- if(ObjectFind(0, "LastTime") < 0)
- {
- //时间初始化
- ObjectCreate(0, "LastTime", OBJ_LABEL, 0, 0, 0);
- //x坐标
- ObjectSetInteger(0, "LastTime", OBJPROP_XDISTANCE, 100);
- //Y坐标
- ObjectSetInteger(0, "LastTime", OBJPROP_YDISTANCE, 2);
- //文本颜色
- ObjectSetInteger(0, "LastTime", OBJPROP_COLOR, clrDimGray);
- ObjectSetInteger(0, "LastTime", OBJPROP_CORNER, CORNER_RIGHT_UPPER);
- ObjectSetInteger(0, "LastTime", OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER);
-
- }
-
- MqlRates rates[];
- CopyRates(m_symbol.Name(), 0, 0, 1, rates);
-
- int total = int(rates[0].time + PeriodSeconds() - TimeCurrent());
- int s = total % 60;
- int m = (total - s) / 60;
-
- //标签文本
- ObjectSetString(0, "LastTime", OBJPROP_TEXT, " Next Bar:" + IntegerToString(m) + ":" + IntegerToString(s) );
- //字体大小
- ObjectSetInteger(0, "LastTime", OBJPROP_FONTSIZE, 10);
- //禁止鼠标选择
- ObjectSetInteger(0, "LastTime", OBJPROP_SELECTABLE, false);
- //绘制
- ChartRedraw(0);
- }
- //获取数组
- bool GetArray(const int handle, const int buffer, const int start_pos, const int count, double &arr_buffer[])
- {
- bool result = false;
-
- if(!ArrayIsDynamic(arr_buffer))
- {
- Print("is not dynamic array!");//非动态数组错误,返回
- return(false);
- }
-
- ArrayFree(arr_buffer);//清空数组
- ResetLastError();//重置最后错误为空
-
- int copied = CopyBuffer(handle, buffer, start_pos, count, arr_buffer);//返回值是复制的数据数,错误时是-1
-
- if(copied != count)
- {
- Print("Failed to copy data from the indicator, error code: %d", GetLastError());
- return(false);
- }
- else
- {
- return(true);
- }
-
- return(result);
- }
- //+------------------------------------------------------------------+
- //| Close selected position |
- //+------------------------------------------------------------------+
- void ClosePosition(const string symbol)
- {
- if(InitTrade(symbol))
- m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbo
- }
- //+------------------------------------------------------------------+
- //| Init trade object |
- //+------------------------------------------------------------------+
- bool InitTrade(const string symbol)
- {
- if(!m_symbol.Name(symbol)) // sets symbol name
- return(false);
- //---
- if(IsFillingTypeAllowed(symbol,SYMBOL_FILLING_FOK))
- m_trade.SetTypeFilling(ORDER_FILLING_FOK);
- else if(IsFillingTypeAllowed(symbol,SYMBOL_FILLING_IOC))
- m_trade.SetTypeFilling(ORDER_FILLING_IOC);
- else
- m_trade.SetTypeFilling(ORDER_FILLING_RETURN);
- //---
- return(true);
- //---
- }
- //+------------------------------------------------------------------+
- //| Checks if the specified filling mode is allowed |
- //+------------------------------------------------------------------+
- bool IsFillingTypeAllowed(string symbol,int fill_type)
- {
- //--- Obtain the value of the property that describes allowed filling modes
- int filling=(int)SymbolInfoInteger(symbol,SYMBOL_FILLING_MODE);
- //--- Return true, if mode fill_type is allowed
- return((filling & fill_type)==fill_type);
- }
- //+------------------------------------------------------------------+
复制代码