profit_strategies.py - Dynamic Profit

in LeoFinance3 months ago

I've been extra busy trying to make these bots profitable.

I'm trying to create a system that stabilizes the market more, and brings greater profit myway so I can maintain these servers.

  • Sets a minimum profit every time
    You never sell unless the price guarantees profit over what you paid.

  • Calculates the exact sell price needed to stay profitable
    The bot doesn’t guess — it math-locks the lowest safe exit.

  • Refuses unprofitable trades automatically
    If a trade doesn’t meet your profit rule, it simply doesn’t happen.

  • Measures the market spread before deciding profit targets
    It looks at how wide the gap is between buyers and sellers before acting.

  • Raises profit expectations when the market is risky
    Wide spreads = higher required profit to compensate for danger.

  • Lowers profit expectations when the market is healthy
    Tight spreads = smaller profits, but more frequent trades.

  • Adapts based on how successful recent trades have been
    If trades are winning often, it loosens up to increase volume.

  • Tightens profit rules when trades stop working
    Poor performance automatically makes the bot more conservative.

  • Clamps profit rules to prevent reckless behavior
    Profit targets can’t drop too low or spike too high accidentally.

  • Guarantees profit even if a sell price is miscalculated
    A final safety check forces the sell price above the buy price.

  • Calculates the real profit percentage after a trade
    Lets you log and analyze how well each trade actually performed.

  • Separates strategy from execution
    The logic is reusable across different bots, tokens, or markets.

  • Designed to protect capital first, profit second
    Survival is built into the system — not added later.


def calculate_min_sell_price(buy_price, min_profit_percent=0.02):
    """Calculate the minimum sell price to achieve desired profit percent (default 2%)."""
    return round(buy_price * (1 + min_profit_percent), 8)

def is_profitable(buy_price, sell_price, min_profit_percent=0.02):
    """
    Return True if sell_price meets or exceeds the minimum profit percent over buy_price.
    Guarantees profit if True.
    """
    min_sell = calculate_min_sell_price(buy_price, min_profit_percent)
    return sell_price >= min_sell

def is_profitable_or_volume_increase(buy_price, sell_price, buy_qty, sell_qty, min_profit_percent=0.02):
    """
    Return True if and only if sell_price meets/exceeds minimum profit percent over buy_price.
    Guarantees profit on every sale.
    """
    min_sell = calculate_min_sell_price(buy_price, min_profit_percent)
    return sell_price >= min_sell

def get_spread_percent(bid, ask):
    """Return bid/ask spread as a decimal percent of mid price."""
    try:
        bid = float(bid)
        ask = float(ask)
    except Exception:
        return 0.0
    if bid <= 0 or ask <= 0:
        return 0.0
    mid = (bid + ask) / 2
    if mid <= 0:
        return 0.0
    spread = max(ask - bid, 0.0)
    return spread / mid

def get_dynamic_min_profit_percent(bid, ask, floor=0.002, ceiling=0.03, spread_multiplier=1.5):
    """
    Return a dynamic minimum profit percent (decimal) based on current spread.
    - floor/ceiling are decimals (0.002 = 0.2%).
    - spread_multiplier scales the spread-derived target.
    """
    spread_pct = get_spread_percent(bid, ask)
    target = spread_pct * spread_multiplier
    if target < floor:
        target = floor
    if target > ceiling:
        target = ceiling
    return round(target, 6)

def adaptive_profit_floor(success_rate, base_floor=0.002, base_ceiling=0.03, 
                         min_floor=0.0005, max_floor=0.05):
    """
    Adapt the profit floor based on success rate (0.0 to 1.0).
    - High success (>0.7) → lower floor to increase volume
    - Low success (<0.3) → raise floor to guarantee better margins
    - Returns adjusted floor clamped between min_floor and max_floor
    """
    if success_rate >= 0.7:
        # High success: lower floor by 30%, but not below min_floor
        adjusted = base_floor * 0.7
    elif success_rate >= 0.5:
        # Moderate success: slight reduction
        adjusted = base_floor * 0.85
    elif success_rate >= 0.3:
        # Low-moderate: keep base
        adjusted = base_floor
    else:
        # Very low success: raise floor by 50%
        adjusted = base_floor * 1.5
    
    # Clamp to bounds
    adjusted = max(adjusted, min_floor)
    adjusted = min(adjusted, max_floor)
    return round(adjusted, 6)

def guarantee_profit(buy_price, sell_price, min_margin=0.0001):
    """
    Ensure sell_price > buy_price with minimum margin.
    If not profitable, adjust sell_price upward.
    Returns guaranteed profitable sell_price.
    """
    guaranteed_sell = buy_price * (1 + min_margin)
    return max(float(sell_price), guaranteed_sell) if sell_price > 0 else guaranteed_sell

def get_profit_percent(buy_price, sell_price):
    """Return the actual profit percent for a given buy/sell price."""
    if buy_price == 0:
        return 0.0
    return round((sell_price - buy_price) / buy_price * 100, 4)



🪙 PeakeCoin Ecosystem

💱 PeakeCoin USDT Bridge (Hive ↔ Polygon/MATIC)
Bridge SWAP.USDT from Hive Engine to USDT on Polygon (MATIC).
Whitelist access, documentation, and bridge status updates:
👉 https://geocities.ws/peakecoin


⚙️ HiveP.I.M.P. — PeakeCoin Intelligent Market Protector
Operated by @hivepimp, P.I.M.P. stabilizes PEK markets and supports liquidity on Hive Engine.
Community liquidity participation strengthens long-term market health.
📈 Open-source code, bots, and documentation:
👉 https://github.com/paulmoon410


🎰 PeakeSino — The PeakeCoin Casino (Beta)
Blockchain-powered games using PEK as the native in-game currency.
Built on Hive with a focus on provable fairness and community-driven growth.
🃏 Play the beta games here:
👉 https://geocities.ws/peakecoin/pek_casino/beta_games/index.html


🙏 Acknowledgements

Thanks to and please follow:
@enginewitty @ecoinstant @neoxian @txracer @thecrazygm @holdonia @aggroed

For their continued support, guidance, and help expanding the PeakeCoin ecosystem.


Sort:  

Is running for once free? the pekcoin fees might be on continuous server?
How to add more coins?

image.png

This is one piece of a collection of files to run the bots. This is part of the brain of the bot. The entire bot depends on the currency that's trading.

If you want an entire bot I can work that out as well.

If you have downloaded the windows program it should run continuously, if not I'll work on that.

it should run continuously, if not I'll work on that

are there any fees for running it?

It purchases .00000001 PEK each cycle if available. I have several bots running buying back PEK all day everyday.

If you want the other scripts I have a version on my GitHub

Yup, I trained AI bot on your repositories

Thanks for keeping them open source

My AI bot? I'm working on that too, lol.

Do you code?

I am Rookie

I'm working on JavaScript games. If you want to help.

I also followed you.