ww58
 ( 0.00% )
- ago
Binance margin. There are no open positions on the account, but the program displays as if they are. This is different from the other modules such as alpaca or ib, and accordingly the code doesn't work.
CODE:
Position foundPosition0 = FindOpenPosition(0); if (foundPosition0 == null) { }



As far as I understand it displays an account balances as an open positions, if there are some coins it displays as a long position, short otherwise, even if it's 0 quantity, which is totally wrong.
0
446
9 Replies

Reply

Bookmark

Sort
Cone8
 ( 23.46% )
- ago
#1
Okay, we can take care of those 0 quantity positions in the next build of the Binance Provider.

For now (or even later), you can work around that in your script like this -
CODE:
Position foundPosition0 = FindOpenPosition(0); if (foundPosition0 == null || foundPosition0.Quantity == 0) { }
1
Cone8
 ( 23.46% )
- ago
#2
That's a Binance Margin account, right? (If so, I see where the issue is.)
0
ww58
 ( 0.00% )
- ago
#3
>That's a Binance Margin account, right?
Yes

>For now (or even later), you can work around that in your script like this
Thanks, I'll try this

>we can take care of those 0 quantity positions in the next build of the Binance Provider
This would be a best solution
0
ww58
 ( 0.00% )
- ago
#4
I've tested with the following code
CODE:
Position foundPosition0 = FindOpenPosition(0); if (foundPosition0 == null || foundPosition0.Quantity == 0) {    PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, bars.Close[idx], 0, "Buy At Limit"); }

I was able to open a position, but it opened as a short one. So I can't do it without a fix.
0
Cone8
 ( 23.46% )
- ago
#5
Come again? Are you saying that a Buy at Limit order opened a short position?
More details, please.
0
ww58
 ( 0.00% )
- ago
#6
> Come again? Are you saying that a Buy at Limit order opened a short position?
Yes, there was the following code
CODE:
public override void Execute(BarHistory bars, int idx) {    Position foundPosition0 = FindOpenPosition(0);    if (foundPosition0 == null || foundPosition0.Quantity == 0)    { PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, bars.Close[idx], 0, "Buy At Limit");    }    else    { //ClosePosition(foundPosition0, OrderType.Stop, foundPosition0.EntryPrice * 0.98, "Sell at stop loss "); ClosePosition(foundPosition0, OrderType.Limit, foundPosition0.EntryPrice * 1.02, "Sell at profit target");    } }
At the first Run now an order was placed, I did not pay attention to the open position type. Then I tried to set SL and TP, they were not set, which is logical because they are designed for a long position, and it was short. Then I removed SL and left only TP. It was set, but as Cover, which should not be the case in a long position. Then I manually sent an order to close the position.

0
Cone8
 ( 23.46% )
- ago
#7
You said that it "opened as a short one". That didn't happen. Maybe you meant that Live Positions inserted a short position into the backtest - yah, that's going to happen because that's what the Broker Position is.

Probably "Live Positions" is reporting a near-0-quantity position that it's calling short. If it's not exactly 0, then the logic won't work. Probably you have very small residuals .. find out what those quantities are and adjust the workaround to ignore quantities less than that.

Run this in a Strategy Window, inserting your account Id. What are the "short" quantities?

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript18 {    public class MyStrategy : UserStrategyBase    {       public override void Initialize(BarHistory bars)       {          string acctId = "Kraken Account (EUR)";    // Enter the account ID as shown in the Accounts tool                    BrokerAccount brokerAccount = GetBrokerAccount(acctId);          if (brokerAccount != null)          {             WriteToDebugLog("Account Value = " + brokerAccount.AccountValue);             WriteToDebugLog("Buying Power = " + brokerAccount.BuyingPower);             WriteToDebugLog("Cash = " + brokerAccount.Cash );             if (double.IsNaN(brokerAccount.Cash))             {                foreach (KeyValuePair<string, double> kvp in brokerAccount.CurrencyBalances)                {                   WriteToDebugLog(kvp.Key + " = " + kvp.Value);                }             }             foreach (BrokerPosition p in brokerAccount.Positions)             {                WriteToDebugLog(p.Symbol + "\t" + p.Quantity + "\t" + p.BasisPrice.ToString("N2") + "\t" + p.CurrentPrice.ToString("N2")+ "\t" + p.Value.ToString("N2") );                            }          }          else          {                WriteToDebugLog("Make sure WealthLab is connected to the account");          }                 }       public override void Execute(BarHistory bars, int idx)       { }       //this method looks through all of the installed brokers, for the specified account       //and returns the live broker position for the specified symbol       private BrokerPosition GetBrokerPosition(string acct, string symbol, PositionType pt)       {          foreach (BrokerBase broker in SignalManager.Brokers)          {             BrokerAccount ba = broker.FindAccount(acct);             if (ba != null)                return ba.FindPosition(symbol, pt);          }          return null;       }       private BrokerAccount GetBrokerAccount(string acct)       {          foreach (BrokerBase broker in SignalManager.Brokers)          {             BrokerAccount ba = broker.FindAccount(acct);             if (ba != null)                return ba;          }          return null;       }          } }


0
ww58
 ( 0.00% )
- ago
#8
>You said that it "opened as a short one". That didn't happen
Perhaps I didn't put it that way, the binance had proper long position, there was no problem here. The problem is that for some reason the program considered it to be a short position, therefore it used the Cover method and not the Sell method, which is used for long positions. I think that's why no OCO order was placed initially.

>Run this in a Strategy Window, inserting your account Id. What are the "short" quantities?
Just like in the screenshot above

SUSHIUSDT 0 0.00 NaN NaN
BTSUSDT 0 0.00 NaN NaN
INJUSDT 0 0.00 NaN NaN
TRXBUSD 0 0.00 NaN NaN
...

>Probably "Live Positions" is reporting a near-0-quantity position that it's calling short
It is exactly equal to zero, which means it is not a position, because such a position cannot exist. If you take SUSHIUSDT as an example, there were no transactions with this coin on the account at all
0
Cone8
 ( 23.46% )
- ago
#9
Okay, I'd recommend you stop using this until we figure out what's going on. Currently we're not able to test Binance Margin functions.

Are you able work with Spot selected?
0

Reply

Bookmark

Sort