Enhanced Version of Simple Martingale Contract Multiplier
!!!!!!!Friendly reminder: this strategy is for learning purposes only; actual trading will certainly result in liquidation!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Simple Martingale The principle is to…
!!!!!!!Friendly reminder: this strategy is for learning purposes only; actual trading will certainly result in liquidation!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Simple Martingale The principle is to double after losing until the desired profit is achieved As it is for backtesting, all orders are market orders, not run in live trading!!
Source JavaScript
/*backtest
start: 2017-06-26 00:00:00
end: 2022-02-16 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"XXX_USDT"}]
*/
var symbols = ["BTC_USDT", "LTC_USDT", "EOS_USDT", "ETH_USDT"]
var buyValue = 1000
function main(){
for(var i=0;i<symbols.length;i++){
exchange.SetCurrency(symbols[i])
var ticker = exchange.GetTicker()
var amount = _N(buyValue/ticker.Sell, 3)
exchange.Buy(ticker.Sell, amount)
Sleep(1000)
}
}
function main(){
while(true){
var ticker = exchange.GetTicker()
var account = exchange.GetAccount()
var price = ticker.Buy
var stocks = account.Stocks + account.FrozenStocks
var balance = account.Balance + account.FrozenBalance
var value = stocks*price + balance
Log('Account value is: ', value)
LogProfit(value)
Sleep(3000)//sleep 3000ms(3s), A loop must has a sleep, or the rate-limit of the exchange will be exceed
//when run in debug tool, add a break here
}
}
function main(){
while(true){
Log(exchange.GetAccount().Balance)
Sleep(2000)
}
}
var tradeHistory = {buyData:{amount:0, money:0}, sellData:{amount:0, money:0}, a:0, b:0, c:0, d:0, total:0}
var updateB = 0
var lastNet = 0
var updateD = 0
if(_G('tradeHistory')){
tradeHistory = _G('tradeHistory')
}
function getDepth(){
var asks = []
var bids = []
var temp = 0
var depth = JSON.parse(HttpQuery('https://api.binance.com/api/v1/depth?symbol=BTCUSDT'))
if(!depth){
return false
}
depth.bids.reverse()
for(i=0;i<depth.bids.length;i++){
temp += parseFloat(depth.bids[i][1])
}
for(i=0;i<depth.bids.length;i++){
temp -= parseFloat(depth.bids[i][1])
bids.push([parseFloat(depth.bids[i][0]), temp])
}
temp = 0
for(i=0;i<depth.asks.length;i++){
temp += parseFloat(depth.asks[i][1])
asks.push([parseFloat(depth.asks[i][0]), temp])
}
return {asks:asks,bids:bids}
}
var chartA = {
__isStock: false,
extension: {
layout: 'single',
col: 6, // Specify the cell value occupied by the width, total value is12
height: 500,
},
title: {
text: 'Trading volume distribution chart'
},
xAxis: [{
title: { text: 'Data' },
alignTicks: false
}, {
title: { text: 'Histogram' },
alignTicks: false,
opposite: true
}],
yAxis: [{
title: { text: 'Data' }
}, {
title: { text: 'Histogram' },
opposite: true
}],
series: [{
name: 'Histogram',
type: 'histogram',
xAxis: 1,
yAxis: 1,
baseSeries: 's1',
zIndex: -1
},{
name: 'Data',
type: 'scatter',
data: [],
id: 's1',
marker: {
radius: 1.5
}
}]
};
var chartB = {
extension: {
layout: 'single', // Do not participate in grouping; display separately, default is grouped 'group'
col: 6,
height: 300, // Specify height
},
credits: {
enabled: false
},
title: {
text: 'Net buying funds'
},
xAxis: {
type: 'datetime'
},
series: [{
name: 'net purchase',
type: 'column',
data: []
}],
}
var chartC = {
extension: {
layout: 'single', // Do not participate in grouping; display separately, default is grouped 'group'
col: 6,
height: 300, // Specify the height // The unit value of the specified width, the total value is12
},
title: {
text: 'transaction price'
},
xAxis: {
type: 'datetime'
},
series: [{
name: 'last',
data: [],
}]
}
var chartD = {
__isStock: false,
extension: {
layout: 'single', // Do not participate in grouping; display separately, default is grouped 'group'
col: 6,
height: 500, // Specify the height // The unit value of the specified width, the total value is12
},
chart: {
type: 'area',
zoomType: 'xy'
},
title: {
text: 'BTC-USDTDepth'
},
xAxis: {
minPadding: 0,
maxPadding: 0,
plotLines: [{
color: '#888',
value: 0.1523,
width: 1,
label: {
text: 'Actual price',
rotation: 90
}
}],
title: {
text: 'Price'
}
},
yAxis: [{
min:0,
lineWidth: 1,
gridLineWidth: 1,
title: null,
tickWidth: 1,
tickLength: 5,
tickPosition: 'inside',
labels: {
align: 'left',
x: 8
}
}, {
opposite: true,
linkedTo: 0,
lineWidth: 1,
gridLineWidth: 0,
title: null,
tickWidth: 1,
tickLength: 5,
tickPosition: 'inside',
labels: {
align: 'right',
x: -8
}
}],
plotOptions: {
area: {
fillOpacity: 0.2,
lineWidth: 1,
step: 'center'
}
},
tooltip: {
headerFormat: '<span style="font-size=10px;">Price: {point.key}</span><br/>',
valueDecimals: 2
},
series: [{
name: 'Bids',
data: [],
color: '#03a7a8'
}, {
name: 'Asks',
data: [],
color: '#fc5857'
}]
};
var chartE = {
__isStock: false,
extension: {
layout: 'single', // Do not participate in grouping; display separately, default is grouped 'group'
col: 6,
height: 500, // Specify the height // The unit value of the specified width, the total value is12
},
chart: {
plotBackgroundColor: null,
plotBorderWidth: 0,
plotShadow: false
},
title: {
text: 'Transaction quantity<br>distribution<br>',
align: 'center',
verticalAlign: 'middle',
y: 40
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
dataLabels: {
enabled: true,
distance: -50,
style: {
fontWeight: 'bold',
color: 'white'
}
},
startAngle: -90,
endAngle: 90,
center: ['50%', '75%'],
size: '110%'
}
},
series: [{
type: 'pie',
name: 'Proportion',
innerSize: '50%',
data: []
}]
}
var chartF = {
extension: {
layout: 'single', // Do not participate in grouping; display separately, default is grouped 'group'
col: 6,
height: 300, // Specify the height // The unit value of the specified width, the total value is12
},
title: {
text: 'Total Net Buys'
},
xAxis: {
type: 'datetime'
},
series: [{
name: 'last',
data: [],
}]
}
var chart = Chart([chartB,chartC,chartD,chartA,chartF,chartE]);
//chart.reset()
function main() {
var client = Dial("wss://stream.binance.com:9443/ws/" + 'btcusdt' + "@trade", 60)
var updateTime = new Date().getTime()
while(true){
var trade = JSON.parse(client.read())
if(trade.m){
tradeHistory.sellData.amount += parseFloat(trade.q)
tradeHistory.sellData.money += parseFloat(trade.q) * parseFloat(trade.p)
}else{
tradeHistory.buyData.amount += parseFloat(trade.q)
tradeHistory.buyData.money += parseFloat(trade.q) * parseFloat(trade.p)
}
var buyNet = _N(tradeHistory.buyData.money - tradeHistory.sellData.money, 3)
var tradeAmount = _N(parseFloat(trade.q),2)>1 ? 1 : _N(parseFloat(trade.q),2)
if(parseFloat(trade.q)<0.01){
tradeHistory.a += 1
}else if(parseFloat(trade.q)<0.1){
tradeHistory.b += 1
}else if(parseFloat(trade.q)<1){
tradeHistory.c += 1
}else{
tradeHistory.d += 1
}
tradeHistory.total +=1
chartE.series[0].data = [
['Less Than0.01', _N(tradeHistory.a/tradeHistory.total,4)],
['Less Than0.1', _N(tradeHistory.b/tradeHistory.total,4)],
['Less Than1', _N(tradeHistory.c/tradeHistory.total,4)],
['Greater than1', _N(tradeHistory.d/tradeHistory.total,4)],
]
if(tradeAmount>0.01){
while(chartA.series[1].data.length>2000){
chartA.series[1].data.shift()
}
chartA.series[1].data.push(tradeAmount)
}
if(Date.now() - updateB > 60*1000){
chart.add(0,[Date.now(), _N(buyNet-lastNet,2)])
lastNet = buyNet
updateB = Date.now()
chart.add(1, [Date.now(), _N(parseFloat(trade.p),2)])
chart.add(6, [Date.now(), _N(buyNet,2)])
}else{
chart.add(0, [Date.now(), _N(buyNet-lastNet,2)], -1)
chart.add(1, [Date.now(), _N(parseFloat(trade.p),2)], -1)
chart.add(6, [Date.now(), _N(buyNet,2)],-1)
}
if(Date.now() - updateD > 6*1000){
var depth = getDepth()
if(!depth){
continue
}
chartD.series[0].data = depth.bids
chartD.series[1].data = depth.asks
updateD = Date.now()
}
chart.update([chartB,chartC,chartD,chartA,chartF,chartE])
}
}
function main() {
transferToMain(100);
}
// fromUTransfer the specified amount from the contract wallet to the spot walletUSDT
function transferToMain(amount){
var ret = null;
if (isOKexExchange()) {
let param = "ccy=USDT" + "&from=18" + "&amt=" + amount.toString() + "&to=6";
ret = exchange.IO("api", "POST", "/api/v5/asset/transfer", param);
} else if (isBinanceExchange()) {
let time = UnixNano() / 1000000;
let param = "type=UMFUTURE_MAIN" + "&asset=USDT" + "&amount=" + amount.toString() + "×tamp=" + time.toString();
exchange.SetBase('https://api.binance.com');
ret = exchange.IO("api", "POST", "/sapi/v1/asset/transfer", param);
exchange.SetBase('https://fapi.binance.com');
} else {
Log("Fund transfer failed, unsupported exchange!");
}
if (ret) {
Log("has been transferred from the contract to the spot account: ", amount, " USDT");
return true;
} else {
Log("Fund transfer failed!");
return false;
}
}
// Determine whether it is a Binance exchange
function isBinanceExchange() {
if (exchange.GetName() == "Futures_Binance") {
return true;
}
return false;
}
// Determine WhetherOKEXexchange
function isOKexExchange() {
if (exchange.GetName() == "Futures_OKCoin") {
return true;
}
return false;
}
var n =$//Initial Order Quantity
var MarginLevel = 50 //Contract Leverage
var profit = 0.5 //Expected return, cannot be less than the handling fee
var bet = 1.5//Multiplier
initialFunds =10000 // Initial capital
var funds = initialFunds // Real-time funds
//Get random number
function sum(m, n) {
var num = Math.floor(Math.random() * (m - n) + n);
return num;
}
function main(){
if (exchange.GetName() !== 'Futures_Binance')
throw "Only supports Binance Futures";
}
function main() {
var ret = exchange.IO("api", "POST", "/fapi/v1/positionSide/dual", "dualSidePosition=true")
// ret : {"code":200,"msg":"success"}
Log(ret)
}
function main() {
exchange.SetContractType("swap")
exchange.SetMarginLevel(MarginLevel)
if (exchange.GetPosition().length > 0) {
throw "No positions should exist before strategy starts."
}
var position = []
while (true) {
position = exchange.GetPosition()
if (position.length == 0) {
//Get random number0,1As direction
var redom = sum(0,2)
Log(redom)
if (redom == 0,1,1,2,3,5,8,13,21,33,55,89) {
n=0.001
exchange.SetDirection("sell")
exchange.Sell(-1, n, "open short")
}
if (redom == 144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368 ) {
n=0.002
exchange.SetDirection("buy")
exchange.Buy(-1, n, "open long")
}
if (redom == 0) {
n=0.001
exchange.SetDirection("sell")
exchange.Sell(-1, n, "open short")
}
if (redom == 1) {
n=0.002
exchange.SetDirection("buy")
exchange.Buy(-1, n, "open long")
}
}
if (position.length > 0) {
if (position[0].Type == 0) {
//Profit greater than expected
if (position[0].Profit > profit) {
Amount=position[0].Amount
exchange.SetDirection("closebuy")
exchange.Sell(-1, position[0].Amount)
Log(profit)
LogProfit (position[0].Amount+profit+funds)
}
//If the negative profit is greater than the margin, increase the position
if (position[0].Profit < position[0].Margin * -2) {
n=n * bet
exchange.SetDirection("buy")
exchange.Buy(-1, position[0].Amount=n)
}
}
if (position[0].Type == 1) {
if (position[0].Profit > profit) {
Amount=position[0].Amount
exchange.SetDirection("closesell")
exchange.Buy(-1, position[0].Amount)
Log(profit)
LogProfit (position[0].Amount+profit+funds)
}
if (position[0].Profit < position[0].Margin * -2) {
n = n * bet
exchange.SetDirection("sell")
exchange.Sell(-1, position[0].Amount=n)
}
}
Sleep(60000)
}
}
}
Published by Zer3192 on the FMZ strategy square · last modified 2022-03-26. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Enhanced-Version-of-Simple-Martingale-Contract-Multiplier.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- CTA Strategy Commodity Futures Simple MartingaleJavaScript · Martingale · 1d
- Fibonacci Sequence StrategyJavaScript · Martingale · 1d
- MartinGale Strategy1JavaScript · Martingale
- Simple Martingale Long and Short Position Increase Ratio ModificationJavaScript · Martingale · 1d
- Random Number Martingale StrategyJavaScript · Martingale
- Simple Martingale BTC Contract Modify Add Position MultiplierJavaScript · Martingale · 1d