Ejemplo n.º 1
0
class InstrumentTest(TestCase):

    def __init__(self, *args, **kwargs):
        super(InstrumentTest, self).__init__(*args, **kwargs)
        fill_dummy_data()
        self.inst = Instrument()
        self.inst.name = "EURUSD"
        self.inst.description = "Eur Usd pair"
        self.inst.is_forex = True

    def test_ccy1(self):
        self.failUnless(self.inst.get_ccy1() == "EUR")

    def test_ccy2(self):
        self.failUnless(self.inst.get_ccy2() == "USD")

    def test_bars(self):
        instrument = Instrument.objects.get(name="GBPUSD")
        list = instrument.get_bar_collection(Period.get_period("H1"))
        self.assertTrue(list) # only confirm a list is returned, tested manually = order working

    def test_bars_timeframe(self):
        instrument = Instrument.objects.get(name="GBPUSD")
        start = datetime.datetime(2014,05,03)
        end = datetime.datetime(2014,05,06,hour=23,minute=59, second=59)
        list = instrument.get_bar_collection_timeframe()
        self.assertTrue(list)
        for i in list:
            self.assertTrue(start <= i <= end)
Ejemplo n.º 2
0
def fill_dummy_data():
    p = Period(name="H1")
    p.save()

    i = Instrument(name="GBPUSD", description="The cable!", is_forex = True)
    i.save()

    price = 1.4567
    initial_date = datetime.datetime(2014,05,01,hour=12,minute=00,second=00, tzinfo=utc)
    for x in range(1000):
        tstamp = initial_date + (x * timedelta(hours=1))
        price += random.randint(1,3) * random.randint(-1,1) * random.randint(0, 10) / 1e4
        ibar = IBar(start_time = tstamp, open=price, high=price, low=price, close=price, period=p, instrument=i)
        ibar.save()
Ejemplo n.º 3
0
 def handle(self, context, user_id):
     average_valuation_processor = AverageValuationProcessor()
     fifo_valuation_processor = FIFOValuationProcessor()
     transactions = list(Transaction.fetch_by_user_id(context, user_id))
     if len(transactions) is 0:
         raise Exception("No transactions found")
     average_valuation = average_valuation_processor(context, transactions)
     fifo_valuation = fifo_valuation_processor(context, transactions)
     instruments = {}
     display_str = "%s %d %s shares at USD %.2f / share on %s"
     string_values = []
     for transaction in transactions:
         action = 'Bought' if transaction.buy_or_sell == 'BUY' else 'Sold'
         quantity = transaction.quantity
         if transaction.position_id not in instruments:
             instruments[transaction.position_id] = Instrument.fetch_by_id(
                 context, transaction.position_id).name
         company = instruments[transaction.position_id]
         price = transaction.price
         date = transaction.date
         if type(date) == datetime:
             date = date.strftime("%d-%b-%y")
         else:
             date = datetime.strptime(date, "%Y-%m-%d %H:%M:%S")\
                     .strftime("%d-%b-%y")
         string_values.append(display_str %
                              (action, quantity, company, price, date))
     string_values.append("FIFO Valuation: $%.2f" % fifo_valuation)
     string_values.append("Average Cost Valuation: $%.2f" %
                          average_valuation)
     print '\n'.join(string_values)
     return string_values
Ejemplo n.º 4
0
 def __init__(self, *args, **kwargs):
     super(InstrumentTest, self).__init__(*args, **kwargs)
     fill_dummy_data()
     self.inst = Instrument()
     self.inst.name = "EURUSD"
     self.inst.description = "Eur Usd pair"
     self.inst.is_forex = True
Ejemplo n.º 5
0
 def sync_instrument(self, instrument: Instrument) -> None:
     blob_filepaths = instrument.get_blob_filepaths()
     for file in instrument.files:
         blob_filepath = blob_filepaths[file]
         sftp_path = f"{instrument.sftp_path}/{file}"
         log.info(
             f"Syncing file from SFTP: {sftp_path} to GCP: {blob_filepath}")
         self.sync_file(blob_filepath, sftp_path)
def verif():
    user = User.current_user(session['user_id'])
    instruments = Instrument.get_list()
    genre = Genre.get_list()
    return render_template('verif.html',
                           user=user,
                           instruments=instruments,
                           genres=genre)
Ejemplo n.º 7
0
    def setUp(self):
        """Create test client, add sample data."""
        
        db.drop_all()
        db.create_all()
        
        Steve1 = User.signup(username="******", password="******", first_name="steven", last_name="damouni", email="*****@*****.**")
        Steveid1 = 1111
        Steve1.id = Steveid1

        genre1 = Genre(genre="classical")
        genre1id = 1111
        genre1.id = genre1id

        region1 = Region(city="Seattle", county="King", state="Washington")
        region1id = 1111
        region1.id = region1id

        instrument1 = Instrument(instrument="piano")
        instrument1id = 1111
        instrument1.id = instrument1id

        db.session.add(genre1)
        db.session.add(region1)
        db.session.add(instrument1)
        db.session.commit()

        Steve1 = User.query.get(Steveid1)
        genre1 = Genre.query.get(genre1id)
        region1 = Region.query.get(region1id)
        instrument1 = Instrument.query.get(instrument1id)

        self.Steve1 = Steve1
        self.Steveid1 = Steveid1

        self.genre1 = genre1
        self.genre1id = genre1id

        self.region1 = region1
        self.region1id = region1id

        self.instrument1 = instrument1
        self.instrument1id = instrument1id

        self.client = app.test_client()
Ejemplo n.º 8
0
def get_instrument_list():
    """
    获取所有股票列表并保存到数据库
    """
    try:
        # 下载数据
        df = ts.get_stock_basics()

        # 按照代码排序
        df.sort_index(inplace=True)

        session = Session()

        codes = [item[0] for item in session.query(Instrument.code).all()]

        for index, row in df.iterrows():
            if index in codes:
                continue

            item = Instrument(index,
                              name=row['name'],
                              industry=nan_2_none(row['industry']),
                              area=nan_2_none(row['area']),
                              pe=value_2_float(row['pe']),
                              outstanding=value_2_float(row['outstanding']),
                              totals=value_2_float(row['totals']),
                              total_assets=value_2_float(row['totalAssets']),
                              liquid_assets=value_2_float(row['liquidAssets']),
                              fixed_assets=value_2_float(row['fixedAssets']),
                              reserved=value_2_float(row['reserved']),
                              reserved_per_share=value_2_float(
                                  row['reservedPerShare']),
                              esp=value_2_float(row['esp']),
                              bvps=value_2_float(row['bvps']),
                              pb=value_2_float(row['pb']),
                              time_2_market=value_2_float(row['timeToMarket']),
                              undp=value_2_float(row['undp']),
                              perundp=value_2_float(row['perundp']),
                              rev=value_2_float(row['rev']),
                              profit=value_2_float(row['profit']),
                              gpr=value_2_float(row['gpr']),
                              npr=value_2_float(row['npr']),
                              holders=value_2_float(row['holders']))

            session.add(item)

        session.commit()

        session.close()
    except Exception as e:
        print('Exception: {}'.format(repr(e)))

        # 发送异常通知
        #text.send_text("处理股票代码数据失败")

    return df
 def get_instrument_folders(self) -> Dict[str, Instrument]:
     instruments = {}
     for folder in self.sftp_connection.listdir(
             self.sftp_config.survey_source_path):
         if re.compile(self.config.instrument_regex).match(folder):
             log.info(f"Instrument folder found - {folder}")
             instruments[folder] = Instrument(
                 sftp_path=f"{self.sftp_config.survey_source_path}/{folder}"
             )
     return instruments
Ejemplo n.º 10
0
 def __call__(self, context, transactions):
     assert 'TransactionRepository' in context
     assert 'InstrumentRepository' in context
     self.transaction_repository = context['TransactionRepository']
     self.instrument_repository = context['InstrumentRepository']
     self.transactions = list(transactions)
     _instrument_ids = [u.position_id for u in self.transactions]
     self.instruments = dict(
         (u.identity, u)
         for u in Instrument.fetch_by_ids(context, _instrument_ids))
     return self.calculate()
Ejemplo n.º 11
0
def process_instrument(case_mover: CaseMover, instrument_name: str,
                       instrument: Instrument) -> None:
    log.info(
        f"Processing instrument - {instrument_name} - {instrument.sftp_path}")
    if case_mover.bdbx_md5_changed(instrument):
        log.info(f"Instrument - {instrument_name} - " +
                 "has no changes to the databse file, skipping...")
    else:
        log.info(f"Syncing instrument - {instrument_name}")
        case_mover.sync_instrument(instrument)
        case_mover.send_request_to_api(instrument.gcp_folder())
Ejemplo n.º 12
0
def new_instrument():
    """
    Add a new instrument
    """
    form = InstrumentForm(request.form)
    if request.method == 'POST' and form.validate():
        # save the instrument
        save_changes('instrument', Instrument(), form, new=True)
        flash('Instrument created successfully!')
        return redirect('/')

    return render_template('new_instrument.html', form=form)
def add_instrument():
    kind = request.json['kind']
    model = request.json['model']
    price = request.json['price']
    color = request.json['color']

    #init instruments with info
    new_instrument = Instrument(kind, model, price, color)

    db.session.add(new_instrument)
    db.session.commit()

    return instrument_schema.jsonify(new_instrument)
Ejemplo n.º 14
0
def test_sync_instrument(mock_sync_file, google_storage, config, mock_sftp):
    instrument = Instrument(
        sftp_path="./ONS/OPN/OPN2103A",
        bdbx_updated_at=datetime.fromisoformat("2021-05-20T10:21:53+00:00"),
        bdbx_md5="my_lovely_md5",
        files=[
            "oPn2103A.BdBx",
        ],
    )
    case_mover = CaseMover(google_storage, config, mock_sftp)
    case_mover.sync_instrument(instrument)
    mock_sync_file.assert_called_once_with(
        "opn2103a/opn2103a.bdbx", "./ONS/OPN/OPN2103A/oPn2103A.BdBx"
    )
 def _get_instrument_files_for_instrument(
         self, instrument: Instrument) -> List[str]:
     instrument_file_list = []
     for instrument_file in self.sftp_connection.listdir_attr(
             instrument.sftp_path):
         file_extension = pathlib.Path(
             instrument_file.filename).suffix.lower()
         if file_extension == ".bdbx":
             instrument.bdbx_updated_at = datetime.fromtimestamp(
                 instrument_file.st_mtime, tz=timezone.utc)
         if file_extension in self.config.extension_list:
             log.info(f"Instrument file found - {instrument_file.filename}")
             instrument_file_list.append(instrument_file.filename)
     return instrument_file_list
Ejemplo n.º 16
0
def test_bdbx_md5_changed_when_no_gcp_file(
    mock_get_blob_md5, google_storage, config, mock_sftp
):
    mock_get_blob_md5.return_value = None
    instrument = Instrument(
        sftp_path="ONS/OPN/OPN2103A",
        bdbx_updated_at=datetime.fromisoformat("2021-05-20T10:21:53+00:00"),
        bdbx_md5="my_lovely_md5",
        files=[
            "oPn2103A.BdBx",
        ],
    )
    case_mover = CaseMover(google_storage, config, mock_sftp)
    assert case_mover.bdbx_md5_changed(instrument) is False
    mock_get_blob_md5.assert_called_with("opn2103a/opn2103a.bdbx")
Ejemplo n.º 17
0
def get_instrument_list(config):
    try:
        # 下载数据
        df = ts.get_stock_basics()

        # 按照代码排序
        df.sort_index(inplace=True)

        # 保存到文件
        df.to_csv(config.INSTRUMENT_FILENAME)

        session = Session()

        session.query(Instrument).delete()

        for index, row in df.iterrows():
            item = Instrument(index,
                              name=row['name'],
                              industry=nan_2_none(row['industry']),
                              area=nan_2_none(row['area']),
                              pe=value_2_float(row['pe']),
                              outstanding=value_2_float(row['outstanding']),
                              totals=value_2_float(row['totals']),
                              total_assets=value_2_float(row['totalAssets']),
                              liquid_assets=value_2_float(row['liquidAssets']),
                              fixed_assets=value_2_float(row['fixedAssets']),
                              reserved=value_2_float(row['reserved']),
                              reserved_per_share=value_2_float(
                                  row['reservedPerShare']),
                              esp=value_2_float(row['esp']),
                              bvps=value_2_float(row['bvps']),
                              pb=value_2_float(row['pb']),
                              time_2_market=value_2_float(row['timeToMarket']),
                              undp=value_2_float(row['undp']),
                              perundp=value_2_float(row['perundp']),
                              rev=value_2_float(row['rev']),
                              profit=value_2_float(row['profit']),
                              gpr=value_2_float(row['gpr']),
                              npr=value_2_float(row['npr']),
                              holders=value_2_float(row['holders']))

            session.add(item)

        session.commit()

        session.close()
    except Exception, e:
        print 'Exception:', repr(e)
 def generate_bdbx_md5(self, instrument: Instrument) -> str:
     bdbx_file = instrument.bdbx_file()
     if not bdbx_file:
         log.info(
             f"No bdbx file for '{instrument.sftp_path}' cannot generate an md5"
         )
         return ""
     bdbx_details = self.sftp_connection.stat(bdbx_file)
     md5sum = hashlib.md5()
     chunks = math.ceil(bdbx_details.st_size / self.config.bufsize)
     sftp_file = self.sftp_connection.open(bdbx_file,
                                           bufsize=self.config.bufsize)
     for chunk in range(chunks):
         sftp_file.seek(chunk * self.config.bufsize)
         md5sum.update(sftp_file.read(self.config.bufsize))
     return md5sum.hexdigest()
Ejemplo n.º 19
0
def save_snapshot(exchange_name, instrument_name, bids_sizes, bids_prices,
                  asks_sizes, asks_prices, ts):
    exchange = Exchange.get(Exchange.name == exchange_name)
    db_instrument = Instrument.get((Instrument.name == instrument_name) &
                                   (Instrument.exchange_name == exchange.name))
    base = db_instrument.base
    quote = db_instrument.quote
    kind = db_instrument.kind
    snap = OrderbookRecord(base=base,
                           quote=quote,
                           exchange=exchange,
                           ask_sizes=asks_sizes,
                           ask_prices=asks_prices,
                           bid_sizes=bids_sizes,
                           bid_prices=bids_prices,
                           timestamp=ts,
                           kind=kind)
    return snap.save()
Ejemplo n.º 20
0
def create(category):
    if request.method == "GET":
        return render_template("create_instrument.html", category=category, categories=categories)
    else:
        data = request.form
        print(data)
        new_instrument = Instrument()
        new_instrument.make = data['make']
        new_instrument.model = data['model']
        new_instrument.year = data['year']
        new_instrument.description = data['description']
        new_instrument.category = category
        session.add(new_instrument)
        session.commit()
        return redirect(location="/{}".format(category))
Ejemplo n.º 21
0
def do_sandbox():

    #USER
    users_by_email = {}
    for (user_index, user_mock) in enumerate(user_mocks):
        query = User.query.filter_by(email=user_mock['email'])
        if query.count() == 0:
            user = User(from_dict=user_mock)
            user.validationToken = None
            Wrapper.check_and_save(user)
            set_from_mock("thumbs", user, user_index)
            print("CREATED user")
            pprint(vars(user))

            if 'role' in user_mock:
                role = Role()
                role.type = user_mock['role']
                role.user = user
                Wrapper.check_and_save(role)
                print("CREATED role")
                pprint(vars(role))
        else:
            user = query.first()
        users_by_email[user.email] = user

    #SAMPLE
    samples_by_name = {}
    for (sample_index, sample_mock) in enumerate(sample_mocks):
        query = Sample.query.filter_by(name=sample_mock['name'])
        if query.count() == 0:
            sample = Sample(from_dict=sample_mock)
            Wrapper.check_and_save(sample)
            set_from_mock("audios", sample, sample_index)
            print("CREATED sample")
            pprint(vars(sample))
        else:
            sample = query.first()
        samples_by_name[sample.name] = sample

    #INSTRUMENT
    instruments_by_name = {}
    for (instrument_index, instrument_mock) in enumerate(instrument_mocks):
        query = Instrument.query.filter_by(name=instrument_mock['name'])
        if query.count() == 0:
            instrument = Instrument(from_dict=instrument_mock)
            Wrapper.check_and_save(instrument)
            print("CREATED instrument")
            pprint(vars(instrument))
        else:
            instrument = query.first()
        instruments_by_name[instrument.name] = instrument

    #HARMONY
    harmonies_by_name = {}
    for (harmony_index, harmony_mock) in enumerate(harmony_mocks):
        query = Harmony.query.filter_by(name=harmony_mock['name'])
        if query.count() == 0:
            harmony = Harmony(from_dict=harmony_mock)
            Wrapper.check_and_save(harmony)
            print("CREATED harmony")
            pprint(vars(harmony))
        else:
            harmony = query.first()
        harmonies_by_name[harmony.name] = harmony

        #NOTE
        notes = []
        for note_index in range(harmony.notesMax):
            query = Note.query.filter_by(index=note_index)
            if query.count() == 0:
                note = Note(from_dict={ "index": note_index })
                if "noteNames" in harmony_mock and note_index in harmony_mock['noteNames']:
                    note.name = harmony_mock['noteNames'][note_index]
                Wrapper.check_and_save(note)
                print("CREATED note")
                pprint(vars(note))
            else:
                note = query.first()
            notes.append(note)

        #SCALE
        #for scaleSize in range(1, harmony.scaleMaxSize):
        for scaleSize in []:
            scale_note_indexes_combinations = harmony.get_scale_note_indexes_combinations(scaleSize)
            scale_note_indexes_combinations_length = str(len(scale_note_indexes_combinations))
            for (scale_index, scale_note_indexes) in enumerate(scale_note_indexes_combinations):
                query = Scale.query.filter_by(
                    combinationIndex=scale_index,
                    harmonyId=harmony.id,
                    size=scaleSize
                )
                if query.count() == 0:
                    scale = Scale()
                    scale.combinationIndex = scale_index
                    scale.harmony = harmony
                    scale.name = harmony_mock['get_scale_name'](scale_note_indexes)
                    scale.tags = harmony_mock['get_scale_tags'](scale_note_indexes)
                    scale.size = scaleSize
                    Wrapper.check_and_save(scale)
                    print("CREATED scale " + str(scale_index) + " / " + scale_note_indexes_combinations_length + "(" + str(scaleSize) + ")")
                    #pprint(vars(scale))
                    for scale_note_index in scale_note_indexes:
                        note = notes[scale_note_index]
                        scale_note = ScaleNote()
                        scale_note.scale = scale
                        scale_note.note = note
                        Wrapper.check_and_save(scale_note)
                        #print("CREATED scale_note")
                        #pprint(vars(scale_note))

    #SOUND
    for sound_mock in sound_mocks:
        instrument = instruments_by_name[sound_mock['instrumentName']]
        sample = samples_by_name[sound_mock['sampleName']]
        query = Sound.query.filter_by(
            instrumentId=instrument.id,
            pitch=sound_mock['pitch'],
            sampleId=sample.id
        )
        if query.count() == 0:
            sound = Sound(from_dict=sound_mock)
            sound.instrument = instrument
            sound.sample = sample
            Wrapper.check_and_save(sound)
            print("CREATED sound")
            pprint(vars(sound))

    #MELODY
    melodies_by_name = {}
    for melody_mock in melody_mocks:
        query = Melody.query.filter_by(name=melody_mock['name'])
        if query.count() == 0:
            melody = Melody(from_dict=melody_mock)
            Wrapper.check_and_save(melody)
            print("CREATED melody")
            pprint(vars(melody))
        else:
            melody = query.first()
        melodies_by_name[melody.name] = melody

    #RHYTHM
    rhythms_by_name = {}
    for rhythm_mock in rhythm_mocks:
        query = Rhythm.query.filter_by(name=rhythm_mock['name'])
        if query.count() == 0:
            rhythm = Rhythm(from_dict=rhythm_mock)
            Wrapper.check_and_save(rhythm)
            print("CREATED rhythm")
            pprint(vars(rhythm))
        else:
            rhythm = query.first()
        rhythms_by_name[rhythm.name] = rhythm

    #PATTERN
    patterns_by_name = {}
    for pattern_mock in pattern_mocks:
        melody = melodies_by_name[pattern_mock['melodyName']]
        rhythm = rhythms_by_name[pattern_mock['rhythmName']]
        query = Pattern.query.filter_by(
            melodyId=melody.id,
            rhythmId=rhythm.id
        )
        if query.count() == 0:
            pattern = Pattern(from_dict=pattern_mock)
            pattern.melody = melody
            pattern.rhythm = rhythm
            Wrapper.check_and_save(pattern)
            print("CREATED pattern")
            pprint(vars(pattern))
        else:
            pattern = query.first()
        patterns_by_name[pattern.name] = pattern


    #SCORE
    scores_by_name = {}
    for score_mock in score_mocks:
        query = Score.query.filter_by(name=score_mock['name'])
        if query.count() == 0:
            score = Score(from_dict=score_mock)
            user = users_by_email[score_mock['userEmail']]
            score.user = user
            Wrapper.check_and_save(score)
            print("CREATED score")
            pprint(vars(score))
        else:
            score = query.first()
        scores_by_name[score.name] = score

    #STAFF
    staves_by_name = {}
    for staff_mock in staff_mocks:
        query = Staff.query.filter_by(
            name=staff_mock['name']
        )
        if query.count() == 0:
            staff = Staff(from_dict=staff_mock)
            Wrapper.check_and_save(staff)
            print("CREATED staff")
            pprint(vars(staff))
        else:
            staff = query.first()
        staves_by_name[staff.name] = staff

    #SCORE STAFF
    for score_staff_mock in score_staff_mocks:
        score = scores_by_name[score_staff_mock['scoreName']]
        staff = staves_by_name[score_staff_mock['staffName']]
        query = ScoreStaff.query.filter_by(
            positionIndex=score_staff_mock['positionIndex'],
            scoreId=score.id,
            staffId=staff.id
        )
        if query.count() == 0:
            score_staff = ScoreStaff(from_dict=score_staff_mock)
            score_staff.score = score
            score_staff.staff = staff
            Wrapper.check_and_save(score_staff)
            print("CREATED score_staff")
            pprint(vars(score_staff))

    #SCORE INSTRUMENT
    for score_instrument_mock in score_instrument_mocks:
        score = scores_by_name[score_instrument_mock['scoreName']]
        instrument = instruments_by_name[score_instrument_mock['instrumentName']]
        query = ScoreInstrument.query.filter_by(
            positionIndex=score_instrument_mock['positionIndex'],
            scoreId=score.id,
            instrumentId=instrument.id
        )
        if query.count() == 0:
            score_instrument = ScoreInstrument(from_dict=score_instrument_mock)
            score_instrument.score = score
            score_instrument.instrument = instrument
            Wrapper.check_and_save(score_instrument)
            print("CREATED score_instrument")
            pprint(vars(score_instrument))

    #VOICE
    voices_by_name = {}
    for voice_mock in voice_mocks:
        query = Voice.query.filter_by(name=voice_mock['name'])
        if query.count() == 0:
            voice = Voice(from_dict=voice_mock)
            Wrapper.check_and_save(voice)
            print("CREATED voice")
            pprint(vars(voice))
        else:
            voice = query.first()
        voices_by_name[voice.name] = voice

    #VOICE PATTERNS
    for voice_pattern_mock in voice_pattern_mocks:
        voice = voices_by_name[voice_pattern_mock['voiceName']]
        pattern = patterns_by_name[voice_pattern_mock['patternName']]
        query = VoicePattern.query.filter_by(
            voiceId=voice.id,
            patternId=pattern.id
        )
        if query.count() == 0:
            voice_pattern = VoicePattern(from_dict=voice_pattern_mock)
            voice_pattern.voice = voice
            voice_pattern.pattern = pattern
            Wrapper.check_and_save(voice_pattern)
            print("CREATED voice_pattern")
            pprint(vars(voice_pattern))


    #STAFF VOICE
    for staff_voice_mock in staff_voice_mocks:
        staff = staves_by_name[staff_voice_mock['staffName']]
        voice = voices_by_name[staff_voice_mock['voiceName']]
        query = StaffVoice.query.filter_by(
            staffId=staff.id,
            voiceId=voice.id
        )
        if query.count() == 0:
            staff_voice = StaffVoice(from_dict=staff_voice_mock)
            staff_voice.staff = staff
            staff_voice.voice = voice
            Wrapper.check_and_save(staff_voice)
            print("CREATED staff_voice")
            pprint(vars(staff_voice))
Ejemplo n.º 22
0
 def bdbx_md5_changed(self, instrument: Instrument) -> bool:
     blob_md5 = self.google_storage.get_blob_md5(
         instrument.get_bdbx_blob_filepath())
     return instrument.bdbx_md5 == blob_md5
Ejemplo n.º 23
0
    def setUp(self):
        from models import Base, db_bootstrap

        self.engine = create_engine('sqlite://', echo=False)
        Base.metadata.create_all(self.engine)

        self.db_session = scoped_session(sessionmaker(bind=self.engine))
        TradeApplication.instance().db_session = self.db_session

        db_bootstrap(self.db_session)

        currencies = [[
            "BTC", u"฿", "Bitcoin", True, 10000, "{:,.8f}",
            u"฿ #,##0.00000000;(฿ #,##0.00000000)", "{:,.8f}",
            u"฿ #,##0.00000000;(฿ #,##0.0000000)"
        ],
                      [
                          "USD", u"$", "Dollar", False, 100, "{:,.2f}",
                          u"¤ #,##0.00;(¤ #,##0.00)", "{:,.2f}",
                          u"¤ #,##0.00;(¤ #,##0.00)"
                      ]]

        for c in currencies:
            e = Currency(code=c[0],
                         sign=c[1],
                         description=c[2],
                         is_crypto=c[3],
                         pip=c[4],
                         format_python=c[5],
                         format_js=c[6],
                         human_format_python=c[7],
                         human_format_js=c[8])
            self.db_session.add(e)
            self.db_session.commit()

        instruments = [["BTCUSD", "USD", "BTC / USD"]]
        for currency_description in instruments:
            e = Instrument(symbol=currency_description[0],
                           currency=currency_description[1],
                           description=currency_description[2])
            self.db_session.add(e)
            self.db_session.commit()

        # user root
        e = User(id=-1,
                 username='******',
                 email='*****@*****.**',
                 password='******',
                 country_code='US',
                 state='NY',
                 transaction_fee_buy=0,
                 transaction_fee_sell=0,
                 verified=3,
                 is_staff=True,
                 is_system=True,
                 is_broker=True,
                 email_lang='en')
        self.db_session.add(e)

        # user blinktrade
        e = User(id=8999999,
                 username='******',
                 email='*****@*****.**',
                 password='******',
                 country_code='US',
                 state='NY',
                 transaction_fee_buy=0,
                 transaction_fee_sell=0,
                 verified=3,
                 is_staff=True,
                 is_broker=True,
                 email_lang='en')
        self.db_session.add(e)

        # user exchange
        e = User(id=5,
                 username='******',
                 email='*****@*****.**',
                 broker_id=8999999,
                 broker_username='******',
                 password='******',
                 country_code='US',
                 state='NY',
                 transaction_fee_buy=0,
                 transaction_fee_sell=0,
                 verified=5,
                 is_broker=True,
                 email_lang='en')
        self.db_session.add(e)
        self.db_session.commit()

        # user exchange bonus
        e = User(id=90000000,
                 username='******',
                 email='*****@*****.**',
                 broker_id=5,
                 broker_username='******',
                 password='******',
                 country_code='US',
                 state='NY',
                 transaction_fee_buy=0,
                 transaction_fee_sell=0,
                 verified=5,
                 is_broker=True,
                 email_lang='en')
        self.db_session.add(e)
        self.db_session.commit()
        self.user_exchange_bonus = e

        # user exchange fees
        e = User(id=90000001,
                 username='******',
                 email='*****@*****.**',
                 broker_id=5,
                 broker_username='******',
                 password='******',
                 country_code='US',
                 state='NY',
                 transaction_fee_buy=0,
                 transaction_fee_sell=0,
                 verified=5,
                 is_broker=True,
                 email_lang='en')
        self.db_session.add(e)
        self.db_session.commit()
        self.user_exchange_fees = e

        # broker exchange
        e = Broker(
            id=5,
            short_name='exchange',
            business_name='BlinkTrade Demo Exchange',
            address='21 Bitcoin Ave',
            signup_label='BlinkTrade Demo Exchange',
            city='New York',
            state='NY',
            zip_code='10000',
            country_code='US',
            lang='en',
            country='United States',
            mandrill_api_key=None,
            mailer_from_name='BlinkTrade',
            mailer_from_email='*****@*****.**',
            mailer_signature='BlinkTrade Demo Exchange',
            mailchimp_list_id='5c7b7818d8',
            phone_number_1=None,
            phone_number_2=None,
            skype='blinktrade',
            email='*****@*****.**',
            verification_jotform=
            'https://secure.jotform.co/form/42336230941852?user_id={{UserID}}&username={{Username}}&broker_id={{BrokerID}}&broker_username={{BrokerUsername}}&email={{Email}}&phoneNumber[country]=1&address[state]={{State}}&address[country]=United+States',
            upload_jotform=
            'https://secure.jotform.co/form/42344880060854?user_id={{UserID}}&username={{Username}}&broker_id={{BrokerID}}&broker_username={{BrokerUsername}}&deposit_method={{DepositMethod}}&control_number={{ControlNumber}}&deposit_id={{DepositID}}',
            currencies='USD',
            withdraw_structure=json.dumps({
                "BTC": [{
                    "method":
                    "bitcoin",
                    "description":
                    "Bitcoin withdrawal",
                    "disclaimer":
                    "",
                    "percent_fee":
                    0,
                    "fixed_fee":
                    0,
                    "limits": {
                        "0": {
                            "enabled": True,
                            "min": 500000,
                            "max": 100000000
                        },
                        "1": {
                            "enabled": True,
                            "min": 500000,
                            "max": 100000000
                        },
                        "2": {
                            "enabled": True,
                            "min": 500000,
                            "max": 100000000
                        },
                        "3": {
                            "enabled": True,
                            "min": 500000
                        },
                        "4": {
                            "enabled": True,
                            "min": 500000
                        },
                        "5": {
                            "enabled": True,
                            "min": 500000
                        }
                    },
                    "fields": [{
                        "side": "client",
                        "name": "Wallet",
                        "validator": "validateAddress",
                        "type": "text",
                        "value": "",
                        "label": "Wallet",
                        "placeholder": ""
                    }, {
                        "side": "broker",
                        "name": "TransactionID",
                        "validator": "validateAlphaNum",
                        "type": "text",
                        "value": "",
                        "label": "TransactionID",
                        "placeholder": ""
                    }]
                }],
                "USD": [{
                    "method":
                    "swift",
                    "description":
                    "Swift International Transfer",
                    "disclaimer":
                    "84 hours, 1%  fee + $25",
                    "percent_fee":
                    1,
                    "fixed_fee":
                    2500000000,
                    "limits": {
                        "0": {
                            "enabled": False
                        },
                        "1": {
                            "enabled": False
                        },
                        "2": {
                            "enabled": False
                        },
                        "3": {
                            "enabled": True,
                            "min": 3500000000,
                            "max": 280000000000
                        },
                        "4": {
                            "enabled": True,
                            "min": 3500000000,
                            "max": 5000000000000
                        },
                        "5": {
                            "enabled": True,
                            "min": 3500000000
                        }
                    },
                    "fields": [{
                        "side": "client",
                        "name": "BankName",
                        "validator": "validateAlphaNum",
                        "type": "text",
                        "value": "",
                        "label": "Banco name",
                        "placeholder": "ex. JPMORGAN CHASE BANK, N.A"
                    }, {
                        "side": "client",
                        "name": "BankSwift",
                        "validator": "validateAlphaNum",
                        "type": "text",
                        "value": "",
                        "label": "Swift code",
                        "placeholder": "ex. CHASUS33"
                    }, {
                        "side": "client",
                        "name": "RoutingNumber",
                        "validator": "validateAlphaNum",
                        "type": "text",
                        "value": "",
                        "label": "Routing Number",
                        "placeholder": "ex. 021000021"
                    }, {
                        "side": "client",
                        "name": "AccountNumber",
                        "validator": "validateAlphaNum",
                        "type": "text",
                        "value": "",
                        "label": "Account Number",
                        "placeholder": "ex. 88888-8"
                    }, {
                        "side": "broker",
                        "name": "TransactionID",
                        "validator": "validateAlphaNum",
                        "type": "text",
                        "value": "",
                        "label": "TransactionID",
                        "placeholder": ""
                    }]
                }]
            }).decode('utf-8'),
            crypto_currencies=json.dumps([{
                "Wallets": [{
                    "managed_by": "BlinkTrade, Exchange Operator, Mediator ",
                    "signatures": [],
                    "type": "cold",
                    "multisig": False,
                    "address": "n3yyGwzyfTxbKB8hkkv2AsQ9nBQgEozsV4"
                }, {
                    "managed_by": "Exchange Operator ",
                    "signatures": [],
                    "type": "hot",
                    "multisig": False,
                    "address": "msQRdMPcwLr3rWsLzG56ABhHtfavHH2yVW"
                }],
                "CurrencyCode":
                "BTC",
                "Confirmations": [[0, 200000000, 1],
                                  [200000000, 20000000000, 3],
                                  [20000000000, 2100000000000000, 6]],
                "CurrencyDescription":
                "Bitcoin"
            }]).decode('utf-8'),
            accept_customers_from=json.dumps(
                [["*"], ["CU", "SO", "SD", "NG", "IR", "KP"]]).decode('utf-8'),
            is_broker_hub=False,
            support_url='mailto:[email protected]',
            tos_url=
            'https://docs.google.com/a/blinktrade.com/document/d/1HyFRs_2Seh4LGZYjPk8bmbxueUjF7RMz-koAM3rG2Pc/pub?embedded=true',
            fee_structure=json.dumps([{
                "Operation":
                "Wire transfer",
                "Fee":
                "1%",
                "Terms":
                "Exchange operator decides its fees"
            }]).decode('utf-8'),
            transaction_fee_buy=60,
            transaction_fee_sell=60,
            accounts=json.dumps({
                "bonus": [90000000, "exchange_bonus", ["USD", 100000000]],
                "fees": [90000001, "exchange_fees"]
            }).decode('utf-8'),
            status='1',
            ranking=5)
        self.db_session.add(e)
        self.db_session.commit()

        e = DepositMethods(id=501,
                           broker_id=5,
                           name='usps',
                           description='USPS Money order',
                           disclaimer='1 business day',
                           type='DTP',
                           percent_fee=0,
                           fixed_fee=500000000,
                           broker_deposit_ctrl_num=501000001,
                           currency='USD',
                           deposit_limits=json.dumps({
                               "0": {
                                   "enabled": False
                               },
                               "1": {
                                   "enabled": False
                               },
                               "2": {
                                   "enabled": False
                               },
                               "3": {
                                   "enabled": True,
                                   "min": 1000000000,
                                   "max": 280000000000
                               },
                               "4": {
                                   "enabled": True,
                                   "min": 1000000000,
                                   "max": 5000000000000
                               },
                               "5": {
                                   "enabled": True,
                                   "min": 1000000000
                               }
                           }).decode('utf-8'),
                           html_template="""
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/2.3.0/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body style="background-color: #ffffff">
    <div class="container">
      <div class="content-fluid">
        <table class="table table-condensed">
          <tr>
            <td>Order ID:</td>
            <td>*|control_number|*</td>
          </tr>
          <tr>
            <td>Created:</td>
            <td>*|created|*</td>
          </tr>
          <tr>
            <td>Deposit Method:</td>
            <td>Money Order</td>
          </tr>
          <tr>
            <td>Instructions:</td>
            <td>
              1. Head to your local United States Postal Service and purchase a money order slip for the correct amount. Learn more about USPS money orders <a href="https://www.usps.com/shop/money-orders.htm">here</a><br/>
              2. Fill out the money order form. <b>Important: Make sure to write your confirmation code directly on it!</b><br/>
              3. Take a picture of the filled out money order<br/>
              4. Upload the photo of the money order in the system<br/>
              5. Send the money order to :
              <strong>Satoshi Nakamoto<strong><br/>
              <strong>21 Bitcoin Street<strong><br/>
              <strong>New York - NY - 10001<strong><br/>
            </td>
          </tr>
          <tr>
            <td>Total Deposit:</td>
            <td>$ *|value|*</td>
          </tr>
          <tr>
            <td>Notes:</td>
            <td> <small>
              Please complete your deposit according to your preferred method. Be sure to send a copy of the Order ID with the receipt of completed payment to us.
            </small> </td>
          </tr>
        </table>
      </div>
    </div>
  </body>
</html>
                        """,
                           parameters='{}')
        self.db_session.add(e)
        self.db_session.commit()
        self.deposit_method_501 = e

        self.deposit_method_501.generate_deposit(self.db_session,
                                                 self.user_exchange_bonus,
                                                 100000000000, None)
Ejemplo n.º 24
0
import umsgpack
import copy

target_exchange, target_instrument = sys.argv[1], sys.argv[2]

#orderbook_service_details = db_service_interface.find_service('OrderbookFeeder', instrument=target_instrument, exchange=target_exchange)

orderbook_service_details = Service.get(
    (Service.name == 'OrderbookFeeder') & (Service.exchange == target_exchange)
    & (Service.instrument == target_instrument))

exchange_name, instrument, shm_path = target_exchange, target_instrument, orderbook_service_details.address
obh_a = RtOrderbookReader(shm_path)

exchange = Exchange.get(Exchange.name == target_exchange)
db_instrument = Instrument.get((Instrument.name == target_instrument)
                               & (Instrument.exchange_name == exchange.name))
base = db_instrument.base
quote = db_instrument.quote

close_db_conn()

last_bids, last_asks = None, None
same_count = 0


def snapshot_orderbook(obh):
    global last_bids
    global last_asks
    global same_count

    bids_prices, asks_prices = [], []