#!/usr/bin/env python
# coding:utf-8
from PoboAPI import *
import datetime
import numpy as np

#开始时间，用于初始化一些参数
def OnStart(context) :
    print("I\'m starting...")
    #登录交易账号，需在主页用户管理中设置账号，并把期货测试替换成您的账户名称
    context.myacc = None
    if "回测期货" in context.accounts :
        print("登录交易账号[回测期货]")
        if context.accounts["回测期货"].Login() :
            context.myacc = context.accounts["回测期货"]

def OnMarketQuotationInitialEx(context, exchange,daynight):
    if exchange != 'SHFE':
        return
    #获取主力合约
    g.code = GetMainContract('SHFE', 'rb',20)
    #订阅K线数据，用于驱动OnBar事件
    SubscribeBar(g.code, BarType.Day)
    
    tday=GetNthTradingDay(2018,12,6,"SHFE")
    
    
    print("tday "+str(tday)) 

def GetNthTradingDay(year,month,nth,exchange="SHFE"):#查询第几个交易日
    
    
    firstday=datetime.date(year=year, month=month, day=1)#first day of the month
    
    if month<12:
    
        lastday=datetime.date (year, month+1, 1) - datetime.timedelta (days = 1)
    else:
        lastday=datetime.date (year+1, 1, 1) - datetime.timedelta (days = 1)
        
    #print(firstday,lastday) 
    tradingdayslist= GetTradingDates(exchange,firstday,lastday)
    
    #print(tradingdayslist)
    
    
    return tradingdayslist[nth-1]
    
    
    
    
    
#实时行情事件，当有新行情出现时调用该事件Ex
def OnBar(context,code,bartype):
    #过滤掉不需要的行情通知
    if code != g.code:
        return
    dyndata = GetQuote(g.code)
    #计算均线
    MA = GetIndicator("MA",code,params=(5,10),bar_type = BarType.Day)
    MA1 = MA["MA(5)"]
    MA2 = MA["MA(10)"]
    if len(MA2)<2:
        return
    #ma1上穿ma2时买入螺纹主力1手
    elif MA1[-1] >= MA2[-1] and MA1[-2]<MA2[-2]:
        QuickInsertOrder(context.myacc,g.code,'buy','open',dyndata.now,10)
    #ma1下穿ma2时卖出平仓
    elif MA1[-1] <= MA2[-1] and MA1[-2]>MA2[-2]:
        QuickInsertOrder(context.myacc,g.code,'sell','close',dyndata.now,10)
        

9a2bb2528c4f0393b85b7dd3091da8f515c8b307
<mask token>


def OnStart(context):
    print("I'm starting...")
    context.myacc = None
    if '回测期货' in context.accounts:
        print('登录交易账号[回测期货]')
        if context.accounts['回测期货'].Login():
            context.myacc = context.accounts['回测期货']


<mask token>


def GetNthTradingDay(year, month, nth, exchange='SHFE'):
    firstday = datetime.date(year=year, month=month, day=1)
    if month < 12:
        lastday = datetime.date(year, month + 1, 1) - datetime.timedelta(days=1
            )
    else:
        lastday = datetime.date(year + 1, 1, 1) - datetime.timedelta(days=1)
    tradingdayslist = GetTradingDates(exchange, firstday, lastday)
    return tradingdayslist[nth - 1]


<mask token>

<mask token>


def OnStart(context):
    print("I'm starting...")
    context.myacc = None
    if '回测期货' in context.accounts:
        print('登录交易账号[回测期货]')
        if context.accounts['回测期货'].Login():
            context.myacc = context.accounts['回测期货']


<mask token>


def GetNthTradingDay(year, month, nth, exchange='SHFE'):
    firstday = datetime.date(year=year, month=month, day=1)
    if month < 12:
        lastday = datetime.date(year, month + 1, 1) - datetime.timedelta(days=1
            )
    else:
        lastday = datetime.date(year + 1, 1, 1) - datetime.timedelta(days=1)
    tradingdayslist = GetTradingDates(exchange, firstday, lastday)
    return tradingdayslist[nth - 1]


def OnBar(context, code, bartype):
    if code != g.code:
        return
    dyndata = GetQuote(g.code)
    MA = GetIndicator('MA', code, params=(5, 10), bar_type=BarType.Day)
    MA1 = MA['MA(5)']
    MA2 = MA['MA(10)']
    if len(MA2) < 2:
        return
    elif MA1[-1] >= MA2[-1] and MA1[-2] < MA2[-2]:
        QuickInsertOrder(context.myacc, g.code, 'buy', 'open', dyndata.now, 10)
    elif MA1[-1] <= MA2[-1] and MA1[-2] > MA2[-2]:
        QuickInsertOrder(context.myacc, g.code, 'sell', 'close', dyndata.
            now, 10)

<mask token>


def OnStart(context):
    print("I'm starting...")
    context.myacc = None
    if '回测期货' in context.accounts:
        print('登录交易账号[回测期货]')
        if context.accounts['回测期货'].Login():
            context.myacc = context.accounts['回测期货']


def OnMarketQuotationInitialEx(context, exchange, daynight):
    if exchange != 'SHFE':
        return
    g.code = GetMainContract('SHFE', 'rb', 20)
    SubscribeBar(g.code, BarType.Day)
    tday = GetNthTradingDay(2018, 12, 6, 'SHFE')
    print('tday ' + str(tday))


def GetNthTradingDay(year, month, nth, exchange='SHFE'):
    firstday = datetime.date(year=year, month=month, day=1)
    if month < 12:
        lastday = datetime.date(year, month + 1, 1) - datetime.timedelta(days=1
            )
    else:
        lastday = datetime.date(year + 1, 1, 1) - datetime.timedelta(days=1)
    tradingdayslist = GetTradingDates(exchange, firstday, lastday)
    return tradingdayslist[nth - 1]


def OnBar(context, code, bartype):
    if code != g.code:
        return
    dyndata = GetQuote(g.code)
    MA = GetIndicator('MA', code, params=(5, 10), bar_type=BarType.Day)
    MA1 = MA['MA(5)']
    MA2 = MA['MA(10)']
    if len(MA2) < 2:
        return
    elif MA1[-1] >= MA2[-1] and MA1[-2] < MA2[-2]:
        QuickInsertOrder(context.myacc, g.code, 'buy', 'open', dyndata.now, 10)
    elif MA1[-1] <= MA2[-1] and MA1[-2] > MA2[-2]:
        QuickInsertOrder(context.myacc, g.code, 'sell', 'close', dyndata.
            now, 10)

from PoboAPI import *
import datetime
import numpy as np


def OnStart(context):
    print("I'm starting...")
    context.myacc = None
    if '回测期货' in context.accounts:
        print('登录交易账号[回测期货]')
        if context.accounts['回测期货'].Login():
            context.myacc = context.accounts['回测期货']


def OnMarketQuotationInitialEx(context, exchange, daynight):
    if exchange != 'SHFE':
        return
    g.code = GetMainContract('SHFE', 'rb', 20)
    SubscribeBar(g.code, BarType.Day)
    tday = GetNthTradingDay(2018, 12, 6, 'SHFE')
    print('tday ' + str(tday))


def GetNthTradingDay(year, month, nth, exchange='SHFE'):
    firstday = datetime.date(year=year, month=month, day=1)
    if month < 12:
        lastday = datetime.date(year, month + 1, 1) - datetime.timedelta(days=1
            )
    else:
        lastday = datetime.date(year + 1, 1, 1) - datetime.timedelta(days=1)
    tradingdayslist = GetTradingDates(exchange, firstday, lastday)
    return tradingdayslist[nth - 1]


def OnBar(context, code, bartype):
    if code != g.code:
        return
    dyndata = GetQuote(g.code)
    MA = GetIndicator('MA', code, params=(5, 10), bar_type=BarType.Day)
    MA1 = MA['MA(5)']
    MA2 = MA['MA(10)']
    if len(MA2) < 2:
        return
    elif MA1[-1] >= MA2[-1] and MA1[-2] < MA2[-2]:
        QuickInsertOrder(context.myacc, g.code, 'buy', 'open', dyndata.now, 10)
    elif MA1[-1] <= MA2[-1] and MA1[-2] > MA2[-2]:
        QuickInsertOrder(context.myacc, g.code, 'sell', 'close', dyndata.
            now, 10)

#!/usr/bin/env python
# coding:utf-8
from PoboAPI import *
import datetime
import numpy as np

#开始时间，用于初始化一些参数
def OnStart(context) :
    print("I\'m starting...")
    #登录交易账号，需在主页用户管理中设置账号，并把期货测试替换成您的账户名称
    context.myacc = None
    if "回测期货" in context.accounts :
        print("登录交易账号[回测期货]")
        if context.accounts["回测期货"].Login() :
            context.myacc = context.accounts["回测期货"]

def OnMarketQuotationInitialEx(context, exchange,daynight):
    if exchange != 'SHFE':
        return
    #获取主力合约
    g.code = GetMainContract('SHFE', 'rb',20)
    #订阅K线数据，用于驱动OnBar事件
    SubscribeBar(g.code, BarType.Day)
    
    tday=GetNthTradingDay(2018,12,6,"SHFE")
    
    
    print("tday "+str(tday)) 

def GetNthTradingDay(year,month,nth,exchange="SHFE"):#查询第几个交易日
    
    
    firstday=datetime.date(year=year, month=month, day=1)#first day of the month
    
    if month<12:
    
        lastday=datetime.date (year, month+1, 1) - datetime.timedelta (days = 1)
    else:
        lastday=datetime.date (year+1, 1, 1) - datetime.timedelta (days = 1)
        
    #print(firstday,lastday) 
    tradingdayslist= GetTradingDates(exchange,firstday,lastday)
    
    #print(tradingdayslist)
    
    
    return tradingdayslist[nth-1]
    
    
    
    
    
#实时行情事件，当有新行情出现时调用该事件Ex
def OnBar(context,code,bartype):
    #过滤掉不需要的行情通知
    if code != g.code:
        return
    dyndata = GetQuote(g.code)
    #计算均线
    MA = GetIndicator("MA",code,params=(5,10),bar_type = BarType.Day)
    MA1 = MA["MA(5)"]
    MA2 = MA["MA(10)"]
    if len(MA2)<2:
        return
    #ma1上穿ma2时买入螺纹主力1手
    elif MA1[-1] >= MA2[-1] and MA1[-2]<MA2[-2]:
        QuickInsertOrder(context.myacc,g.code,'buy','open',dyndata.now,10)
    #ma1下穿ma2时卖出平仓
    elif MA1[-1] <= MA2[-1] and MA1[-2]>MA2[-2]:
        QuickInsertOrder(context.myacc,g.code,'sell','close',dyndata.now,10)
        
