2018年7月31日 星期二

MT4 Leverage, Margin, Balance, Equity, Free Margin

Screenshot Explain
==================

May refer to
https://www.luckscout.com/leverage-margin-balance-equity-free-margin-and-margin-level-in-forex-trading/

Initially I had $200,000.

In trade tab,
I opened buy 1 lot (100 ounces) of GOLD at $1,223.26 (ask price) and I should need to pay $122,326 for GOLD with $77,674 USD left.
At this moment my total balance of all assets is still $200,000 whatever what I bought.

Few days later, bid ask prices are $1,211.94 and $1,222.44.
If I had to sell I would need to sell at bid price, and so my GOLD asset has value of $1,221.94.
Trade tab shows my profit -$132.00 which is ($1,221.94 - $1223.26) x 100 ounces.
Also I need to pay the swap of -$55.31 and so my total profit is -$187.31.

MT4 calculates the equity = $200,000 - $187.31 = $199,812.69
As the marginis fixed at $1000, the Free margin is $198,812.69 / $1,000 * 100% = 19881.27%
MT4 pumping mode publishes the margin level with balance $200,000, equity $199,812, volume 100, margin 1000.
MT4 derives margin free and margin level from equity and margin.




Golden Formula
==============

For each trade, calculate profit by difference of open price and market price, add

-----

In MT4 the profit is a derived value not a stored value.
Profit is derived from Margin where Margin contains equity and balance.

In MT4 ReportAPI ReportSegregated.cpp
Pnl = MarginLevel.equity - MarginLevel.balance
Equity is calculated by UserRecord.balance + UserRecord.Credit + PnL.PnL

2018年7月25日 星期三

Mi Wifi VPN Server

ROM:
http://hk.miui.com/thread-2780-1-1.html
http://bigota.miwifi.com/xiaoqiang/rom/r1cm/miwifi_r1cm_firmware_46a10_2.7.63.bin

使用小米路由器搭建VPN服務器(2)
https://kknews.cc/zh-hk/tech/4p862lv.html
http://www1.miwifi.com/miwifi_open.html
https://d.miwifi.com/rom/ssh
https://d.miwifi.com/rom/ssh?userId=1686824632

root@XiaoQiang:~# cat /proc/mtd
dev:    size   erasesize  name
mtd0: 01000000 00010000 "ALL"
mtd1: 00030000 00010000 "Bootloader"
mtd2: 00010000 00010000 "Config"
mtd3: 00010000 00010000 "Factory"
mtd4: 00c80000 00010000 "OS1"
mtd5: 00b11ea4 00010000 "rootfs"
mtd6: 00200000 00010000 "OS2"
mtd7: 00100000 00010000 "overlay"
mtd8: 00010000 00010000 "crash"
mtd9: 00010000 00010000 "reserved"
mtd10: 00010000 00010000 "Bdata"

cd /tmp
mkdir rom
dd if=/dev/mtd0 of=/tmp/rom/ALL.bin
dd if=/dev/mtd1 of=/tmp/rom/Bootloader.bin
dd if=/dev/mtd2 of=/tmp/rom/Config.bin
dd if=/dev/mtd3 of=/tmp/rom/Factory.bin
dd if=/dev/mtd4 of=/tmp/rom/OS1.bin
dd if=/dev/mtd5 of=/tmp/rom/rootfs.bin
dd if=/dev/mtd6 of=/tmp/rom/OS2.bin
dd if=/dev/mtd7 of=/tmp/rom/overlay.bin
dd if=/dev/mtd8 of=/tmp/rom/crash.bin
dd if=/dev/mtd9 of=/tmp/rom/reserved.bin
dd if=/dev/mtd10 of=/tmp/rom/Bdata.bin
tar -zcvf rom.tar.gz rom
恢覆指令 (以後備用)
mtd write /tmp/rom/Bootloader.bin Bootloader
mtd write /tmp/rom/Config.bin Config
mtd write /tmp/rom/Factory.bin Factory
mtd write /tmp/rom/OS1.bin OS1
mtd write /tmp/rom/rootfs.bin rootfs
mtd write /tmp/rom/OS2.bin OS2
mtd write /tmp/rom/overlay.bin overlay
mtd write /tmp/rom/crash.bin crash
mtd write /tmp/rom/reserved.bin reserved
mtd write /tmp/rom/Bdata.bin Bdata

OpenWRT


Use Breed to install it
openwrt-18.06.1-ramips-mt7620-miwifi-mini-squashfs-sysupgrade.bin

SSH 2200

https://www.jianshu.com/p/73cad120841d

221.124.79.187:2200 root mobi


End

2018年7月24日 星期二

MySQL 5.7 with Visual Studio 2017 C++


Create empty C++ solution
Select x64
Project > C++ > General > Additional include Path > Add include path "C:\Program Files\MySQL\MySQL Server 5.7\include"
From C:\Program Files\MySQL\MySQL Server 5.7\lib, copy below 3 files
libmysql.dll
libmysql.lib
mysqlclient.lib

main.cpp

#include "mysql.h"
#pragma comment(lib,"libmysql.lib")
#include <iostream>
int main() {
 try
 {
  MYSQL mysql = { 0 };
  mysql_init(&mysql);
  if (!mysql_real_connect(&mysql, "localhost", "root", "Tin.netS1", "", 3306, NULL, 0))
  {
   std::cerr << mysql_error(&mysql) << '\n';
   return -1;
  }

  const char* q1 = "create database if not exists test;";
  std::cout << q1 << '\n';
  if (mysql_real_query(&mysql, q1, strlen(q1)) != 0)
  {
   std::cerr << mysql_error(&mysql) << '\n';
   return -1;
  }

  const char* q2 = "create table if not exists test.temp_table( ID CHAR(10), Name VARCHAR(255), Descs VARCHAR(255), PRIMARY KEY (ID))";
  std::cout << q2 << '\n';
  if (mysql_real_query(&mysql, q2, strlen(q2)) != 0)
  {
   std::cerr << mysql_error(&mysql) << '\n';
   return -1;
  }
  std::cout << "SQL completed successfully" << '\n';
 }
 catch (std::exception& e)
 {
  std::cerr << e.what() << '\n';
  return -1;
 }
 catch (...)
 {
  std::cerr << "Unknown exception" << '\n';
  return -1;
 }
 return 0;
}

query.cpp
#include "winsock.h" 
#include "mysql.h"
#pragma comment(lib,"libmysql.lib")
#include 

int main() {
 try
 {
  MYSQL *mysql;
  MYSQL_RES *result;
  MYSQL_ROW row;
  my_bool reconnect = 0;
  MYSQL_STMT    *stmt;
  MYSQL_BIND    bind[1];
  MYSQL_BIND    bResult[1];
  unsigned long length[1];
  int        int_data;

  mysql = mysql_init(NULL);
  mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect);
  mysql_real_connect(mysql, "localhost", "root", "Tin.netS1", "test", 3306, NULL, 0);

  mysql_query(mysql, "drop table test2");
  mysql_query(mysql, "create table test2(n int, m int)");
  mysql_query(mysql, "insert into test2 values (1,2), (1,3), (1,4), (1,5)");

  const char *normalSql = "select m from test2 where n = 1";
  // 1. normal buffer
  mysql_query(mysql, normalSql);
  result = mysql_store_result(mysql);
  while (row = mysql_fetch_row(result)) {
   printf("1 - %d\n", atoi(row[0]));
  }
  mysql_free_result(result);

  // 2. normal unbuffer
  mysql_query(mysql, normalSql);
  result = mysql_use_result(mysql);
  while (row = mysql_fetch_row(result)) {
   printf("2 - %d\n", atoi(row[0]));
  }
  mysql_free_result(result);

  const char *stmtSql = "select m from test2 where n = ?";

  //3. stmt buffer
  stmt = mysql_stmt_init(mysql);

  mysql_stmt_prepare(stmt, stmtSql, strlen(stmtSql));

  memset(bind, 0, sizeof(bind));
  bind[0].buffer_type = MYSQL_TYPE_LONG;
  bind[0].buffer = (char *)&int_data;
  mysql_stmt_bind_param(stmt, bind);

  memset(bResult, 0, sizeof(bResult));
  bResult[0].buffer_type = MYSQL_TYPE_LONG;
  bResult[0].buffer = (char *)&int_data;
  mysql_stmt_bind_result(stmt, bResult);

  int_data = 1;
  mysql_stmt_execute(stmt);
  mysql_stmt_store_result(stmt);

  while (!mysql_stmt_fetch(stmt)) {
   printf("3 - %d \n", int_data);
  }

  mysql_stmt_close(stmt);

  // 4. stmt unbuffer
  stmt = mysql_stmt_init(mysql);

  mysql_stmt_prepare(stmt, stmtSql, strlen(stmtSql));

  memset(bind, 0, sizeof(bind));
  bind[0].buffer_type = MYSQL_TYPE_LONG;
  bind[0].buffer = (char *)&int_data;
  mysql_stmt_bind_param(stmt, bind);

  memset(bResult, 0, sizeof(bResult));
  bResult[0].buffer_type = MYSQL_TYPE_LONG;
  bResult[0].buffer = (char *)&int_data;
  mysql_stmt_bind_result(stmt, bResult);

  int_data = 1;
  mysql_stmt_execute(stmt);

  while (!mysql_stmt_fetch(stmt)) {
   printf("4 - %d \n", int_data);
  }

  mysql_stmt_close(stmt);

  // 5. stmt server cursor default
  stmt = mysql_stmt_init(mysql);

  mysql_stmt_prepare(stmt, stmtSql, strlen(stmtSql));

  unsigned long type = (unsigned long)CURSOR_TYPE_READ_ONLY;
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*)&type);

  memset(bind, 0, sizeof(bind));
  bind[0].buffer_type = MYSQL_TYPE_LONG;
  bind[0].buffer = (char *)&int_data;
  mysql_stmt_bind_param(stmt, bind);

  memset(bResult, 0, sizeof(bResult));
  bResult[0].buffer_type = MYSQL_TYPE_LONG;
  bResult[0].buffer = (char *)&int_data;
  mysql_stmt_bind_result(stmt, bResult);

  int_data = 1;
  mysql_stmt_execute(stmt);

  while (!mysql_stmt_fetch(stmt)) {
   printf("5 - %d \n", int_data);
  }

  mysql_stmt_close(stmt);


  // 6. stmt server cursor setting
  stmt = mysql_stmt_init(mysql);

  mysql_stmt_prepare(stmt, stmtSql, strlen(stmtSql));

  type = (unsigned long)CURSOR_TYPE_READ_ONLY;
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*)&type);

  unsigned long prefetch_rows = 2;
  mysql_stmt_attr_set(stmt, STMT_ATTR_PREFETCH_ROWS, (void*)&prefetch_rows);

  memset(bind, 0, sizeof(bind));
  bind[0].buffer_type = MYSQL_TYPE_LONG;
  bind[0].buffer = (char *)&int_data;
  mysql_stmt_bind_param(stmt, bind);

  memset(bResult, 0, sizeof(bResult));
  bResult[0].buffer_type = MYSQL_TYPE_LONG;
  bResult[0].buffer = (char *)&int_data;
  mysql_stmt_bind_result(stmt, bResult);

  int_data = 1;
  mysql_stmt_execute(stmt);

  while (!mysql_stmt_fetch(stmt)) {
   printf("6 - %d \n", int_data);
  }

  mysql_stmt_close(stmt);

  mysql_close(mysql);

  std::cout << "SQL completed successfully" << '\n';
  std::cin.get();
 }
 catch (std::exception& e)
 {
  std::cerr << e.what() << '\n';
  return -1;
 }
 catch (...)
 {
  std::cerr << "Unknown exception" << '\n';
  return -1;
 }
 return 0;
}


End

Why KDB?



http://www.timestored.com/b/kdb-qsql-query-vs-sql/

Joining Records on Nearest Time

Lastly we want to consider performing a time based join. A common finance query is to find the prevailing quote for a given set of trades.

2018年7月5日 星期四

C# MongoDB Driver

Visual Studio 2017 > Tools > NuGet Package Manager > PM Console
.NET Framework 4.6.1
Install-Package mongocsharpdriver -Version 2.7.0

2023 Promox on Morefine N6000 16GB 512GB

2023 Promox on Morefine N6000 16GB 512GB Software Etcher 100MB (not but can be rufus-4.3.exe 1.4MB) Proxmox VE 7.4 ISO Installer (1st ISO re...