import AlgoPlus.TORAStockV3x as torastock


def loop_callback():
    global my_strategy

    step = 100
    my_strategy.count += 1

    if my_strategy.count == step * 1:
        new_order = torastock.buyOpen(my_strategy.trader, my_strategy.standard_id, 100, torastock.ENUM_OrderType_LimitAndWait, 19.5)
        my_strategy.order_map[new_order.OrderID] = new_order
    elif my_strategy.count == step * 2:
        new_order = torastock.buyOpen(my_strategy.trader, my_strategy.standard_id, 200, torastock.ENUM_OrderType_HomeBestLimitAndWait, 0)
        my_strategy.order_map[new_order.OrderID] = new_order
    elif my_strategy.count == step * 3:
        new_order = torastock.buyOpen(my_strategy.trader, my_strategy.standard_id, 300, torastock.ENUM_OrderType_BestLimitAndWait, 0)
        my_strategy.order_map[new_order.OrderID] = new_order
    elif my_strategy.count == step * 4:
        my_strategy.lastOrder = torastock.sellClose(my_strategy.trader, my_strategy.standard_id, 400, torastock.ENUM_OrderType_FiveBestLimitAndWait, 0)
    elif my_strategy.count == step * 5:
        new_order = torastock.buyOpen(my_strategy.trader, my_strategy.standard_id, 500, torastock.ENUM_OrderType_FrontierLimitAndWait, 0)
        my_strategy.order_map[new_order.OrderID] = new_order
    elif my_strategy.count == step * 6:
        new_order = torastock.buyOpen(my_strategy.trader, my_strategy.standard_id, 600, torastock.ENUM_OrderType_LimitAndWait, 18)
        my_strategy.order_map[new_order.OrderID] = new_order


class MyStrategy:
    def __init__(self, standard_id):
        self.login_field = torastock.CAPLoginField()
        self.trader = None
        self.standard_id = standard_id
        self.count = 0
        self.order_map = {}


if __name__ == '__main__':

    print(f"||Version||,Version:{torastock.getVersion()}")

    my_strategy = MyStrategy('000001')

    my_strategy.login_field.UserType = torastock.ENUM_UserType_NSIGHTStock
    my_strategy.login_field.UserID = ""
    my_strategy.login_field.Password = ""
    # NSight股票仿真交易环境（http://n-sight.com.cn）
    my_strategy.login_field.TraderFrontAddress = "tcp://210.14.72.11:4400"
    my_strategy.login_field.MdFrontAddress = "tcp://210.14.72.11:4402"

    my_strategy.trader = torastock.init(1, my_strategy.login_field, None, None, loop_callback, None)

    if my_strategy.trader is not None:
        print("||init||,Succeed")
        torastock.subscribeOne(my_strategy.trader, my_strategy.standard_id)
        torastock.loop()
    else:
        error_id = torastock.getInitError()
        message = torastock.getMessageByErrorID(error_id)
        print(f"||init||,ErrorID:{error_id},Message:{message}")
