面向对象编程
面向对象
自己以前写的代码,写的
股票交易的自动化交易被禁止了,可以使用优矿来半自动化交易。但是可以利用微信通知后手动交易。
全自动化交易平台(国外):
oanda平台
IB(Interactive Brokers)平台,盈透证券
面向过程编程和面向对象编程的对比
面向过程
系统中灭一个模块代表了某个总体的一个主要步骤
通过分解将一个大问题分解为较下的步骤。
适用于比较简单的策略
面向对象
对系统进行分解成多个部分,美国部分都有自己独特的行为,各部分共同协作完成目标
开始分解系统时,必须要么从面向过程角度开始,要么从面向对象的角度开始。
通过对象之间的交互来实现最终结果。对于比较复杂的策略,可以通过复用性结果来简化。具体就是将不同的功能封装成不同的类,这样对于策略的使用者而言只需要关注各个类之间的交互,而不过度关注类内部的代码。大大简化了使用者的障碍
面向对象的简介
类:
用来描述具有相同属性和方法的对象的集合。定义了该集合中每个对象所共有的属性和方法。
类具有属性,可在所有由它产生的实例中公用
类具有方法,是类中定义的函数,可在所有由它产生的实例中公用
类通过实例化产生实例(对象)
实例对象:
由类创建的一个具体的对象。
类:
类的创建与属性设置:类的属性通过在类的创建的代码中进行创建并赋值
延申
class FirstClass:#创建了一个名为FirstClass的类 #定义两个类的属性 name='FirstClass name: AQF' language='FirstClass name: python' #定义了类的方法 def welcome(): print('Hello from first class')
class FirstClass:#创建了一个名为FirstClass的类 #定义两个类的属性 name='FirstClass name: AQF' language='FirstClass name: python' #定义了类的方法 def welcome(): print('Hello from first class') #类的属性和方法的调用 print(FirstClass.name) print(FirstClass.language) print(FirstClass.welcome()) # 实例 first_instance=FirstClass()#示例化过程,通过类创建一个实例,first_instance继承了FirstClass()这个类的所有属性,但是没有继承方法 print(first_instance.name) print(first_instance.language) #注意这里会报错
这是因为对于在类中定义的没有添加参数的函数,所以实例无法继承这个方法;
结果
FirstClass name: AQF FirstClass name: python Hello from first class None FirstClass name: AQF FirstClass name: python
通过__dict__方法可以查看first_instance的属性
class FirstClass:#创建了一个名为FirstClass的类 #定义两个类的属性 name='FirstClass name: AQF' language='FirstClass name: python' #定义了类的方法 def welcome(): print('Hello from first class') # 实例 first_instance=FirstClass()#示例化过程,通过类创建一个实例,first_instance继承了FirstClass()这个类的所有属性,但是没有继承方法 print(first_instance.__dict__)#结果为空{},那是因为以前看到first_instance的属性都是继承FirstClass(),而不属于自己
新的实例属性
class FirstClass:#创建了一个名为FirstClass的类 #定义两个类的属性 name='FirstClass name: AQF' language='FirstClass name: python' #定义了类的方法 def welcome(): print('Hello from first class') # 实例 first_instance=FirstClass()#示例化过程,通过类创建一个实例,first_instance继承了FirstClass()这个类的所有属性,但是没有继承方法 #创建新的实例属性 first_instance.content='first_instance content:quantitative finance'#添加实例属性content first_instance.student='first_instance student:quant'#添加实例属性student print(first_instance.__dict__)#这次有结果了
结果是
{'content': 'first_instance content:quantitative finance', 'student': 'first_instance student:quant'}
可以看出为first_instance添加了content和student两个属性
实例对类属性的继承
class FirstClass:#创建了一个名为FirstClass的类 #定义两个类的属性 name='FirstClass name: AQF' language='FirstClass name: python' #定义了类的方法 def welcome(): print('Hello from first class') # 实例 first_instance=FirstClass()#示例化过程,通过类创建一个实例,first_instance继承了FirstClass()这个类的所有属性,但是没有继承方法 #创建新的实例属性 first_instance.content='first_instance content:quantitative finance'#添加实例属性content first_instance.student='first_instance student:quant'#添加实例属性student second_instance=FirstClass() print(second_instance.name) print(second_instance.language) print(second_instance.__dict__)#结果为空 #second_instance和first_instance都是由同一类FirstClass创建,因此都会继承FirstClass中的属性,但first_instance创建后,进行赋值创建的实例属性,second_instance不会继承
first_instance在创立后新添加的content, student也不会影响到创建它的FirstClass类。以上可以总结为,当一个实例创建后,新添加的实例属性不会影响创建他的类以及由同一类创建的其他实例。(前提是属性不能是可变的;)
而当某个实例所继承的类属性发生变化时,相应所有实例的属性也会受到影响
class FirstClass:#创建了一个名为FirstClass的类 #定义两个类的属性 name='FirstClass name: AQF' language='FirstClass name: python' #定义了类的方法 def welcome(): print('Hello from first class') # 实例 first_instance=FirstClass()#示例化过程,通过类创建一个实例,first_instance继承了FirstClass()这个类的所有属性,但是没有继承方法 #创建新的实例属性 second_instance=FirstClass() FirstClass.name = 'AQF_changed' print(first_instance.name) print(second_instance.name)
结果是
AQF_changed
AQF_changed
可以看出name属性被改变
编写可由实例继承的方法
面向对象的实例
class People(): # 定义属性 name='' age=0 #定义初始化函数 def __init__(self,n,a): self.name=n self.age=a def speak(self): print('%s:I am age is %d'%(self.name,self.age)) #类的实例化过程 p=People('Tom',10) # 调用类中的具体方法 p.speak()
示例化结果2
class Dog(): def __init__(self,name,age): self.name=name self.age=age def sit(self): print(self.name.title()+'is siting now') def roll(self): print(self.name.title()+'is rolling now') my_dog=Dog('Robin',5) # print(my_dog.name) print(my_dog.sit())
结果
Robinis siting now
None
示例化结果3
class Car(): def __init__(self,brand,model,year): self.brand=brand self.model=model self.year=year def get_info(self): car_info=str(self.year)+' '+self.brand+' '+self.model return car_info my_car=Car('BMW','X5',2017) print(my_car.brand) print(my_car.get_info())
结果
BMW
2017 BMW X5
示例化结果4
注意类有类的属性和方法,但是我们也可以在实例中定义实例的属性和方法。
比如实例中的miles就是实例中单独定义的
class Car(): def __init__(self,brand,model,year): self.brand=brand self.model=model self.year=year self.miles=0 def get_info(self): car_info=str(self.year)+' '+self.brand+' '+self.model return car_info def get_miles(self): print('This car has runed'+' '+str(self.miles)+' '+'miles') my_car=Car('BMW','X5',2017) print(my_car.brand) print(my_car.get_info()) print(my_car.get_miles()) my_car.miles=60 print(my_car.get_miles())
结果
BMW 2017 BMW X5 This car has runed 0 miles None This car has runed 60 miles None
示例化结果5
class Car(): def __init__(self,brand,model,year): self.brand=brand self.model=model self.year=year self.miles=0 def get_info(self): car_info=str(self.year)+' '+self.brand+' '+self.model return car_info def get_miles(self): print('This car has runed'+' '+str(self.miles)+' '+'miles') def set_miles(self,miles): self.miles=miles my_car=Car('BMW','X5',2017) print(my_car.brand) print(my_car.get_info()) print(my_car.get_miles()) my_car.miles=60 print(my_car.get_miles()) my_car.set_miles(70) print(my_car.get_miles())
123
646