void CheckIndicator(iMACD macd, bool isCompleteBar, bool displayIndicator = true) { double MACD, signal, hist; if (macd.isPrimed()) // indicator is "primed" when it has enough data to calculate values { macd.Value(out MACD, out signal, out hist); // calculate MACD values //if (display) cout("---MACD:{0} signal:{1} hist:{2}", MACD, signal, hist); // display the values if (displayIndicator) { cout("{0}>---MACD:{1:0.00000000}--- [{2}]", m_pair, hist, DateTime.Now.ToShortTimeString()); // display only the histMACD value } m_indicator.Add(new double[] { MACD, signal, hist }); // add the values to the m_indicator List if (m_pos > 0 && hist < 0) // CHECK TO SEE IF WE CROSSED pos-to-neg or neg-to-pos { if (isCompleteBar) { TradeSell(); // long position and histMACD negative = SELL } } else if (m_pos < 0 && hist > 0) { if (isCompleteBar) { TradeBuy(); // short position and histMACD positive = BUY } } } }
public void RecordMACD(KLine _curKL, ref iEMA ema11, ref iEMA ema22, ref iMACD macd) { ema11.ReceiveTick(_curKL.close); ema22.ReceiveTick(_curKL.close); double DI = ((double)_curKL.close * 2 + _curKL.highest + _curKL.lowest) / 4.0; macd.ReceiveTick(DI / 100); }
public bool ReceiveData(ref int cnt, ref KLine _KL, ref List <KLine> _lst, ref iEMA _ema11, ref iEMA _ema22, ref iMACD _macd, int period, DateTime _CurTime, int open, int highest, int lowest, int close, int amount) { cnt++; if (cnt == 1) { _KL = new KLine(FIFTEEN_MINUTES, _CurTime, open, highest, lowest, close, amount); } else { _KL.amount += amount; if (lowest < _KL.lowest) { _KL.lowest = lowest; } else if (highest > _KL.highest) { _KL.highest = highest; } if (cnt == period / ONE_MINUTE) { _KL.close = close; _KL.datetime = g_CurTime; _lst.Add(_KL); cnt = 0; //記錄MACD RecordMACD(_KL, ref _ema11, ref _ema22, ref _macd); //K棒收集完成 return(true); } } return(false); }
private List<decimal> CandlesMACD(string exchange, string symbol, int periodMinutes = 1, DateTime? startDate = null, DateTime? endDate = null, int? limit = null) { int periodSeconds = periodMinutes * 60; var candles = m_api[exchange].GetCandles(symbol, periodSeconds, startDate, endDate, limit); var macd = new iMACD(12, 26, 9); var histList = new List<decimal>(); foreach (var c in candles) { var timeString = c.Timestamp.ToString("yyyy-MM-dd HH:mm:ss"); //Console.WriteLine("{0} {1} {2} o:{3} h:{4} l:{5} c:{6} vol:{7} period:{8} wavg:{9}", timeString, c.ExchangeName, c.Name, c.OpenPrice, c.HighPrice, c.LowPrice, c.ClosePrice, c.BaseVolume, c.PeriodSeconds, c.WeightedAverage); macd.ReceiveTick(c.ClosePrice); if (macd.IsPrimed) { /*decimal macdValue, signalValue, histValue; macd.DecimalValue(out macdValue, out signalValue, out histValue); //Console.WriteLine("{0:0.00}", histValue); // {1:0.00} {2:0.00}", macdValue, signalValue, histValue); histList.Add(histValue);*/ histList.Add(macd.Value); } } return histList; }
public void StartTrade(string pair, decimal tradeUnitSize) { m_tradeUnitSize = tradeUnitSize; m_macd = new iMACD(10, 26, 9); BittrexOHLC(pair, false); // get the initial set of OHLC data (and calculate latest MACD values) var hist = m_indicator.Last()[2]; // get the most recent "histogram" MACD value (watch this going positive/negative) if (hist > 0) // set our initial "position" according to current "histogram" MACD value { m_pos = 1; } else { m_pos = -1; } /*// ADD ORDER * var ticker = kraken.GetTicker(pair); * xpair = ohlc.Pairs.Keys.First(); * ticker.Display("ticker:"); * int dplaces = 1; * decimal percent = 0.05M; * var bid = Math.Round(ticker[xpair].BidPrice - percent * ticker[xpair].BidPrice, dplaces); // bid - 5% * var ask = Math.Round(ticker[xpair].AskPrice + percent * ticker[xpair].BidPrice, dplaces); // ask + 5% * var addorder = kraken.AddOrder(pair, "buy", bid, 0.001M, 2); * string[] txids; * if (addorder != null) * { * txids = addorder.Txid; * var descr = addorder.Descr; * cout(addorder.ToString()); * } * else * txids = null; * * // ORDERS, TRADES, POSITIONS * var openOrders = kraken.GetOpenOrders(); * var closedOrders = kraken.GetClosedOrders(); * openOrders.Display("open orders:"); * closedOrders.Display("closed orders:"); * var qo = kraken.QueryOrders(txids); * var qt = kraken.QueryTrades(txids); * var op = kraken.GetOpenPositions(txids); * * bool cancel = true; * if (cancel && txids != null) * { * // CANCEL PREVIOUSLY ADDED ORDER * foreach (var id in txids) * { * var cancelorder = kraken.CancelOrder(id); * cout(cancelorder.ToString()); * } * }*/ //ShowThreadInfo("Application"); var tind = Task.Run(() => IndicatorThread(pair)); //t.Wait(); var torders = Task.Run(() => OrdersThread(pair)); System.Console.WriteLine("Starting BITTREX trading system..."); //System.Console.ReadLine(); tind.Wait(); }
public void StartTrade(decimal tradeUnitSize, int barIntervalMinutes, bool tradeLive = false, bool backTestOnly = false) { m_tradeUnitSize = tradeUnitSize; m_barIntervalMinutes = barIntervalMinutes; //m_tradeLive = tradeLive; m_tradeStartTime = DateTime.Now.ToCompactDateTime(); // time that we started this trade InitializeTradeLog(); // write the column headers to the trade log file cout("Starting BINANCE MACD trade: '{0}' interval={1} size={2}", m_pair, m_barIntervalMinutes, m_tradeUnitSize); m_macd = new iMACD(10, 26, 9); // create the MACD indicator m_macdLive = new iMACD(10, 26, 9); // create intra-bar version of the MACD indicator m_tradeLive = false; // turn OFF live trading while we process historical bars try { //KrakenOHLC(pair, m_barIntervalMinutes, false, false); // get the initial set of OHLC data (and calculate latest MACD values) GetOHLC(m_barIntervalMinutes, displayIndicator: false, limit: 500); // get the initial set of OHLC data (and calculate latest MACD values) //GetOHLC(m_barIntervalMinutes, false, true, 500); // get the initial set of OHLC data (and calculate latest MACD values) var hist = m_indicator.Last()[2]; // get the most recent "histogram" MACD value (watch this going positive/negative) cout("{0}>---initial MACD value: {1:0.00000000}", PairId, hist); // display the initial indicator value m_tradeLive = tradeLive; if (hist > 0) // set our initial "position" according to current "histogram" MACD value { m_pos = 1; } else { m_pos = -1; } } catch (Exception ex) { cout("{0}> An error occurred initializing the BINANCE MACD trade: {1}\nRestarting the trade usually solves the issue.", m_pair, ex.Message); return; } // If we are only running a backtest, then exit after running through the initial historical bar data if (backTestOnly) { return; } /*// ADD ORDER * var ticker = kraken.GetTicker(pair); * xpair = ohlc.Pairs.Keys.First(); * ticker.Display("ticker:"); * int dplaces = 1; * decimal percent = 0.05M; * var bid = Math.Round(ticker[xpair].BidPrice - percent * ticker[xpair].BidPrice, dplaces); // bid - 5% * var ask = Math.Round(ticker[xpair].AskPrice + percent * ticker[xpair].BidPrice, dplaces); // ask + 5% * var addorder = kraken.AddOrder(pair, "buy", bid, 0.001M, 2); * string[] txids; * if (addorder != null) * { * txids = addorder.Txid; * var descr = addorder.Descr; * cout(addorder.ToString()); * } * else * txids = null; * * // ORDERS, TRADES, POSITIONS * var openOrders = kraken.GetOpenOrders(); * var closedOrders = kraken.GetClosedOrders(); * openOrders.Display("open orders:"); * closedOrders.Display("closed orders:"); * var qo = kraken.QueryOrders(txids); * var qt = kraken.QueryTrades(txids); * var op = kraken.GetOpenPositions(txids); * * bool cancel = true; * if (cancel && txids != null) * { * // CANCEL PREVIOUSLY ADDED ORDER * foreach (var id in txids) * { * var cancelorder = kraken.CancelOrder(id); * cout(cancelorder.ToString()); * } * }*/ //ShowThreadInfo("Application"); var tind = Task.Run(() => IndicatorThread()); //var torders = Task.Run(() => OrdersThread()); //var tbalances = Task.Run(() => BalancesThread()); cout("Starting {0} trading system: {1} ...", m_exch.Name, m_pair); cout("Trades will be written to file: '{0}'", m_tradeLogPathname); }