void GotNewBar(string symbol, int interval) { // get current barlist for this symbol+interval BarList bl = track_barlists[symbol, interval]; // issue an event (todo: should I put this functionality into MessageResponseTemplate.cs? or should I get rid of GotNewBar at all? hmm...) will stay here for a while.. BsonDocument bson_doc = new BsonDocument(); bson_doc = new BsonDocument(); bson_doc["Symbol"] = bl.RecentBar.Symbol; bson_doc["High"] = bl.RecentBar.High.ToString(); bson_doc["Low"] = bl.RecentBar.Low.ToString(); bson_doc["Open"] = bl.RecentBar.Open.ToString(); bson_doc["Close"] = bl.RecentBar.Close.ToString(); bson_doc["Volume"] = bl.RecentBar.Volume.ToString(); bson_doc["isNew"] = bl.RecentBar.isNew; bson_doc["Bartime"] = bl.RecentBar.Bartime; bson_doc["Bardate"] = bl.RecentBar.Bardate; bson_doc["isValid"] = bl.RecentBar.isValid; bson_doc["Interval"] = bl.RecentBar.Interval; bson_doc["time"] = bl.RecentBar.time; //send_event(MimeType.got_new_bar, "bar", bson_doc.ToString()); log_file.WriteLine("got_new_bar" + bson_doc.ToString()); // get index for symbol int idx = track_symbols.getindex(symbol); string dbg_msg = "GotNewBar(" + symbol + ", " + interval + "):"; // check for first cross on first interval if (interval == _response_barsize_s) { decimal ema = Calc.Avg(Calc.EndSlice(bl.Close(), _ema_bar)); track_ema[symbol].Add(ema); // drawings... if (bl.Close().Length > _ema_bar) { // draw 2 sma lines: sendchartlabel(ema, time, System.Drawing.Color.FromArgb(0xff, 0x01, 0x01)); // do the trade (if required) decimal[] ema_arr = track_ema[symbol].ToArray(); decimal prev_ema = ema_arr[ema_arr.Length - 2]; decimal curr_ema = ema_arr[ema_arr.Length - 1]; decimal delta_ema = curr_ema - prev_ema; // sma just crossed? bool should_buy = track_positions[symbol].isFlat && delta_ema >= 0.002m; bool should_sell = false; dbg_msg += " delta_ema=" + delta_ema.ToString("000.000"); /* * dbg_msg += " fast=" + curr_sma_fast.ToString("000.000"); * dbg_msg += " pr_slow=" + prev_sma_slow.ToString("000.000"); * dbg_msg += " pr_fast=" + prev_sma_fast.ToString("000.000"); * dbg_msg += " [" + symbol + "].isFlat=" + track_positions[symbol].isFlat.ToString(); * dbg_msg += " track_positions[symbol].ToString=" + track_positions[symbol].ToString(); * dbg_msg += " should_buy=" + should_buy.ToString(); * dbg_msg += " should_sell=" + should_sell.ToString(); */ //senddebug("GotNewBar(): " + debug_position_tracker(symbol)); if (should_buy) { // come buy some! (c) Duke Nukem string comment = " BuyMarket(" + symbol + ", " + EntrySize.ToString() + ")"; sendorder(new BuyMarket(symbol, EntrySize, comment)); log_file.WriteLine("close position: " + comment); dbg_msg += comment; } if (false) // we do all the selling on tick() { if (!track_positions[symbol].isFlat && should_sell) // we don't short, so also check if !flat //if ( should_sell) // we don't short, so also check if !flat { sendorder(new SellMarket(symbol, EntrySize)); dbg_msg += " SellMarket(" + symbol + ", " + EntrySize.ToString() + ")"; } } } } //else //{ // // // dbg_msg += "GotNewBar() other interval=" + interval; //} // spit out one dbg message line per bar D(dbg_msg); // nice way to notify of current tracker values sendindicators(gt.GetIndicatorValues(idx, gens())); return; }
void GotNewBar(string symbol, int interval) { //if (symbol.ToUpper() != "ABX") return; // get current barlist for this symbol+interval BarList bl = track_barlists[symbol, interval]; // get index for symbol int idx = track_symbols.getindex(symbol); string dbg_msg = "GotNewBar(" + symbol + ", " + interval + "):"; // check for first cross on first interval if (interval == (int)BarInterval.Minute) { decimal no_tracker_slow_ma = Calc.Avg(Calc.EndSlice(bl.Close(), _slow_ma_bar)); decimal no_tracker_fast_ma = Calc.Avg(Calc.EndSlice(bl.Close(), _fast_ma_bar)); track_sma_slow[symbol].Add(no_tracker_slow_ma); track_sma_fast[symbol].Add(no_tracker_fast_ma); // drawings... if (bl.Close().Length > 1) { // this is how we draw line, which connects all bars close. //decimal val = bl.Close()[bl.Close().Length - 2]; // length-1 would be last, so length-2 is our previus bar //int time_prev = bl.Time()[bl.Time().Length - 2]; //sendchartlabel(val, time_prev, System.Drawing.Color.Green); // draw 2 sma lines: sendchartlabel(no_tracker_slow_ma, time, System.Drawing.Color.Blue); sendchartlabel(no_tracker_fast_ma, time, System.Drawing.Color.FromArgb(0xff, 0x01, 0x01)); // wtf? why red line multiplies after each sell order?! //sendchartlabel(bl.Close()[bl.Close().Length - 2], // time, // (time - 60).ToString(), // System.Drawing.Color.Green); // do the trade (if required) decimal[] sma_slow = track_sma_slow[symbol].ToArray(); decimal[] sma_fast = track_sma_fast[symbol].ToArray(); decimal prev_sma_slow = sma_slow[sma_slow.Length - 2]; decimal prev_sma_fast = sma_fast[sma_fast.Length - 2]; decimal curr_sma_slow = sma_slow[sma_slow.Length - 1]; // imho quite ugly.. decimal curr_sma_fast = sma_fast[sma_fast.Length - 1]; // todo: read more on how to work with Lists // sma just crossed? bool should_buy = prev_sma_slow > prev_sma_fast && curr_sma_slow < curr_sma_fast; bool should_sell = prev_sma_slow <prev_sma_fast && curr_sma_slow> curr_sma_fast; dbg_msg += " slow=" + curr_sma_slow.ToString("000.000"); dbg_msg += " fast=" + curr_sma_fast.ToString("000.000"); dbg_msg += " pr_slow=" + prev_sma_slow.ToString("000.000"); dbg_msg += " pr_fast=" + prev_sma_fast.ToString("000.000"); dbg_msg += " [" + symbol + "].isFlat=" + track_positions[symbol].isFlat.ToString(); dbg_msg += " track_positions[symbol].ToString=" + track_positions[symbol].ToString(); dbg_msg += " should_buy=" + should_buy.ToString(); dbg_msg += " should_sell=" + should_sell.ToString(); //senddebug("GotNewBar(): " + debug_position_tracker(symbol)); if (should_buy) { // come buy some! (c) Duke Nukem sendorder(new BuyMarket(symbol, EntrySize)); dbg_msg += " BuyMarket(" + symbol + ", " + EntrySize.ToString() + ")"; } if (!track_positions[symbol].isFlat && should_sell) // we don't short, so also check if !flat //if ( should_sell) // we don't short, so also check if !flat { sendorder(new SellMarket(symbol, EntrySize)); dbg_msg += " SellMarket(" + symbol + ", " + EntrySize.ToString() + ")"; } } } //else //{ // // // dbg_msg += "GotNewBar() other interval=" + interval; //} // spit out one dbg message line per bar //senddebug(dbg_msg); // nice way to notify of current tracker values sendindicators(gt.GetIndicatorValues(idx, gens())); return; }