[EXPERIMENTAL][pshaiBot] Simple Market Maker (leverage - Binance & Bybit) ORtchimod - Option Alternativ Sales Mod optimised
stableDescription
Many thanks for the basics and ideas from PSHAI and smokyho! Without you, the script would not have been created!
I have been trying to put my ideas into the script over the last few months and it has grown and grown. You can see the result here.
It is far from finished, but you are welcome to test it in paper mode. The real tests are going well. Please use it only if you understand the options!
I have the following extensions to PShai's and smokyho's stuff:
- 3 different modies with different slot distances and sizes
- dynamic profit
- safety mode in case of strong pump or dumps
- alternative profit mode - with the market - for the pump phases ;)
- manual buying
Problems:
- HTS calculates the entry price incorrectly. I use my own calculation of the entry price, but from time to time errors creep in. Entry price can be adjusted. The entry price is only shown in the log.
- Profit is not displayed in the bot
- Error message after first purchase, has no effect.
- I am still working on a better "stop loss" for Profitmod 2.
- Backtest does not work
- and much more ;)
in the next updates:
- Alternative sell mode for shorts
- Manual selling
- Optimisation of the selling mode safety
I am always open for new ideas! With which settings do you have the best success?
This version is optimised for alternative sales mode. I will publish another one for the security mode. The options are very different.
Original:
Simple Market Maker
by pshai @ 2020
Introduction:
This simple market maker makes the market!
It doesn’t stop, it has no limits (other than max. pos. size)
and it’s amazing. Get familiar with the bot before using it!
I strongly suggest doing backtests and especially running it
using a simulated account until you are confident that you know that
this bot knows what it is doing! Also remember that backtests cannot
represent the results you would see with a live bot; the difference
is very…different.
About trading commands….
This bot uses exit order commands only, but dont be alarmed!
It is merely a hack that I found while exploring, and this hack
allowed me to not bother handling positions whatsoever!
In other words, the bot works well with these; it just looks weird.
–== !NEW! Hedge Mode ==–
With this new feature you are able to trade on Binance Futures, OKEx
and other excahnges that support hedge mode. Enable this mode ONLY
when you are certain that hedging is supported on your chosen exchange!
*THINGS TO NOTE:*
– This bot will not work well on:
–> Bybit; bot is too intense for their API, dont trade there…
–> Bitmex; API Incompatibilities
– BE VERY CAREFUL with fixed high leverage!
~~ May the profits be with you ~~
~pshai
Consider donating to support my work!
PSHAI
BTC: 3KBNiSWXLBdkM8s5CKizo9rUK7ViA2PuFA
Smokyho
BTC: 35KY1GPFtxKoJ6Bzri6sLYQPcmGZhHfRac
ORtchi
BTC: 1MuMTfRSbVLuBJwz8bhZzhKckoC2iL6dY4
ETH: 0x0bafde44120a9512e2e57c9e9b996eab3d8bab31
Binance: bnb136ns6lfw4zs5hg4n85vdthaad7hq5m4gtkgf23 MEMO: 105862840
HaasScript
-- Modified version of Phsai's amazing Simple Market Maker that intended
-- ONLY FOR BINANCE FUTURES USDT/COIN and BYBIT FUTURES USDT with HEDGE MODE ENABLED
--
-- Consider donating to support our work!
-- Phsai BTC: 3FRx1EkG4T4izrkaS34xeZHJFK4kQefHKf
-- Smokyho BTC: 35KY1GPFtxKoJ6Bzri6sLYQPcmGZhHfRac
-- ORtchi BTC: 1MuMTfRSbVLuBJwz8bhZzhKckoC2iL6dY4
-- ETH: 0x0bafde44120a9512e2e57c9e9b996eab3d8bab31
-- Binance: bnb136ns6lfw4zs5hg4n85vdthaad7hq5m4gtkgf23 MEMO: 105862840
-- --------------------------------------------------------------------------
EnableHighSpeedUpdates(true)
HideOrderSettings()
HideTradeAmountSettings()
-- Check BYBIT or BINANCE
local getMarket = PriceMarket()
local isBybit = StringContains(getMarket, 'BYBIT')
-- inputs
InputGroupHeader('Slots')
local slotCount = Input('01. Slot Count', 1, 'How many orders are constantly kept open on both long and short side')
local minSpread = Input('02. Minimum Spread %', 0, 'Minimum spread percentage between the first long and short entries. This setting only works when bot has no position.')
local slotCancel = Input('03. Cancel Distance %', 0.236, 'How much price can move to the opposite direction before orders are cancelled and replaced')
local slotSpread_1 = Input('04. Slot Spread % Mod1', 0.08, 'Percentage based spread value between each slot Mod 1')
local slotSpread_2 = Input('05. Slot Spread % Mod2', 0.42, 'Percentage based spread value between each slot Mod 2')
local slotSpread_3 = Input('06. Slot Spread % Mod3', 0.642, 'Percentage based spread value between each slot Mod 3')
local mindist = Input('07. min. % Spread Mod3', 0.2, 'Minimum distance when re-buying in Mod3, only exists in Mod 3')
InputGroupHeader('Slots Short')
local okShort = Input('08. Allow Short', false, 'Allow bot to open Short')
local slotSize_1 = Input('09. Slot Size short Mod1', 1, 'Short contract size in mod 1, mod 1 is the agressive initial mod')
local maxSizeS_1 = Input('10. Max Cont. % Short Mod1', 0.4, 'Maximum number of short contracts in mod 1')
local slotSize_2 = Input('11. Slot Size short Mod2', 1, 'Short contract size in mod 2, mod 2 is the medium mod')
local maxSizeS_2 = Input('12. Max Cont. % Short Mod2', 20, 'Maximum number of short contracts in mod 2')
local slotSize_3 = Input('13. Slot Size short Mod3', 1, 'Short contract size in mod 3, mod 3 is the careful mod')
local maxSizeS_3 = Input('14. Max Cont. % Short Mod3', 30, 'Maximum number of short contracts in mod 3')
InputGroupHeader('Slots Long')
local okLong = Input('15. Allow Long', true, 'Allow bot to open Long')
local slotSizel_1 = Input('16. Slot Size long Mod1', 1, 'Long contract size in mod 1, mod 1 is the aggressive initial mod')
local maxSizeL_1 = Input('17. Max Cont. Long Mod1', 30, 'Maximum number of Long Contracts in the Mod 1')
local slotSizel_2 = Input('18. Slot Size long Mod2', 1, 'Long contract size in mod 2, mod 2 is the medium mod')
local maxSizeL_2 = Input('19. Max Cont. Long Mod2', 50, 'Maximum number of Long Contracts in the Mod 2')
local slotSizel_3 = Input('20. Slot Size long Mod3', 1, 'Long contract size in mod 3, mod 3 is the careful mod')
local maxSizeL_3 = Input('21. Max Cont. Long Mod3', 100, 'Maximum number of Long Contracts in the Mod3')
local compound = false
local wtfBal = true
local wtfAmount = false
InputGroupHeader('Budget ')
local maxSizeM = Input('22. Max. Open '..AmountLabel(), 300, 'Maximum open contracts at any given time also as minimum for Dynamic Max Open. After exceeding this value, the bot will dump a portion of position at a loss.')
local autoMax = Input('23. Dynamic Max Open', true, 'Dynamically change max open contracts based on available balanca')
local leverage = Input('24. Leverage', 4, 'MUST be filled even if using Cross Margin. Important for trading budget.')
local contVal = Input('25. COIN-M Value', 10, 'ONLY if bot trading on COIN-M Futures then enter the Contract Value. Ignore if trade on USDT-M')
local maxBudget = Input('26. Balance Budget', 0.8, 'How much from wallet balance allocated for this bot. 0.5 is 50% of wallet balance.')
local maxOpen = Input('27. Position Budget', 1, 'How much from the Balance Budget allocated for opening positions. 0.1 is 10% of Balance Budget')
local slotSizeM = Input('28. Slot Size', 1, 'Trade amount per slot')
local autoSlot = Input('29. Dynamic Slot Size', true, 'DSSize - Dynamically change slot size favoring trending side.')
local slotBudget1 = Input('30. Max. Open Divider', 300, 'DSSize feature - Divide max open position with this value to get new Slot Size. Example Max Open is 1000 and devider is 200 then slot size is 5')
local slotBudget2 = Input('31. Max. Open Divider', 300, 'DSSize feature - Divide max open position with this value to get new Slot Size. Example Max Open is 1000 and devider is 200 then slot size is 5')
local slotBudget3 = Input('32. Max. Open Divider', 300, 'DSSize feature - Divide max open position with this value to get new Slot Size. Example Max Open is 1000 and devider is 200 then slot size is 5')
InputGroupHeader('Profit')
local takeProfitS = Input('35. Take-Profit % Short', 0.25, 'From what percentage should shorts be sold. The percentage refers to the market price. Depends on Dyn Profit')
local takeProfitL = Input('36. Take-Profit % Long', 0.25, 'From what percentage should shorts be sold. The percentage refers to the market price. Depends on Dyn Profit')
local dynprofit = Input('37. Dynamic Profit', 0.92, 'How much does the profit have to fall before selling 1=0% 0=100%')
local tpOrderType = InputOrderType('38. TP Order Type', MarketOrderType, 'Sellmod Take Profit')
local contsize = contVal
InputGroupHeader('Profitmod with the market')
local ProfitMod2 = Input('39. Profitmod 2 allow', true, 'Deactivates the normal profit mod and activates the alternative profit mod 2')
local minroislotshort = 500000
local maxroislotlong = Input('40. % Buy Price Long', 0.6, 'From how much % distance from the entry price may long be bought?')
local slroislotlong = Input('41.SL % PriceLong', 0.3, 'From how much % distance to the entry price the position should be strongly reduced')
local sellslotlong = Input('42. % Sell long', 0.1, 'What percentage of the long should be sold. 0,1= 10% 1=100%')
local slotSizel_4 = Input('43. % Buy long Mod', 1.1, 'How much % to buy Long Contracte')
local maxSizeL_4 = Input('44. Max Cont. Long Mod', 25, 'Maximum number of Long Contracte in LongMod ')
local minSizeL_4 = Input('45. Min Cont. Long Mod', 10, 'Minimum number of Long Contracte in LongMod')
local slotSpread_4 = Input('46. Slot Spread % Mod', 0.08, 'Percentage distance between the slots and between the current price in the long mode')
local oksldyn = Input('47. Dynamic Profit', true, 'Increases size and gain distance')
local slminrois = Input('47_1. Increase profit margin', 1.03, 'By what factor should the profit be increased with each sale?')
local chsize = Input('47_2. Increase win size', 1.1, 'By what factor should the size be increased with each sale?.')
local okchle = Input('47_3. change Leverage', true, 'May the leverage be increased?')
local maxchle = Input('47_4. Maximaler Leverage', 100, 'Maximum leverage in Profitmod')
InputGroupHeader('Safetymod ')
local oksafty = Input('49. Safetymod allow', false, 'Allow bot to open Safetymod, works only with normal Profitmod')
local safty0 = Input('50. Safety Mod1 %', -4, ' From which % should the Safetymod 1 be activated?')
local saftysize0 = Input('51. Safetysize Mod1 ', 0.25, 'How much should be purchased 1= 0% ; 2= 100% ')
local maxSizeS_safty0 = Input('52. Max Cont. Short Safety1', 7, 'Maximum number of Short Contracte in Safetymod 1')
local maxSizeL_safty0 = Input('53. Max Cont. Long Safety1', 7, 'Maximum number of Long Contracte in Safetymod 1')
local safty2 = Input('54. Safetymod2 %', -5, 'From which % should the Safetymod 2 be activated? ')
local saftysize2 = Input('55. Safetysize2 ', 1.5, 'How much larger should the safety side be 1=exactly 2= twice ')
local safty3 = Input('56. Safetymod3 %', -6, 'From which % should the Safetymod 3 be activated? ')
local saftysize3 = Input('58. Safetysize3 ', 2, 'How much larger should the safety side be 1=exactly 2= twice ')
local safty4 = Input('59. Safetymod4 %', -7, 'From which % should the Safetymod 4 be activated?')
local saftysize4 = Input('60. Safetysize4 ', 2.7, 'How much larger should the safety side be 1=exactly 2= twice ')
local safty = Input('61. Safetymod5 %', -8, 'From which % should the Safetymod 5 be activated?')
local saftysize = Input('62. Safetysize5 ', 3.5, 'How much larger should the safety side be 1=exactly 2= twice')
local saftyprofit = Input('63. Safetyprofit2-5 ', 0.01, ' How much profit to sell long and short at the same time')
local dynprofitsafty = Input('64. Dyn Safety Profit2-5', 0.92, 'How much does the profit have to fall before selling 1=0% 0=100%')
local okbacksafty = Input('65. Safetysell allow', false, 'Allow bot to sell in the Safetymod')
local backsafty = Input('66. Safety back %', 0.7, 'How much must the price fall before it sells parts?')
local saftyprofitsell = Input('67. Safetyprofitsell ', 0.2, 'Up to how much profit % may the position be reduced if the juice level is reduced. 1 = curent Price 1.001 = 0,1% ')
local maxSizeS_safty = Input('68. Max Cont. Short Safety2-5', 100, 'Maximum number of Short Contracte in Safetymod 2-5')
local maxSizeL_safty = Input('69. Max Cont. Long Safety2-5', 100, 'Maximum number of Long Contracte in Safetymod 2-5')
InputGroupHeader('Emergency sale')
local reduceSize = Input('33. Size Reduction % ', 0, 'CAUTION! When Max Size Mod 3 is reached, percentage Contracte is sold. Should normally have 0!!!')
local reduceOrderType = InputOrderType('34. Reduction Order Type', MarketOrderType, 'Sellmod')
InputGroupHeader('Manuelles Einkaufen')
local limit_open_short = Input('70. open Limit Order Short!!', 0, 'Buy short contracts per limit order, price required')
local limit_preis_short = Input('71. Price Limit Order Short!!', 0, 'Price Limit Order Buy Short Contracte')
local limit_open_long = Input('72. open Limit Order Long!!', 0, 'Buy long contracts per limit order, price required')
local limit_preis_long = Input('73. Price Limit Order Long!!', 0, 'Price Limit Order Buy Long Contracte')
local limit_open_akt_short = Input('74.open Lim cur Price Sho!!', 0, 'Buy short contracts per limit order, current price')
local limit_open_akt_long = Input('75.open Lim cur Price Lon!!', 10, 'Buy long contracts per limit order, current price')
InputGroupHeader('Einstiegspreisnderung ')
local newentryshort = Input('76. Entry Short change', -1, 'If the entry price is incorrect, it can be changed; after the change, set it back to -1.')
local newentrylong = Input('77. Entry Long change', -1, 'If the entry price is incorrect, it can be changed; after the change, set it back to -1.')
CC_VPM()
minSpread = minSpread / 2.0
local slotCancel_1 = slotCancel
local slotCancel_2 = slotCancel
local slotCancel_3 = slotCancel
local slotCancel_4 = slotSpread_4
local slotSpread = slotSpread_3
local slotSize = slotSize_3
local slotSizel = slotSizel_3
local maxSizeL = maxSizeL_3
local maxSizeS = maxSizeS_3
local hedgeMode = true
local hedgeRoi = -11111
local safel = 0
local safes = 0
local safesell = 0
local pamt_l2 = Load('pamt_l2',0)
local pamt_s2 = Load('pamt_s2',0)
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
-- wallet check
local leverage_save = Load('leverage_save', leverage)
local cp = CurrentPrice()
local TMC = TradeMarketContainer(getMarket)
local MTA = TMC.minimumTradeAmount
local profitLabel = ProfitLabel()
if profitLabel == nil then profitLabel = QuoteCurrency() end
local hedge_longPosId = Load('hedge_longPosId', NewGuid())
local hedge_shortPosId = Load('hedge_shortPosId', NewGuid())
local availBal = WalletAmount(AccountGuid(), profitLabel)
local longAmount = LongAmount()
local shortAmount = ShortAmount()
local getProfitL = GetCurrentProfit(PositionLong)
local getProfitS = GetCurrentProfit(PositionShort)
local getProfit = getProfitL + getProfitS
local usedLong = UsedMargin(AccountGuid(), GetPositionEnterPrice(hedge_longPosId), longAmount, leverage_save)
local usedShort = UsedMargin(AccountGuid(), GetPositionEnterPrice(hedge_shortPosId), shortAmount, leverage_save)
local walletBal = WalletAmount(AccountGuid(), profitLabel)
local botProfit = GetBotProfit()
if isBybit then
wallMount = availBal + usedLong + usedShort - getProfit
xchange = 'BYBIT'
else
wallMount = availBal
xchange = 'BINANCE'
end
-- balance warning
local walletBal = compound and (wallMount + botProfit) or wallMount
local workBal = usedLong + usedShort - getProfit
local budgetBal = maxBudget * walletBal
local balRatio = budgetBal > 0 and workBal / budgetBal or 0
local roundc
if balRatio > 0.5 and balRatio < 0.8 then LogWarning('Working balance is > 50% of Budget!!!') end
if balRatio > 0.8 then LogWarning('Working balance is > 80% of Budget!!!') end
Log('Balance Monitor '..xchange..' -> Budget Balance: '..Round(budgetBal, 5)..' '..profitLabel..' | Working Balance: '..Round(workBal, 5)..' '..profitLabel..' | Ratio: '..Round(balRatio, 3))
if profitLabel == 'USD' or profitLabel == 'USDT' then
maxMax = walletBal * maxBudget * maxOpen / cp.close
roundc = 3
else
maxMax = walletBal * maxBudget * maxOpen * cp.close / contVal
roundc = 0
end
-- hedge it!
-- price and data
local cp = CurrentPrice()
local c = ClosePrices()
local rsi = RSI(c, 9) -- RSI is used to determine which side to start off with. this is to eliminate ghost positions.
local signal = rsi > 52 and -1 or rsi < 48 and 1 or 0
-- positions
local hedge_longPosId = Load('hedge_longPosId', NewGuid())
local hedge_shortPosId = Load('hedge_shortPosId', NewGuid())
local dir_l = GetPositionDirection(hedge_longPosId)
local aep_l = GetPositionEnterPrice(hedge_longPosId)
local pamt_l = GetPositionAmount(hedge_longPosId)
local proi_l = GetPositionROI(hedge_longPosId)
local dir_s = GetPositionDirection(hedge_shortPosId)
local aep_s = GetPositionEnterPrice(hedge_shortPosId)
local pamt_s = GetPositionAmount(hedge_shortPosId)
local proi_s = GetPositionROI(hedge_shortPosId)
local profitcurrentlong = GetCurrentProfit(PositionLong)
local profitcurrentshort = GetCurrentProfit(PositionShort)
local enterpricelong1 = Load('enterpricelong1',0)
local enterpriceshort1 = Load('enterpriceshort1',0)
local enterpricedifflong = Load('enterpricedifflong',0)
local enterpricediffshort = Load('enterpricediffshort',0)
local tradeprofitshort = Load('tradeprofitshort',0)
local tradeprofitlong = Load('tradeprofitlong',0)
local pricechangelong = Load('pricechangelong', 0)
local pricechangeshort = Load('pricechangeshort', 0)
local profitcalcshort = 0
local profitcalclong = 0
local enterpricedifflongbuy = 0
local enterpricediffshortbuy = 0
local enterpricedifflongsell = 0
local enterpricediffshortsell = 0
local posprofitlong = GetPositionProfit (hedge_longPosId)
local posprofitshort = GetPositionProfit (hedge_shortPosId)
local enterp2 = AverageEnterPrice(hedge_longPosId)
Log("GetPositionEnterPrice "..aep_l.." AverageEnterPrice "..enterp2)
-- not using spread if we have a position
if pamt_l > 0 or pamt_s > 0 then
minSpread = 0
end
-- manage position ids
if pamt_l == 0 and IsPositionClosed(hedge_longPosId) then
enterpricelong1 = 0
enterpricedifflongbuy = 0
enterpricedifflongsell = 0
profitcalclong = 0
tradeprofitlong = 0
pamt_l2 = 0
if IsAnyOrderOpen(hedge_longPosId) then
CancelAllOrders(hedge_longPosId)
else
hedge_longPosId = NewGuid()
dir_l = GetPositionDirection(hedge_longPosId)
aep_l = GetPositionEnterPrice(hedge_longPosId)
pamt_l = GetPositionAmount(hedge_longPosId)
proi_l = GetPositionROI(hedge_longPosId)
end
end
if pamt_s == 0 and IsPositionClosed(hedge_shortPosId) then
enterpriceshort1 = 0
enterpricediffshortbuy = 0
enterpricediffshortsell = 0
profitcalcshort = 0
tradeprofitshort = 0
pamt_s2 = 0
if IsAnyOrderOpen(hedge_shortPosId) then
CancelAllOrders(hedge_shortPosId)
else
hedge_shortPosId = NewGuid()
dir_s = GetPositionDirection(hedge_shortPosId)
aep_s = GetPositionEnterPrice(hedge_shortPosId)
pamt_s = GetPositionAmount(hedge_shortPosId)
proi_s = GetPositionROI(hedge_shortPosId)
end
end
-- get pos id
local getPositionId = function(isLong)
return isLong and hedge_longPosId or hedge_shortPosId
end
-- Enter price + Profit Calc
-- Preisanpassung
if pricechangeshort == 0 and newentryshort > - 1 then
enterpriceshort1 = newentryshort
pricechangeshort = 1
end
if pricechangeshort == 1 and newentryshort < 0 then
pricechangeshort = 0
end
if pricechangelong == 0 and newentrylong > - 1 then
enterpricelong1 = newentrylong
pricechangelong = 1
end
if pricechangelong == 1 and newentrylong < 0 then
pricechangelong = 0
end
Save('pricechangelong', pricechangelong)
Save('pricechangeshort', pricechangeshort)
if pamt_l > 0 then
enterpricedifflongbuy = (cp.bid * 100 / enterpricelong1 ) - 100
enterpricedifflongsell = (cp.ask * 100 / enterpricelong1 ) - 100
profitcalclong = ( (1 /enterpricelong1) - (1 /cp.ask)) * pamt_l * contsize
end
if pamt_s > 0 then
enterpricediffshortbuy = (enterpriceshort1 * 100 / cp.ask ) - 100
enterpricediffshortsell = (enterpriceshort1 * 100 / cp.bid ) - 100
profitcalcshort = ( (1 / cp.bid) - (1 /enterpriceshort1) ) * pamt_s * contsize
end
local profitpositions = profitcalcshort + profitcalclong
local lastlongp = LastLongPrice()
local lastshortp = LastShortPrice()
local lastexitshortp = LastExitShortPrice()
local lastexitlongp = LastExitLongPrice()
local pamt_l_diff = 0
local pamt_s_diff = 0
if pamt_l2 > pamt_l then
pamt_l_diff = pamt_l2 - pamt_l
local tradeprofitlong1 = pamt_l_diff * contsize * ( (1 / lastexitlongp) -(1 / enterpricelong1) )
tradeprofitlong = tradeprofitlong + tradeprofitlong1
pamt_l2 = pamt_l
end
if pamt_s2 > pamt_s then
pamt_s_diff = pamt_s2 - pamt_s
local tradeprofitshort1 = pamt_s_diff * contsize * ( (1 / enterpriceshort1) -(1 / lastexitshortp))
tradeprofitshort = tradeprofitshort + tradeprofitshort1
pamt_s2 = pamt_s
end
if pamt_l2 < pamt_l then
pamt_l_diff = pamt_l - pamt_l2
local enterpricelongnew_1 = pamt_l2 * enterpricelong1
local enterpricelongnew_2 = pamt_l_diff * lastlongp
local enterpricelongnew = (enterpricelongnew_1 + enterpricelongnew_2) / pamt_l
pamt_l2 = pamt_l
enterpricelong1 = enterpricelongnew
end
if pamt_s2 < pamt_s then
pamt_s_diff = pamt_s - pamt_s2
local enterpriceshortnew_1 = pamt_s2 * enterpriceshort1
local enterpriceshortnew_2 = pamt_s_diff * lastshortp
local enterpriceshortnew = (enterpriceshortnew_1 + enterpriceshortnew_2) / pamt_s
pamt_s2 = pamt_s
enterpriceshort1 = enterpriceshortnew
end
Log('Pricebase cp.bid: '..cp.bid)
Save('enterpricelong1',enterpricelong1)
Save('enterpriceshort1',enterpriceshort1)
Save('pamt_l2',pamt_l2)
Save('pamt_s2',pamt_s2)
Save('tradeprofitlong',tradeprofitlong)
Save('tradeprofitshort',tradeprofitshort)
-- slot function
local slot = function(isLong, index, amount, spread, cancelDist, canPlace)
local prefix = isLong and 'L' or 'S'
local name = prefix .. index
local cmd = isLong and PlaceGoLongOrder or PlaceGoShortOrder
local priceBase = isLong
and cp.bid
or cp.ask
local spr = minSpread + spread * index
local posId = getPositionId(isLong)
local aep = isLong and enterpricelong1 or enterpriceshort1
-- if we have average entry price
if aep > 0 then
priceBase = isLong
and Min(aep, priceBase)
or Max(aep, priceBase)
end
-- get price
local price = isLong
and SubPerc(priceBase, spr)
or AddPerc(priceBase, spr)
local oid = Load(name..'oid', '') -- order id
if oid != '' then
local order = OrderContainer(oid)
if order.isOpen then
local delta = isLong
and Delta(AddPerc(order.price, spr), priceBase)
or Delta(priceBase, SubPerc(order.price, spr))
if delta >= cancelDist then
CancelOrder(oid)
LogWarning('Delta cancelled '..name)
elseif not canPlace then
CancelOrder(oid)
LogWarning('Not allowed right now '..name)
end
else
oid = ''
end
else
if canPlace then
SetFee(Abs(MakersFee())*-1)
oid = cmd(price, amount, {type = MakerOrCancelOrderType, note = name, timeout = 3600, positionId = posId})
end
end
Save(name..'oid', oid)
end
-- slot2 function
local slot2 = function(isLong, index, amount, spread, cancelDist, canPlace)
local prefix = isLong and 'L' or 'S'
local name = prefix .. index
local cmd = isLong and PlaceGoLongOrder or PlaceGoShortOrder
local priceBase = isLong
and cp.bid
or cp.ask
local spr = 0
local posId = getPositionId(isLong)
local aep = isLong and enterpricelong1 or enterpriceshort1
-- if we have average entry price
if aep > 0 then
priceBase = isLong
and cp.bid
or cp.ask
end
-- get price
local price = isLong
and cp.bid
or cp.ask
local oid = Load(name..'oid', '') -- order id
if oid != '' then
local order = OrderContainer(oid)
if order.isOpen then
local delta = isLong
and Delta(AddPerc(order.price, spr), priceBase)
or Delta(priceBase, SubPerc(order.price, spr))
if delta >= cancelDist then
CancelOrder(oid)
LogWarning('Delta cancelled '..name)
elseif not canPlace then
CancelOrder(oid)
LogWarning('Not allowed right now '..name)
end
else
oid = ''
end
else
if canPlace then
SetFee(Abs(MakersFee())*-1)
oid = cmd(cp.close, amount, {type = MakerOrCancelOrderType, note = name, timeout = 3600, positionId = posId})
end
end
Save(name..'oid', oid)
end
-- slot3 function
local slot3 = function(isLong, index, amount, spread, cancelDist, canPlace)
local prefix = isLong and 'L' or 'S'
local name = prefix .. index
local cmd = isLong and PlaceGoLongOrder or PlaceGoShortOrder
local priceBase = isLong
and cp.bid
or cp.ask
local spr = 0
local posId = getPositionId(isLong)
local aep = isLong and enterpricelong1 or enterpriceshort1
-- if we have average entry price
if aep > 0 then
priceBase = isLong
and cp.bid
or cp.ask
end
-- get price
local price = isLong
and cp.bid
or cp.ask
local oid = Load(name..'oid', '') -- order id
if oid != '' then
local order = OrderContainer(oid)
if order.isOpen then
local delta = isLong
and Delta(AddPerc(order.price, spr), priceBase)
or Delta(priceBase, SubPerc(order.price, spr))
if delta >= cancelDist then
CancelOrder(oid)
LogWarning('Delta cancelled '..name)
elseif not canPlace then
CancelOrder(oid)
LogWarning('Not allowed right now '..name)
end
else
oid = ''
end
else
if canPlace then
SetFee(Abs(MakersFee())*-1)
oid = cmd( limit_preis_long , amount, {type = MakerOrCancelOrderType, note = name, timeout = 3600, positionId = posId})
end
end
Save(name..'oid', oid)
end
-- slot4 function
local slot4 = function(isLong, index, amount, spread, cancelDist, canPlace)
local prefix = isLong and 'L' or 'S'
local name = prefix .. index
local cmd = isLong and PlaceGoLongOrder or PlaceGoShortOrder
local priceBase = isLong
and cp.bid
or cp.ask
local spr = 0
local posId = getPositionId(isLong)
local aep = isLong and enterpricelong1 or enterpriceshort1
-- if we have average entry price
if aep > 0 then
priceBase = isLong
and cp.bid
or cp.ask
end
-- get price
local price = isLong
and cp.bid
or cp.ask
local oid = Load(name..'oid', '') -- order id
if oid != '' then
local order = OrderContainer(oid)
if order.isOpen then
local delta = isLong
and Delta(AddPerc(order.price, spr), priceBase)
or Delta(priceBase, SubPerc(order.price, spr))
if delta >= cancelDist then
CancelOrder(oid)
LogWarning('Delta cancelled '..name)
elseif not canPlace then
CancelOrder(oid)
LogWarning('Not allowed right now '..name)
end
else
oid = ''
end
else
if canPlace then
SetFee(Abs(MakersFee())*-1)
oid = cmd( limit_preis_short , amount, {type = MakerOrCancelOrderType, note = name, timeout = 3600, positionId = posId})
end
end
Save(name..'oid', oid)
end
-- update take-profit
local updateTakeProfit = function(isLong, entryPrice, targetRoi, cancelDist)
local prefix = isLong and 'Long' or 'Short'
local name = prefix .. ' Take-Profit'
local oid = Load(prefix .. 'tp_oid', '')
local timer = Load(prefix .. 'tp_timer', 0)
local maxprofit = Load(prefix ..'tp_maxprofit', 0)
local maxprofitmin = Load(prefix ..'tp_maxprofitmin', 0)
local posId = getPositionId(isLong)
local tp_delta = isLong and Delta(entryPrice, cp.bid) or Delta(cp.ask, entryPrice)
if tp_delta >= targetRoi and maxprofit < tp_delta then
maxprofit = tp_delta
maxprofitmin = maxprofit * dynprofit
end
if tp_delta < targetRoi then
maxprofit = 0
maxprofitmin = 0
end
if oid != '' then
local order = OrderContainer(oid)
if order.isOpen then
local delta = isLong
and Delta(order.price, cp.close)
or Delta(cp.close, order.price)
if delta >= cancelDist then
CancelOrder(oid)
LogWarning('Delta cancelled '..name)
end
else
if order.isCancelled then
timer = 0
end
oid = ''
end
else
if tp_delta <= maxprofitmin then
if tp_delta >= targetRoi and Time() >= timer then
SetFee(tpOrderType == MarketOrderType and TakersFee() or Abs(MakersFee())*-1)
oid = PlaceExitPositionOrder({type = tpOrderType, note = name, timeout = 3600, positionId = posId})
timer = Time() + 180 -- 3min
end
end
end
Save(prefix .. 'tp_oid', oid)
Save(prefix .. 'tp_timer', timer)
Save(prefix .. 'tp_maxprofit', maxprofit)
Save(prefix .. 'tp_maxprofitmin', maxprofitmin)
end
-- update position size
local updatePositionManagement = function(isLong, currentSize, sizeLimit, cancelDist)
local prefix = isLong and 'Long' or 'Short'
local name = prefix .. ' Size Reduction'
local oid = Load(prefix .. 'pos_oid', '')
local posId = getPositionId(isLong)
local amount = SubPerc(currentSize, 100 - reduceSize) -- take X% of position
local price = isLong
and cp.ask
or cp.bid
local cmd = isLong
and PlaceExitLongOrder
or PlaceExitShortOrder
local timer = Load(prefix .. 'pos_timer', Time())
if oid != '' then
local order = OrderContainer(oid)
if order.isOpen then
local delta = isLong
and Delta(order.price, cp.close)
or Delta(cp.close, order.price)
if delta >= cancelDist then
CancelOrder(oid)
LogWarning('Delta cancelled '..name)
end
else
oid = ''
end
else
if currentSize > sizeLimit and Time() >= timer and reduceSize > 0 then
SetFee(reduceOrderType == MarketOrderType and TakersFee() or Abs(MakersFee())*-1)
oid = cmd(price, amount, {type = reduceOrderType, note = name, timeout = 3600, positionId = posId})
timer = Time() + 120 -- 2min
end
end
Save(prefix .. 'pos_oid', oid)
Save(prefix .. 'pos_timer', timer)
end
-- sellsafety
local sellsafty = function(isLong, amountsell, cancelDist)
local prefix = isLong and 'Long' or 'Short'
local name = prefix .. ' sellsafty'
local oid = Load(prefix .. 'pos_oid', '')
local posId = getPositionId(isLong)
local amount = amountsell
local price = isLong
and cp.ask
or cp.bid
local cmd = isLong
and PlaceExitLongOrder
or PlaceExitShortOrder
local timer = Load(prefix .. 'pos_timer', Time())
if oid != '' then
local order = OrderContainer(oid)
if order.isOpen then
local delta = isLong
and Delta(order.price, cp.close)
or Delta(cp.close, order.price)
if delta >= cancelDist then
CancelOrder(oid)
LogWarning('Delta cancelled '..name)
end
else
oid = ''
end
else
if Time() >= timer then
SetFee(reduceOrderType == LimitOrderType and TakersFee() or Abs(MakersFee())*-1)
oid = cmd(price, amount, {type = reduceOrderType, note = name, timeout = 300, positionId = posId})
timer = Time() + 60 -- 1min
end
end
Save(prefix .. 'pos_oid', oid)
Save(prefix .. 'pos_timer', timer)
end
-- selllong Profit
local sellprofit = function(isLong, amountsell, cancelDist)
local prefix = isLong and 'Long' or 'Short'
local name = prefix .. ' selllongprofit'
local oid = Load(prefix .. 'pos_oid', '')
local posId = getPositionId(isLong)
local amount = amountsell
local price = isLong
and cp.ask
or cp.bid
local cmd = isLong
and PlaceExitLongOrder
or PlaceExitShortOrder
local timer = Load(prefix .. 'pos_timer', Time())
if oid != '' then
local order = OrderContainer(oid)
if order.isOpen then
local delta = isLong
and Delta(order.price, cp.close)
or Delta(cp.close, order.price)
if delta >= cancelDist then
CancelOrder(oid)
LogWarning('Delta cancelled '..name)
end
else
oid = ''
end
else
if Time() >= timer then
SetFee(reduceOrderType == LimitOrderType and TakersFee() or Abs(MakersFee())*-1)
oid = cmd(price, amount, {type = reduceOrderType, note = name, timeout = 300, positionId = posId})
timer = Time() + 60 -- 1min
end
end
Save(prefix .. 'pos_oid', oid)
Save(prefix .. 'pos_timer', timer)
end
-- selllong Stop loss
local longstoploss = function(isLong, amountsell, cancelDist)
local prefix = isLong and 'Long' or 'Short'
local name = prefix .. ' longSL'
local oid = Load(prefix .. 'pos_oid', '')
local posId = getPositionId(isLong)
local amount = amountsell
local price = isLong
and enterpricelong1 * 1.001
or cp.bid
local cmd = isLong
and PlaceExitLongOrder
or PlaceExitShortOrder
local timer = Load(prefix .. 'pos_timer', Time())
if Time() >= timer then
SetFee(reduceOrderType == LimitOrderType and TakersFee() or Abs(MakersFee())*-1)
Log("bevor SL")
oid = cmd(price, amount, {type = reduceOrderType, note = name, timeout = 120, positionId = posId})
timer = Time() + 60 -- 1min
LogWarning("Stop Loss 2 , Price:" ..price.."Size"..amount )
end
Save(prefix .. 'pos_oid', oid)
Save(prefix .. 'pos_timer', timer)
end
-- dynamic slot size and max position
--max position
local maxCheck = autoMax and (maxMax * leverage_save)
local maxValue = autoMax and (maxCheck > maxSizeM) and maxCheck or maxSizeM
local maxSize = autoMax and maxValue or maxSizeM
-- check max open
if autoMax then
if maxCheck > maxSizeM then
Log('Dynamic Max Open: '..Round(maxCheck, roundc)..' '..AmountLabel())
else
Log('Dynamic Max Open: '..Round(maxCheck, roundc)..' but using default value: '..Round(maxSizeM, roundc)..' '..AmountLabel())
end
end
-- auto slot size
local slotCheck1 = autoSlot and (maxSize / slotBudget1)
local slotCheck2 = autoSlot and (maxSize / slotBudget2)
local slotCheck3 = autoSlot and (maxSize / slotBudget3)
local slotValues1 = autoSlot and (slotCheck1 > slotSize_1) and slotCheck1 or slotSize_1
local slotValues2 = autoSlot and (slotCheck2 > slotSize_2) and slotCheck2 or slotSize_2
local slotValues3 = autoSlot and (slotCheck3 > slotSize_3) and slotCheck3 or slotSize_3
local slotValuel1 = autoSlot and (slotCheck1 > slotSizel_1) and slotCheck1 or slotSizel_1
local slotValuel2 = autoSlot and (slotCheck2 > slotSizel_2) and slotCheck2 or slotSizel_2
local slotValuel3 = autoSlot and (slotCheck3 > slotSizel_3) and slotCheck3 or slotSizel_3
local slotSize1 = autoSlot and slotValues1 or slotSize_1
local slotSize2 = autoSlot and slotValues2 or slotSize_2
local slotSize3 = autoSlot and slotValues3 or slotSize_3
local slotSizel1 = autoSlot and slotValuel1 or slotSizel_1
local slotSizel2 = autoSlot and slotValuel2 or slotSizel_2
local slotSizel3 = autoSlot and slotValuel3 or slotSizel_3
if autoSlot then
if okShort then
if slotCheck1 > slotSize_1 then
slotSize_1 = slotSize1
Log('Dynamic Slot S1 Size: '..Round(slotSize1, roundc))
else
Log('DSSize: S1 '..Round(slotCheck1, roundc)..' But using default value: '..Round(slotSize_1, roundc))
end
if slotCheck2 > slotSize_2 then
slotSize_2 = slotSize2
Log('Dynamic Slot S2 Size: '..Round(slotSize2, roundc))
else
Log('DSSize: S2 '..Round(slotCheck2, roundc)..' But using default value: '..Round(slotSize_2, roundc))
end
if slotCheck3 > slotSize_3 then
slotSize_3 = slotSize3
Log('Dynamic Slot S3 Size: '..Round(slotSize3, roundc))
else
Log('DSSize: S3 '..Round(slotCheck3, roundc)..' But using default value: '..Round(slotSize_3, roundc))
end
end
if okLong then
if slotCheck1 > slotSizel_1 then
slotSizel_1 = slotSizel1
Log('Dynamic Slot L1 Size: '..Round(slotSizel1, roundc))
else
Log('DSSize: L1 '..Round(slotCheck1, roundc)..' But using default value: '..Round(slotSizel_1, roundc))
end
if slotCheck2 >= slotSizel_2 then
slotSizel_2 = slotSizel2
Log('Dynamic Slot L2 Size: '..Round(slotSizel2, roundc))
else
Log('DSSize: L2 '..Round(slotCheck2, roundc)..' But using default value: '..Round(slotSizel_2,roundc))
end
if slotCheck3 > slotSizel_3 then
slotSizel_3 = slotSizel3
Log('Dynamic Slot L3 Size: '..Round(slotSizel3, roundc))
else
Log('DSSize: L3 '..Round(slotCheck3, roundc)..' But using default value: '..Round(slotSizel_3, roundc))
end
end
else
if okShort then
Log('Manual Slot S1 Size: '..Round(slotSize_1, roundc))
Log('Manual Slot S2 Size: '..Round(slotSize_2, roundc))
Log('Manual Slot S3 Size: '..Round(slotSize_3, roundc))
end
if okLong then
Log('Manual Slot L1 Size: '..Round(slotSizel_1, roundc))
Log('Manual Slot L2 Size: '..Round(slotSizel_2, roundc))
Log('Manual Slot L3 Size: '..Round(slotSizel_3, roundc))
end
end
-- manuell Slots
-- Agressiv Mod1
local slotSpreadl = 0
local slotSpreads = 0
local slotCancels = 0
local slotCancell = 0
local mod3long = 0
local mod3short = 0
if autoMax == true and maxCheck > maxSizeM then
Log("AUTOMATIC MAXIMUM")
maxSizeL_1 = maxCheck * maxSizeL_1 / 100
maxSizeL_2 = maxCheck * maxSizeL_2 / 100
maxSizeL_3 = maxCheck * maxSizeL_3 / 100
maxSizeS_1 = maxCheck * maxSizeS_1 / 100
maxSizeS_2 = maxCheck * maxSizeS_2 / 100
maxSizeS_3 = maxCheck * maxSizeS_3 / 100
maxSizeS_safty0 = maxCheck * maxSizeS_safty0 / 100
maxSizeS_safty = maxCheck * maxSizeS_safty / 100
maxSizeL_safty0 = maxCheck * maxSizeL_safty0 / 100
maxSizeL_safty = maxCheck * maxSizeL_safty / 100
maxSizeL_4 = maxCheck * maxSizeL_4 / 100
minSizeL_4 = maxCheck * minSizeL_4 / 100
if pamt_l < maxSizeL_1 then
slotSpreadl = slotSpread_1
slotCancell = slotCancel_1
slotSizel = slotSizel_1
end
if pamt_l < maxSizeL_2 and pamt_l >= maxSizeL_1 then
slotSizel = slotSizel_2
slotSpreadl = slotSpread_2
slotCancell = slotCancel_2
end
if pamt_l < maxSizeL_3 and pamt_l >= maxSizeL_2 then
slotSizel = slotSizel_3
slotSpreadl = slotSpread_3
slotCancell = slotCancel_3
mod3long = 1
end
if pamt_s < maxSizeS_1 then
slotSpreads = slotSpread_1
slotCancels = slotCancel_1
slotSizes = slotSize_1
end
if pamt_s < maxSizeS_2 and pamt_s >= maxSizeS_1 then
slotSpreads = slotSpread_2
slotCancels = slotCancel_2
slotSizes = slotSize_2
end
if pamt_s < maxSizeS_3 and pamt_s >= maxSizeS_2 then
slotSpreads = slotSpread_3
slotCancels = slotCancel_3
slotSizes = slotSize_3
mod3short = 1
end
else
Log("MANUAL MAXIMUM")
maxSizeL_1 = maxSizeM * maxSizeL_1 / 100
maxSizeL_2 = maxSizeM * maxSizeL_2 /100
maxSizeL_3 = maxSizeM * maxSizeL_3 / 100
maxSizeS_1 = maxSizeM * maxSizeS_1 / 100
maxSizeS_2 = maxSizeM * maxSizeS_2 /100
maxSizeS_3 = maxSizeM * maxSizeS_3 / 100
maxSizeL_4 = maxSizeM * maxSizeL_4 / 100
minSizeL_4 = maxSizeM * minSizeL_4 / 100
maxSizeS_safty0 = maxSizeM * maxSizeS_safty0 / 100
maxSizeS_safty = maxSizeM * maxSizeS_safty / 100
maxSizeL_safty0 = maxSizeM * maxSizeL_safty0 / 100
maxSizeL_safty = maxSizeM * maxSizeL_safty / 100
if pamt_l < maxSizeL_1 then
slotSizel = slotSizel_1
slotSpreadl = slotSpread_1
slotCancell = slotCancel_1
end
if pamt_s < maxSizeS_1 then
slotSizes = slotSize_1
slotSpreads = slotSpread_1
slotCancels = slotCancel_1
end
-- Medium Mod2
if pamt_l < maxSizeL_2 and pamt_l >= maxSizeL_1 then
slotSizel = slotSizel_2
slotSpreadl = slotSpread_2
slotCancell = slotCancel_2
end
if pamt_s < maxSizeS_2 and pamt_s >= maxSizeS_1 then
slotSizes = slotSize_2
slotSpreads = slotSpread_2
slotCancels = slotCancel_2
end
-- Normal Mod3
if pamt_l < maxSizeL_3 and pamt_l >= maxSizeL_2 then
slotSizel = slotSizel_3
slotSpreadl = slotSpread_3
slotCancell = slotCancel_3
mod3long = 1
end
if pamt_s < maxSizeS_3 and pamt_s >= maxSizeS_2 then
slotSizes = slotSize_3
slotSpreads = slotSpread_3
slotCancels = slotCancel_3
mod3short = 1
end
end
if okLong then
Log('LONG Max Size Mod1 '..Round(maxSizeL_1,roundc))
Log('LONG Max Size Mod2 '..Round(maxSizeL_2,roundc))
Log('LONG Max Size Mod3 '..Round(maxSizeL_3,roundc))
Log('LONG Max Start Size Profitmod '..Round(maxSizeL_4,roundc))
Log('LONG Size Stopp Loss '..Round(minSizeL_4,roundc))
Log('LONG SlotSpread '..Round(slotSpreadl,2))
if oksafty then
Log("LONG Max Size Safetymod 1 "..Round(maxSizeL_safty0,roundc))
Log("LONG Max Size Safetymod 2-5 "..Round(maxSizeL_safty,roundc))
end
end
if okShort then
Log('SHORT Max Size Mod1 '..Round(maxSizeS_1,roundc))
Log('SHORT Max Size Mod2 '..Round(maxSizeS_2,roundc))
Log('SHORT Max Size Mod3 '..Round(maxSizeS_3,roundc))
if oksafty then
Log("SHORT Max Size Safetymod 1 "..Round(maxSizeS_safty0,roundc))
Log("SHORT Max Size Safetymod 2-5 "..Round(maxSizeS_safty,roundc))
end
end
local maxgewinn = Load('maxgewinn', 0)
local mingewinn = Load('mingewinn', 0)
local safesell = Load('safesell', 0)
local difflong = 0
local diffshort = 0
local amountsell = 0
if oksafty == true and ProfitMod2 == false then
-- Safty 1
if enterpricedifflongbuy <= safty0 and enterpricedifflongbuy > safty and pamt_l * saftysize0 < maxSizeS_safty0 and pamt_s < pamt_l * saftysize0 and enterpricediffshortsell >= 0 and safesell == 0 then
difflong = ((pamt_l * saftysize0) - pamt_s)
if maxSizeS_safty0 >= (pamt_s + difflong) then
Log('Repurchase Safety1 Number Short - not maximum' ..difflong)
Log('Size Long' ..pamt_l)
Log('Size Short' ..pamt_s)
Log('maxSizeS_safety0' ..maxSizeS_safty0)
Log('safetysize0' ..saftysize0)
slot2(false, 0, difflong , 0, 0.01, proi_s >= -10000 )
end
if maxSizeS_safty0 < (pamt_s + difflong) then
difflong = maxSizeS_safty0 - pamt_s
if difflong > 0 then
Log('Repurchase Safety1 Number Short - Maximum' ..difflong)
Log('Size Long' ..pamt_l)
Log('Size Short' ..pamt_s)
Log('maxSizeS_safety0' ..maxSizeS_safty0)
Log('safetysize0' ..saftysize0)
slot2(false, 0, difflong , 0, 0.01, proi_s >= -10000 )
end
end
end
if enterpricediffshortbuy <= safty0 and enterpricediffshortbuy > safty2 and pamt_l * saftysize0 < maxSizeL_safty0 and pamt_l < pamt_s * saftysize0 and enterpricedifflongsell >= 0 and safesell == 0 then
diffshort = ((pamt_s * saftysize0) - pamt_l)
if maxSizeL_safty0 >= (pamt_l + diffshort) then
Log('Repurchase Safety1 Number Long - not Maximum' ..diffshort)
Log('Size Long' ..pamt_l)
Log('Size Short' ..pamt_s)
Log('maxSizeL_safety0' ..maxSizeL_safty0)
Log('safetysize0' ..saftysize0)
slot2(true, 0, diffshort, 0, 0.01, proi_l >= -10000 )
end
if maxSizeL_safty0 < (pamt_l + diffshort) then
diffshort = maxSizeL_safty0 - pamt_l
if diffshort > 0 then
Log('Repurchase Safety1 Number Long - Maximum' ..diffshort)
Log('Size Long' ..pamt_l)
Log('Size Short' ..pamt_s)
Log('maxSizeL_safety0' ..maxSizeL_safty0)
Log('safetysize0' ..saftysize0)
slot2(true, 0, diffshort, 0, 0.01, proi_l >= -10000 )
end
end
end
if enterpricedifflongsell < safty0 and enterpricedifflongsell > safty2 + backsafty and pamt_l * saftysize2 >= pamt_s and enterpricediffshortsell >= saftyprofitsell and okbacksafty then
modsaftyl = 1
amountsell = pamt_s - pamt_l * saftysize0
sellsafty(false, amountsell, slotCancel)
Log('Sell short Safty 1' ..amountsell)
end
if enterpricedifflongsell > safty0 then
modsaftyl = 0
end
if enterpricediffshortsell > safty0 then
modsaftys = 0
end
if enterpricediffshortsell < safty0 and enterpricediffshortsell >= safty2 + backsafty and pamt_s * saftysize2 >= pamt_l and enterpricedifflongsell >= saftyprofitsell and okbacksafty then
modsaftys = 1
amountsell = pamt_l - pamt_s * saftysize0
sellsafty(true, amountsell, slotCancel)
Log('Sell long Safty 1' ..amountsell)
end
-- Safty 2-5
local modsaftyl = Load('modsaftyl', 0)
local modsaftys = Load('modsaftys', 0)
if enterpricediffshortsell > safty2 then
safes = 0
else
safes = 1
end
if enterpricedifflongsell > safty2 then
safel = 0
else
safel =1
end
-- Safty 2
if enterpricedifflongsell < safty2 and enterpricedifflongsell > safty3 + backsafty and pamt_l * saftysize3 >= pamt_s and enterpricediffshortsell >= saftyprofitsell and okbacksafty then
modsaftyl = 2
amountsell = pamt_s - pamt_l * saftysize2
sellsafty(false, amountsell, slotCancel)
Log('Sell short Safety 2' ..amountsell)
end
if enterpricedifflongsell <= safty2 and enterpricedifflongsell > safty3 and pamt_l * saftysize2 > pamt_s and enterpricediffshortsell >= 0 and safesell == 0 then
modsaftyl = 2
difflong = ((pamt_l * saftysize2) - pamt_s)
if difflong + pamt_s <= maxSizeS_safty then
slot2(false, 0, difflong , 0, 0.01, proi_s >= 0 )
else
difflong = maxSizeS_safty - pamt_s
slot2(false, 0, difflong , 0, 0.01, proi_s >= 0 )
end
Log('Buy short Safety 2' ..difflong)
end
if enterpricediffshortsell < safty2 and enterpricediffshortsell >= safty3 + backsafty and pamt_s * saftysize3 >= pamt_l and enterpricedifflongsell >= saftyprofitsell and okbacksafty then
modsaftys = 2
amountsell = pamt_l - pamt_s * saftysize2
sellsafty(true, amountsell, slotCancel)
Log('Sell long Safety 2' ..amountsell)
end
if enterpricediffshortsell < safty2 and enterpricediffshortsell > safty3 and pamt_s * saftysize2 > pamt_l and proi_l >= 0 and safesell == 0 then
modsaftys = 2
diffshort = ((pamt_s * saftysize2) - pamt_l)
if diffshort + pamt_l <= maxSizeL_safty then
slot2(true, 0, diffshort, 0, 0.01, proi_l >= 0 )
else
diffshort = maxSizeL_safty - pamt_l
slot2(true, 0, diffshort, 0, 0.01, proi_l >= 0 )
end
Log('Buy long Safety 2' ..diffshort)
end
-- Safty 3
if enterpricedifflongsell < safty3 and enterpricedifflongsell > safty4 + backsafty and pamt_l * saftysize4 >= pamt_s and enterpricediffshortsell >= saftyprofitsell and okbacksafty then
amountsell = pamt_s - pamt_l * saftysize3
sellsafty(false, amountsell, slotCancel)
Log('Sell short Safety 3' ..amountsell)
modsaftyl = 3
end
if enterpricedifflongsell <= safty3 and enterpricedifflongsell > safty4 and pamt_l * saftysize3 > pamt_s and enterpricediffshortsell >= 0 and safesell == 0 then
modsaftyl = 3
difflong = ((pamt_l * saftysize3) - pamt_s)
if difflong + pamt_s <= maxSizeS_safty then
slot2(false, 0, difflong , 0, 0.01, proi_s >= 0 )
else
difflong = maxSizeS_safty - pamt_s
slot2(false, 0, difflong , 0, 0.01, proi_s >= 0 )
end
Log('Buy short Safety 3' ..difflong)
end
if enterpricediffshortsell < safty3 and enterpricediffshortsell > safty4 + backsafty and pamt_s * saftysize4 >= pamt_l and enterpricedifflongsell >= saftyprofitsell and okbacksafty then
modsaftys = 3
amountsell = pamt_l - pamt_s * saftysize3
sellsafty(true, amountsell, slotCancel)
Log('Sell long Safety 3' ..amountsell)
end
if enterpricediffshortsell < safty2 and enterpricediffshortsell > safty3 and pamt_s * saftysize3 > pamt_l and proi_l >= 0 and safesell == 0 then
modsaftys = 3
diffshort = ((pamt_s * saftysize3) - pamt_l)
if diffshort + pamt_l <= maxSizeL_safty then
slot2(true, 0, diffshort, 0, 0.01, proi_l >= 0 )
else
diffshort = maxSizeL_safty - pamt_l
slot2(true, 0, diffshort, 0, 0.01, proi_l >= 0 )
end
Log('Buy long Safety 3' ..diffshort)
end
-- Safty 4
if enterpricedifflongsell < safty4 and enterpricedifflongsell > safty + backsafty and pamt_l * saftysize >= pamt_s and enterpricediffshortsell >= saftyprofitsell and okbacksafty then
modsaftyl = 4
amountsell = pamt_s - pamt_l * saftysize4
sellsafty(false, amountsell, slotCancel)
Log('Sell short Safety 4' ..amountsell)
end
if enterpricedifflongsell <= safty4 and enterpricedifflongsell > safty and pamt_l * saftysize4 > pamt_s and enterpricediffshortsell >= 0 and safesell == 0 then
modsaftyl = 4
difflong = ((pamt_l * saftysize4) - pamt_s)
if difflong + pamt_s <= maxSizeS_safty then
slot2(false, 0, difflong , 0, 0.01, proi_s >= 0 )
else
difflong = maxSizeS_safty - pamt_s
slot2(false, 0, difflong , 0, 0.01, proi_s >= 0 )
end
Log('Buy short Safety 4' ..difflong)
end
if enterpricediffshortsell < safty4 and enterpricediffshortsell > safty + backsafty and pamt_s * saftysize >= pamt_l and enterpricedifflongsell >= saftyprofitsell and okbacksafty then
modsaftys = 4
amountsell = pamt_l - pamt_s * saftysize4
sellsafty(true, amountsell, slotCancel)
Log('Sell long Safety 4' ..amountsell)
end
if enterpricediffshortsell < safty4 and enterpricediffshortsell > safty and pamt_s * saftysize4 > pamt_l and proi_l >= 0 and safesell == 0 then
modsaftys = 4
diffshort = ((pamt_s * saftysize4) - pamt_l)
if diffshort + pamt_l <= maxSizeL_safty then
slot2(true, 0, diffshort, 0, 0.01, proi_l >= 0 )
else
diffshort = maxSizeL_safty - pamt_l
slot2(true, 0, diffshort, 0, 0.01, proi_l >= 0 )
end
Log('Buy long Safety 4' ..diffshort)
end
-- Safty 5
if enterpricedifflongsell <= safty and pamt_l * saftysize > pamt_s and enterpricediffshortsell >= 0 and safesell == 0 then
modsaftyl = 5
difflong = ((pamt_l * saftysize) - pamt_s)
if difflong + pamt_s <= maxSizeS_safty then
slot2(false, 0, difflong , 0, 0.01, proi_s >= 0 )
else
difflong = maxSizeS_safty - pamt_s
slot2(false, 0, difflong , 0, 0.01, proi_s >= 0 )
end
end
if enterpricediffshortsell < safty and pamt_s * saftysize > pamt_l and enterpricedifflongsell >= 0 and safesell == 0 then
modsafty5 = 5
diffshort = ((pamt_s * saftysize) - pamt_l)
if diffshort + pamt_l <= maxSizeL_safty then
slot2(true, 0, diffshort, 0, 0.01, proi_l >= 0 )
else
diffshort = maxSizeL_safty - pamt_l
slot2(true, 0, diffshort, 0, 0.01, proi_l >= 0 )
end
end
Log('Safetymod long'..modsaftyl)
Log('Safetymod short'..modsaftys)
Save('modsaftyl', modsaftyl)
Save('modsaftys', modsaftys)
-- Profit Safty
local maxgewinn = Load('maxgewinn', 0)
local mingewinn = Load('mingewinn', 0)
local safesell = Load('safesell', 0)
local gewinn_s = profitcalcshort
local gewinn_l = profitcalclong
local gewinn = posprofitshort + posprofitlong
if maxgewinn < gewinn and gewinn >= saftyprofit then
maxgewinn = gewinn
mingewinn = maxgewinn * dynprofitsafty
end
if gewinn < saftyprofit then
maxgewinn = 0
mingewinn = 0
end
if gewinn > saftyprofit and ((enterpricediffshortsell < safty and pamt_l > pamt_s) or (enterpricedifflongsell < safty and pamt_s > pamt_l) ) then
if gewinn <= mingewinn then
safesell = 1
maxSizeL = 0
maxSizeS = 0
reduceSize = 100
end
end
if safesell == 1 and pamt_l == 0 and pamt_s ==0 then
safesell = 0
end
if safesell == 1 then
maxSizeL = 0
maxSizeS = 0
reduceSize = 100
end
Log('Dissolve Safetymod, sell everything when 1 - '..safesell)
Log('Safetymod Profit'..gewinn)
Log('Safetymod Max Profit'..maxgewinn)
Log('Safetymod Min Profit'..mingewinn)
Save('maxgewinn', maxgewinn)
Save('mingewinn', mingewinn)
Save('safesell', safesell)
end
-- Buy Limit current Price
local limit_open_akt_long_now = 0
if limit_open_akt_long > 0 and limit_long_akt_aktiv != 1 then
limit_open_akt_long_now = limit_open_akt_long
slot2(true, 42, limit_open_akt_long_now , 0, 0.2, proi_l >= hedgeRoi )
limit_long_akt_aktiv = 1
limit_open_akt_long_now = 0
end
if limit_open_akt_long == 0 then
limit_long_akt_aktiv = 0
end
local limit_open_akt_short_now = 0
if limit_open_akt_short > 0 and limit_short_akt_aktiv != 1 then
limit_open_akt_short_now = limit_open_akt_short
slot2(false, 42, limit_open_akt_short_now , 0, 1, proi_s >= hedgeRoi )
limit_short_akt_aktiv = 1
limit_open_akt_short_now = 0
end
if limit_open_akt_short == 0 then
limit_short_akt_aktiv = 0
end
-- Buy Limit select Price
local limit_open_long_now = 0
if limit_open_long > 0 and limit_preis_long > 0 and limit_long_aktiv != 1 then
limit_open_long_now = limit_open_long
slot3(true, 42, limit_open_long_now , 0, 1, proi_l >= hedgeRoi )
limit_long_aktiv = 1
limit_open_long_now = 0
end
if limit_open_long == 0 then
limit_long_aktiv = 0
end
local limit_open_short_now = 0
if limit_open_short > 0 and limit_preis_short > 0 and limit_short_aktiv != 1 then
limit_open_short_now = limit_open_short
slot4(false, 42, limit_open_short_now , 0, 1, proi_s >= hedgeRoi )
limit_short_aktiv = 1
end
if limit_open_short == 0 then
limit_short_aktiv = 0
end
if ProfitMod2 == true and okShort == false and okLong == true then
Log("ProfitMod2")
-- longmod aktiv
local longmod = Load('longmod', 0)
local maxroislotlong_save = Load('maxroislotlong_save', maxroislotlong)
local slroislotlong_save = Load('slroislotlong_save', slroislotlong)
local maxSizeL_4_save = Load('maxSizeL_4_save', maxSizeL_4)
local minSizeL_4_save = Load('minSizeL_4_save', minSizeL_4)
if enterpricelong1 * ((100 + maxroislotlong_save) /100 ) < cp.ask and aep_l > 0 then
longmod = 1
Log("Profitmod activates")
end
if pamt_l == 0 then
longmod = 0
maxroislotlong_save = maxroislotlong
slroislotlong_save = slroislotlong
maxSizeL_4_save = maxSizeL_4
minSizeL_4_save = minSizeL_4
leverage_save = leverage
minSizeL_4_save = minSizeL_4
end
-- Sell longmod
if longmod == 1 and pamt_l >= maxSizeL_4_save and enterpricelong1 * ((100 + maxroislotlong_save) /100 ) < cp.bid then
amountsell = Round((pamt_l * sellslotlong), roundc)
sellprofit(true, amountsell, slotCancel_4)
LogWarning ("LONGMOD SELL"..amountsell)
if oksldyn then
maxroislotlong_save = maxroislotlong_save * slminrois
slroislotlong_save = slroislotlong_save * slminrois
maxSizeL_4_save = maxSizeL_4_save * chsize
LogWarning ("Profit increases! Profit: "..maxroislotlong_save.."% SL: "..slroislotlong_save.."% Maximum Size Profitmod "..maxSizeL_4_save.."Contracte" )
if okchle and maxSizeL_4_save >= maxCheck and maxchle > leverage_save then
leverage_save = leverage_save + 1
end
if okchle and maxSizeL_4_save >= maxCheck and maxchle <= leverage_save then
maxSizeL_4_save = maxCheck
end
end
end
if longmod == 0 then
maxroislotlong_save = maxroislotlong
slroislotlong_save = slroislotlong
maxSizeL_4_save = maxSizeL_4
minSizeL_4_save = minSizeL_4
leverage_save = leverage
minSizeL_4_save = minSizeL_4
if pamt_l > maxSizeL_4_save then
maxSizeL_4_save = pamt_l
end
end
if longmod == 1 and enterpricelong1 * ((100 + slroislotlong_save) /100 ) >= cp.bid then
amountsell = Round((pamt_l - minSizeL_4_save), roundc)
LogWarning ("STOP LOSS "..amountsell.." Profitnmod deactivated")
if amountsell >= 1 then
longstoploss(true, amountsell, 0.4)
end
if pamt_l <= minSizeL_4_save then
longmod = 0
if slminrois > 1 then
maxroislotlong_save = maxroislotlong
slroislotlong_save = slroislotlong
maxSizeL_4_save = maxSizeL_4
leverage_save = leverage
LogWarning ("Profit margin set to start! Profit: "..maxroislotlong_save.."% SL: "..slroislotlong_save.."% Profitmod auf "..Round(maxSizeL_4_save,0).."Contracte")
end
end
end
LogWarning("Profitmod Start: "..maxroislotlong_save.."% Stopp Loss "..slroislotlong_save.."% Maximum Size Profitmod "..Round(maxSizeL_4_save,0).." LONGMOD AKTIV:"..longmod )
Save('longmod', longmod)
Save('maxroislotlong_save', maxroislotlong_save)
Save('slroislotlong_save', slroislotlong_save)
Save('maxSizeL_4_save', maxSizeL_4_save)
Save('minSizeL_4_save', minSizeL_4_save)
if enterpricelong1 * ((100 + maxroislotlong_save) /100 ) < cp.ask and pamt_l < maxSizeL_4_save then
local amountbuy = Round(((pamt_l * slotSizel_4) - pamt_l), roundc)
slot2(true, 20, amountbuy, slotSpread_4, slotCancel_4, ( proi_s >= hedgeRoi))-- long slot
end
else
Log("ProfitMod1")
if safel == 0 and safes == 0 and safesell == 0 then
updateTakeProfit(true, enterpricelong1, takeProfitL, slotCancel)
updateTakeProfit(false, enterpriceshort1, takeProfitS, slotCancel)
end
leverage_save = leverage
end
Save('leverage_save', leverage_save)
-- da logica
-- take profit
-- risk management
if safesell ==1 or reduceSize > 0 then
updatePositionManagement(true, pamt_l, maxSizeL, slotCancel)
updatePositionManagement(false, pamt_s, maxSizeS, slotCancel)
end
-- update slots
if okLong == true then
if mod3long == 1 and enterpricedifflongsell >= mindist then
Log('Mod 3 Long Distance too small, no repurchasing!')
else
if safel == 1 or safesell == 1 or pamt_l >= maxSizeL_3 then
Log('Max long or saftymod')
else
for i = 1, slotCount do
slot(true, i, slotSizel, slotSpreadl, slotCancell, (pamt_s == 0 and signal == 1) or proi_s >= hedgeRoi)-- long slot
end
end
end
end
if okShort == true then
if mod3short == 1 and enterpricediffshortsell >= mindist then
Log('Mod 3 Short Distance too small, no repurchasing!')
else
if safes == 1 or safesell == 1 or pamt_s >= maxSizeS_3 then
Log('Max short or saftymod')
else
for i = 1, slotCount do
slot(false, i, slotSizes , slotSpreads, slotCancels, (pamt_l == 0 and signal == -1) or proi_l >= hedgeRoi) -- short slot
end
end
end
end
if enterpricelong1 > 0 then
local posId = getPositionId(true)
Plot(0, 'AvgEP Long', enterpricelong1, {c=Teal, id=posId, w=2})
end
if enterpriceshort1 > 0 then
local posId = getPositionId(false)
Plot(0, 'AvgEP Short', enterpriceshort1, {c=Purple, id=posId, w=2})
end
if okLong then Log('Size LONG '..pamt_l) end
if okShort then Log('Size SHORT '..pamt_s) end
if okLong then Log('profitcalclong aktuell '..Round(profitcalclong,5)) end
if okShort then Log('profitcalcshort aktuell'..Round(profitcalcshort,5)) end
LogWarning('Positions Profit: '..Round(profitpositions,5))
if okLong then LogWarning('LONG Difference Entry '..Round(enterpricedifflongsell,2).."%") end
if okLong then LogWarning('LONG own Enter Price: '..Round(enterpricelong1,2)) end
if okShort then LogWarning('SHORT Difference Entry '..Round(enterpricediffshortsell,2).."%") end
if okShort then LogWarning('SHORT own Enter Price: '..Round(enterpriceshort1,2)) end
Log("Leverage: "..leverage_save)
Log("Version 642-1 LOVE MUSIC HATE FASCISM")
Save('hedge_longPosId', hedge_longPosId)
Save('hedge_shortPosId', hedge_shortPosId)
Log(' ')
1 Comment
Sign in to leave a comment.
The Safetymod was only tested at Binance! Hedge mode is required!