Tuesday, August 22, 2023

Kotlin - Convert list of String to Map with count of each element as value

 

Kotlin Code to transform List into Map with list elements as Key and value as count of each element. Map contains only unique keys.

 



fun main() {
    countListElements(listOf("Apple", "orange", "mango", "orange"))
}

fun countListElements(fruits: List String>) {
    val fruitMap = fruits.groupingBy { it }.eachCount()
    print(fruitMap)
}

 

Friday, April 21, 2023

Algotrading: Exit Positions in Fyers API

Example code snippet for exiting positions in Fyers API


Code:


if __name__ == '__main__':
    token = get_auth_code()
    fyers = fyersModel.FyersModel(client_id=client_id, token=token, log_path='/tmp/')
    print(fyers.positions())

    data = [{"id": "NSE:BANKNIFTY23APR42900CE-MARGIN"}, {"id": "NSE:BANKNIFTY23APR41300PE-MARGIN"}]
    pos_res = fyers.exit_positions(data)

    print(pos_res)

    pos_res = fyers.exit_positions()

    print(pos_res)
  

Please copy the code for get_auth_code() from previous blogpost for Fyers V2 login

 

Happy trading. Enjoy.