forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Connecting FTN95 to the server via TCP/IP socket

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
DanRRight



Joined: 10 Mar 2008
Posts: 2813
Location: South Pole, Antarctica

PostPosted: Thu Sep 24, 2009 11:21 pm    Post subject: Connecting FTN95 to the server via TCP/IP socket Reply with quote

There exist saying:"If you want to be successful investor, just save your money and wait for recessions". That means now is exactly such a time.

What i need from my Fortran colleagues is the following.
Can you guys help me to connect API of Ameritrade or Quote Tracker to FTN95?

Fortran is ideal for such applications as controlling your stocks and trading since it is easy to manipulate textual content because API exchanges data with online broker in plain ASCII text using or not XML.

Then there exist Clearwin+ which makes all things very easy and self-explanatory. You can write the program which gets stock quotes and analyses them and then informs you about any changes in stocks or indexes which is hard to control manually looking into broker trading software screen like Ameritrade Command Center, Scottrade Pro or Quote Tracker.

What i have done before was HTTP connection to internal Quote Tracker software HTTP server via open port with FTN95 READ_URL@ subroutine.

It works like charm: suppose you need to know what symbols are in your portfolio. You make a command in Fortran

call read_URL@(http://127.0.0.1:16240/req?EnumEquities(),FileSave,0,ier )

and get from Server list of your stocks saved in the FileSave file:

OK
MSFT,INTC,AMD,SVFT(Silverfrost Smile ....

Or you need Citi bank stock prices from 13:00:00 to 13:01:00, you do

call read_URL@(http://127.0.0.1:16240/req?GetHistory(C,13:00:00,13:01:00,0.001,NO),FileSave,0,ier)

and get
OK
09/24/2009,13:00:04,4.86,4.86,4.86,4.86,0
09/24/2009,13:00:19,4.89,4.89,4.89,4.89,285637
09/24/2009,13:00:39,4.84,4.84,4.84,4.84,285637
.........
Really simple.

Unfortunately this easy way of communication with trading software is not allowing you to do everything else you can do. You can automate trading, get news, level I and II quotes etc using another method they recommend. Everything will be encrypted like you usually do if you do trading. For that you need to use Streaming API. Lot of people do that but no one using Fortran. The DLL, Help file and examples in Delphi, C++, C#, VB, Borland C++ Builder are available but i do not know these languages "intimately" and can not really pay for transferring that into Fortran.

They do basically what Clearwin can easily and much more effectively do too. The simplest code could be as small as 10 lines instead of their tens of thousands.

So my requests to anyone who interested are the following:

1) for communication with QuoteTracker: if you know any of these languages and know how to make TCP/IP socket connection and make them available to FTN95, can you create simplest example which is doing just one simple function their API allows like "Login" (connection to the socket) and request a quote from QT like in example above ?

2) for direct communication with Ameritrade: if you know how to use DLL or Activex, can you make it available to FTN95? Same simplest example how to do that is only needed

First one is more important since it does not depend of specific online brokerage, you can use it with any.
This as i said will require just a half a page of Clearwin text, making streaming socket to connect to Quote Tracker internal server (some standard procedure) and execute a single function from the first link below. Everything else then will be clear and done automatically. I am sure if we publish similar example on their site for comparison they will not believe their eyes how simple everything is in FTN95 Fortran

I can send you all the info and files. You can download QuoteTtracker, initial info and examples about communication with its streaming API from here

https://www.quotetracker.com/help/QTStreamingAPI.shtml
http://www.quotetracker.com/help/qtserver.shtml

By many reviews, Quote Tracker is one of the the greatest trading software which can be used literally with any online broker in the world with the best customer responds.


Last edited by DanRRight on Sat Oct 10, 2009 2:27 am; edited 1 time in total
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2813
Location: South Pole, Antarctica

PostPosted: Mon Sep 28, 2009 1:03 pm    Post subject: Reply with quote

Here is an example of same look and feel code written in FTN95 for comparison with other language examples in link 1 above.

Compare these 100 lines of Fortran to 1000+ lines in C++. Isn't it amazing?This code is very close analogy of what is needed but unfortunately it is using HTTP communication method (link 2). If somebody will help to get TCP/IP socket connection done this is approximately how final code will look.
Part 1 is here, combine it with Part 2
Code:

!    2009-09-27 Dan R.Right
!    Compilation> ftn95 filename.f95 /free /link
!    In QuoteTracker settings (Options/Edit Preferences/Misc)
!    allow its API Server and HTTP port 16240
!
  module all_defs
   use clrwin
   character (len=32) :: AppName, AppVer, AppAPIVer, Password,  ReqTypeL1(4)
   character (len=32) ::  SymbolsForL1, SymbolHist, ReqTypeHist(2)
   character (len=32) ::  FromDateTimeHist, ToDateTimeHist, fileSave_URL
   character ch_read_url*128, save64*64, tmp128*128
   real*8 FreqHistQuote
   integer IOlun, iTICK, iL1
   CONTAINS
   integer function cbGetHist()
   if(iTICK.eq.0.or.iTICK.eq.2) write (save64,'(e10.3)') FreqHistQuote
   if(iTICK.eq.1)               write (save64,'(e10.3)') 0.001
   ch_read_url='http://127.0.0.1:16240/req?GetHistory(DELL,9/02/2009,0,0.001,NO)'
   ch_read_url(39:)=' '
   ch_read_url=trim(ch_read_url)//trim(SymbolHist)//','//FromDateTimeHist(1:10)//&
               &','//ToDateTimeHist(1:10)//','//save64(2:10)//',NO'
   call READ_URL@(ch_read_url,fileSave_URL,0,ier)
   if(ier.eq.1) goto 1
   open(unit=21,file=fileSave_URL,STATUS='readonly', err=1)
   do i=1,1000000
   read(   21,'(a)',err=1,end=1) tmp128
   write(ioLun,'(a)')          tmp128
   enddo
1  close (21,err=1)
   cbGetHist=1
   end function cbGetHist

   integer function cbGetVer()
   ch_read_url='http://127.0.0.1:16240/req?GetVersion()'
   call READ_URL@(ch_read_url,fileSave_URL,0,ier)
   if(ier.eq.1) goto 1
   open(unit=21,file=fileSave_URL,STATUS='readonly', err=1)
   do i=1,1000000
   read(   21,'(a)',err=1,end=1) tmp128
   write(ioLun,'(a)')          tmp128
   enddo
1  close (21,err=1)
   cbGetVer=1
   end function cbGetVer

   integer function L1Quote()
   ch_read_url='http://127.0.0.1:16240/req?GetLastQuote(stock,stock,stock...)'
   ch_read_url(41:)=' '
   ch_read_url(41:)=trim(ch_read_url)//SymbolsForL1//')'
   call READ_URL@(ch_read_url,fileSave_URL,0,ier)
   if(ier.eq.1) goto 1
   open(unit=21,file=fileSave_URL,STATUS='readonly', err=1)
   do i=1,1000000
   read(21,'(a)',err=1,end=1) tmp128
   write(ioLun,'(a)')         tmp128
   enddo
1  close (21,err=1)   
   L1Quote =1
   end function L1Quote
   integer function cbVoid()
   cbVoid=1
   end function cbVoid
  end module all_defs


Last edited by DanRRight on Mon Oct 05, 2009 4:44 am; edited 5 times in total
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2813
Location: South Pole, Antarctica

PostPosted: Mon Sep 28, 2009 1:05 pm    Post subject: Reply with quote

Part 2
Code:

!---------------------------------------------------------------------------------
  program DEMO
   use clrwin
   use all_defs
 
   AppName='DEMO'
   ReqTypeL1(1)='SNAP';ReqTypeL1(2)='SUB'; ReqTypeL1(3)='UBSUB'; ReqTypeL1(4)='UBSUBALL'
   ReqTypeHist(1)='TICK';ReqTypeHist(2)='OHLC'
   SymbolHist='DELL'
   SymbolsForL1='MA,V,LVS,KBH,^IXIC'
   FreqHistQuote=1.
   AppVer='1.0.0'
   AppAPIVer='1.000'
   Password=' '
   iTICK=0; iL1=0
   ioLun = 11
   fileSave_URL= 'histQ'
   FromDateTimeHist= '2009/09/22'
   ToDateTimeHist  = '2009/09/25'

   i=winio@('%ww[no_border]%sy[3d_thin]%nr%nd&')
   i=winio@('%ac[Esc]&','exit')
   i=winio@('%sf%ts&',0.8d0)   
   i=winio@('%3.1ob&')
   i=winio@('  AppName%ff%14rs%ff&',AppName)
   i=winio@('  AppVer%ff%14rs%ff&',AppVer)
   i=winio@('  AppAPIVer%ff%14rs%ff&',AppAPIver)
   i=winio@('  Password%ff%14rs%ff %ff %ff&',password)
   i=winio@('   %^tt[Login]%ff&',cbGetVer)
   i=winio@('   %^tt[Clear]&',cbVoid)
   i=winio@('%cb&')
   i=winio@('  ReqType%ff%`15ls%ff&',ReqTypeL1,4,iL1)
   i=winio@('  Symbol%ff%16.5re%ff %ff %ff %ff %ff %ff %ff&',SymbolsForL1)
   i=winio@('    %^tt[LastQuote]%ff&',L1Quote)
   i=winio@('%cb&')
   i=winio@('  ReqType%ff%`15ls%ff&',ReqTypeHist,2,iTICK)
   i=winio@('  Freq%ff%16rf%ff&',FreqHistQuote)
   i=winio@('  Symbol%ff%15rs%ff&',SymbolHist)
   i=winio@('  From%ff%15rs%ff&',FromDateTimeHist)
   i=winio@('  To%ff%15rs%ff&',ToDateTimeHist)
   i=winio@('  %^tt[GetHistory]%ff&',cbGetHist)
   i=winio@('%cb%ff%nl&')
   i=winio@('%50.16cw[hscroll,vscroll]',ioLun)
  end
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2813
Location: South Pole, Antarctica

PostPosted: Fri Oct 09, 2009 11:51 pm    Post subject: Reply with quote

OK, i have additional info which may help those who know how to connect C++ code to Fortran, or better to Clearwin. I can't but for you that would be easy

If from the languages supplied in QuoteTracker website it is not clear which part of the text is responsible for making TCP/IP protocol connection here is another way: NETCAT for Windows.You can download it from the net, it has EXE and the source which is very small. Wikipedia has a link on Netcat 1.1.1 NT http://en.wikipedia.org/wiki/Netcat

I succeeded to do all needed functions via NETCAT but all is done manually. I type each command in DOS Prompt and get respond on screen from the Quotetracker. This should be done via Clearwin and Fortran text itself which will be doing that for you like in the code example above.

So to completely understand what is needed i describe the steps i am doing manually now.
1) i start netcat in dos prompt
nc -v 127.0.0.1 16240

and get respond from the Quotetracker server:
OK <CONNECT><SERVERVERSION>1.10</SERVERVERSION><SERVERTIME>10/08/2009 17:01:32<SERVERTIME><EASTERNTIME>10/08/2009 20:01:38</EASTERNTIME><CONNECT>

Now netcat is connected to the server.

2) You HAVE TO login first typing

<LOGIN>
<AppName>Widget Trader Pro</AppName>
<AppVer>1.0.0</AppVer>
</LOGIN>{FF}

and get OK in respond.

3) Then the same way you can get quotes from the server

<L1QUOTES>
<ReqType>SUB</ReqType>
<Symbols>DELL,IBM,MSFT</Symbols>
<Symbols>AA,INTC</Symbols>
</L1QUOTES>{FF}

{FF} is char=255, the end of the line before you hit return. You add it that via keyboard keeping ALT DOWN and TYPING 255 in numeric keypad

Done! Everything else is done same way

So, if anyone can take out the part of C++ code which connects code to TCP/IP then everything else is clear: the Clearwin will fill all requests just clicking one button like in example code above and get responds from server on its %cw screen (or/and in the file like in READ_URL@ or internally in some memory buffer. i do not know how TCP/IP protocol works, does it specify first before upload the size of needed buffer? And i do not know how to make this buffer available to Fortran)

C'mon C++ programmers, please help with that damn TCP/IP part ! It is done in all languages besides Fortran! Start expanding Fortran into the Web. Our future is inevitably on the net anyway. The next Clearwin called "ClearWeb" will be using network features, so that your code will be residing on some computer and you'll access it from your cell phone Smile
Back to top
View user's profile Send private message
jjgermis



Joined: 21 Jun 2006
Posts: 404
Location: Nürnberg, Germany

PostPosted: Tue Nov 03, 2009 7:35 am    Post subject: Reply with quote

Hi DanRRight,

this seems like quite an interessting application. Unfortunately TCP/IP is beyond my knowledge Sad In general I am often in the same situation: you need to do some simple job to make your program perfect. Then you arrive at a point where you can not get support for Fortran. Not long ago I tried the COM-interface. I can only start the it and then when it gets to the real stuff, I am stuck.

I always believe in examples. If only we can have a document with solely cool examples, it would make life much easier Very Happy If I take a look at the FTN install directory there are some interessting examples - but without any docucmentation. I do not expect something that explains every line of code: a short description will do. If those examples are rather put on the web for download rather than in the install directory, users might have more of it.

Christmas is due shortly Laughing
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Tue Nov 03, 2009 11:45 am    Post subject: Reply with quote

I do not have any relevant examples.
I am wondering if it may be possible to use the routines OPENCOMMDEVICE@ etc. together with a serial to IP converter.

Otherwise there could be a third party DLL that could be used for this purpose.

I have no experience in this area so I cannot give an further details.
Back to top
View user's profile Send private message AIM Address
DanRRight



Joined: 10 Mar 2008
Posts: 2813
Location: South Pole, Antarctica

PostPosted: Mon Apr 05, 2021 11:03 pm    Post subject: Reply with quote

I found this post 12 years ago and was thinking that if i knew how to implement Websocket with this Fortran i'd (and you too) been already millionaire.

Because imagine this: you bought some stocks, they of course go up and down in price. When they go up you have gain, and when they go down YOU DO NOT LOSE (or lose very little). Because the autotrading mechanism will sell them ultrafast using stop loss order. And submit again when necessary. Orders nowadays are free, zero commissions

Intel Fortran has implemented Wininet which allows that. Can we these days make communication of Fortran program with the server via duplex Websocket port (to do login, send and receive commands) ?


Last edited by DanRRight on Tue Apr 06, 2021 12:43 pm; edited 1 time in total
Back to top
View user's profile Send private message
StamK



Joined: 12 Oct 2016
Posts: 159

PostPosted: Tue Apr 06, 2021 12:15 pm    Post subject: Reply with quote

I am sure there are many programs written in python or php for these (just google for github trading algorithms). The problem with stop loss order is that you have fees to pay all the time, so if the drops are more sudden than the increases such a simple algorithm will make you lose money (while if you had kept it without selling it would eventually have increased). Or at least that is what happens on highly volatile stocks (eg. cryptocurrencies!).
Back to top
View user's profile Send private message
John-Silver



Joined: 30 Jul 2013
Posts: 1520
Location: Aerospace Valley

PostPosted: Tue Apr 06, 2021 12:29 pm    Post subject: Reply with quote

Quote:
Or at least that is what happens on highly volatile stocks (eg. cryptocurrencies!).


... until THE day cometh when they don't 'recover' (which is a dead certainty for anything as 'obscure' as cryptocurrencies !).... or you've just simply lost your acess pw !

(ref. relatively recent news articles about the bloke who couldn't (and still can't) remember his - one assumes because he can't find the scrap of paper on which he wrote it !!!!)

imo it's just another 'scam' of pyramid selling proportions or greater even
with SO many people already caught in the loop of anguish, and losses.

act of Life no. 1 : Everybody can't win.

(descends from soapbox)
_________________
''Computers (HAL and MARVIN excepted) are incredibly rigid. They question nothing. Especially input data.Human beings are incredibly trusting of computers and don't check input data. Together cocking up even the simplest calculation ... Smile "
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2813
Location: South Pole, Antarctica

PostPosted: Tue Apr 06, 2021 12:53 pm    Post subject: Reply with quote

StamK,
I know that. But I am ready to use any Fortran as a maximum, but no multilanguage programming. Otherwise in 2-3-5 years you find yourself with dead program. All these programs which have done things in this example above are long ago dead. My way to use TCP/IP port is still working. Now i need WebSocket duplex way of communications to be implemented in Fortran. Will try to ask folks in the Intel Fortran forum if here will be no progress. Start thinking about this early, people, all will do that 10 years from now and you will regret, because actually these things are simple and even primitive

Stocks commissions are free for most brokerages in USA since Oct 2019. And other frees are 4x smaller since this end of February.

Stop loss orders have their problems in highly volatile market of course. Analyzing my losses i find that in most cases the way of fast autotrading stop order way would work. For sure i will know when will try the program to run on test data.

But waiting for recovery is not an option i consider right.


Last edited by DanRRight on Tue Apr 06, 2021 12:59 pm; edited 1 time in total
Back to top
View user's profile Send private message
John-Silver



Joined: 30 Jul 2013
Posts: 1520
Location: Aerospace Valley

PostPosted: Tue Apr 06, 2021 12:59 pm    Post subject: Reply with quote

prediction: the 'other fees ' you refer to will increase x5 once the 'crisis is over !

... you heard it hear first !Smile

Good Luck, but be careful of the stock 'monsters' out there ready to eat you alive.
_________________
''Computers (HAL and MARVIN excepted) are incredibly rigid. They question nothing. Especially input data.Human beings are incredibly trusting of computers and don't check input data. Together cocking up even the simplest calculation ... Smile "
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2813
Location: South Pole, Antarctica

PostPosted: Tue Apr 06, 2021 1:01 pm    Post subject: Reply with quote

No way John. These fees were rudiment of human trading as opposed to more and more prevalent frequent computerized autotrading. They were small anyway. And if you do not do 1000 trades per day they were just nothing, just couple dollars per 100k sold.

/* By the way QuoteTracker mensioned in this my old post now called Medved Trader. You may give it a try, as it is working with 100s stock brokers of many different countries. It has an API which allows autotrade via WebSocket port

If you missed 2001 recession, 2008-2009 recession and 2020 recession when stocks dropped 10 times and gained 10 times in no time then who are you? Aren't you reckless about money ? Unfortunately i am exactly such dumbo person Sad.

And why i did not participate in this action ? Because i had no automatic mechanism to protect myself from market slides and due to that this game is called gambling and i do not gamble. Now we can make such protection possible.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group