Telegram bot for options analysis

Options are a type of securities that arouse constant interest even among novice traders. Many articles have been written about options, so we will omit the introductory part and assume that the reader is familiar with the basic terms of option trading. If anything, then their interpretation is easy to find on the Internet.





After the first acquaintance with the theory, novice traders in options have a question: under what conditions will this set of options give a profit, and what kind. Even an experienced option trader cannot immediately answer this question if you have more than one option. This requires an analysis of the option portfolio (a set of options and, possibly, futures), which is most clearly done in graphical form, on the diagram of the dependence of profit / loss on the value of the underlying asset. Currently, there are several sites for this, as well as individual programs.





The author of the article has implemented the analysis of options using a telegram bot , which makes it possible to quickly obtain information about the investment portfolio on a smartphone. 





We will assume that, in addition to options, the reader is familiar with telegram bots. Therefore, there will be no description of creating a bot from receiving a token to processing the keyboard. Instead, we list the conceptual problems that arose during the creation of the program, and how to solve them.





Remembering the state of the bot between webhooks

The bot receives messages from the user through the webhook mechanism built into the telegram. Each webhook restarts the program, but each user needs a different set of data. Users are identified by viewing the input stream:





$id_init = file_get_contents('php://input');

$id=sbs($id_init, '"from":{"id":',',"is_bot":');  //      

function sbs ($str,$m1,$m2){     //  str     - m1  m2

$p1=strpos($str,$m1)+strlen($m1);  // - 

$p2=strpos($str,$m2);

return substr($str,$p1,$p2-$p1);

}
      
      



The following data structure is built for each user:





  1. : – , ,  ( message_Id) , ( , ..), 





  2. (  , , , , )  









.





  , y=kx+b, 





  y –   /





        –





  png GD :





1) -  (, )





2) (   , ,   Y)





3) , X    , Y – , :





$typ – ,





$q – ( )





$cena –





$strike –





$x0 –





$sx –





function pparr($typ, $q, $cena, $strike,$x0,$sx)//    -  x=>y   

// ,  ( ),     ( ) 

    if ($q<0)  {    $q=-$q;

                    $drct='-';

            }                   

            else $drct='+';

     $a=array();

     $b=array();

     $delta=$sx;  //       $scalx

     for ($i=0;$i<740;$i++){   //   740  

        $xkk=$x0+$delta*$i; //   X

        if ($typ=='fut') {

            if ($drct=='+') $a[$xkk]=($xkk-$cena)*$q;

            else $a[$xkk]=(-$xkk+$cena)*$q;

        }

        if ($typ=='call'){

            if ($drct=='+') {  if ($xkk<=$strike) $a[$xkk]=-$cena*$q; else $a[$xkk]=$q*($xkk-$strike-$cena);}

            elseif ($xkk<=$strike) $a[$xkk]=$q*$cena; else $a[$xkk]=(-$xkk+$strike+$cena)*$q;}

            

        }

        if ($typ=='put'){

            if ($drct=='+') {  if ($xkk<=$strike) $a[$xkk]=(-$xkk+$strike-$cena)*$q; else $a[$xkk]=-$q*$cena;}

            elseif ($xkk<=$strike) $a[$xkk]=($xkk-$strike+$cena)*$q; else $a[$xkk]=$cena*$q;}

            

        }

     $b[(string)$xkk]=(string)$a[$xkk];

     }

return $b;         

};
      
      



4)      , ( )  ( ). , know-how ,   / Y. - X+,X-,Y+,Y- . , .





.  . – , .  , , ().  , , , , ( , ).  , . ( - ) ,   .  , ( , , )   () . .  , -,   , . 





- , .





, :





=+(-_)*;





_ , 





–   , , , ,





– , , , ,





  , , . , , , .





- :  , , ,  . , , . .





:





An entry in the portfolio of buying a call option strike 75000 expiration date 06/03/2021 at a price of 25
"" 75000 03.06.2021 25
Profit / loss graph for the purchased call option depending on the value of the underlying asset at the expiration date
/ ""

=23.





:   ( ) [] 03.06.2021  75000 ,  23 . 75023 , .





:   23. , , 23.





– .





.





An entry in the portfolio of selling a put option strike 72750 at a price of 44 with an expiration date of 06/03/2021
"" 72750 44 03.06.2021
A graph of the dependence of profit / loss on a sold put option on the value of the underlying asset as of the expiration date
/ ""

= 5436.





, 72750 44. 72706 . .





– , 72706. !   (5436), .





?  5436 44? , . , 3 .





.





?





An entry in the portfolio of selling the put option strike 72750 and selling the call option strike 75000
"" 72750 "" 75000
A graph of the dependence of profit / loss on the portfolio on the value of the underlying asset at the expiration date
/

!





– – , 72700  ( «»)  75190 ( «»). 





, .





,   72700 75190,  , .





-

:













  • «»,  ,   (   )









  • , , png,





  • , ,  : « », « », « »  





«»  InlineKeyboard.





  :





  • «-» , ,





  • «»   . ,





  •    .





.





,     « »( , ).





- .  , : (Si),  (BR), (RI). .





- , .





- / (P/L  ).





- t.me/@test09062020bot.  « ».








All Articles