質問編集履歴
4
1番目のコードでのポジションを少し変更しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -66,15 +66,65 @@
|
|
66
66
|
|
67
67
|
states = [trend + spread]
|
68
68
|
|
69
|
+
position = 0
|
70
|
+
|
71
|
+
# 2(pの計算だけ違うがそれを除けば同じ) : 損切の確認、statesに値を追加
|
72
|
+
|
73
|
+
elif position == 0:
|
74
|
+
|
75
|
+
sub = 0
|
76
|
+
|
77
|
+
p = [(trend - s) * pip_cost for s in states]
|
78
|
+
|
79
|
+
for b in range(0,len(p)):
|
80
|
+
|
81
|
+
r = [-40.0, True] if p[b] <= -40 else [p[b], False]
|
82
|
+
|
83
|
+
if r[1]:
|
84
|
+
|
85
|
+
pip.append(r[0])
|
86
|
+
|
87
|
+
states.pop(b-sub)
|
88
|
+
|
89
|
+
sub += 1
|
90
|
+
|
91
|
+
states.append(trend[t] + spread)
|
92
|
+
|
93
|
+
position = 0
|
94
|
+
|
95
|
+
# 3(pの計算が違うだけ) : 利益確定をし新しい状態を持つ
|
96
|
+
|
97
|
+
elif position == 1:
|
98
|
+
|
99
|
+
p = [(s - trend) * pip_cost for s in states]
|
100
|
+
|
101
|
+
for b in p:
|
102
|
+
|
103
|
+
b = -40.0 if b <= -40 else b
|
104
|
+
|
105
|
+
pip.append(b)
|
106
|
+
|
107
|
+
states = [trend + spread]
|
108
|
+
|
109
|
+
position = 0
|
110
|
+
|
111
|
+
elif action == 1:
|
112
|
+
|
113
|
+
# 1
|
114
|
+
|
115
|
+
if position == 3:
|
116
|
+
|
117
|
+
states = [trend - spread]
|
118
|
+
|
69
119
|
position = 1
|
70
120
|
|
71
|
-
# 2
|
121
|
+
# 2
|
72
122
|
|
73
123
|
elif position == 1:
|
74
124
|
|
75
125
|
sub = 0
|
76
126
|
|
77
|
-
p = [(trend
|
127
|
+
p = [(s - trend) * pip_cost for s in states]
|
78
128
|
|
79
129
|
for b in range(0,len(p)):
|
80
130
|
|
@@ -88,75 +138,119 @@
|
|
88
138
|
|
89
139
|
sub += 1
|
90
140
|
|
91
|
-
|
141
|
+
states.append(trend - spread)
|
142
|
+
|
143
|
+
position = 1
|
144
|
+
|
145
|
+
# 3
|
146
|
+
|
147
|
+
elif position == 0:
|
148
|
+
|
149
|
+
p = [(trend - s) * pip_cost for s in states]
|
150
|
+
|
151
|
+
for b in p:
|
152
|
+
|
153
|
+
b = -40.0 if b <= -40 else b
|
154
|
+
|
155
|
+
pip.append(b)
|
156
|
+
|
157
|
+
states = [trend[t] - spread]
|
92
158
|
|
93
159
|
position = 1
|
94
160
|
|
161
|
+
return states,pip,position
|
162
|
+
|
163
|
+
```
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
```python
|
168
|
+
|
169
|
+
'''
|
170
|
+
|
171
|
+
一番目のコードと同様に番号を振る。
|
172
|
+
|
95
|
-
|
173
|
+
速度アップを図るためにcythonを使用しています。
|
174
|
+
|
96
|
-
|
175
|
+
'''
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
def reward2(double trend,list pip,int action,int position,list states,double pip_cost,double spread):
|
180
|
+
|
97
|
-
|
181
|
+
cdef list r
|
98
|
-
|
99
|
-
|
182
|
+
|
100
|
-
|
101
|
-
|
183
|
+
cdef list p
|
102
|
-
|
103
|
-
|
184
|
+
|
104
|
-
|
105
|
-
pip.append(b)
|
106
|
-
|
107
|
-
states = [trend + spread]
|
108
|
-
|
109
|
-
position = 1
|
110
|
-
|
111
|
-
e
|
185
|
+
cdef int sub = 0
|
112
|
-
|
186
|
+
|
113
|
-
|
187
|
+
# 2と3 : アクションの値によって式が異なるが同じ処理(1番目のコードと)
|
114
|
-
|
188
|
+
|
115
|
-
|
189
|
+
if position != 3:
|
116
|
-
|
117
|
-
|
190
|
+
|
118
|
-
|
119
|
-
position = 2
|
120
|
-
|
121
|
-
# 2
|
122
|
-
|
123
|
-
|
191
|
+
if action == 0:
|
124
|
-
|
192
|
+
|
125
|
-
|
193
|
+
#pipの計算
|
126
|
-
|
127
|
-
p = [(s - trend) * pip_cost for s in states]
|
128
|
-
|
129
|
-
for b in range(0,len(p)):
|
130
|
-
|
131
|
-
r = [-40.0, True] if p[b] <= -40 else [p[b], False]
|
132
|
-
|
133
|
-
if r[1]:
|
134
|
-
|
135
|
-
pip.append(r[0])
|
136
|
-
|
137
|
-
states.pop(b-sub)
|
138
|
-
|
139
|
-
sub += 1
|
140
|
-
|
141
|
-
states.append(trend - spread)
|
142
|
-
|
143
|
-
position = 2
|
144
|
-
|
145
|
-
# 3
|
146
|
-
|
147
|
-
elif position == 1:
|
148
194
|
|
149
195
|
p = [(trend - s) * pip_cost for s in states]
|
150
196
|
|
197
|
+
else:
|
198
|
+
|
199
|
+
p = [(s - trend) * pip_cost for s in states]
|
200
|
+
|
201
|
+
spread *= -1
|
202
|
+
|
203
|
+
# positionの値をアクションと同様にしています。
|
204
|
+
|
205
|
+
# 2 : 前回のアクションと同様なら実行する。
|
206
|
+
|
207
|
+
if action == position:
|
208
|
+
|
209
|
+
for b in range(0, len(p)):
|
210
|
+
|
211
|
+
# tl = 40を超えていれば損切とみなし、-40に固定する。
|
212
|
+
|
213
|
+
r = True if p[b] <= -40 else False
|
214
|
+
|
215
|
+
# 発生している場合は該当の状態を削除する(2重評価を避けるため)
|
216
|
+
|
217
|
+
if r:
|
218
|
+
|
219
|
+
pip.append(-40)
|
220
|
+
|
221
|
+
states.pop(b - sub)
|
222
|
+
|
223
|
+
sub += 1
|
224
|
+
|
225
|
+
states.append(trend + spread)
|
226
|
+
|
227
|
+
position = action
|
228
|
+
|
229
|
+
# 3 : 前回のアクションと異なるなら実行する
|
230
|
+
|
231
|
+
else:
|
232
|
+
|
151
233
|
for b in p:
|
152
234
|
|
153
235
|
b = -40.0 if b <= -40 else b
|
154
236
|
|
155
237
|
pip.append(b)
|
156
238
|
|
157
|
-
states = [trend
|
239
|
+
states = [trend + spread]
|
158
|
-
|
240
|
+
|
159
|
-
position =
|
241
|
+
position = action
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
# 1 : time stepが0の時に実行する。
|
246
|
+
|
247
|
+
if position == 3:
|
248
|
+
|
249
|
+
states = [trend + spread] if action == 0 else [trend - spread]
|
250
|
+
|
251
|
+
position = action
|
252
|
+
|
253
|
+
|
160
254
|
|
161
255
|
return states,pip,position
|
162
256
|
|
@@ -164,274 +258,180 @@
|
|
164
258
|
|
165
259
|
|
166
260
|
|
167
|
-
```python
|
168
|
-
|
169
|
-
'''
|
170
|
-
|
171
|
-
一番目のコードと同様に番号を振る。
|
172
|
-
|
173
|
-
速度アップを図るためにcythonを使用しています。
|
174
|
-
|
175
|
-
'''
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
def reward2(double trend,list pip,int action,int position,list states,double pip_cost,double spread):
|
180
|
-
|
181
|
-
cdef list r
|
182
|
-
|
183
|
-
cdef list p
|
184
|
-
|
185
|
-
cdef int sub = 0
|
186
|
-
|
187
|
-
# 2と3 : アクションの値によって式が異なるが同じ処理(1番目のコードと)
|
188
|
-
|
189
|
-
if position != 3:
|
190
|
-
|
191
|
-
if action == 0:
|
192
|
-
|
193
|
-
#pipの計算
|
194
|
-
|
195
|
-
p = [(trend - s) * pip_cost for s in states]
|
196
|
-
|
197
|
-
else:
|
198
|
-
|
199
|
-
p = [(s - trend) * pip_cost for s in states]
|
200
|
-
|
201
|
-
spread *= -1
|
202
|
-
|
203
|
-
# positionの値をアクションと同様にしています。
|
204
|
-
|
205
|
-
# 2 : 前回のアクションと同様なら実行する。
|
206
|
-
|
207
|
-
if action == position:
|
208
|
-
|
209
|
-
for b in range(0, len(p)):
|
210
|
-
|
211
|
-
# tl = 40を超えていれば損切とみなし、-40に固定する。
|
212
|
-
|
213
|
-
r = True if p[b] <= -40 else False
|
214
|
-
|
215
|
-
# 発生している場合は該当の状態を削除する(2重評価を避けるため)
|
216
|
-
|
217
|
-
if r:
|
218
|
-
|
219
|
-
pip.append(-40)
|
220
|
-
|
221
|
-
states.pop(b - sub)
|
222
|
-
|
223
|
-
sub += 1
|
224
|
-
|
225
|
-
states.append(trend + spread)
|
226
|
-
|
227
|
-
position = action
|
228
|
-
|
229
|
-
# 3 : 前回のアクションと異なるなら実行する
|
230
|
-
|
231
|
-
else:
|
232
|
-
|
233
|
-
for b in p:
|
234
|
-
|
235
|
-
b = -40.0 if b <= -40 else b
|
236
|
-
|
237
|
-
pip.append(b)
|
238
|
-
|
239
|
-
states = [trend + spread]
|
240
|
-
|
241
|
-
position = action
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
# 1 : time stepが0の時に実行する。
|
246
|
-
|
247
|
-
if position == 3:
|
248
|
-
|
249
|
-
states = [trend + spread] if action == 0 else [trend - spread]
|
250
|
-
|
251
|
-
position = action
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
return states,pip,position
|
256
|
-
|
257
261
|
```
|
258
262
|
|
259
|
-
|
263
|
+
'''
|
264
|
+
|
265
|
+
コードの比較
|
266
|
+
|
267
|
+
1番目のコードの1
|
268
|
+
|
269
|
+
if position == 3: # action == 0
|
270
|
+
|
271
|
+
states = [trend + spread]
|
272
|
+
|
273
|
+
position = 1
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
if position == 3: # action == 1
|
278
|
+
|
279
|
+
states = [trend - spread]
|
280
|
+
|
281
|
+
position = 2
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
2番目のコードの1
|
286
|
+
|
287
|
+
elif position == 3:
|
288
|
+
|
289
|
+
states = [trend + spread] if action == 0 else [trend - spread] # action == 0 states = [trend + spread], action == 1 states = [trend - spread]
|
290
|
+
|
291
|
+
position = action
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
1番目のコードの2
|
296
|
+
|
297
|
+
elif position == 1: # action == 0
|
298
|
+
|
299
|
+
sub = 0
|
300
|
+
|
301
|
+
p = [(trend - s) * pip_cost for s in states]
|
302
|
+
|
303
|
+
for b in range(0,len(p)):
|
304
|
+
|
305
|
+
r = True if p[b] <= -40 else False
|
306
|
+
|
307
|
+
if r:
|
308
|
+
|
309
|
+
pip.append(-40)
|
310
|
+
|
311
|
+
states.pop(b-sub)
|
312
|
+
|
313
|
+
sub += 1
|
314
|
+
|
315
|
+
states.append(trend + spread)
|
316
|
+
|
317
|
+
position = 1
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
elif position == 2: # action == 1
|
322
|
+
|
323
|
+
sub = 0
|
324
|
+
|
325
|
+
p = [(s - trend) * pip_cost for s in states]
|
326
|
+
|
327
|
+
for b in range(0,len(p)):
|
328
|
+
|
329
|
+
r = True if p[b] <= -40 else False
|
330
|
+
|
331
|
+
if r:
|
332
|
+
|
333
|
+
pip.append(-40)
|
334
|
+
|
335
|
+
states.pop(b-sub)
|
336
|
+
|
337
|
+
sub += 1
|
338
|
+
|
339
|
+
states.append(trend - spread)
|
340
|
+
|
341
|
+
position = 2
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
2番目にコードの2と3
|
346
|
+
|
347
|
+
if position != 3:
|
348
|
+
|
349
|
+
if action == 0:
|
350
|
+
|
351
|
+
p = [(trend - s) * pip_cost for s in states]
|
352
|
+
|
353
|
+
else:
|
354
|
+
|
355
|
+
p = [(s - trend) * pip_cost for s in states]
|
356
|
+
|
357
|
+
spread *= -1
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
2番目のコードの2
|
362
|
+
|
363
|
+
if action == position:
|
364
|
+
|
365
|
+
sub = 0
|
366
|
+
|
367
|
+
for b in range(0,len(p)):
|
368
|
+
|
369
|
+
r = True if p[b] <= -40 else False
|
370
|
+
|
371
|
+
if r:
|
372
|
+
|
373
|
+
pip.append(-40)
|
374
|
+
|
375
|
+
states.pop(b - sub)
|
376
|
+
|
377
|
+
sub += 1
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
1番目のコードの3
|
382
|
+
|
383
|
+
elif position == 2: # action == 0
|
384
|
+
|
385
|
+
p = [(s - trend) * pip_cost for s in states]
|
386
|
+
|
387
|
+
for b in p:
|
388
|
+
|
389
|
+
b = -40.0 if b <= -40 else b
|
390
|
+
|
391
|
+
pip.append(b)
|
392
|
+
|
393
|
+
states = [trend + spread]
|
394
|
+
|
395
|
+
states = [trend + spread]
|
396
|
+
|
397
|
+
position = 1
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
elif position == 1: action == 1
|
402
|
+
|
403
|
+
p = [(trend - s) * pip_cost for s in states]
|
404
|
+
|
405
|
+
for b in p:
|
406
|
+
|
407
|
+
b = -40.0 if b <= -40 else b
|
408
|
+
|
409
|
+
pip.append(b)
|
410
|
+
|
411
|
+
states = [trend + spread]
|
412
|
+
|
413
|
+
position = 2
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
2番目の3
|
418
|
+
|
419
|
+
else:
|
420
|
+
|
421
|
+
for b in p:
|
422
|
+
|
423
|
+
b = -40.0 if b <= -40 else b
|
424
|
+
|
425
|
+
pip.append(b)
|
426
|
+
|
427
|
+
states = [trend + spread]
|
428
|
+
|
429
|
+
position = action
|
430
|
+
|
431
|
+
'''
|
260
432
|
|
261
433
|
```
|
262
434
|
|
263
|
-
'''
|
264
|
-
|
265
|
-
コードの比較
|
266
|
-
|
267
|
-
1番目のコードの1
|
268
|
-
|
269
|
-
if position == 3: # action == 0
|
270
|
-
|
271
|
-
states = [trend + spread]
|
272
|
-
|
273
|
-
position = 1
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
if position == 3: # action == 1
|
278
|
-
|
279
|
-
states = [trend - spread]
|
280
|
-
|
281
|
-
position = 2
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
2番目のコードの1
|
286
|
-
|
287
|
-
elif position == 3:
|
288
|
-
|
289
|
-
states = [trend + spread] if action == 0 else [trend - spread] # action == 0 states = [trend + spread], action == 1 states = [trend - spread]
|
290
|
-
|
291
|
-
position = action
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
1番目のコードの2
|
296
|
-
|
297
|
-
elif position == 1: # action == 0
|
298
|
-
|
299
|
-
sub = 0
|
300
|
-
|
301
|
-
p = [(trend - s) * pip_cost for s in states]
|
302
|
-
|
303
|
-
for b in range(0,len(p)):
|
304
|
-
|
305
|
-
r = True if p[b] <= -40 else False
|
306
|
-
|
307
|
-
if r:
|
308
|
-
|
309
|
-
pip.append(-40)
|
310
|
-
|
311
|
-
states.pop(b-sub)
|
312
|
-
|
313
|
-
sub += 1
|
314
|
-
|
315
|
-
states.append(trend + spread)
|
316
|
-
|
317
|
-
position = 1
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
elif position == 2: # action == 1
|
322
|
-
|
323
|
-
sub = 0
|
324
|
-
|
325
|
-
p = [(s - trend) * pip_cost for s in states]
|
326
|
-
|
327
|
-
for b in range(0,len(p)):
|
328
|
-
|
329
|
-
r = True if p[b] <= -40 else False
|
330
|
-
|
331
|
-
if r:
|
332
|
-
|
333
|
-
pip.append(-40)
|
334
|
-
|
335
|
-
states.pop(b-sub)
|
336
|
-
|
337
|
-
sub += 1
|
338
|
-
|
339
|
-
states.append(trend - spread)
|
340
|
-
|
341
|
-
position = 2
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
2番目にコードの2と3
|
346
|
-
|
347
|
-
if position != 3:
|
348
|
-
|
349
|
-
if action == 0:
|
350
|
-
|
351
|
-
p = [(trend - s) * pip_cost for s in states]
|
352
|
-
|
353
|
-
else:
|
354
|
-
|
355
|
-
p = [(s - trend) * pip_cost for s in states]
|
356
|
-
|
357
|
-
spread *= -1
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
2番目のコードの2
|
362
|
-
|
363
|
-
if action == position:
|
364
|
-
|
365
|
-
sub = 0
|
366
|
-
|
367
|
-
for b in range(0,len(p)):
|
368
|
-
|
369
|
-
r = True if p[b] <= -40 else False
|
370
|
-
|
371
|
-
if r:
|
372
|
-
|
373
|
-
pip.append(-40)
|
374
|
-
|
375
|
-
states.pop(b - sub)
|
376
|
-
|
377
|
-
sub += 1
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
1番目のコードの3
|
382
|
-
|
383
|
-
elif position == 2: # action == 0
|
384
|
-
|
385
|
-
p = [(s - trend) * pip_cost for s in states]
|
386
|
-
|
387
|
-
for b in p:
|
388
|
-
|
389
|
-
b = -40.0 if b <= -40 else b
|
390
|
-
|
391
|
-
pip.append(b)
|
392
|
-
|
393
|
-
states = [trend + spread]
|
394
|
-
|
395
|
-
states = [trend + spread]
|
396
|
-
|
397
|
-
position = 1
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
elif position == 1: action == 1
|
402
|
-
|
403
|
-
p = [(trend - s) * pip_cost for s in states]
|
404
|
-
|
405
|
-
for b in p:
|
406
|
-
|
407
|
-
b = -40.0 if b <= -40 else b
|
408
|
-
|
409
|
-
pip.append(b)
|
410
|
-
|
411
|
-
states = [trend + spread]
|
412
|
-
|
413
|
-
position = 2
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
2番目の3
|
418
|
-
|
419
|
-
else:
|
420
|
-
|
421
|
-
for b in p:
|
422
|
-
|
423
|
-
b = -40.0 if b <= -40 else b
|
424
|
-
|
425
|
-
pip.append(b)
|
426
|
-
|
427
|
-
states = [trend + spread]
|
428
|
-
|
429
|
-
position = action
|
430
|
-
|
431
|
-
'''
|
432
|
-
|
433
|
-
```
|
434
|
-
|
435
435
|
|
436
436
|
|
437
437
|
##画像
|
3
変数の説明を追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,6 +18,34 @@
|
|
18
18
|
|
19
19
|
そして価格が上がることが期待されるので利益確定は、現時点での価格を10とし計算式は 10-statesになります。
|
20
20
|
|
21
|
+
|
22
|
+
|
23
|
+
##変数の説明
|
24
|
+
|
25
|
+
外国為替ではPIPで報酬を決定します。
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
trend:報酬の決定に関する価格(その時間での始値になる)
|
30
|
+
|
31
|
+
pip:発生した報酬をためていくリスト
|
32
|
+
|
33
|
+
action:その時間での売るか買うどちらこの行動
|
34
|
+
|
35
|
+
position:前回のアクション
|
36
|
+
|
37
|
+
states:購入または売った価格が含まれたリスト
|
38
|
+
|
39
|
+
pip_cost:例えばUSDJPYでの1PIPは、10,000* .0001 = 1.のようになります。わかりにくいと思いますが、.0001のほうがtrend-statesで、それを1PIPに変化せるための数字
|
40
|
+
|
41
|
+
spread:購入または売り関する手数料、例えば購入なら価格が上がることが期待されるのでスプレッドを足すことになる。
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
番号2と番号3では変数rの処理が違いますが、番号2では損切(損が一定以上になった場合その時点で利益確定をする)が発生しているのかどうか、番号3でも同様ですが直接数字を計算します。
|
46
|
+
|
47
|
+
|
48
|
+
|
21
49
|
```python
|
22
50
|
|
23
51
|
'''
|
@@ -162,6 +190,8 @@
|
|
162
190
|
|
163
191
|
if action == 0:
|
164
192
|
|
193
|
+
#pipの計算
|
194
|
+
|
165
195
|
p = [(trend - s) * pip_cost for s in states]
|
166
196
|
|
167
197
|
else:
|
@@ -413,3 +443,7 @@
|
|
413
443
|
2番目のコードの結果(何回繰り返しても最初は必ずプラスになる。)
|
414
444
|
|
415
445
|
![イメージ説明](0a367287e4d7542fc19162515da2e244.jpeg)
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
分からないことがあれば、できるだけ情報を追加したいと思います。
|
2
質問内容を変更しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,24 +1,52 @@
|
|
1
|
+
### 達成したいこと・問題点
|
2
|
+
|
1
|
-
|
3
|
+
1番目のコードと2番目のコードは流れ自体は変わりません。ただ一番目のコードは見にくく改善しようと2番目のコードに直しました。しかし、結果は同じになるはずなのですが違いが出てきてしまいます。
|
4
|
+
|
2
|
-
|
5
|
+
### コードの説明
|
6
|
+
|
3
|
-
|
7
|
+
コード全体の趣旨としては、アクションは0か1のみになり前回と同様のアクションが選択されれば追加の購入(スプレッドを足す)または売り(スプレッドで引く)をstatesに追加します。
|
8
|
+
|
9
|
+
また、損切が発生している場合は、ロスカットをピップに追加し該当のstatesを削除します。
|
10
|
+
|
11
|
+
前回とアクションが異なれば利益確定をし、新しいstatesを定義します。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
例えば価格が1の時購入しスプレッドが1だと仮定すれば, states = 1+1になります。
|
16
|
+
|
17
|
+
tlが4だとしても途中評価がなければ不当な利益確定になってしまいます。
|
18
|
+
|
19
|
+
そして価格が上がることが期待されるので利益確定は、現時点での価格を10とし計算式は 10-statesになります。
|
4
20
|
|
5
21
|
```python
|
6
22
|
|
23
|
+
'''
|
24
|
+
|
25
|
+
重複している部分に同じ番号を振る
|
26
|
+
|
27
|
+
以下のように多少の計算式が異なるだけでほぼ同じ処理をしている。
|
28
|
+
|
29
|
+
'''
|
30
|
+
|
7
|
-
def reward(trend,
|
31
|
+
def reward(trend,pip,action,position,states,pip_cost,spread):
|
8
32
|
|
9
33
|
if action == 0:
|
10
34
|
|
35
|
+
# 1 : time stepが0の時に実行
|
36
|
+
|
11
37
|
if position == 3:
|
12
38
|
|
13
|
-
states = [trend
|
39
|
+
states = [trend + spread]
|
14
40
|
|
15
41
|
position = 1
|
16
42
|
|
43
|
+
# 2(pの計算だけ違うがそれを除けば同じ) : 損切の確認、statesに値を追加
|
44
|
+
|
17
45
|
elif position == 1:
|
18
46
|
|
19
47
|
sub = 0
|
20
48
|
|
21
|
-
p = [(trend
|
49
|
+
p = [(trend - s) * pip_cost for s in states]
|
22
50
|
|
23
51
|
for b in range(0,len(p)):
|
24
52
|
|
@@ -36,9 +64,11 @@
|
|
36
64
|
|
37
65
|
position = 1
|
38
66
|
|
67
|
+
# 3(pの計算が違うだけ) : 利益確定をし新しい状態を持つ
|
68
|
+
|
39
69
|
elif position == 2:
|
40
70
|
|
41
|
-
p = [(s - trend
|
71
|
+
p = [(s - trend) * pip_cost for s in states]
|
42
72
|
|
43
73
|
for b in p:
|
44
74
|
|
@@ -46,43 +76,129 @@
|
|
46
76
|
|
47
77
|
pip.append(b)
|
48
78
|
|
49
|
-
states = [trend
|
79
|
+
states = [trend + spread]
|
50
80
|
|
51
81
|
position = 1
|
52
82
|
|
53
83
|
elif action == 1:
|
54
84
|
|
85
|
+
# 1
|
86
|
+
|
55
87
|
if position == 3:
|
56
88
|
|
89
|
+
states = [trend - spread]
|
90
|
+
|
91
|
+
position = 2
|
92
|
+
|
93
|
+
# 2
|
94
|
+
|
95
|
+
elif position == 2:
|
96
|
+
|
97
|
+
sub = 0
|
98
|
+
|
99
|
+
p = [(s - trend) * pip_cost for s in states]
|
100
|
+
|
101
|
+
for b in range(0,len(p)):
|
102
|
+
|
103
|
+
r = [-40.0, True] if p[b] <= -40 else [p[b], False]
|
104
|
+
|
105
|
+
if r[1]:
|
106
|
+
|
107
|
+
pip.append(r[0])
|
108
|
+
|
109
|
+
states.pop(b-sub)
|
110
|
+
|
111
|
+
sub += 1
|
112
|
+
|
113
|
+
states.append(trend - spread)
|
114
|
+
|
115
|
+
position = 2
|
116
|
+
|
117
|
+
# 3
|
118
|
+
|
119
|
+
elif position == 1:
|
120
|
+
|
121
|
+
p = [(trend - s) * pip_cost for s in states]
|
122
|
+
|
123
|
+
for b in p:
|
124
|
+
|
125
|
+
b = -40.0 if b <= -40 else b
|
126
|
+
|
127
|
+
pip.append(b)
|
128
|
+
|
57
129
|
states = [trend[t] - spread]
|
58
130
|
|
59
131
|
position = 2
|
60
132
|
|
133
|
+
return states,pip,position
|
134
|
+
|
135
|
+
```
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
```python
|
140
|
+
|
141
|
+
'''
|
142
|
+
|
143
|
+
一番目のコードと同様に番号を振る。
|
144
|
+
|
145
|
+
速度アップを図るためにcythonを使用しています。
|
146
|
+
|
147
|
+
'''
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
def reward2(double trend,list pip,int action,int position,list states,double pip_cost,double spread):
|
152
|
+
|
153
|
+
cdef list r
|
154
|
+
|
155
|
+
cdef list p
|
156
|
+
|
157
|
+
cdef int sub = 0
|
158
|
+
|
159
|
+
# 2と3 : アクションの値によって式が異なるが同じ処理(1番目のコードと)
|
160
|
+
|
61
|
-
|
161
|
+
if position != 3:
|
62
|
-
|
162
|
+
|
63
|
-
|
163
|
+
if action == 0:
|
164
|
+
|
64
|
-
|
165
|
+
p = [(trend - s) * pip_cost for s in states]
|
166
|
+
|
167
|
+
else:
|
168
|
+
|
65
|
-
p = [(s - trend
|
169
|
+
p = [(s - trend) * pip_cost for s in states]
|
170
|
+
|
66
|
-
|
171
|
+
spread *= -1
|
172
|
+
|
173
|
+
# positionの値をアクションと同様にしています。
|
174
|
+
|
175
|
+
# 2 : 前回のアクションと同様なら実行する。
|
176
|
+
|
177
|
+
if action == position:
|
178
|
+
|
67
|
-
for b in range(0,len(p)):
|
179
|
+
for b in range(0, len(p)):
|
180
|
+
|
68
|
-
|
181
|
+
# tl = 40を超えていれば損切とみなし、-40に固定する。
|
182
|
+
|
69
|
-
r =
|
183
|
+
r = True if p[b] <= -40 else False
|
184
|
+
|
70
|
-
|
185
|
+
# 発生している場合は該当の状態を削除する(2重評価を避けるため)
|
186
|
+
|
71
|
-
if r
|
187
|
+
if r:
|
72
|
-
|
188
|
+
|
73
|
-
pip.append(
|
189
|
+
pip.append(-40)
|
74
|
-
|
190
|
+
|
75
|
-
states.pop(b-sub)
|
191
|
+
states.pop(b - sub)
|
76
|
-
|
192
|
+
|
77
|
-
sub += 1
|
193
|
+
sub += 1
|
78
|
-
|
194
|
+
|
79
|
-
states.append(trend
|
195
|
+
states.append(trend + spread)
|
80
|
-
|
196
|
+
|
81
|
-
position =
|
197
|
+
position = action
|
82
|
-
|
198
|
+
|
83
|
-
|
199
|
+
# 3 : 前回のアクションと異なるなら実行する
|
84
|
-
|
200
|
+
|
85
|
-
|
201
|
+
else:
|
86
202
|
|
87
203
|
for b in p:
|
88
204
|
|
@@ -90,9 +206,21 @@
|
|
90
206
|
|
91
207
|
pip.append(b)
|
92
208
|
|
93
|
-
states = [trend
|
209
|
+
states = [trend + spread]
|
94
|
-
|
210
|
+
|
95
|
-
position =
|
211
|
+
position = action
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
# 1 : time stepが0の時に実行する。
|
216
|
+
|
217
|
+
if position == 3:
|
218
|
+
|
219
|
+
states = [trend + spread] if action == 0 else [trend - spread]
|
220
|
+
|
221
|
+
position = action
|
222
|
+
|
223
|
+
|
96
224
|
|
97
225
|
return states,pip,position
|
98
226
|
|
@@ -100,100 +228,188 @@
|
|
100
228
|
|
101
229
|
|
102
230
|
|
103
|
-
```python
|
104
|
-
|
105
|
-
def reward2(double trend,list pip,int action,int position,list states,double pip_cost,double spread):
|
106
|
-
|
107
|
-
cdef list r
|
108
|
-
|
109
|
-
cdef list p
|
110
|
-
|
111
|
-
cdef int sub = 0
|
112
|
-
|
113
|
-
# actionは0か1つまり、買うか売るという行動以外取りません
|
114
|
-
|
115
|
-
if position != 3:
|
116
|
-
|
117
|
-
if action == 0:
|
118
|
-
|
119
|
-
#buyなら購入した状態から現時点での価格があっがていることを期待するため
|
120
|
-
|
121
|
-
p = [(trend - s) * pip_cost for s in states]
|
122
|
-
|
123
|
-
else:
|
124
|
-
|
125
|
-
#buyの逆
|
126
|
-
|
127
|
-
p = [(s - trend) * pip_cost for s in states]
|
128
|
-
|
129
|
-
spread *= -1
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
if action == position:
|
134
|
-
|
135
|
-
# 損切が発生しているかどうかの確認
|
136
|
-
|
137
|
-
for b in range(0, len(p)):
|
138
|
-
|
139
|
-
r = [-40.0, True] if p[b] <= -40 else [p[b], False]
|
140
|
-
|
141
|
-
# 発生している場合は該当の状態を削除する(2重評価を避けるため)
|
142
|
-
|
143
|
-
if r[1]:
|
144
|
-
|
145
|
-
pip.append(r[0])
|
146
|
-
|
147
|
-
states.pop(b - sub)
|
148
|
-
|
149
|
-
sub += 1
|
150
|
-
|
151
|
-
states.append(trend + spread)
|
152
|
-
|
153
|
-
position = action
|
154
|
-
|
155
|
-
else:
|
156
|
-
|
157
|
-
#前回とアクションが違うなら利確を実行し、新しい状態を持つ
|
158
|
-
|
159
|
-
for b in p:
|
160
|
-
|
161
|
-
b = -40.0 if b <= -40 else b
|
162
|
-
|
163
|
-
pip.append(b)
|
164
|
-
|
165
|
-
states = [trend + spread]
|
166
|
-
|
167
|
-
position = action
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
# taimusutepが0の時に状態を持つ
|
172
|
-
|
173
|
-
if position == 3:
|
174
|
-
|
175
|
-
states = [trend + spread] if action == 0 else [trend - spread]
|
176
|
-
|
177
|
-
position = action
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
return states,pip,position
|
182
|
-
|
183
231
|
```
|
184
232
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
233
|
+
'''
|
234
|
+
|
235
|
+
コードの比較
|
236
|
+
|
237
|
+
1番目のコードの1
|
238
|
+
|
239
|
+
if position == 3: # action == 0
|
240
|
+
|
241
|
+
states = [trend + spread]
|
242
|
+
|
243
|
+
position = 1
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
if position == 3: # action == 1
|
248
|
+
|
249
|
+
states = [trend - spread]
|
250
|
+
|
251
|
+
position = 2
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
2番目のコードの1
|
256
|
+
|
257
|
+
elif position == 3:
|
258
|
+
|
259
|
+
states = [trend + spread] if action == 0 else [trend - spread] # action == 0 states = [trend + spread], action == 1 states = [trend - spread]
|
260
|
+
|
261
|
+
position = action
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
1番目のコードの2
|
266
|
+
|
267
|
+
elif position == 1: # action == 0
|
268
|
+
|
269
|
+
sub = 0
|
270
|
+
|
271
|
+
p = [(trend - s) * pip_cost for s in states]
|
272
|
+
|
273
|
+
for b in range(0,len(p)):
|
274
|
+
|
275
|
+
r = True if p[b] <= -40 else False
|
276
|
+
|
277
|
+
if r:
|
278
|
+
|
279
|
+
pip.append(-40)
|
280
|
+
|
281
|
+
states.pop(b-sub)
|
282
|
+
|
283
|
+
sub += 1
|
284
|
+
|
285
|
+
states.append(trend + spread)
|
286
|
+
|
287
|
+
position = 1
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
elif position == 2: # action == 1
|
292
|
+
|
293
|
+
sub = 0
|
294
|
+
|
295
|
+
p = [(s - trend) * pip_cost for s in states]
|
296
|
+
|
297
|
+
for b in range(0,len(p)):
|
298
|
+
|
299
|
+
r = True if p[b] <= -40 else False
|
300
|
+
|
301
|
+
if r:
|
302
|
+
|
303
|
+
pip.append(-40)
|
304
|
+
|
305
|
+
states.pop(b-sub)
|
306
|
+
|
307
|
+
sub += 1
|
308
|
+
|
309
|
+
states.append(trend - spread)
|
310
|
+
|
311
|
+
position = 2
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
2番目にコードの2と3
|
316
|
+
|
317
|
+
if position != 3:
|
318
|
+
|
319
|
+
if action == 0:
|
320
|
+
|
321
|
+
p = [(trend - s) * pip_cost for s in states]
|
322
|
+
|
323
|
+
else:
|
324
|
+
|
325
|
+
p = [(s - trend) * pip_cost for s in states]
|
326
|
+
|
327
|
+
spread *= -1
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
2番目のコードの2
|
332
|
+
|
333
|
+
if action == position:
|
334
|
+
|
335
|
+
sub = 0
|
336
|
+
|
337
|
+
for b in range(0,len(p)):
|
338
|
+
|
339
|
+
r = True if p[b] <= -40 else False
|
340
|
+
|
341
|
+
if r:
|
342
|
+
|
343
|
+
pip.append(-40)
|
344
|
+
|
345
|
+
states.pop(b - sub)
|
346
|
+
|
347
|
+
sub += 1
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
1番目のコードの3
|
352
|
+
|
353
|
+
elif position == 2: # action == 0
|
354
|
+
|
355
|
+
p = [(s - trend) * pip_cost for s in states]
|
356
|
+
|
357
|
+
for b in p:
|
358
|
+
|
359
|
+
b = -40.0 if b <= -40 else b
|
360
|
+
|
361
|
+
pip.append(b)
|
362
|
+
|
363
|
+
states = [trend + spread]
|
364
|
+
|
365
|
+
states = [trend + spread]
|
366
|
+
|
367
|
+
position = 1
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
elif position == 1: action == 1
|
372
|
+
|
373
|
+
p = [(trend - s) * pip_cost for s in states]
|
374
|
+
|
375
|
+
for b in p:
|
376
|
+
|
377
|
+
b = -40.0 if b <= -40 else b
|
378
|
+
|
379
|
+
pip.append(b)
|
380
|
+
|
381
|
+
states = [trend + spread]
|
382
|
+
|
383
|
+
position = 2
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
2番目の3
|
388
|
+
|
389
|
+
else:
|
390
|
+
|
391
|
+
for b in p:
|
392
|
+
|
393
|
+
b = -40.0 if b <= -40 else b
|
394
|
+
|
395
|
+
pip.append(b)
|
396
|
+
|
397
|
+
states = [trend + spread]
|
398
|
+
|
399
|
+
position = action
|
400
|
+
|
401
|
+
'''
|
402
|
+
|
403
|
+
```
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
##画像
|
408
|
+
|
409
|
+
1番目のコードの結果(何回繰り返しても最初は必ずマイナスになる。)
|
410
|
+
|
411
|
+
![1番目のコード](4710e294bf8399948889242f6d5dc4cf.jpeg)
|
412
|
+
|
413
|
+
2番目のコードの結果(何回繰り返しても最初は必ずプラスになる。)
|
414
|
+
|
415
|
+
![イメージ説明](0a367287e4d7542fc19162515da2e244.jpeg)
|
1
画像追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -185,3 +185,15 @@
|
|
185
185
|
|
186
186
|
|
187
187
|
一番目のコードは無駄に長く、省けると思ったので短くしてみたのですが報酬の結果に違いが大幅に出てきてしまいました。
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
画像を追加します。
|
192
|
+
|
193
|
+
一番目のコードが以下の画像と似通った値になります。(最初は必ずマイナスその後は大体がプラス)
|
194
|
+
|
195
|
+
![イメージ説明](7ff6b8892e05ebc54f704b6ace6e2669.jpeg)
|
196
|
+
|
197
|
+
2番目のコードは最初は必ずプラスでその後がほぼ10epock程度しか確認していませんが、全てマイナスの結果になります。
|
198
|
+
|
199
|
+
![イメージ説明](37e92dbb1b0ea52815ac88685dd2f018.jpeg)
|