# source: https://raw.githubusercontent.com/hamidreza07/freqai-strategy/5bd42947b5300922a342d153182906e7cdbe8a40/startegy%20test/2/bigshort/bigshort.py
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
# flake8: noqa: F401

# --- Do not remove these libs ---
import numpy as np  # noqa
import pandas as pd  # noqa
from pandas import DataFrame
from datetime import datetime, timedelta, timezone
from freqtrade.exchange import timeframe_to_minutes
from freqtrade.strategy import (BooleanParameter, CategoricalParameter, DecimalParameter,
                                IStrategy, IntParameter, merge_informative_pair, informative)

from freqtrade.optimize.space import Categorical, Dimension, Integer, SKDecimal, Real  # noqa
# --------------------------------
# Add your lib to import here
import talib.abstract as ta
import pandas_ta as pta
import freqtrade.vendor.qtpylib.indicators as qtpylib
from technical.pivots_points import pivots_points
from typing import Any, Dict, List


class Github_hamidreza07_freqai_strategy__bigshort__20231226_132626(IStrategy):

    INTERFACE_VERSION = 3


    timeframe = '1m'
    informative_timeframe = '15m'
    can_short: bool = True

    minimal_roi = {
        "0": 0
    }


    stoploss = -1

    # Trailing stoploss
    trailing_stop= False
    trailing_stop_positive=0.01
    trailing_stop_positive_offset= 0.012
    trailing_only_offset_is_reached= False

    # Run "populate_indicators()" only for new candle.
    process_only_new_candles = True

    use_exit_signal = True
    exit_profit_only = False
    ignore_roi_if_entry_signal = False
    
    startup_candle_count: int = 50


    order_types = {
        'entry': 'limit',
        'exit': 'limit',
        'stoploss': 'market',
        'stoploss_on_exchange': False
    }

    order_time_in_force = {
        'entry': 'gtc',
        'exit': 'gtc'
    }
    
    @property
    def plot_config(self):
        return {
            # Main plot indicators (Moving averages, ...)
            'main_plot': {
                "MACD": {
                    'fastd': {'color': 'blue'},
                    'fastk': {'color': 'orange'},
                },
                "RSI": {
                    'rsi': {'color': 'red'},
                },
                "Pivot": {
                    'pivot': {'color': 'black'},
                },
                'SMA': {
                    'sma15': {'color': 'white'},
                    'sma50': {'color': 'yellow'},
                },
            },
            'subplots': {

            }
        }
    @property
    def protections(self):
        return  [
            {
                "method": "CooldownPeriod",
                "stop_duration_candles": 60
            }
        ]    

    def leverage(self, pair: str, current_time: 'datetime', current_rate: float,
                 proposed_leverage: float, max_leverage: float, side: str,
                 **kwargs) -> float:
        if pair == "ETH/USDT" or pair == "BTC/USDT":
            return 20

        return 10
    
    def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: float,
                            proposed_stake: float, min_stake: float, max_stake: float,
                            entry_tag: str, **kwargs) -> float:


        return self.wallets.get_total_stake_amount() / 5
    


    def informative_pairs(self):

        pairs = self.dp.current_whitelist()
        informative_pairs = [(pair, self.informative_timeframe) for pair in pairs]
        return informative_pairs
    

    
    @informative('15m')
    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        
        pp = pivots_points(dataframe)
        dataframe['pivot'] = pp["r1"]

        dataframe['doji_short'] = ta.CDLEVENINGDOJISTAR(dataframe)  
        dataframe['doji_long'] = ta.CDLMORNINGSTAR(dataframe) 

        # Stochastic Fast
        stoch_fast = ta.STOCHF(dataframe)
        dataframe['fastd'] = stoch_fast['fastd']
        dataframe['fastk'] = stoch_fast['fastk']

        macd, macdsignal, macdhist = ta.MACD(dataframe['close'], fastperiod=12, slowperiod=26, signalperiod=9)
        
        dataframe['macdf'] = macd
        dataframe['macdd'] = macdsignal
        dataframe['macdh'] = macdhist
       
        # # SMA - Simple Moving Average
        dataframe['sma15'] = ta.SMA(dataframe, timeperiod=15)
        dataframe['sma50'] = ta.SMA(dataframe, timeperiod=50)

        return dataframe



    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        
        dataframe.loc[
            (
                # LONG 
                (dataframe["fastk_15m"] > 80) &
                (dataframe["fastd_15m"] > 80) &
                (qtpylib.crossed_below(dataframe['fastk_15m'], dataframe['fastd_15m'])) &
                (dataframe['close'] > dataframe['sma15_15m']) &  # MA above close
                (dataframe['close'] > dataframe['sma50_15m']) &
                (dataframe['volume'] > 0)  # Make sure Volume is not 0
            ),
            'enter_long'] = 0

        dataframe.loc[
            (
                # SHORT
                # GUARDS  
                (dataframe["fastk_15m"] < 20) &
                (dataframe["fastd_15m"] < 20) &
                (dataframe['macdf'] < 0 ) &
                #(dataframe['close'] < dataframe['sma15_15m']) &  
                #(dataframe['close'] < dataframe['sma50_15m']) &

                # KICK
                (qtpylib.crossed_below(dataframe['fastk_15m'], dataframe['fastd_15m'])) &

                (dataframe['volume'] > 0)  # Make sure Volume is not 0
            ),
            'enter_short'] = 1

        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        Based on TA indicators, populates the exit signal for the given dataframe
        :param dataframe: DataFrame
        :param metadata: Additional information, like the currently traded pair
        :return: DataFrame with exit columns populated
        """
        dataframe.loc[
            (
                (qtpylib.crossed_above(dataframe['fastk_15m'], dataframe['fastd_15m']))
            ),
            'exit_short'] = 1

        return dataframe