MMQLEngine/specifications/0001_LanguageSyntax.md

218 lines
4.8 KiB
Markdown

# Table of Contents
* [Action Statements](#action-statements)
* [CREATE EXCHANGE](#create-exchange-statement)
* [CREATE SPREAD](#create-spread-statement)
* [UPDATE TRIGGER](#update-trigger-statement)
* [LIST TRIGGERS](#nop)
* [SELL](#sell-statement)
* [BUY](#buy-statement)
* [Functions](#functions)
* [PURCHASING_POWER](#purchasing_power)
* [CURRENT_PRICE](#current_price)
* [Types of Exchange](#types-of-exchange)
* [IB_API](#ib_api)
* [HIVE](#hive)
* [MOCK](#mock-exchange)
# Action Statements
## CREATE EXCHANGE Statement
```
CREATE EXCHANGE [Name] TYPE [ExchangeType]
URI [API CALLBACK URI]
[...Exchange type-specific PARAMS]
```
### Examples
```
CREATE EXCHANGE 'IB' TYPE IB_API
URI 'https://127.0.0.1:11590/'
```
## CREATE SPREAD Statement
This is a shortcut function to create a buy/sell order pairs with hedge triggers
```
TODO
```
### Examples
```
CREATE SPREAD OF 10 BTC FOR QQQToken FROM "CryptoExchange"
HEDGE BTC FOR USD FROM "CryptoExchange"
HEDGE QQQToken FOR QQQ FROM "StockExchange"
```
## UPDATE TRIGGER
```
TODO
```
## SELL Statement
```
[SELL] [Amounts] OF [FinancialInstrument]
FOR [Amount]
FROM [Exchange]
WITH LIMIT OF [Amounts]
AND
[Other Action Statements]
FOR EVERY
[Amounts] OF [FinancialInstrument] [SOLD]
FROM [Exchange]/ANYWHERE
AND/OR
[Amounts] OF [FinancialInstrument] [BOUGHT]
FROM [Exchange]/ANYWHERE
AS 'ACTION_NAME'
```
### Examples
#### Simple Sell
The following statements are identical
```
SELL 1 SHARES OF QQQ FROM "MyBrokerAccount"
SELL 1 UNIT OF QQQ FROM "MyBrokerAccount"
SELL 1 QQQ FROM "MyBrokerAccount"
```
#### Sell Trigger
```
SELL 1 SHARES OF QQQ
FOR 10 USD
FROM "MyBrokerAccount"
WITH LIMIT OF 100 SHARES
FOR EVERY
2 SHARES OF QQQ_TOKEN SOLD
FROM "CoinBase"
AS 'My Hedge Trigger'
```
## BUY Statement
```
[BUY] [Amounts] OF [FinancialInstrument]
FOR [Amount]
FROM [Exchange]
WITH LIMIT OF [Amounts]
AND
[Other Action Statements]
FOR EVERY
[Amounts] OF [FinancialInstrument] [SOLD]
FROM [Exchange]/ANYWHERE
AND/OR
[Amounts] OF [FinancialInstrument] [BOUGHT]
FROM [Exchange]/ANYWHERE
```
### Examples
```
BUY 1 SHARES OF BTC_ETF
FOR 10 USD
FROM "MyBrokerAccount"
WITH LIMIT OF PURCHASING_POWER( "MyBrokerAccount", "QQQ" )
FOR EVERY
1 BTC OF USD_BTC SOLD
FROM "CoinBase"
```
# functions
## PURCHASING_POWER
`PURCHASING_POWER( Exchange, FinancialInstrument )`
Returns the maximum [Amounts] of [FinancialInstrument] can purchase from [Exchange]
## CURRENT_PRICE
`CURRENT_PRICE( Exchange, FinancialInstrument )`
Returns the current price of a financial instrument from exchange
# Examples
## Creating a spread
* Max position: $1000
* Spread: $10
* QQQ etf price(Qetf): $100
| Bid | Ask |
|-----------------|----------------|
| 10shares@$95 | 10shares@$105 |
| ($1000 of Qetf) | ($800 of Qetf) |
If a trader hit ur ASK by bidding 2 shares of QQQ token
| Bid | Ask |
|-----------------|----------------|
| 10shares@$95 | 8shares@$105 |
| ($1000 of Qetf) | ($800 of Qetf) |
If another trader hit ur ASK with a 20 shares of bid, then only 8 shares of his order can be filled.
* No need to handle his remaining 12 shares , its not ur responsibility, its his "broker/app/program" to deal with unfilled order.
| Bid | Ask |
|-----------------|----------------|
| 10shares@$95 | |
| ($1000 of Qetf) | |
### SELL Order
```
SELL 10 SHARES OF "QQQ_TOKEN"
FOR CURRENT_PRICE( "CryptoExchange", "QQQ_TOKEN" ) + 5 BTC
WITH LIMIT OF 1000 BTC
FROM "CryptoExchange"
```
### BUY Order
```
BUY 10 SHARES OF "QQQ_TOKEN"
FOR CURRENT_PRICE( "CryptoExchange", "QQQ_TOKEN" ) - 5 BTC
WITH LIMIT OF 1000 BTC
FROM "CryptoExchange"
```
## Hedging
### BUY Trigger for every QQQToken handed out
```
BUY 1 SHARES OF "QQQ"
FOR CURRENT_PRICE( "StockExchange", "QQQ" )
FROM "StockExchange"
FOR EVERY 1 SHARES OF "QQQ_TOKEN" SOLD FROM "CryptoExchange"
```
### SELL Trigger for every QQQToken received
```
SELL 1 SHARES OF "QQQ"
FOR CURRENT_PRICE( "StockExchange", "QQQ" )
FROM "StockeExchange"
FOR EVERY 1 SHARES OF "QQQ_TOKEN" BOUGHT FROM "CryptoExchange"
```
### BUY Trigger for every BTC handed out
```
BUY 1 BTC OF BTC
FOR CURRENT_PRICE( "CryptoExchange", "USD.BTC" )
FROM "CryptoExchange"
FOR EVERY 1 BTC OF BTC SOLD FROM "CryptoExchange"
```
### SELL Trigger for every BTC received
```
SELL 1 BTC OF BTC
FOR CURRENT_PRICE( "CryptoExchange", "USD.BTC" )
FROM "CryptoExchange"
FOR EVERY 1 BTC OF BTC BOUGHT FROM "CryptoExchange"
```
# Types of Exchange
## Mock Exchange
The mock exchange is an exchange for testing purpose