Ejemplo n.º 1
0
def gain_over_time(ma, doPlot):
  print "## moving average of %i" % ma
  policy = TradingPolicySimpleMA(ma, Broker()) 
  exe = TradeExecuter(startDate, policy )
  
  exe.runAll(r)   
  
  res = (capitalYield,     annualYield, 
   capitalYieldWCost,      annualYieldWCost, 
   indexYield,             annualIndexYield, 
   tradeEfficiencyWCost,   tradeEfficiency,
   tradeCost)        = exe.getStat(1)
  
  print "start time = %i" % exe.initialTradingTime
  print "capitalHistory gain with cost = %.3f, annual yield = %.3f%s (%.2f years)" % (capitalYieldWCost, (annualYieldWCost-1)*100, "%", exe.nbTradeDay/365.0) 
  print "capitalHistory gain           = %.3f, annual yield = %.3f%s (%.2f years)" % (capitalYield, (annualYield-1)*100, "%", exe.nbTradeDay/365.0) 
  print "index   gain                  = %.3f, annual yield = %.3f%s (%.2f years)" % (indexYield, (annualIndexYield-1)*100, "%", exe.nbTradeDay/365.0) 
  print "EFFICIENCY = %.3f" % (tradeEfficiencyWCost)
  print "number of trade  = %i over %i trading day (%.2f%s), ie every %.2f days" % \
                    (exe.nbTrade, exe.nbTradeDay, float(exe.nbTrade)/exe.nbTradeDay*100, "%", exe.nbTradeDay/exe.nbTrade)
  print "total trade cost = %.2f" % (tradeCost)
       
  if doPlot:     
#   rect2 = [left, 0.1, width, 0.4]
#   ax2 = fig.add_axes(rect2) 
    plt.plot(
           range(len(prices[exe.initialTradingTime:])), prices[exe.initialTradingTime:],
           range(len(policy.maLarge[exe.initialTradingTime:])), policy.maLarge[exe.initialTradingTime:],
  #         range(len(ma10[initialTradingTime:])), ma10[initialTradingTime:],
           range(len(exe.capitalHistory)), exe.capitalHistory,
           range(len(exe.tradeStatus)), exe.tradeStatus,
           )
    
  return [res, exe.nbTrade, exe.nbTradeDay]
Ejemplo n.º 2
0
def gain_versus_ma(startMA, endMA, stepMA):
  res      = []
  effiency = []
  effiencyWCost = []
  
  maRange = range(startMA, endMA, stepMA)
  
  for ma in maRange:
    policy = TradingPolicySimpleMA(ma, Broker()) 
    exe = TradeExecuter(startDate, policy )
    
    exe.runAll(r)   
    
    ( capitalYield,         annualYield, 
      capitalYieldWCost,    annualYieldWCost, 
      indexYield,           annualIndexYield, 
      tradeEfficiencyWCost, tradeEfficiency,   
      tradeCost )                = exe.getStat(1)
     
    res.append(capitalYieldWCost)
    effiencyWCost.append(tradeEfficiencyWCost)
    effiency.append(tradeEfficiency)
    
  rect1 = [left, 0.55, width, 0.4]
 
  ax = fig.add_axes(rect1)  
  ax.plot(
#           maRange, res,
           maRange, effiency,
           maRange, effiencyWCost
           )