public Performance(Performance p) { perf = p.perf; total = p.total; price = p.price; num = p.num; }
public Performance AddBuy(int price, int seq) { Trade trade; String cmd; Performance per = new Performance(); if (sellnum != 0) { do { remain--; trade = tqueue.Dequeue(); trade.type = Trade.TRADE_BUY; trade.end = price; trade.remain = remain; trade.performance = trade.price - trade.end - 1; per.perf += trade.performance; performance += trade.performance; trade.seq = seq; cmd = String.Format("INSERT INTO Trade (type, no, price, end, remain, performance, seq) VALUES ("); cmd += System.Convert.ToString(trade.type) + ","; cmd += System.Convert.ToString(trade.no) + ","; cmd += System.Convert.ToString(trade.price) + ","; cmd += System.Convert.ToString(trade.end) + ","; cmd += System.Convert.ToString(trade.remain) + ","; cmd += System.Convert.ToString(trade.performance) + ","; cmd += System.Convert.ToString(trade.seq) + ");"; kit.CmdNonQuery(cmd); sellnum--; } while (sellnum > 0); } trade = new Trade(); buynum++; remain++; trade.type = Trade.TRADE_BUY; trade.no = buynum; trade.price = price; trade.end = 0; trade.remain = remain; trade.performance = 0; trade.seq = seq; tqueue.Enqueue(new Trade(trade)); cmd = String.Format("INSERT INTO Trade (type, no, price, end, remain, performance, seq) VALUES ("); cmd += System.Convert.ToString(trade.type) + ","; cmd += System.Convert.ToString(trade.no) + ","; cmd += System.Convert.ToString(trade.price) + ","; cmd += System.Convert.ToString(trade.end) + ","; cmd += System.Convert.ToString(trade.remain) + ","; cmd += System.Convert.ToString(trade.performance) + ","; cmd += System.Convert.ToString(trade.seq) + ");"; kit.CmdNonQuery(cmd); per.total = performance; per.price = trade.price; per.num = buynum; return per; }
public List<SQLiteKit> CalForce(String path, int scope, int stop) { TickBase tickBase = new TickBase(path); TickBaseHandle handle = new TickBaseHandle(); GraphBase graphBase = new GraphBase(System.DateTime.Now, 100); FutureTick tick; TickGraph graph = new TickGraph(); TickGraph average = new TickGraph(); TickGraph[] graph_a = new TickGraph[50]; TickGraph[] average_a = new TickGraph[50]; int seq = 0; int make = 0; long lastSec = 0; long lastPrice = 0; long lastAvg = 0; long lastDirect = 0; long lastHigh = 0; long lastLow = 0; long lastIndex = 0; long td = 0; long direct = 0; long dotPV = 0; long posForce = 0; long nagForce = 0; Performance per = new Performance(); graphBase.CreateTable("TXF_K"); graphBase.CreateTable("TXF_A"); for (int i = 1; i <= 50; i++) { String table = String.Format("TXF_K{0}", i); graphBase.CreateTable(table); graph_a[i - 1] = new TickGraph(); table = String.Format("TXF_A{0}", i); graphBase.CreateTable(table); average_a[i - 1] = new TickGraph(); } do { tick = tickBase.GetFutureTick("TXF", ref handle); if (tick == null) { break; } if (tick.buy < 0 && tick.sell < 0) { continue; } if (graph.type == TICK_GRAPH.TICK_GRAPH_NONE) { lastSec = tick.seqsec; lastPrice = tick.price; dotPV = 0; graph.type = TICK_GRAPH.TICK_GRAPH_KBAR; graph.value[TickGraph.VALUE_A] = tick.price; graph.value[TickGraph.VALUE_B] = tick.price; graph.value[TickGraph.VALUE_C] = tick.price; graph.value[TickGraph.VALUE_D] = tick.price; graph.value[TickGraph.VALUE_E] = handle.index; average.type = TICK_GRAPH.TICK_GRAPH_LINE; average.value[TickGraph.VALUE_B] = tick.price * tick.quanty; average.value[TickGraph.VALUE_C] = tick.quanty; average.value[TickGraph.VALUE_A] = tick.price; lastAvg = -1; lastDirect = 0; } else { /*if (tick.price - graph.value[TickGraph.VALUE_D] > scope || graph.value[TickGraph.VALUE_C] - tick.price > scope || tick.seqsec - lastSec >= sec)*/ //if (Math.Abs(dotPV) > Math.Abs(average.value[TickGraph.VALUE_A] - lastAvg) * scope if (Math.Abs(dotPV) > scope /*if (per.num == 0 && Math.Abs(dotPV) > scope || per.num > 0 && Math.Abs(dotPV) > (scope / per.num)*/ /*|| direct > 0 && dotPV < -stop || direct < 0 && dotPV > stop*/ || td == 0 && direct > 0 && average.value[TickGraph.VALUE_A] < lastAvg || td > 0 && tick.price > td || td == 0 && direct < 0 && average.value[TickGraph.VALUE_A] > lastAvg || td < 0 && tick.price < -td || direct > 0 && tick.price < lastLow || direct < 0 && tick.price > lastHigh || handle.last == 1) { td = 0; if (lastAvg != -1) { if (direct > 0 && average.value[TickGraph.VALUE_A] < lastAvg) { td = lastHigh; } else if (direct < 0 && average.value[TickGraph.VALUE_A] > lastAvg) { td = -lastLow; } if (average.value[TickGraph.VALUE_A] > lastAvg) { direct = 1; } else if (average.value[TickGraph.VALUE_A] < lastAvg) { direct = -1; } } average.value[TickGraph.VALUE_A] = average.value[TickGraph.VALUE_B] / average.value[TickGraph.VALUE_C]; /*if (dotPV > 0) { direct = 1; } else if (dotPV < 0) { direct = -1; }*/ graph.value[TickGraph.VALUE_J] = direct; graph.value[TickGraph.VALUE_K] = dotPV; seq++; if (handle.last == 1/* || average.value[TickGraph.VALUE_A] == lastAvg*/) { per = Reduce(tick.price, seq, -1); graph.value[TickGraph.VALUE_F] = 0; graph.value[TickGraph.VALUE_G] = per.price; graph.value[TickGraph.VALUE_H] = per.perf; graph.value[TickGraph.VALUE_I] = per.total; Console.WriteLine("\tmake = {0} , Buy {1}", per.total, tick.price); } else if (direct == 1) { if (average.value[TickGraph.VALUE_A] > lastAvg) { per = AddBuy(tick.price, seq); graph.value[TickGraph.VALUE_F] = 1; graph.value[TickGraph.VALUE_G] = per.price; graph.value[TickGraph.VALUE_H] = per.perf; graph.value[TickGraph.VALUE_I] = per.total; Console.WriteLine("\tmake = {0} , Buy {1}", per.total, tick.price); } else if (dotPV < 0 || average.value[TickGraph.VALUE_A] < lastAvg) { per = AddSell(tick.price, seq); graph.value[TickGraph.VALUE_F] = -1; graph.value[TickGraph.VALUE_G] = per.price; graph.value[TickGraph.VALUE_H] = per.perf; graph.value[TickGraph.VALUE_I] = per.total; Console.WriteLine("\tmake = {0} , Sell {1}", per.total, tick.price); } } else if (direct == -1) { if (average.value[TickGraph.VALUE_A] < lastAvg) { per = AddSell(tick.price, seq); graph.value[TickGraph.VALUE_F] = -1; graph.value[TickGraph.VALUE_G] = per.price; graph.value[TickGraph.VALUE_H] = per.perf; graph.value[TickGraph.VALUE_I] = per.total; Console.WriteLine("\tmake = {0} , Sell {1}", per.total, tick.price); } else if (dotPV > 0 || average.value[TickGraph.VALUE_A] > lastAvg) { per = AddBuy(tick.price, seq); graph.value[TickGraph.VALUE_F] = 1; graph.value[TickGraph.VALUE_G] = per.price; graph.value[TickGraph.VALUE_H] = per.perf; graph.value[TickGraph.VALUE_I] = per.total; Console.WriteLine("\tmake = {0} , Buy {1}", per.total, tick.price); } } graph.value[TickGraph.VALUE_L] = posForce; graph.value[TickGraph.VALUE_M] = nagForce; graph.value[TickGraph.VALUE_N] = td; dotPV = 0; posForce = 0; nagForce = 0; lastSec = tick.seqsec; lastIndex = handle.index; lastPrice = tick.price; lastAvg = average.value[TickGraph.VALUE_A]; lastHigh = graph.value[TickGraph.VALUE_C]; lastLow = graph.value[TickGraph.VALUE_D]; graphBase.AddNewItem("TXF_A", average); seq = graphBase.AddNewItem("TXF_K", graph); graph = new TickGraph(); graph.type = TICK_GRAPH.TICK_GRAPH_KBAR; graph.value[TickGraph.VALUE_A] = tick.price; graph.value[TickGraph.VALUE_B] = tick.price; graph.value[TickGraph.VALUE_C] = tick.price; graph.value[TickGraph.VALUE_D] = tick.price; graph.value[TickGraph.VALUE_E] = handle.index; average = new TickGraph(); average.type = TICK_GRAPH.TICK_GRAPH_LINE; average.value[TickGraph.VALUE_B] = tick.price * tick.quanty; average.value[TickGraph.VALUE_C] = tick.quanty; average.value[TickGraph.VALUE_A] = tick.price; for (int i = 1; i <= 50; i++) { graph_a[i - 1].type = TICK_GRAPH.TICK_GRAPH_LINE; graph_a[i - 1].value[TickGraph.VALUE_A] = graphBase.CalAverage("TXF_K", TickGraph.VALUE_C, i); String table = String.Format("TXF_K{0}", i); graphBase.AddNewItem(table, graph_a[i - 1]); average_a[i - 1].type = TICK_GRAPH.TICK_GRAPH_LINE; average_a[i - 1].value[TickGraph.VALUE_A] = graphBase.CalAverage("TXF_A", TickGraph.VALUE_A, i); table = String.Format("TXF_A{0}", i); graphBase.AddNewItem(table, average_a[i - 1]); } } else { graph.value[TickGraph.VALUE_B] = tick.price; if (tick.price > graph.value[TickGraph.VALUE_C]) graph.value[TickGraph.VALUE_C] = tick.price; if (tick.price < graph.value[TickGraph.VALUE_D]) graph.value[TickGraph.VALUE_D] = tick.price; average.value[TickGraph.VALUE_B] += tick.price * tick.quanty; average.value[TickGraph.VALUE_C] += tick.quanty; average.value[TickGraph.VALUE_A] = average.value[TickGraph.VALUE_B] / average.value[TickGraph.VALUE_C]; if (tick.price >= lastPrice) { long f = (tick.price - lastPrice) * (tick.price - lastPrice) * tick.quanty; dotPV += f; posForce += f; } else { long f = (tick.price - lastPrice) * (tick.price - lastPrice) * tick.quanty; dotPV -= f; nagForce += f; } lastPrice = tick.price; } } } while (true); return tickBase.GetFiles(); }
public Performance Reduce(int price, int seq, int num) { Trade trade; String cmd; Performance per = new Performance(); if (sellnum != 0) { if (num == -1) { num = sellnum; } do { remain--; trade = tqueue.Dequeue(); trade.type = Trade.TRADE_BUY; trade.end = price; trade.remain = remain; trade.performance = trade.price - trade.end - 1; per.perf += trade.performance; performance += trade.performance; trade.seq = seq; cmd = String.Format("INSERT INTO Trade (type, no, price, end, remain, performance, seq) VALUES ("); cmd += System.Convert.ToString(trade.type) + ","; cmd += System.Convert.ToString(trade.no) + ","; cmd += System.Convert.ToString(trade.price) + ","; cmd += System.Convert.ToString(trade.end) + ","; cmd += System.Convert.ToString(trade.remain) + ","; cmd += System.Convert.ToString(trade.performance) + ","; cmd += System.Convert.ToString(trade.seq) + ");"; kit.CmdNonQuery(cmd); sellnum--; } while (--num > 0); per.num = sellnum; } else if (buynum != 0) { if (num == -1) { num = buynum; } do { remain--; trade = tqueue.Dequeue(); trade.type = Trade.TRADE_SELL; trade.end = price; trade.remain = remain; trade.performance = trade.end - trade.price - 1; per.perf += trade.performance; performance += trade.performance; trade.seq = seq; cmd = String.Format("INSERT INTO Trade (type, no, price, end, remain, performance, seq) VALUES ("); cmd += System.Convert.ToString(trade.type) + ","; cmd += System.Convert.ToString(trade.no) + ","; cmd += System.Convert.ToString(trade.price) + ","; cmd += System.Convert.ToString(trade.end) + ","; cmd += System.Convert.ToString(trade.remain) + ","; cmd += System.Convert.ToString(trade.performance) + ","; cmd += System.Convert.ToString(trade.seq) + ");"; kit.CmdNonQuery(cmd); buynum--; } while (--num > 0); per.num = buynum; } per.total = performance; return per; }