0bps

Strategy vault / Other

Did You Use the Correct Position Average Price

Get the average holding price, most people use position = exchanges[0].GetPosition() avgPrice = position[0]["Price"] But this is actually not allowed. Print the Binance contract position information.: [map[Amount:5…

PythonOther
Author
LiteFly
Complexity
Starter · 19 lines
Parameters
0

Get the average holding price, most people use position = exchanges[0].GetPosition() avgPrice = position[0]["Price"] But this is actually not allowed. Print the Binance contract position information.: [map[Amount:5 ContractType:swap FrozenAmount:0 Info:map[entryPrice:55173.32071038 isAutoAddMargin:false isolatedMargin:0.00000000 isolatedWallet:0 leverage:20 liquidationPrice:0 marginType:cross markPrice:55171.20000000 maxQty:50 notionalValue:-0.00906269 positionAmt:-5 positionSide:BOTH symbol:BTCUSD_PERP unRealizedProfit:0.00000034] Margin:0.0004531349689693174 MarginLevel:20 Price:55173.32071038 Profit:3.4e-07 Type:1]]

It was found that there are two prices, entryPrice Price, and contract transactions are settled on different exchanges every day. After settlement, Price will change, and entryPrice is the real original position price., If you use Price to calculate the yield for take-profit and stop-loss at this time, it may result in significant losses.

For the above reasons, packaged the average position price functions of three major exchanges, take it without thanks

Source Python

def  getAvgPrice(position):
    if hasattr(position[0],'Info') and hasattr(position[0].Info,'cost_open'):# Huobi
        return position[0].Info.cost_open
    elif hasattr(position[0],'Info') and  hasattr(position[0].Info,'avg_cost'):#OKex
        return position[0].Info.avg_cost
    elif hasattr(position[0],'Info') and  hasattr(position[0].Info,'entryPrice'):#binance
        return position[0].Info.entryPrice
    else:
        return position[0]["Price"] 

def main():
    Log(exchange.GetAccount())
    position = exchanges[0].GetPosition()
    if len(position)>0:
        avgPrice = getAvgPrice(position)
        Log(avgPrice)
    
    

Published by LiteFly on the FMZ strategy square · last modified 2021-03-11. 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.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Did-You-Use-the-Correct-Position-Average-Price.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open